Initial plugin scaffold: lesson scheduling WordPress plugin
Some checks failed
CI / Coding Standards (push) Failing after 2m31s
CI / PHPStan (push) Failing after 50s
CI / Tests (PHP 8.1) (push) Successful in 50s
CI / Tests (PHP 8.2) (push) Successful in 48s
CI / Tests (PHP 8.3) (push) Successful in 40s
CI / No Debug Code (push) Successful in 2s
Some checks failed
CI / Coding Standards (push) Failing after 2m31s
CI / PHPStan (push) Failing after 50s
CI / Tests (PHP 8.1) (push) Successful in 50s
CI / Tests (PHP 8.2) (push) Successful in 48s
CI / Tests (PHP 8.3) (push) Successful in 40s
CI / No Debug Code (push) Successful in 2s
- Custom DB tables for availability slots and lesson bookings - Instructor (wp-admin) and student (front-end) roles with custom capabilities - REST API under us-scheduler/v1 for availability CRUD and booking - [us_booking] and [us_student_login] shortcodes for student front end - PHPUnit + Brain\Monkey unit test suite (29 tests) - Gitea Actions CI: lint, PHPStan, tests on PHP 8.1/8.2/8.3, no-debug check - Feature docs under docs/features/ Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
102
.gitea/workflows/ci.yml
Normal file
102
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- develop
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Coding Standards
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: '8.3'
|
||||||
|
tools: composer:v2
|
||||||
|
|
||||||
|
- name: Cache Composer packages
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ~/.composer/cache
|
||||||
|
key: composer-${{ hashFiles('composer.json') }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: composer install --prefer-dist --no-progress --no-interaction
|
||||||
|
|
||||||
|
- name: Run PHPCS
|
||||||
|
run: composer cs
|
||||||
|
|
||||||
|
static-analysis:
|
||||||
|
name: PHPStan
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: '8.3'
|
||||||
|
tools: composer:v2
|
||||||
|
|
||||||
|
- name: Cache Composer packages
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ~/.composer/cache
|
||||||
|
key: composer-${{ hashFiles('composer.json') }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: composer install --prefer-dist --no-progress --no-interaction
|
||||||
|
|
||||||
|
- name: Run PHPStan
|
||||||
|
run: composer lint
|
||||||
|
|
||||||
|
test:
|
||||||
|
name: Tests (PHP ${{ matrix.php }})
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
php:
|
||||||
|
- '8.1'
|
||||||
|
- '8.2'
|
||||||
|
- '8.3'
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: ${{ matrix.php }}
|
||||||
|
extensions: mbstring, intl
|
||||||
|
coverage: none
|
||||||
|
tools: composer:v2
|
||||||
|
|
||||||
|
- name: Cache Composer packages
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ~/.composer/cache
|
||||||
|
key: ${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: composer install --prefer-dist --no-progress --no-interaction
|
||||||
|
|
||||||
|
- name: Run PHPUnit
|
||||||
|
run: composer test
|
||||||
|
|
||||||
|
no-debug:
|
||||||
|
name: No Debug Code
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Check for debug statements
|
||||||
|
run: |
|
||||||
|
if grep -rn --include="*.php" -E "(var_dump|var_export|print_r|error_log|dd\(|dump\()" src/; then
|
||||||
|
echo "Debug code found in src/ — please remove before merging."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
vendor/
|
||||||
|
composer.lock
|
||||||
|
coverage/
|
||||||
|
.phpunit.result.cache
|
||||||
|
*.log
|
||||||
94
CLAUDE.md
Normal file
94
CLAUDE.md
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer install # Install all dependencies
|
||||||
|
|
||||||
|
composer test # Run the full test suite (required after every change)
|
||||||
|
composer lint # PHPStan static analysis
|
||||||
|
composer cs # PHPCS coding standards check
|
||||||
|
composer cs:fix # Auto-fix coding standards
|
||||||
|
|
||||||
|
# Run a single test file
|
||||||
|
./vendor/bin/phpunit tests/Unit/Data/AvailabilityRepositoryTest.php
|
||||||
|
|
||||||
|
# Run a single test by name
|
||||||
|
./vendor/bin/phpunit --filter testInsertCallsWpdbInsertAndReturnsId
|
||||||
|
```
|
||||||
|
|
||||||
|
**Run `composer test` after every code change before considering a task complete.**
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
### Plugin Bootstrap
|
||||||
|
`unsupervised-schedular.php` defines constants (`USC_VERSION`, `USC_PLUGIN_DIR`, `USC_PLUGIN_URL`), registers activation/deactivation hooks, then calls `Plugin::boot()` on `plugins_loaded`. No logic lives in the root file.
|
||||||
|
|
||||||
|
### Directory Structure
|
||||||
|
```
|
||||||
|
src/ — All plugin PHP (PSR-4 namespace: Unsupervised\Schedular\)
|
||||||
|
templates/ — PHP view files included by controllers/shortcodes
|
||||||
|
assets/ — CSS and JS (vanilla JS, no build step)
|
||||||
|
tests/Unit/ — PHPUnit unit tests (PSR-4: Unsupervised\Schedular\Tests\)
|
||||||
|
docs/features/— One markdown file per feature describing data model, API, and test locations
|
||||||
|
```
|
||||||
|
|
||||||
|
### Data Storage
|
||||||
|
Two custom database tables (created via `dbDelta` on activation):
|
||||||
|
- `{prefix}us_availability` — instructor availability windows
|
||||||
|
- `{prefix}us_lessons` — booked lessons
|
||||||
|
|
||||||
|
All database access goes through repository classes in `src/Data/`. No direct `$wpdb` calls outside repositories.
|
||||||
|
|
||||||
|
### Key Classes
|
||||||
|
|
||||||
|
| Class | Responsibility |
|
||||||
|
|---|---|
|
||||||
|
| `Plugin` | Wires all components together on `plugins_loaded` |
|
||||||
|
| `Installer` | Creates DB tables and roles on activation |
|
||||||
|
| `Roles\RoleManager` | Registers `us_instructor` and `us_student` roles with custom caps |
|
||||||
|
| `Data\AvailabilityRepository` | CRUD for availability slots |
|
||||||
|
| `Data\BookingRepository` | CRUD for lesson bookings |
|
||||||
|
| `Model\AvailabilitySlot` | Immutable value object for a slot row |
|
||||||
|
| `Model\Lesson` | Immutable value object for a lesson row |
|
||||||
|
| `Admin\AdminMenu` | Registers wp-admin menu pages |
|
||||||
|
| `Admin\AvailabilityController` | Instructor availability management page |
|
||||||
|
| `Admin\LessonController` | Admin and instructor lesson list pages |
|
||||||
|
| `Api\RestRegistrar` | Registers all REST routes under `us-scheduler/v1` |
|
||||||
|
| `Api\AvailabilityEndpoint` | REST handlers for availability CRUD |
|
||||||
|
| `Api\BookingEndpoint` | REST handlers for booking and status updates |
|
||||||
|
| `Frontend\ShortcodeRegistrar` | Registers `[us_booking]` and `[us_student_login]` shortcodes |
|
||||||
|
| `Frontend\BookingPage` | Renders student booking UI shell (JS takes over) |
|
||||||
|
| `Frontend\LoginPage` | Renders front-end student login form |
|
||||||
|
|
||||||
|
### REST API Namespace
|
||||||
|
All endpoints live under `/wp-json/us-scheduler/v1/`. Permissions are enforced via `permission_callback` using capability checks (`manage_availability`, `book_lesson`), never role name checks.
|
||||||
|
|
||||||
|
### Testing Approach
|
||||||
|
Tests use [Brain\Monkey](https://brain-wp.github.io/BrainMonkey/) to stub WordPress functions without a full WP installation, and Mockery to mock `$wpdb` and other dependencies.
|
||||||
|
|
||||||
|
All test classes extend `tests/Unit/TestCase.php`, which handles `Monkey\setUp()` / `Monkey\tearDown()` and stubs all WP translation/escape functions automatically.
|
||||||
|
|
||||||
|
**Brain\Monkey API notes:**
|
||||||
|
- `Functions\when('fn')->alias(fn() => ...)` — stub with a closure (NOT `returnUsing()`)
|
||||||
|
- `Functions\when('fn')->justReturn($val)` — stub returning a fixed value
|
||||||
|
- `Functions\expect('fn')->once()->with(...)` — assert call count and arguments
|
||||||
|
- Use `Functions\when()` (not `Functions\expect()`) when you need argument-routing (e.g. `get_role` returning different values per argument) to avoid chaining ambiguity
|
||||||
|
- Mockery matchers (e.g. `\Mockery::type()`) inside plain PHP arrays do not work with `with()` — use `\Mockery::on(fn($arr) => ...)` or `\Mockery::any()` instead
|
||||||
|
- When mocking `$wpdb`, set `$mock->prefix = 'wp_'` explicitly — it is a public property, not a method
|
||||||
|
|
||||||
|
### Adding a Feature
|
||||||
|
1. Write the feature doc in `docs/features/<feature-name>.md` (data model, API, classes, test paths).
|
||||||
|
2. Implement the classes under `src/`.
|
||||||
|
3. Add template(s) under `templates/` if needed.
|
||||||
|
4. Write unit tests under `tests/Unit/` mirroring the `src/` directory structure.
|
||||||
|
5. Run `composer test` — all tests must pass before the feature is complete.
|
||||||
|
|
||||||
|
### CI
|
||||||
|
Gitea Actions (`.gitea/workflows/ci.yml`) runs on every push and pull request:
|
||||||
|
- **lint** — PHPCS WordPress coding standards
|
||||||
|
- **static-analysis** — PHPStan level 6
|
||||||
|
- **test** — PHPUnit on PHP 8.1, 8.2, 8.3
|
||||||
|
- **no-debug** — rejects commits with `var_dump`, `error_log`, etc. in `src/`
|
||||||
35
assets/css/frontend.css
Normal file
35
assets/css/frontend.css
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
.us-login-form label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.us-login-form input[type="text"],
|
||||||
|
.us-login-form input[type="password"] {
|
||||||
|
width: 100%;
|
||||||
|
max-width: 340px;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.us-error {
|
||||||
|
color: #c00;
|
||||||
|
border-left: 4px solid #c00;
|
||||||
|
padding-left: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#us-booking-app .us-slot {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 12px 16px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#us-booking-error {
|
||||||
|
color: #c00;
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
76
assets/js/booking.js
Normal file
76
assets/js/booking.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
/* global usScheduler */
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const app = document.getElementById('us-booking-app');
|
||||||
|
if (!app) return;
|
||||||
|
|
||||||
|
const slotList = document.getElementById('us-slot-list');
|
||||||
|
const confirm = document.getElementById('us-booking-confirmation');
|
||||||
|
const errorBox = document.getElementById('us-booking-error');
|
||||||
|
const { restUrl, nonce } = usScheduler;
|
||||||
|
|
||||||
|
function apiFetch(path, options = {}) {
|
||||||
|
return fetch(restUrl + path, {
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-WP-Nonce': nonce,
|
||||||
|
...(options.headers || {}),
|
||||||
|
},
|
||||||
|
}).then(async (res) => {
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok) throw new Error(data.message || 'Request failed');
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showError(message) {
|
||||||
|
errorBox.textContent = message;
|
||||||
|
errorBox.style.display = 'block';
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderSlots(slots) {
|
||||||
|
if (!slots.length) {
|
||||||
|
slotList.innerHTML = '<p>No available lesson slots at this time.</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
slotList.innerHTML = slots.map((slot) => `
|
||||||
|
<div class="us-slot">
|
||||||
|
<span>${escHtml(slot.start_dt)} – ${escHtml(slot.end_dt)}</span>
|
||||||
|
<button data-slot-id="${slot.id}" class="us-book-btn">Book</button>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
|
||||||
|
slotList.querySelectorAll('.us-book-btn').forEach((btn) => {
|
||||||
|
btn.addEventListener('click', () => bookSlot(Number(btn.dataset.slotId)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function escHtml(str) {
|
||||||
|
return String(str)
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
function bookSlot(slotId) {
|
||||||
|
errorBox.style.display = 'none';
|
||||||
|
|
||||||
|
apiFetch('bookings', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ slot_id: slotId }),
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
slotList.style.display = 'none';
|
||||||
|
confirm.style.display = 'block';
|
||||||
|
})
|
||||||
|
.catch((err) => showError(err.message));
|
||||||
|
}
|
||||||
|
|
||||||
|
apiFetch('availability')
|
||||||
|
.then(renderSlots)
|
||||||
|
.catch((err) => showError(err.message));
|
||||||
|
}());
|
||||||
41
composer.json
Normal file
41
composer.json
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"name": "unsupervised/schedular",
|
||||||
|
"description": "WordPress plugin for instructor/student lesson scheduling",
|
||||||
|
"type": "wordpress-plugin",
|
||||||
|
"license": "GPL-2.0-or-later",
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^10.5",
|
||||||
|
"brain/monkey": "^2.6",
|
||||||
|
"mockery/mockery": "^1.6",
|
||||||
|
"phpstan/phpstan": "^1.10",
|
||||||
|
"szepeviktor/phpstan-wordpress": "^1.3",
|
||||||
|
"php-stubs/wordpress-stubs": "^6.0",
|
||||||
|
"squizlabs/php_codesniffer": "^3.7",
|
||||||
|
"wp-coding-standards/wpcs": "^3.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Unsupervised\\Schedular\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Unsupervised\\Schedular\\Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "phpunit --configuration phpunit.xml",
|
||||||
|
"test:coverage": "phpunit --configuration phpunit.xml --coverage-html coverage/",
|
||||||
|
"lint": "phpstan analyse src/ --level=6 --configuration phpstan.neon",
|
||||||
|
"cs": "phpcs --standard=WordPress src/",
|
||||||
|
"cs:fix": "phpcbf --standard=WordPress src/"
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
docs/features/availability-management.md
Normal file
39
docs/features/availability-management.md
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# Feature: Availability Management
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Instructors define date/time windows during which they are available for lessons. Students book from these windows.
|
||||||
|
|
||||||
|
## Data Model — `{prefix}us_availability`
|
||||||
|
|
||||||
|
| Column | Type | Notes |
|
||||||
|
|----------------|------------------|----------------------------------------------|
|
||||||
|
| `id` | BIGINT UNSIGNED | Primary key |
|
||||||
|
| `instructor_id`| BIGINT UNSIGNED | WordPress user ID |
|
||||||
|
| `start_dt` | DATETIME | Slot start — stored as `Y-m-d H:i:s` |
|
||||||
|
| `end_dt` | DATETIME | Slot end — stored as `Y-m-d H:i:s` |
|
||||||
|
| `is_booked` | TINYINT(1) | 0 = available, 1 = booked |
|
||||||
|
| `created_at` | DATETIME | Insertion time |
|
||||||
|
|
||||||
|
## Admin Interface
|
||||||
|
Instructors access **My Availability** in wp-admin (`?page=us-availability`).
|
||||||
|
- Add a slot: provide start and end datetime
|
||||||
|
- Delete a slot: only allowed if `is_booked = 0`
|
||||||
|
|
||||||
|
## REST API
|
||||||
|
| Method | Endpoint | Permission |
|
||||||
|
|----------|-----------------------------------|-------------------------|
|
||||||
|
| `GET` | `/wp-json/us-scheduler/v1/availability` | `book_lesson` |
|
||||||
|
| `POST` | `/wp-json/us-scheduler/v1/availability` | `manage_availability` |
|
||||||
|
| `DELETE` | `/wp-json/us-scheduler/v1/availability/{id}` | `manage_availability` + slot owner |
|
||||||
|
|
||||||
|
`GET` supports query params: `instructor_id`, `from` (datetime), `to` (datetime).
|
||||||
|
|
||||||
|
## Implementation
|
||||||
|
- Repository: `Unsupervised\Schedular\Data\AvailabilityRepository`
|
||||||
|
- Model: `Unsupervised\Schedular\Model\AvailabilitySlot`
|
||||||
|
- Admin controller: `Unsupervised\Schedular\Admin\AvailabilityController`
|
||||||
|
- REST endpoint: `Unsupervised\Schedular\Api\AvailabilityEndpoint`
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
- `tests/Unit/Data/AvailabilityRepositoryTest.php`
|
||||||
|
- `tests/Unit/Model/AvailabilitySlotTest.php`
|
||||||
52
docs/features/lesson-booking.md
Normal file
52
docs/features/lesson-booking.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# Feature: Lesson Booking
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Students browse available slots and submit a booking. Instructors then confirm or cancel from wp-admin or via the REST API.
|
||||||
|
|
||||||
|
## Data Model — `{prefix}us_lessons`
|
||||||
|
|
||||||
|
| Column | Type | Notes |
|
||||||
|
|----------------|------------------|--------------------------------------------------|
|
||||||
|
| `id` | BIGINT UNSIGNED | Primary key |
|
||||||
|
| `slot_id` | BIGINT UNSIGNED | FK → `us_availability.id` |
|
||||||
|
| `student_id` | BIGINT UNSIGNED | WordPress user ID |
|
||||||
|
| `instructor_id`| BIGINT UNSIGNED | WordPress user ID (denormalised for fast queries) |
|
||||||
|
| `status` | VARCHAR(20) | `pending` / `confirmed` / `cancelled` |
|
||||||
|
| `notes` | TEXT | Optional student notes |
|
||||||
|
| `created_at` | DATETIME | Insertion time |
|
||||||
|
|
||||||
|
## Booking Flow
|
||||||
|
1. Student opens the page with `[us_booking]` shortcode.
|
||||||
|
2. JS fetches `GET /availability` → renders available slots.
|
||||||
|
3. Student clicks **Book** → `POST /bookings` with `slot_id`.
|
||||||
|
4. Server creates the lesson row (`status = pending`) and sets `us_availability.is_booked = 1`.
|
||||||
|
5. Instructor sees the booking under **My Lessons** in wp-admin.
|
||||||
|
6. Instructor updates status via `PATCH /bookings/{id}/status`.
|
||||||
|
|
||||||
|
## REST API
|
||||||
|
| Method | Endpoint | Permission |
|
||||||
|
|-----------|------------------------------------------------|-------------------------------|
|
||||||
|
| `GET` | `/wp-json/us-scheduler/v1/bookings` | Any logged-in user |
|
||||||
|
| `POST` | `/wp-json/us-scheduler/v1/bookings` | `book_lesson` |
|
||||||
|
| `PATCH` | `/wp-json/us-scheduler/v1/bookings/{id}/status`| `manage_availability` or admin |
|
||||||
|
|
||||||
|
`GET /bookings` returns the caller's own lessons (student view) or upcoming lessons for the instructor if the caller has `manage_availability`.
|
||||||
|
|
||||||
|
## Admin Interface
|
||||||
|
- **Scheduler** (`manage_options` only): all upcoming lessons across all instructors
|
||||||
|
- **My Lessons** (`view_own_lessons`): upcoming lessons for the logged-in instructor
|
||||||
|
|
||||||
|
## Frontend Shortcodes
|
||||||
|
- `[us_booking]` — student booking form; requires `book_lesson` capability
|
||||||
|
- `[us_student_login]` — front-end login form for students
|
||||||
|
|
||||||
|
## Implementation
|
||||||
|
- Repository: `Unsupervised\Schedular\Data\BookingRepository`
|
||||||
|
- Model: `Unsupervised\Schedular\Model\Lesson`
|
||||||
|
- Admin controller: `Unsupervised\Schedular\Admin\LessonController`
|
||||||
|
- REST endpoint: `Unsupervised\Schedular\Api\BookingEndpoint`
|
||||||
|
- Frontend: `Unsupervised\Schedular\Frontend\BookingPage`, `LoginPage`
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
- `tests/Unit/Data/BookingRepositoryTest.php`
|
||||||
|
- `tests/Unit/Model/LessonTest.php`
|
||||||
28
docs/features/user-roles.md
Normal file
28
docs/features/user-roles.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Feature: User Roles
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Two custom WordPress user roles control access to all scheduling features.
|
||||||
|
|
||||||
|
## Roles
|
||||||
|
|
||||||
|
### Instructor (`us_instructor`)
|
||||||
|
Created on plugin activation. Logs in via standard wp-admin. Can:
|
||||||
|
- Manage their own availability slots (add/delete)
|
||||||
|
- View their upcoming confirmed/pending lessons in wp-admin
|
||||||
|
|
||||||
|
**Capabilities:** `read`, `manage_availability`, `view_own_lessons`
|
||||||
|
|
||||||
|
### Student (`us_student`)
|
||||||
|
Logs in via the front-end `[us_student_login]` shortcode. Can:
|
||||||
|
- Browse available lesson slots from all instructors
|
||||||
|
- Book a lesson slot
|
||||||
|
|
||||||
|
**Capabilities:** `read`, `book_lesson`, `view_own_lessons`
|
||||||
|
|
||||||
|
## Implementation
|
||||||
|
- Class: `Unsupervised\Schedular\Roles\RoleManager`
|
||||||
|
- Roles are created on `plugins_loaded → init` and on plugin activation via `Installer`.
|
||||||
|
- Permissions are checked with `current_user_can()` against the capability string, not the role name.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
- `tests/Unit/Roles/RoleManagerTest.php`
|
||||||
11
phpstan.neon
Normal file
11
phpstan.neon
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
includes:
|
||||||
|
- vendor/szepeviktor/phpstan-wordpress/extension.neon
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
level: 6
|
||||||
|
paths:
|
||||||
|
- src
|
||||||
|
bootstrapFiles:
|
||||||
|
- tests/bootstrap.php
|
||||||
|
ignoreErrors:
|
||||||
|
- '#Unsafe usage of new static\(\)#'
|
||||||
22
phpunit.xml
Normal file
22
phpunit.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||||
|
bootstrap="tests/bootstrap.php"
|
||||||
|
colors="true"
|
||||||
|
displayDetailsOnTestsThatTriggerErrors="true"
|
||||||
|
displayDetailsOnTestsThatTriggerWarnings="true"
|
||||||
|
displayDetailsOnTestsThatTriggerDeprecations="true"
|
||||||
|
>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Unit">
|
||||||
|
<directory>tests/Unit</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<source>
|
||||||
|
<include>
|
||||||
|
<directory suffix=".php">src</directory>
|
||||||
|
</include>
|
||||||
|
</source>
|
||||||
|
</phpunit>
|
||||||
61
src/Admin/AdminMenu.php
Normal file
61
src/Admin/AdminMenu.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Admin;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class AdminMenu
|
||||||
|
{
|
||||||
|
private AvailabilityController $availabilityController;
|
||||||
|
private LessonController $lessonController;
|
||||||
|
|
||||||
|
public function __construct(AvailabilityRepository $availability, BookingRepository $bookings)
|
||||||
|
{
|
||||||
|
$this->availabilityController = new AvailabilityController($availability);
|
||||||
|
$this->lessonController = new LessonController($bookings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
add_action('admin_menu', [$this, 'addPages']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addPages(): void
|
||||||
|
{
|
||||||
|
// Admin-only dashboard: all upcoming lessons
|
||||||
|
add_menu_page(
|
||||||
|
__('Scheduler', 'unsupervised-schedular'),
|
||||||
|
__('Scheduler', 'unsupervised-schedular'),
|
||||||
|
'manage_options',
|
||||||
|
'us-scheduler',
|
||||||
|
[$this->lessonController, 'renderAdminDashboard'],
|
||||||
|
'dashicons-calendar-alt',
|
||||||
|
30
|
||||||
|
);
|
||||||
|
|
||||||
|
// Instructor: manage their own availability
|
||||||
|
add_menu_page(
|
||||||
|
__('My Availability', 'unsupervised-schedular'),
|
||||||
|
__('My Availability', 'unsupervised-schedular'),
|
||||||
|
RoleManager::CAP_MANAGE_AVAILABILITY,
|
||||||
|
'us-availability',
|
||||||
|
[$this->availabilityController, 'renderPage'],
|
||||||
|
'dashicons-clock',
|
||||||
|
31
|
||||||
|
);
|
||||||
|
|
||||||
|
// Instructor: view their upcoming lessons
|
||||||
|
add_menu_page(
|
||||||
|
__('My Lessons', 'unsupervised-schedular'),
|
||||||
|
__('My Lessons', 'unsupervised-schedular'),
|
||||||
|
RoleManager::CAP_VIEW_LESSONS,
|
||||||
|
'us-my-lessons',
|
||||||
|
[$this->lessonController, 'renderInstructorLessons'],
|
||||||
|
'dashicons-welcome-learn-more',
|
||||||
|
32
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/Admin/AvailabilityController.php
Normal file
54
src/Admin/AvailabilityController.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Admin;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Model\AvailabilitySlot;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class AvailabilityController
|
||||||
|
{
|
||||||
|
public function __construct(private AvailabilityRepository $repository) {}
|
||||||
|
|
||||||
|
public function renderPage(): void
|
||||||
|
{
|
||||||
|
if (! current_user_can(RoleManager::CAP_MANAGE_AVAILABILITY)) {
|
||||||
|
wp_die(esc_html__('You do not have permission to manage availability.', 'unsupervised-schedular'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$instructorId = get_current_user_id();
|
||||||
|
|
||||||
|
if (isset($_POST['usc_action']) && check_admin_referer('usc_availability_action')) {
|
||||||
|
$this->handleFormAction($instructorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
$slots = $this->repository->findByInstructor($instructorId);
|
||||||
|
|
||||||
|
include USC_PLUGIN_DIR . 'templates/admin/availability.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function handleFormAction(int $instructorId): void
|
||||||
|
{
|
||||||
|
$action = sanitize_key($_POST['usc_action'] ?? '');
|
||||||
|
|
||||||
|
if ($action === 'add') {
|
||||||
|
$startDt = sanitize_text_field($_POST['start_dt'] ?? '');
|
||||||
|
$endDt = sanitize_text_field($_POST['end_dt'] ?? '');
|
||||||
|
|
||||||
|
if ($startDt !== '' && $endDt !== '') {
|
||||||
|
$this->repository->insert(new AvailabilitySlot($instructorId, $startDt, $endDt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($action === 'delete') {
|
||||||
|
$slotId = absint($_POST['slot_id'] ?? 0);
|
||||||
|
if ($slotId > 0) {
|
||||||
|
$slot = $this->repository->findById($slotId);
|
||||||
|
if ($slot && $slot->instructorId === $instructorId) {
|
||||||
|
$this->repository->delete($slotId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/Admin/LessonController.php
Normal file
34
src/Admin/LessonController.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Admin;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class LessonController
|
||||||
|
{
|
||||||
|
public function __construct(private BookingRepository $repository) {}
|
||||||
|
|
||||||
|
public function renderAdminDashboard(): void
|
||||||
|
{
|
||||||
|
if (! current_user_can('manage_options')) {
|
||||||
|
wp_die(esc_html__('You do not have permission to view this page.', 'unsupervised-schedular'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$lessons = $this->repository->findAllUpcoming();
|
||||||
|
|
||||||
|
include USC_PLUGIN_DIR . 'templates/admin/lessons.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderInstructorLessons(): void
|
||||||
|
{
|
||||||
|
if (! current_user_can(RoleManager::CAP_VIEW_LESSONS)) {
|
||||||
|
wp_die(esc_html__('You do not have permission to view lessons.', 'unsupervised-schedular'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$lessons = $this->repository->findUpcomingForInstructor(get_current_user_id());
|
||||||
|
|
||||||
|
include USC_PLUGIN_DIR . 'templates/admin/lessons.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
102
src/Api/AvailabilityEndpoint.php
Normal file
102
src/Api/AvailabilityEndpoint.php
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Api;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Model\AvailabilitySlot;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class AvailabilityEndpoint
|
||||||
|
{
|
||||||
|
public function __construct(private AvailabilityRepository $repository) {}
|
||||||
|
|
||||||
|
public function registerRoutes(string $namespace): void
|
||||||
|
{
|
||||||
|
register_rest_route($namespace, '/availability', [
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::READABLE,
|
||||||
|
'callback' => [$this, 'index'],
|
||||||
|
'permission_callback' => [$this, 'canBook'],
|
||||||
|
'args' => [
|
||||||
|
'instructor_id' => ['type' => 'integer', 'default' => 0],
|
||||||
|
'from' => ['type' => 'string', 'default' => ''],
|
||||||
|
'to' => ['type' => 'string', 'default' => ''],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::CREATABLE,
|
||||||
|
'callback' => [$this, 'create'],
|
||||||
|
'permission_callback' => [$this, 'canManage'],
|
||||||
|
'args' => [
|
||||||
|
'start_dt' => ['type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field'],
|
||||||
|
'end_dt' => ['type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
register_rest_route($namespace, '/availability/(?P<id>\d+)', [
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::DELETABLE,
|
||||||
|
'callback' => [$this, 'delete'],
|
||||||
|
'permission_callback' => [$this, 'canManage'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
{
|
||||||
|
$slots = $this->repository->findAvailable(
|
||||||
|
(int) $request->get_param('instructor_id'),
|
||||||
|
(string) $request->get_param('from'),
|
||||||
|
(string) $request->get_param('to'),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new \WP_REST_Response(array_map(fn(AvailabilitySlot $s) => $s->toArray(), $slots), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
{
|
||||||
|
$slot = new AvailabilitySlot(
|
||||||
|
instructorId: get_current_user_id(),
|
||||||
|
startDt: (string) $request->get_param('start_dt'),
|
||||||
|
endDt: (string) $request->get_param('end_dt'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$id = $this->repository->insert($slot);
|
||||||
|
|
||||||
|
return new \WP_REST_Response(['id' => $id], 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(\WP_REST_Request $request): \WP_REST_Response|\WP_Error
|
||||||
|
{
|
||||||
|
$id = absint($request->get_param('id'));
|
||||||
|
$slot = $this->repository->findById($id);
|
||||||
|
|
||||||
|
if ($slot === null) {
|
||||||
|
return new \WP_Error('not_found', __('Slot not found.', 'unsupervised-schedular'), ['status' => 404]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($slot->instructorId !== get_current_user_id()) {
|
||||||
|
return new \WP_Error('forbidden', __('You cannot delete this slot.', 'unsupervised-schedular'), ['status' => 403]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($slot->isBooked) {
|
||||||
|
return new \WP_Error('slot_booked', __('Cannot delete a booked slot.', 'unsupervised-schedular'), ['status' => 409]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->repository->delete($id);
|
||||||
|
|
||||||
|
return new \WP_REST_Response(null, 204);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canBook(): bool
|
||||||
|
{
|
||||||
|
return is_user_logged_in() && current_user_can(RoleManager::CAP_BOOK_LESSON);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canManage(): bool
|
||||||
|
{
|
||||||
|
return is_user_logged_in() && current_user_can(RoleManager::CAP_MANAGE_AVAILABILITY);
|
||||||
|
}
|
||||||
|
}
|
||||||
123
src/Api/BookingEndpoint.php
Normal file
123
src/Api/BookingEndpoint.php
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Api;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Model\Lesson;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class BookingEndpoint
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private AvailabilityRepository $availability,
|
||||||
|
private BookingRepository $bookings,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function registerRoutes(string $namespace): void
|
||||||
|
{
|
||||||
|
register_rest_route($namespace, '/bookings', [
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::READABLE,
|
||||||
|
'callback' => [$this, 'myLessons'],
|
||||||
|
'permission_callback' => [$this, 'isLoggedIn'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::CREATABLE,
|
||||||
|
'callback' => [$this, 'book'],
|
||||||
|
'permission_callback' => [$this, 'canBook'],
|
||||||
|
'args' => [
|
||||||
|
'slot_id' => ['type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint'],
|
||||||
|
'notes' => ['type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_textarea_field'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
register_rest_route($namespace, '/bookings/(?P<id>\d+)/status', [
|
||||||
|
[
|
||||||
|
'methods' => \WP_REST_Server::EDITABLE,
|
||||||
|
'callback' => [$this, 'updateStatus'],
|
||||||
|
'permission_callback' => [$this, 'canManage'],
|
||||||
|
'args' => [
|
||||||
|
'status' => [
|
||||||
|
'type' => 'string',
|
||||||
|
'required' => true,
|
||||||
|
'enum' => Lesson::VALID_STATUSES,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function myLessons(\WP_REST_Request $request): \WP_REST_Response
|
||||||
|
{
|
||||||
|
$userId = get_current_user_id();
|
||||||
|
$lessons = current_user_can(RoleManager::CAP_MANAGE_AVAILABILITY)
|
||||||
|
? $this->bookings->findUpcomingForInstructor($userId)
|
||||||
|
: $this->bookings->findByStudent($userId);
|
||||||
|
|
||||||
|
return new \WP_REST_Response(array_map(fn(Lesson $l) => $l->toArray(), $lessons), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function book(\WP_REST_Request $request): \WP_REST_Response|\WP_Error
|
||||||
|
{
|
||||||
|
$slotId = (int) $request->get_param('slot_id');
|
||||||
|
$slot = $this->availability->findById($slotId);
|
||||||
|
|
||||||
|
if ($slot === null) {
|
||||||
|
return new \WP_Error('not_found', __('Slot not found.', 'unsupervised-schedular'), ['status' => 404]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($slot->isBooked) {
|
||||||
|
return new \WP_Error('slot_taken', __('This slot is already booked.', 'unsupervised-schedular'), ['status' => 409]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$lesson = new Lesson(
|
||||||
|
slotId: $slotId,
|
||||||
|
studentId: get_current_user_id(),
|
||||||
|
instructorId: $slot->instructorId,
|
||||||
|
notes: (string) $request->get_param('notes') ?: null,
|
||||||
|
);
|
||||||
|
|
||||||
|
$id = $this->bookings->insert($lesson);
|
||||||
|
$this->availability->markBooked($slotId);
|
||||||
|
|
||||||
|
return new \WP_REST_Response(['id' => $id, 'status' => Lesson::STATUS_PENDING], 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateStatus(\WP_REST_Request $request): \WP_REST_Response|\WP_Error
|
||||||
|
{
|
||||||
|
$id = absint($request->get_param('id'));
|
||||||
|
$lesson = $this->bookings->findById($id);
|
||||||
|
|
||||||
|
if ($lesson === null) {
|
||||||
|
return new \WP_Error('not_found', __('Booking not found.', 'unsupervised-schedular'), ['status' => 404]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($lesson->instructorId !== get_current_user_id() && ! current_user_can('manage_options')) {
|
||||||
|
return new \WP_Error('forbidden', __('You cannot update this booking.', 'unsupervised-schedular'), ['status' => 403]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->bookings->updateStatus($id, (string) $request->get_param('status'));
|
||||||
|
|
||||||
|
return new \WP_REST_Response(['id' => $id, 'status' => $request->get_param('status')], 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isLoggedIn(): bool
|
||||||
|
{
|
||||||
|
return is_user_logged_in();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canBook(): bool
|
||||||
|
{
|
||||||
|
return is_user_logged_in() && current_user_can(RoleManager::CAP_BOOK_LESSON);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canManage(): bool
|
||||||
|
{
|
||||||
|
return is_user_logged_in() && (
|
||||||
|
current_user_can(RoleManager::CAP_MANAGE_AVAILABILITY) || current_user_can('manage_options')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/Api/RestRegistrar.php
Normal file
32
src/Api/RestRegistrar.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Api;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
|
||||||
|
class RestRegistrar
|
||||||
|
{
|
||||||
|
public const NAMESPACE = 'us-scheduler/v1';
|
||||||
|
|
||||||
|
private AvailabilityEndpoint $availabilityEndpoint;
|
||||||
|
private BookingEndpoint $bookingEndpoint;
|
||||||
|
|
||||||
|
public function __construct(AvailabilityRepository $availability, BookingRepository $bookings)
|
||||||
|
{
|
||||||
|
$this->availabilityEndpoint = new AvailabilityEndpoint($availability);
|
||||||
|
$this->bookingEndpoint = new BookingEndpoint($availability, $bookings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
add_action('rest_api_init', [$this, 'registerRoutes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function registerRoutes(): void
|
||||||
|
{
|
||||||
|
$this->availabilityEndpoint->registerRoutes(self::NAMESPACE);
|
||||||
|
$this->bookingEndpoint->registerRoutes(self::NAMESPACE);
|
||||||
|
}
|
||||||
|
}
|
||||||
117
src/Data/AvailabilityRepository.php
Normal file
117
src/Data/AvailabilityRepository.php
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Data;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Model\AvailabilitySlot;
|
||||||
|
|
||||||
|
class AvailabilityRepository
|
||||||
|
{
|
||||||
|
private string $table;
|
||||||
|
|
||||||
|
public function __construct(private \wpdb $db)
|
||||||
|
{
|
||||||
|
$this->table = $db->prefix . 'us_availability';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insert(AvailabilitySlot $slot): int
|
||||||
|
{
|
||||||
|
$this->db->insert(
|
||||||
|
$this->table,
|
||||||
|
[
|
||||||
|
'instructor_id' => $slot->instructorId,
|
||||||
|
'start_dt' => $slot->startDt,
|
||||||
|
'end_dt' => $slot->endDt,
|
||||||
|
'is_booked' => 0,
|
||||||
|
'created_at' => current_time('mysql'),
|
||||||
|
],
|
||||||
|
['%d', '%s', '%s', '%d', '%s']
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->db->insert_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find unbooked slots, optionally filtered by instructor and date range.
|
||||||
|
*
|
||||||
|
* @return list<AvailabilitySlot>
|
||||||
|
*/
|
||||||
|
public function findAvailable(int $instructorId = 0, string $from = '', string $to = ''): array
|
||||||
|
{
|
||||||
|
$where = ['is_booked = 0'];
|
||||||
|
$params = [];
|
||||||
|
|
||||||
|
if ($instructorId > 0) {
|
||||||
|
$where[] = 'instructor_id = %d';
|
||||||
|
$params[] = $instructorId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($from !== '') {
|
||||||
|
$where[] = 'start_dt >= %s';
|
||||||
|
$params[] = $from;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($to !== '') {
|
||||||
|
$where[] = 'end_dt <= %s';
|
||||||
|
$params[] = $to;
|
||||||
|
}
|
||||||
|
|
||||||
|
$whereClause = implode(' AND ', $where);
|
||||||
|
$sql = "SELECT * FROM {$this->table} WHERE {$whereClause} ORDER BY start_dt ASC";
|
||||||
|
|
||||||
|
$rows = $params
|
||||||
|
? $this->db->get_results($this->db->prepare($sql, $params))
|
||||||
|
: $this->db->get_results($sql);
|
||||||
|
|
||||||
|
return array_map(AvailabilitySlot::fromRow(...), $rows ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all slots for an instructor (booked and unbooked).
|
||||||
|
*
|
||||||
|
* @return list<AvailabilitySlot>
|
||||||
|
*/
|
||||||
|
public function findByInstructor(int $instructorId): array
|
||||||
|
{
|
||||||
|
$rows = $this->db->get_results(
|
||||||
|
$this->db->prepare(
|
||||||
|
"SELECT * FROM {$this->table} WHERE instructor_id = %d ORDER BY start_dt ASC",
|
||||||
|
$instructorId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_map(AvailabilitySlot::fromRow(...), $rows ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findById(int $id): ?AvailabilitySlot
|
||||||
|
{
|
||||||
|
$row = $this->db->get_row(
|
||||||
|
$this->db->prepare("SELECT * FROM {$this->table} WHERE id = %d", $id)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $row ? AvailabilitySlot::fromRow($row) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function markBooked(int $id): bool
|
||||||
|
{
|
||||||
|
return (bool) $this->db->update(
|
||||||
|
$this->table,
|
||||||
|
['is_booked' => 1],
|
||||||
|
['id' => $id],
|
||||||
|
['%d'],
|
||||||
|
['%d']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an unbooked slot. Returns false if the slot is already booked.
|
||||||
|
*/
|
||||||
|
public function delete(int $id): bool
|
||||||
|
{
|
||||||
|
return (bool) $this->db->delete(
|
||||||
|
$this->table,
|
||||||
|
['id' => $id, 'is_booked' => 0],
|
||||||
|
['%d', '%d']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
125
src/Data/BookingRepository.php
Normal file
125
src/Data/BookingRepository.php
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Data;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Model\Lesson;
|
||||||
|
|
||||||
|
class BookingRepository
|
||||||
|
{
|
||||||
|
private string $table;
|
||||||
|
|
||||||
|
public function __construct(private \wpdb $db)
|
||||||
|
{
|
||||||
|
$this->table = $db->prefix . 'us_lessons';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insert(Lesson $lesson): int
|
||||||
|
{
|
||||||
|
$this->db->insert(
|
||||||
|
$this->table,
|
||||||
|
[
|
||||||
|
'slot_id' => $lesson->slotId,
|
||||||
|
'student_id' => $lesson->studentId,
|
||||||
|
'instructor_id' => $lesson->instructorId,
|
||||||
|
'status' => $lesson->status,
|
||||||
|
'notes' => $lesson->notes,
|
||||||
|
'created_at' => current_time('mysql'),
|
||||||
|
],
|
||||||
|
['%d', '%d', '%d', '%s', '%s', '%s']
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->db->insert_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findById(int $id): ?Lesson
|
||||||
|
{
|
||||||
|
$row = $this->db->get_row(
|
||||||
|
$this->db->prepare("SELECT * FROM {$this->table} WHERE id = %d", $id)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $row ? Lesson::fromRow($row) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upcoming lessons for an instructor (status != cancelled, slot in the future).
|
||||||
|
*
|
||||||
|
* @return list<Lesson>
|
||||||
|
*/
|
||||||
|
public function findUpcomingForInstructor(int $instructorId): array
|
||||||
|
{
|
||||||
|
$avTable = str_replace('us_lessons', 'us_availability', $this->table);
|
||||||
|
|
||||||
|
$rows = $this->db->get_results(
|
||||||
|
$this->db->prepare(
|
||||||
|
"SELECT l.* FROM {$this->table} l
|
||||||
|
JOIN {$avTable} a ON a.id = l.slot_id
|
||||||
|
WHERE l.instructor_id = %d
|
||||||
|
AND l.status != %s
|
||||||
|
AND a.start_dt >= %s
|
||||||
|
ORDER BY a.start_dt ASC",
|
||||||
|
$instructorId,
|
||||||
|
Lesson::STATUS_CANCELLED,
|
||||||
|
current_time('mysql')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_map(Lesson::fromRow(...), $rows ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All lessons for a student.
|
||||||
|
*
|
||||||
|
* @return list<Lesson>
|
||||||
|
*/
|
||||||
|
public function findByStudent(int $studentId): array
|
||||||
|
{
|
||||||
|
$rows = $this->db->get_results(
|
||||||
|
$this->db->prepare(
|
||||||
|
"SELECT * FROM {$this->table} WHERE student_id = %d ORDER BY created_at DESC",
|
||||||
|
$studentId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_map(Lesson::fromRow(...), $rows ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All upcoming lessons across all instructors (admin view).
|
||||||
|
*
|
||||||
|
* @return list<Lesson>
|
||||||
|
*/
|
||||||
|
public function findAllUpcoming(): array
|
||||||
|
{
|
||||||
|
$avTable = str_replace('us_lessons', 'us_availability', $this->table);
|
||||||
|
|
||||||
|
$rows = $this->db->get_results(
|
||||||
|
$this->db->prepare(
|
||||||
|
"SELECT l.* FROM {$this->table} l
|
||||||
|
JOIN {$avTable} a ON a.id = l.slot_id
|
||||||
|
WHERE l.status != %s
|
||||||
|
AND a.start_dt >= %s
|
||||||
|
ORDER BY a.start_dt ASC",
|
||||||
|
Lesson::STATUS_CANCELLED,
|
||||||
|
current_time('mysql')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_map(Lesson::fromRow(...), $rows ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateStatus(int $id, string $status): bool
|
||||||
|
{
|
||||||
|
if (! in_array($status, Lesson::VALID_STATUSES, true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (bool) $this->db->update(
|
||||||
|
$this->table,
|
||||||
|
['status' => $status],
|
||||||
|
['id' => $id],
|
||||||
|
['%s'],
|
||||||
|
['%d']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
src/Data/Schema.php
Normal file
43
src/Data/Schema.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Data;
|
||||||
|
|
||||||
|
class Schema
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns CREATE TABLE statements for dbDelta.
|
||||||
|
*
|
||||||
|
* @return list<string>
|
||||||
|
*/
|
||||||
|
public static function tables(string $prefix, string $charset): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"CREATE TABLE {$prefix}us_availability (
|
||||||
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
|
instructor_id BIGINT UNSIGNED NOT NULL,
|
||||||
|
start_dt DATETIME NOT NULL,
|
||||||
|
end_dt DATETIME NOT NULL,
|
||||||
|
is_booked TINYINT(1) NOT NULL DEFAULT 0,
|
||||||
|
created_at DATETIME NOT NULL,
|
||||||
|
PRIMARY KEY (id),
|
||||||
|
KEY instructor_id (instructor_id),
|
||||||
|
KEY start_dt (start_dt)
|
||||||
|
) {$charset};",
|
||||||
|
|
||||||
|
"CREATE TABLE {$prefix}us_lessons (
|
||||||
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
|
slot_id BIGINT UNSIGNED NOT NULL,
|
||||||
|
student_id BIGINT UNSIGNED NOT NULL,
|
||||||
|
instructor_id BIGINT UNSIGNED NOT NULL,
|
||||||
|
status VARCHAR(20) NOT NULL DEFAULT 'pending',
|
||||||
|
notes TEXT,
|
||||||
|
created_at DATETIME NOT NULL,
|
||||||
|
PRIMARY KEY (id),
|
||||||
|
KEY slot_id (slot_id),
|
||||||
|
KEY student_id (student_id),
|
||||||
|
KEY instructor_id (instructor_id)
|
||||||
|
) {$charset};",
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/Frontend/BookingPage.php
Normal file
39
src/Frontend/BookingPage.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Frontend;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class BookingPage
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private AvailabilityRepository $availability,
|
||||||
|
private BookingRepository $bookings,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function render(array $atts): string
|
||||||
|
{
|
||||||
|
if (! is_user_logged_in()) {
|
||||||
|
return sprintf(
|
||||||
|
'<p>%s <a href="%s">%s</a>.</p>',
|
||||||
|
esc_html__('Please', 'unsupervised-schedular'),
|
||||||
|
esc_url(wp_login_url(get_permalink())),
|
||||||
|
esc_html__('log in to book a lesson', 'unsupervised-schedular')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! current_user_can(RoleManager::CAP_BOOK_LESSON)) {
|
||||||
|
return '<p>' . esc_html__('This page is for students only.', 'unsupervised-schedular') . '</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_enqueue_style('us-scheduler');
|
||||||
|
wp_enqueue_script('us-scheduler');
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
include USC_PLUGIN_DIR . 'templates/frontend/booking-page.php';
|
||||||
|
return (string) ob_get_clean();
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/Frontend/LoginPage.php
Normal file
44
src/Frontend/LoginPage.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Frontend;
|
||||||
|
|
||||||
|
class LoginPage
|
||||||
|
{
|
||||||
|
public function render(array $atts): string
|
||||||
|
{
|
||||||
|
if (is_user_logged_in()) {
|
||||||
|
$redirect = esc_url(get_permalink());
|
||||||
|
return sprintf(
|
||||||
|
'<p>%s <a href="%s">%s</a>.</p>',
|
||||||
|
esc_html__('You are already logged in.', 'unsupervised-schedular'),
|
||||||
|
$redirect,
|
||||||
|
esc_html__('View available lessons', 'unsupervised-schedular')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$error = '';
|
||||||
|
$redirect = sanitize_url(get_permalink() ?? '');
|
||||||
|
|
||||||
|
if (isset($_POST['us_login']) && check_admin_referer('us_student_login')) {
|
||||||
|
$credentials = [
|
||||||
|
'user_login' => sanitize_user($_POST['log'] ?? ''),
|
||||||
|
'user_password' => $_POST['pwd'] ?? '',
|
||||||
|
'remember' => isset($_POST['rememberme']),
|
||||||
|
];
|
||||||
|
|
||||||
|
$user = wp_signon($credentials, false);
|
||||||
|
|
||||||
|
if (is_wp_error($user)) {
|
||||||
|
$error = esc_html__('Invalid username or password.', 'unsupervised-schedular');
|
||||||
|
} else {
|
||||||
|
wp_safe_redirect($redirect);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
include USC_PLUGIN_DIR . 'templates/frontend/login-page.php';
|
||||||
|
return (string) ob_get_clean();
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/Frontend/ShortcodeRegistrar.php
Normal file
37
src/Frontend/ShortcodeRegistrar.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Frontend;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
|
||||||
|
class ShortcodeRegistrar
|
||||||
|
{
|
||||||
|
private BookingPage $bookingPage;
|
||||||
|
private LoginPage $loginPage;
|
||||||
|
|
||||||
|
public function __construct(AvailabilityRepository $availability, BookingRepository $bookings)
|
||||||
|
{
|
||||||
|
$this->bookingPage = new BookingPage($availability, $bookings);
|
||||||
|
$this->loginPage = new LoginPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
add_shortcode('us_booking', [$this->bookingPage, 'render']);
|
||||||
|
add_shortcode('us_student_login', [$this->loginPage, 'render']);
|
||||||
|
add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enqueueAssets(): void
|
||||||
|
{
|
||||||
|
wp_register_style('us-scheduler', USC_PLUGIN_URL . 'assets/css/frontend.css', [], USC_VERSION);
|
||||||
|
wp_register_script('us-scheduler', USC_PLUGIN_URL . 'assets/js/booking.js', [], USC_VERSION, true);
|
||||||
|
|
||||||
|
wp_localize_script('us-scheduler', 'usScheduler', [
|
||||||
|
'restUrl' => rest_url('us-scheduler/v1/'),
|
||||||
|
'nonce' => wp_create_nonce('wp_rest'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/Installer.php
Normal file
30
src/Installer.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Data\Schema;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class Installer
|
||||||
|
{
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->createTables();
|
||||||
|
(new RoleManager())->createRoles();
|
||||||
|
flush_rewrite_rules();
|
||||||
|
update_option('us_schedular_version', USC_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createTables(): void
|
||||||
|
{
|
||||||
|
global $wpdb;
|
||||||
|
$charset = $wpdb->get_charset_collate();
|
||||||
|
|
||||||
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||||
|
|
||||||
|
foreach (Schema::tables($wpdb->prefix, $charset) as $sql) {
|
||||||
|
dbDelta($sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/Model/AvailabilitySlot.php
Normal file
37
src/Model/AvailabilitySlot.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Model;
|
||||||
|
|
||||||
|
class AvailabilitySlot
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly int $instructorId,
|
||||||
|
public readonly string $startDt,
|
||||||
|
public readonly string $endDt,
|
||||||
|
public readonly bool $isBooked = false,
|
||||||
|
public readonly ?int $id = null,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public static function fromRow(object $row): self
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
instructorId: (int) $row->instructor_id,
|
||||||
|
startDt: $row->start_dt,
|
||||||
|
endDt: $row->end_dt,
|
||||||
|
isBooked: (bool) $row->is_booked,
|
||||||
|
id: (int) $row->id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'instructor_id' => $this->instructorId,
|
||||||
|
'start_dt' => $this->startDt,
|
||||||
|
'end_dt' => $this->endDt,
|
||||||
|
'is_booked' => $this->isBooked,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/Model/Lesson.php
Normal file
47
src/Model/Lesson.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Model;
|
||||||
|
|
||||||
|
class Lesson
|
||||||
|
{
|
||||||
|
public const STATUS_PENDING = 'pending';
|
||||||
|
public const STATUS_CONFIRMED = 'confirmed';
|
||||||
|
public const STATUS_CANCELLED = 'cancelled';
|
||||||
|
|
||||||
|
/** @var list<string> */
|
||||||
|
public const VALID_STATUSES = [self::STATUS_PENDING, self::STATUS_CONFIRMED, self::STATUS_CANCELLED];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public readonly int $slotId,
|
||||||
|
public readonly int $studentId,
|
||||||
|
public readonly int $instructorId,
|
||||||
|
public readonly string $status = self::STATUS_PENDING,
|
||||||
|
public readonly ?string $notes = null,
|
||||||
|
public readonly ?int $id = null,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public static function fromRow(object $row): self
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
slotId: (int) $row->slot_id,
|
||||||
|
studentId: (int) $row->student_id,
|
||||||
|
instructorId: (int) $row->instructor_id,
|
||||||
|
status: $row->status,
|
||||||
|
notes: $row->notes,
|
||||||
|
id: (int) $row->id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'slot_id' => $this->slotId,
|
||||||
|
'student_id' => $this->studentId,
|
||||||
|
'instructor_id' => $this->instructorId,
|
||||||
|
'status' => $this->status,
|
||||||
|
'notes' => $this->notes,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/Plugin.php
Normal file
28
src/Plugin.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Admin\AdminMenu;
|
||||||
|
use Unsupervised\Schedular\Api\RestRegistrar;
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Frontend\ShortcodeRegistrar;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
|
||||||
|
class Plugin
|
||||||
|
{
|
||||||
|
public static function boot(): void
|
||||||
|
{
|
||||||
|
load_plugin_textdomain('unsupervised-schedular', false, dirname(plugin_basename(USC_PLUGIN_FILE)) . '/languages');
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
$availability = new AvailabilityRepository($wpdb);
|
||||||
|
$bookings = new BookingRepository($wpdb);
|
||||||
|
|
||||||
|
(new RoleManager())->register();
|
||||||
|
(new AdminMenu($availability, $bookings))->register();
|
||||||
|
(new RestRegistrar($availability, $bookings))->register();
|
||||||
|
(new ShortcodeRegistrar($availability, $bookings))->register();
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/Roles/RoleManager.php
Normal file
46
src/Roles/RoleManager.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Roles;
|
||||||
|
|
||||||
|
class RoleManager
|
||||||
|
{
|
||||||
|
public const INSTRUCTOR = 'us_instructor';
|
||||||
|
public const STUDENT = 'us_student';
|
||||||
|
|
||||||
|
public const CAP_MANAGE_AVAILABILITY = 'manage_availability';
|
||||||
|
public const CAP_VIEW_LESSONS = 'view_own_lessons';
|
||||||
|
public const CAP_BOOK_LESSON = 'book_lesson';
|
||||||
|
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
add_action('init', [$this, 'createRoles']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createRoles(): void
|
||||||
|
{
|
||||||
|
if (get_role(self::INSTRUCTOR) === null) {
|
||||||
|
add_role(
|
||||||
|
self::INSTRUCTOR,
|
||||||
|
__('Instructor', 'unsupervised-schedular'),
|
||||||
|
[
|
||||||
|
'read' => true,
|
||||||
|
self::CAP_MANAGE_AVAILABILITY => true,
|
||||||
|
self::CAP_VIEW_LESSONS => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (get_role(self::STUDENT) === null) {
|
||||||
|
add_role(
|
||||||
|
self::STUDENT,
|
||||||
|
__('Student', 'unsupervised-schedular'),
|
||||||
|
[
|
||||||
|
'read' => true,
|
||||||
|
self::CAP_BOOK_LESSON => true,
|
||||||
|
self::CAP_VIEW_LESSONS => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
67
templates/admin/availability.php
Normal file
67
templates/admin/availability.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var list<\Unsupervised\Schedular\Model\AvailabilitySlot> $slots */
|
||||||
|
?>
|
||||||
|
<div class="wrap">
|
||||||
|
<h1><?php esc_html_e('My Availability', 'unsupervised-schedular'); ?></h1>
|
||||||
|
|
||||||
|
<h2><?php esc_html_e('Add Slot', 'unsupervised-schedular'); ?></h2>
|
||||||
|
<form method="post">
|
||||||
|
<?php wp_nonce_field('usc_availability_action'); ?>
|
||||||
|
<input type="hidden" name="usc_action" value="add">
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><label for="start_dt"><?php esc_html_e('Start', 'unsupervised-schedular'); ?></label></th>
|
||||||
|
<td><input type="datetime-local" name="start_dt" id="start_dt" required></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><label for="end_dt"><?php esc_html_e('End', 'unsupervised-schedular'); ?></label></th>
|
||||||
|
<td><input type="datetime-local" name="end_dt" id="end_dt" required></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<?php submit_button(esc_html__('Add Slot', 'unsupervised-schedular')); ?>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<h2><?php esc_html_e('Current Slots', 'unsupervised-schedular'); ?></h2>
|
||||||
|
|
||||||
|
<?php if (empty($slots)) : ?>
|
||||||
|
<p><?php esc_html_e('No availability slots configured.', 'unsupervised-schedular'); ?></p>
|
||||||
|
<?php else : ?>
|
||||||
|
<table class="wp-list-table widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php esc_html_e('Start', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('End', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Status', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Actions', 'unsupervised-schedular'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($slots as $slot) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html($slot->startDt); ?></td>
|
||||||
|
<td><?php echo esc_html($slot->endDt); ?></td>
|
||||||
|
<td><?php echo $slot->isBooked ? esc_html__('Booked', 'unsupervised-schedular') : esc_html__('Available', 'unsupervised-schedular'); ?></td>
|
||||||
|
<td>
|
||||||
|
<?php if (! $slot->isBooked) : ?>
|
||||||
|
<form method="post" style="display:inline;">
|
||||||
|
<?php wp_nonce_field('usc_availability_action'); ?>
|
||||||
|
<input type="hidden" name="usc_action" value="delete">
|
||||||
|
<input type="hidden" name="slot_id" value="<?php echo esc_attr((string) $slot->id); ?>">
|
||||||
|
<button type="submit" class="button button-small button-link-delete">
|
||||||
|
<?php esc_html_e('Delete', 'unsupervised-schedular'); ?>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
43
templates/admin/lessons.php
Normal file
43
templates/admin/lessons.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var list<\Unsupervised\Schedular\Model\Lesson> $lessons */
|
||||||
|
?>
|
||||||
|
<div class="wrap">
|
||||||
|
<h1><?php esc_html_e('Lessons', 'unsupervised-schedular'); ?></h1>
|
||||||
|
|
||||||
|
<?php if (empty($lessons)) : ?>
|
||||||
|
<p><?php esc_html_e('No upcoming lessons.', 'unsupervised-schedular'); ?></p>
|
||||||
|
<?php else : ?>
|
||||||
|
<table class="wp-list-table widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php esc_html_e('Student', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Instructor', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Slot ID', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Status', 'unsupervised-schedular'); ?></th>
|
||||||
|
<th><?php esc_html_e('Notes', 'unsupervised-schedular'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($lessons as $lesson) : ?>
|
||||||
|
<?php
|
||||||
|
$student = get_userdata($lesson->studentId);
|
||||||
|
$instructor = get_userdata($lesson->instructorId);
|
||||||
|
?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html($student ? $student->display_name : (string) $lesson->studentId); ?></td>
|
||||||
|
<td><?php echo esc_html($instructor ? $instructor->display_name : (string) $lesson->instructorId); ?></td>
|
||||||
|
<td><?php echo esc_html((string) $lesson->slotId); ?></td>
|
||||||
|
<td><?php echo esc_html($lesson->status); ?></td>
|
||||||
|
<td><?php echo esc_html($lesson->notes ?? ''); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
16
templates/frontend/booking-page.php
Normal file
16
templates/frontend/booking-page.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<div id="us-booking-app" data-nonce="<?php echo esc_attr(wp_create_nonce('wp_rest')); ?>">
|
||||||
|
<div id="us-slot-list">
|
||||||
|
<p><?php esc_html_e('Loading available slots…', 'unsupervised-schedular'); ?></p>
|
||||||
|
</div>
|
||||||
|
<div id="us-booking-confirmation" style="display:none;">
|
||||||
|
<p><?php esc_html_e('Your lesson has been booked. The instructor will confirm shortly.', 'unsupervised-schedular'); ?></p>
|
||||||
|
</div>
|
||||||
|
<div id="us-booking-error" style="display:none;" role="alert"></div>
|
||||||
|
</div>
|
||||||
37
templates/frontend/login-page.php
Normal file
37
templates/frontend/login-page.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var string $error */
|
||||||
|
/** @var string $redirect */
|
||||||
|
?>
|
||||||
|
<div class="us-login-form">
|
||||||
|
<?php if ($error !== '') : ?>
|
||||||
|
<p class="us-error" role="alert"><?php echo esc_html($error); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<form method="post" action="">
|
||||||
|
<?php wp_nonce_field('us_student_login'); ?>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<label for="log"><?php esc_html_e('Username', 'unsupervised-schedular'); ?></label>
|
||||||
|
<input type="text" name="log" id="log" autocomplete="username" required>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label for="pwd"><?php esc_html_e('Password', 'unsupervised-schedular'); ?></label>
|
||||||
|
<input type="password" name="pwd" id="pwd" autocomplete="current-password" required>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="rememberme">
|
||||||
|
<?php esc_html_e('Remember me', 'unsupervised-schedular'); ?>
|
||||||
|
</label>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<input type="submit" name="us_login" value="<?php esc_attr_e('Log In', 'unsupervised-schedular'); ?>">
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
149
tests/Unit/Data/AvailabilityRepositoryTest.php
Normal file
149
tests/Unit/Data/AvailabilityRepositoryTest.php
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit\Data;
|
||||||
|
|
||||||
|
use Brain\Monkey\Functions;
|
||||||
|
use Mockery;
|
||||||
|
use Unsupervised\Schedular\Data\AvailabilityRepository;
|
||||||
|
use Unsupervised\Schedular\Model\AvailabilitySlot;
|
||||||
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||||
|
|
||||||
|
class AvailabilityRepositoryTest extends TestCase
|
||||||
|
{
|
||||||
|
private \wpdb $db;
|
||||||
|
private AvailabilityRepository $repo;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->db = Mockery::mock(\wpdb::class);
|
||||||
|
$this->db->prefix = 'wp_';
|
||||||
|
$this->repo = new AvailabilityRepository($this->db);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInsertCallsWpdbInsertAndReturnsId(): void
|
||||||
|
{
|
||||||
|
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
|
||||||
|
|
||||||
|
$this->db->shouldReceive('insert')
|
||||||
|
->once()
|
||||||
|
->with(
|
||||||
|
'wp_us_availability',
|
||||||
|
Mockery::on(static function (array $data): bool {
|
||||||
|
return $data['instructor_id'] === 5
|
||||||
|
&& $data['start_dt'] === '2026-04-01 09:00:00'
|
||||||
|
&& $data['is_booked'] === 0;
|
||||||
|
}),
|
||||||
|
['%d', '%s', '%s', '%d', '%s']
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->db->insert_id = 42;
|
||||||
|
|
||||||
|
$slot = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
|
||||||
|
$result = $this->repo->insert($slot);
|
||||||
|
|
||||||
|
self::assertSame(42, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByIdReturnsNullWhenNotFound(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('prepare')
|
||||||
|
->once()
|
||||||
|
->andReturn('SELECT * FROM wp_us_availability WHERE id = 99');
|
||||||
|
|
||||||
|
$this->db->shouldReceive('get_row')
|
||||||
|
->once()
|
||||||
|
->andReturn(null);
|
||||||
|
|
||||||
|
$result = $this->repo->findById(99);
|
||||||
|
|
||||||
|
self::assertNull($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByIdReturnsSlotWhenFound(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '10',
|
||||||
|
'instructor_id' => '5',
|
||||||
|
'start_dt' => '2026-04-01 09:00:00',
|
||||||
|
'end_dt' => '2026-04-01 10:00:00',
|
||||||
|
'is_booked' => '0',
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||||
|
$this->db->shouldReceive('get_row')->andReturn($row);
|
||||||
|
|
||||||
|
$slot = $this->repo->findById(10);
|
||||||
|
|
||||||
|
self::assertInstanceOf(AvailabilitySlot::class, $slot);
|
||||||
|
self::assertSame(10, $slot->id);
|
||||||
|
self::assertSame(5, $slot->instructorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMarkBookedUpdatesRecord(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('update')
|
||||||
|
->once()
|
||||||
|
->with('wp_us_availability', ['is_booked' => 1], ['id' => 7], ['%d'], ['%d'])
|
||||||
|
->andReturn(1);
|
||||||
|
|
||||||
|
$result = $this->repo->markBooked(7);
|
||||||
|
|
||||||
|
self::assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDeleteReturnsFalseWhenRowNotDeleted(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('delete')
|
||||||
|
->once()
|
||||||
|
->with('wp_us_availability', ['id' => 1, 'is_booked' => 0], ['%d', '%d'])
|
||||||
|
->andReturn(0);
|
||||||
|
|
||||||
|
self::assertFalse($this->repo->delete(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindAvailableWithNoFiltersUsesNoParams(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('get_results')
|
||||||
|
->once()
|
||||||
|
->with(Mockery::pattern('/WHERE is_booked = 0/'))
|
||||||
|
->andReturn([]);
|
||||||
|
|
||||||
|
$result = $this->repo->findAvailable();
|
||||||
|
|
||||||
|
self::assertSame([], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindAvailableWithInstructorFilterPreparesQuery(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('prepare')
|
||||||
|
->once()
|
||||||
|
->with(Mockery::pattern('/instructor_id = %d/'), Mockery::any())
|
||||||
|
->andReturn('SELECT ...');
|
||||||
|
|
||||||
|
$this->db->shouldReceive('get_results')->andReturn([]);
|
||||||
|
|
||||||
|
$this->repo->findAvailable(instructorId: 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByInstructorReturnsSlots(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '5',
|
||||||
|
'instructor_id' => '3',
|
||||||
|
'start_dt' => '2026-04-01 09:00:00',
|
||||||
|
'end_dt' => '2026-04-01 10:00:00',
|
||||||
|
'is_booked' => '0',
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||||
|
$this->db->shouldReceive('get_results')->andReturn([$row]);
|
||||||
|
|
||||||
|
$slots = $this->repo->findByInstructor(3);
|
||||||
|
|
||||||
|
self::assertCount(1, $slots);
|
||||||
|
self::assertInstanceOf(AvailabilitySlot::class, $slots[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
126
tests/Unit/Data/BookingRepositoryTest.php
Normal file
126
tests/Unit/Data/BookingRepositoryTest.php
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit\Data;
|
||||||
|
|
||||||
|
use Brain\Monkey\Functions;
|
||||||
|
use Mockery;
|
||||||
|
use Unsupervised\Schedular\Data\BookingRepository;
|
||||||
|
use Unsupervised\Schedular\Model\Lesson;
|
||||||
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||||
|
|
||||||
|
class BookingRepositoryTest extends TestCase
|
||||||
|
{
|
||||||
|
private \wpdb $db;
|
||||||
|
private BookingRepository $repo;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->db = Mockery::mock(\wpdb::class);
|
||||||
|
$this->db->prefix = 'wp_';
|
||||||
|
$this->repo = new BookingRepository($this->db);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInsertCallsWpdbInsertAndReturnsId(): void
|
||||||
|
{
|
||||||
|
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
|
||||||
|
|
||||||
|
$this->db->shouldReceive('insert')
|
||||||
|
->once()
|
||||||
|
->with(
|
||||||
|
'wp_us_lessons',
|
||||||
|
Mockery::on(static function (array $data): bool {
|
||||||
|
return $data['slot_id'] === 10
|
||||||
|
&& $data['student_id'] === 5
|
||||||
|
&& $data['status'] === Lesson::STATUS_PENDING;
|
||||||
|
}),
|
||||||
|
['%d', '%d', '%d', '%s', '%s', '%s']
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->db->insert_id = 77;
|
||||||
|
|
||||||
|
$lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3);
|
||||||
|
$result = $this->repo->insert($lesson);
|
||||||
|
|
||||||
|
self::assertSame(77, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByIdReturnsNullWhenNotFound(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||||
|
$this->db->shouldReceive('get_row')->andReturn(null);
|
||||||
|
|
||||||
|
self::assertNull($this->repo->findById(99));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByIdReturnsLesson(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '15',
|
||||||
|
'slot_id' => '10',
|
||||||
|
'student_id' => '5',
|
||||||
|
'instructor_id' => '3',
|
||||||
|
'status' => 'pending',
|
||||||
|
'notes' => null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||||
|
$this->db->shouldReceive('get_row')->andReturn($row);
|
||||||
|
|
||||||
|
$lesson = $this->repo->findById(15);
|
||||||
|
|
||||||
|
self::assertInstanceOf(Lesson::class, $lesson);
|
||||||
|
self::assertSame(15, $lesson->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateStatusReturnsFalseForInvalidStatus(): void
|
||||||
|
{
|
||||||
|
$result = $this->repo->updateStatus(1, 'invalid');
|
||||||
|
self::assertFalse($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateStatusCallsWpdbUpdate(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('update')
|
||||||
|
->once()
|
||||||
|
->with(
|
||||||
|
'wp_us_lessons',
|
||||||
|
['status' => Lesson::STATUS_CONFIRMED],
|
||||||
|
['id' => 1],
|
||||||
|
['%s'],
|
||||||
|
['%d']
|
||||||
|
)
|
||||||
|
->andReturn(1);
|
||||||
|
|
||||||
|
self::assertTrue($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdateStatusReturnsFalseWhenDbFails(): void
|
||||||
|
{
|
||||||
|
$this->db->shouldReceive('update')->andReturn(0);
|
||||||
|
|
||||||
|
self::assertFalse($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindByStudentReturnsLessons(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '1',
|
||||||
|
'slot_id' => '2',
|
||||||
|
'student_id' => '5',
|
||||||
|
'instructor_id' => '3',
|
||||||
|
'status' => 'pending',
|
||||||
|
'notes' => null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||||
|
$this->db->shouldReceive('get_results')->andReturn([$row]);
|
||||||
|
|
||||||
|
$lessons = $this->repo->findByStudent(5);
|
||||||
|
|
||||||
|
self::assertCount(1, $lessons);
|
||||||
|
self::assertInstanceOf(Lesson::class, $lessons[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
68
tests/Unit/Model/AvailabilitySlotTest.php
Normal file
68
tests/Unit/Model/AvailabilitySlotTest.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit\Model;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Model\AvailabilitySlot;
|
||||||
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||||
|
|
||||||
|
class AvailabilitySlotTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstructorAndProperties(): void
|
||||||
|
{
|
||||||
|
$slot = new AvailabilitySlot(
|
||||||
|
instructorId: 5,
|
||||||
|
startDt: '2026-04-01 09:00:00',
|
||||||
|
endDt: '2026-04-01 10:00:00',
|
||||||
|
isBooked: false,
|
||||||
|
id: 42,
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertSame(5, $slot->instructorId);
|
||||||
|
self::assertSame('2026-04-01 09:00:00', $slot->startDt);
|
||||||
|
self::assertSame('2026-04-01 10:00:00', $slot->endDt);
|
||||||
|
self::assertFalse($slot->isBooked);
|
||||||
|
self::assertSame(42, $slot->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromRowMapsCorrectly(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '7',
|
||||||
|
'instructor_id' => '3',
|
||||||
|
'start_dt' => '2026-05-10 14:00:00',
|
||||||
|
'end_dt' => '2026-05-10 15:00:00',
|
||||||
|
'is_booked' => '1',
|
||||||
|
];
|
||||||
|
|
||||||
|
$slot = AvailabilitySlot::fromRow($row);
|
||||||
|
|
||||||
|
self::assertSame(7, $slot->id);
|
||||||
|
self::assertSame(3, $slot->instructorId);
|
||||||
|
self::assertTrue($slot->isBooked);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToArrayContainsExpectedKeys(): void
|
||||||
|
{
|
||||||
|
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00', false, 10);
|
||||||
|
$arr = $slot->toArray();
|
||||||
|
|
||||||
|
self::assertArrayHasKey('id', $arr);
|
||||||
|
self::assertArrayHasKey('instructor_id', $arr);
|
||||||
|
self::assertArrayHasKey('start_dt', $arr);
|
||||||
|
self::assertArrayHasKey('end_dt', $arr);
|
||||||
|
self::assertArrayHasKey('is_booked', $arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefaultIsBookedIsFalse(): void
|
||||||
|
{
|
||||||
|
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
|
||||||
|
self::assertFalse($slot->isBooked);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefaultIdIsNull(): void
|
||||||
|
{
|
||||||
|
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00');
|
||||||
|
self::assertNull($slot->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
68
tests/Unit/Model/LessonTest.php
Normal file
68
tests/Unit/Model/LessonTest.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit\Model;
|
||||||
|
|
||||||
|
use Unsupervised\Schedular\Model\Lesson;
|
||||||
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||||
|
|
||||||
|
class LessonTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testStatusConstants(): void
|
||||||
|
{
|
||||||
|
self::assertSame('pending', Lesson::STATUS_PENDING);
|
||||||
|
self::assertSame('confirmed', Lesson::STATUS_CONFIRMED);
|
||||||
|
self::assertSame('cancelled', Lesson::STATUS_CANCELLED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidStatusesContainsAllThree(): void
|
||||||
|
{
|
||||||
|
self::assertContains(Lesson::STATUS_PENDING, Lesson::VALID_STATUSES);
|
||||||
|
self::assertContains(Lesson::STATUS_CONFIRMED, Lesson::VALID_STATUSES);
|
||||||
|
self::assertContains(Lesson::STATUS_CANCELLED, Lesson::VALID_STATUSES);
|
||||||
|
self::assertCount(3, Lesson::VALID_STATUSES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstructorDefaults(): void
|
||||||
|
{
|
||||||
|
$lesson = new Lesson(slotId: 1, studentId: 2, instructorId: 3);
|
||||||
|
|
||||||
|
self::assertSame(Lesson::STATUS_PENDING, $lesson->status);
|
||||||
|
self::assertNull($lesson->notes);
|
||||||
|
self::assertNull($lesson->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromRowMapsCorrectly(): void
|
||||||
|
{
|
||||||
|
$row = (object) [
|
||||||
|
'id' => '99',
|
||||||
|
'slot_id' => '10',
|
||||||
|
'student_id' => '20',
|
||||||
|
'instructor_id' => '30',
|
||||||
|
'status' => 'confirmed',
|
||||||
|
'notes' => 'Bring your guitar.',
|
||||||
|
];
|
||||||
|
|
||||||
|
$lesson = Lesson::fromRow($row);
|
||||||
|
|
||||||
|
self::assertSame(99, $lesson->id);
|
||||||
|
self::assertSame(10, $lesson->slotId);
|
||||||
|
self::assertSame(20, $lesson->studentId);
|
||||||
|
self::assertSame(30, $lesson->instructorId);
|
||||||
|
self::assertSame('confirmed', $lesson->status);
|
||||||
|
self::assertSame('Bring your guitar.', $lesson->notes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToArrayContainsExpectedKeys(): void
|
||||||
|
{
|
||||||
|
$lesson = new Lesson(1, 2, 3, Lesson::STATUS_PENDING, 'Note', 5);
|
||||||
|
$arr = $lesson->toArray();
|
||||||
|
|
||||||
|
self::assertArrayHasKey('id', $arr);
|
||||||
|
self::assertArrayHasKey('slot_id', $arr);
|
||||||
|
self::assertArrayHasKey('student_id', $arr);
|
||||||
|
self::assertArrayHasKey('instructor_id', $arr);
|
||||||
|
self::assertArrayHasKey('status', $arr);
|
||||||
|
self::assertArrayHasKey('notes', $arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
70
tests/Unit/Roles/RoleManagerTest.php
Normal file
70
tests/Unit/Roles/RoleManagerTest.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit\Roles;
|
||||||
|
|
||||||
|
use Brain\Monkey\Functions;
|
||||||
|
use Unsupervised\Schedular\Roles\RoleManager;
|
||||||
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||||
|
|
||||||
|
class RoleManagerTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testRegisterAddsInitHook(): void
|
||||||
|
{
|
||||||
|
Functions\expect('add_action')
|
||||||
|
->once()
|
||||||
|
->with('init', \Mockery::any());
|
||||||
|
|
||||||
|
(new RoleManager())->register();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateRolesSkipsExistingRoles(): void
|
||||||
|
{
|
||||||
|
Functions\when('get_role')->alias(static fn() => new \stdClass());
|
||||||
|
Functions\expect('add_role')->never();
|
||||||
|
|
||||||
|
(new RoleManager())->createRoles();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateRolesAddsInstructorRoleWithCorrectCaps(): void
|
||||||
|
{
|
||||||
|
Functions\when('get_role')->alias(static function (string $role): ?object {
|
||||||
|
return $role === RoleManager::INSTRUCTOR ? null : new \stdClass();
|
||||||
|
});
|
||||||
|
|
||||||
|
Functions\expect('add_role')
|
||||||
|
->once()
|
||||||
|
->with(
|
||||||
|
RoleManager::INSTRUCTOR,
|
||||||
|
\Mockery::any(),
|
||||||
|
\Mockery::on(static function (array $caps): bool {
|
||||||
|
return ($caps['read'] ?? false) === true
|
||||||
|
&& ($caps[RoleManager::CAP_MANAGE_AVAILABILITY] ?? false) === true
|
||||||
|
&& ($caps[RoleManager::CAP_VIEW_LESSONS] ?? false) === true;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
(new RoleManager())->createRoles();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateRolesAddsStudentRoleWithCorrectCaps(): void
|
||||||
|
{
|
||||||
|
Functions\when('get_role')->alias(static function (string $role): ?object {
|
||||||
|
return $role === RoleManager::STUDENT ? null : new \stdClass();
|
||||||
|
});
|
||||||
|
|
||||||
|
Functions\expect('add_role')
|
||||||
|
->once()
|
||||||
|
->with(
|
||||||
|
RoleManager::STUDENT,
|
||||||
|
\Mockery::any(),
|
||||||
|
\Mockery::on(static function (array $caps): bool {
|
||||||
|
return ($caps['read'] ?? false) === true
|
||||||
|
&& ($caps[RoleManager::CAP_BOOK_LESSON] ?? false) === true
|
||||||
|
&& ($caps[RoleManager::CAP_VIEW_LESSONS] ?? false) === true;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
(new RoleManager())->createRoles();
|
||||||
|
}
|
||||||
|
}
|
||||||
27
tests/Unit/TestCase.php
Normal file
27
tests/Unit/TestCase.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular\Tests\Unit;
|
||||||
|
|
||||||
|
use Brain\Monkey;
|
||||||
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||||
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
||||||
|
|
||||||
|
abstract class TestCase extends BaseTestCase
|
||||||
|
{
|
||||||
|
use MockeryPHPUnitIntegration;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Monkey\setUp();
|
||||||
|
Monkey\Functions\stubTranslationFunctions();
|
||||||
|
Monkey\Functions\stubEscapeFunctions();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
Monkey\tearDown();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
15
tests/bootstrap.php
Normal file
15
tests/bootstrap.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
require_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
// Minimal WordPress constants required by plugin code under test.
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
define('ABSPATH', sys_get_temp_dir() . '/wordpress/');
|
||||||
|
}
|
||||||
|
|
||||||
|
define('WPINC', 'wp-includes');
|
||||||
|
define('USC_VERSION', '1.0.0');
|
||||||
|
define('USC_PLUGIN_FILE', dirname(__DIR__) . '/unsupervised-schedular.php');
|
||||||
|
define('USC_PLUGIN_DIR', dirname(__DIR__) . '/');
|
||||||
|
define('USC_PLUGIN_URL', 'http://example.com/wp-content/plugins/unsupervised-schedular/');
|
||||||
18
uninstall.php
Normal file
18
uninstall.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (! defined('WP_UNINSTALL_PLUGIN')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}us_lessons");
|
||||||
|
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}us_availability");
|
||||||
|
|
||||||
|
remove_role('us_instructor');
|
||||||
|
remove_role('us_student');
|
||||||
|
|
||||||
|
delete_option('us_schedular_version');
|
||||||
41
unsupervised-schedular.php
Normal file
41
unsupervised-schedular.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: Unsupervised Scheduler
|
||||||
|
* Plugin URI: https://unsupervised.ca
|
||||||
|
* Description: Instructor/student lesson scheduling for WordPress.
|
||||||
|
* Version: 1.0.0
|
||||||
|
* Requires at least: 6.0
|
||||||
|
* Requires PHP: 8.1
|
||||||
|
* Author: Unsupervised
|
||||||
|
* License: GPL-2.0-or-later
|
||||||
|
* Text Domain: unsupervised-schedular
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Unsupervised\Schedular;
|
||||||
|
|
||||||
|
if (! defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
define('USC_VERSION', '1.0.0');
|
||||||
|
define('USC_PLUGIN_FILE', __FILE__);
|
||||||
|
define('USC_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||||
|
define('USC_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||||
|
define('USC_TABLE_AVAILABILITY', $GLOBALS['wpdb']->prefix . 'us_availability');
|
||||||
|
define('USC_TABLE_LESSONS', $GLOBALS['wpdb']->prefix . 'us_lessons');
|
||||||
|
|
||||||
|
require_once USC_PLUGIN_DIR . 'vendor/autoload.php';
|
||||||
|
|
||||||
|
register_activation_hook(__FILE__, static function (): void {
|
||||||
|
(new Installer())->run();
|
||||||
|
});
|
||||||
|
|
||||||
|
register_deactivation_hook(__FILE__, static function (): void {
|
||||||
|
flush_rewrite_rules();
|
||||||
|
});
|
||||||
|
|
||||||
|
add_action('plugins_loaded', static function (): void {
|
||||||
|
Plugin::boot();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user