Merge pull request 'Default lessons to a week view on the booking page and in wp-admin' (#80) from fix/week-view-default into main
CI / Tests (PHP 8.2) (push) Successful in 38s
CI / Tests (PHP 8.1) (push) Successful in 43s
CI / No Debug Code (push) Successful in 2s
CI / Coding Standards (push) Successful in 2m52s
CI / PHPStan (push) Successful in 2m53s
CI / Tests (PHP 8.3) (push) Successful in 2m42s
CI / Build Plugin Zip (push) Successful in 2m46s
CI / Tests (PHP 8.2) (push) Successful in 38s
CI / Tests (PHP 8.1) (push) Successful in 43s
CI / No Debug Code (push) Successful in 2s
CI / Coding Standards (push) Successful in 2m52s
CI / PHPStan (push) Successful in 2m53s
CI / Tests (PHP 8.3) (push) Successful in 2m42s
CI / Build Plugin Zip (push) Successful in 2m46s
Reviewed-on: #80
This commit was merged in pull request #80.
This commit is contained in:
@@ -49,6 +49,36 @@ class WeekCalendar {
|
||||
return $days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bucket arbitrary items into the seven days of the week starting at
|
||||
* `$weekStart` (`Y-m-d`), using `$dayOf` to extract each item's `Y-m-d` day.
|
||||
* Every day is present, empty or not, in calendar order.
|
||||
*
|
||||
* @template T
|
||||
* @param list<T> $items
|
||||
* @param callable(T): string $dayOf
|
||||
* @return list<array{date: string, items: list<T>}>
|
||||
*/
|
||||
public static function bucket( string $weekStart, array $items, callable $dayOf ): array {
|
||||
$start = self::parseDay( $weekStart ) ?? new \DateTimeImmutable( 'today' );
|
||||
|
||||
$byDay = [];
|
||||
foreach ( $items as $item ) {
|
||||
$byDay[ $dayOf( $item ) ][] = $item;
|
||||
}
|
||||
|
||||
$days = [];
|
||||
for ( $i = 0; $i < 7; $i++ ) {
|
||||
$date = $start->modify( '+' . $i . ' days' )->format( 'Y-m-d' );
|
||||
$days[] = [
|
||||
'date' => $date,
|
||||
'items' => $byDay[ $date ] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
|
||||
private static function parseDay( string $value ): ?\DateTimeImmutable {
|
||||
$day = \DateTimeImmutable::createFromFormat( '!Y-m-d', $value );
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Unsupervised\Schedular\Booking;
|
||||
use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
||||
use Unsupervised\Schedular\Availability\AvailabilitySlot;
|
||||
use Unsupervised\Schedular\Availability\WeekCalendar;
|
||||
use Unsupervised\Schedular\Payment\Payment;
|
||||
use Unsupervised\Schedular\Payment\PaymentRepository;
|
||||
use Unsupervised\Schedular\Val;
|
||||
@@ -27,7 +28,7 @@ class LessonController {
|
||||
|
||||
$rows = array_map( fn( Lesson $lesson ): array => $this->row( $lesson ), $this->repository->findAllUpcoming() );
|
||||
|
||||
include USC_PLUGIN_DIR . 'templates/admin/lessons.php';
|
||||
$this->renderLessonsPage( $rows, 'us-scheduler' );
|
||||
}
|
||||
|
||||
public function renderInstructorLessons(): void {
|
||||
@@ -39,6 +40,29 @@ class LessonController {
|
||||
|
||||
$rows = array_map( fn( Lesson $lesson ): array => $this->row( $lesson ), $this->repository->findUpcomingForInstructor( get_current_user_id() ) );
|
||||
|
||||
$this->renderLessonsPage( $rows, 'us-my-lessons' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the lessons template with its calendar view state: week (default)
|
||||
* or list, plus which week the week view shows.
|
||||
*
|
||||
* @param list<array<string, mixed>> $rows
|
||||
*/
|
||||
private function renderLessonsPage( array $rows, string $pageSlug ): void {
|
||||
// View-state query params only (which view, which week) — nothing is
|
||||
// mutated from them, so no nonce applies.
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
$view = 'list' === sanitize_key( Val::string( wp_unslash( $_GET['usc_view'] ?? '' ) ) ) ? 'list' : 'week';
|
||||
$requestedWeek = sanitize_text_field( Val::string( wp_unslash( $_GET['usc_week'] ?? '' ) ) );
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
$weekStart = WeekCalendar::weekStart( $requestedWeek, Val::int( get_option( 'start_of_week', 1 ) ), current_time( 'Y-m-d' ) );
|
||||
$weekDays = WeekCalendar::bucket( $weekStart, $rows, static fn( array $row ): string => Val::string( $row['day'] ) );
|
||||
$prevWeek = ( new \DateTimeImmutable( $weekStart ) )->modify( '-7 days' )->format( 'Y-m-d' );
|
||||
$nextWeek = ( new \DateTimeImmutable( $weekStart ) )->modify( '+7 days' )->format( 'Y-m-d' );
|
||||
$baseUrl = admin_url( 'admin.php?page=' . $pageSlug );
|
||||
|
||||
include USC_PLUGIN_DIR . 'templates/admin/lessons.php';
|
||||
}
|
||||
|
||||
@@ -92,6 +116,8 @@ class LessonController {
|
||||
'student' => $student ? $student->display_name : (string) $lesson->studentId,
|
||||
'instructor' => $instructor ? $instructor->display_name : (string) $lesson->instructorId,
|
||||
'time' => $slot ? $this->formatSlotTime( $slot ) : '—',
|
||||
'day' => $slot ? substr( $slot->startDt, 0, 10 ) : '',
|
||||
'time_short' => $slot ? Val::string( mysql2date( 'g:i A', $slot->startDt ) ) : '—',
|
||||
'status' => $lesson->status,
|
||||
'notes' => $lesson->notes ?? '',
|
||||
'payment_id' => $payment ? (int) $payment->id : 0,
|
||||
|
||||
Reference in New Issue
Block a user