%s %s.
', - esc_html__('Please', 'unsupervised-schedular'), - esc_url(wp_login_url(get_permalink())), - esc_html__('log in to book a lesson', 'unsupervised-schedular') - ); - } +class BookingPage { - if (! current_user_can(RoleManager::CAP_BOOK_LESSON)) { - return '' . esc_html__('This page is for students only.', 'unsupervised-schedular') . '
'; - } + /** + * Renders the booking shortcode output. + * + * @param array%s %s.
', + esc_html__( 'Please', 'unsupervised-schedular' ), + esc_url( wp_login_url( get_permalink() ) ), + esc_html__( 'log in to book a lesson', 'unsupervised-schedular' ) + ); + } - wp_enqueue_style('us-scheduler'); - wp_enqueue_script('us-scheduler'); + if ( ! current_user_can( RoleManager::CAP_BOOK_LESSON ) ) { + return '' . esc_html__( 'This page is for students only.', 'unsupervised-schedular' ) . '
'; + } - ob_start(); - include USC_PLUGIN_DIR . 'templates/frontend/booking-page.php'; - return (string) ob_get_clean(); - } + 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(); + } } diff --git a/src/Frontend/LoginPage.php b/src/Frontend/LoginPage.php index d779753..5258e95 100644 --- a/src/Frontend/LoginPage.php +++ b/src/Frontend/LoginPage.php @@ -3,45 +3,47 @@ declare(strict_types=1); namespace Unsupervised\Schedular\Frontend; -class LoginPage -{ - /** - * @param array%s %s.
', - esc_html__('You are already logged in.', 'unsupervised-schedular'), - $redirect, - esc_html__('View available lessons', 'unsupervised-schedular') - ); - } +class LoginPage { - $error = ''; - $redirect = sanitize_url((string) get_permalink()); + /** + * Renders the student login shortcode output. + * + * @param array%s %s.
', + esc_html__( 'You are already logged in.', 'unsupervised-schedular' ), + $redirect, + esc_html__( 'View available lessons', 'unsupervised-schedular' ) + ); + } - 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']), - ]; + $error = ''; + $redirect = sanitize_url( (string) get_permalink() ); - $user = wp_signon($credentials, false); + if ( isset( $_POST['us_login'] ) && check_admin_referer( 'us_student_login' ) ) { + $credentials = [ + 'user_login' => sanitize_user( wp_unslash( $_POST['log'] ?? '' ) ), + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- passwords must not be sanitized. + 'user_password' => wp_unslash( $_POST['pwd'] ?? '' ), + 'remember' => isset( $_POST['rememberme'] ), + ]; - if (is_wp_error($user)) { - $error = esc_html__('Invalid username or password.', 'unsupervised-schedular'); - } else { - wp_safe_redirect($redirect); - exit; - } - } + $user = wp_signon( $credentials, false ); - ob_start(); - include USC_PLUGIN_DIR . 'templates/frontend/login-page.php'; - return (string) ob_get_clean(); - } + 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(); + } } diff --git a/src/Frontend/ShortcodeRegistrar.php b/src/Frontend/ShortcodeRegistrar.php index 4eaaf2c..a6d9acd 100644 --- a/src/Frontend/ShortcodeRegistrar.php +++ b/src/Frontend/ShortcodeRegistrar.php @@ -3,32 +3,33 @@ declare(strict_types=1); namespace Unsupervised\Schedular\Frontend; -class ShortcodeRegistrar -{ - private BookingPage $bookingPage; - private LoginPage $loginPage; +class ShortcodeRegistrar { - public function __construct() - { - $this->bookingPage = new BookingPage(); - $this->loginPage = new LoginPage(); - } + private BookingPage $bookingPage; + private LoginPage $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 __construct() { + $this->bookingPage = new BookingPage(); + $this->loginPage = new LoginPage(); + } - 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); + 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' ] ); + } - wp_localize_script('us-scheduler', 'usScheduler', [ - 'restUrl' => rest_url('us-scheduler/v1/'), - 'nonce' => wp_create_nonce('wp_rest'), - ]); - } + 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' ), + ] + ); + } } diff --git a/src/Installer.php b/src/Installer.php index b83bc97..ec97793 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -6,25 +6,23 @@ 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); - } +class Installer { - private function createTables(): void - { - global $wpdb; - $charset = $wpdb->get_charset_collate(); + public function run(): void { + $this->createTables(); + ( new RoleManager() )->createRoles(); + flush_rewrite_rules(); + update_option( 'us_schedular_version', USC_VERSION ); + } - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + private function createTables(): void { + global $wpdb; + $charset = $wpdb->get_charset_collate(); - foreach (Schema::tables($wpdb->prefix, $charset) as $sql) { - dbDelta($sql); - } - } + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + foreach ( Schema::tables( $wpdb->prefix, $charset ) as $sql ) { + dbDelta( $sql ); + } + } } diff --git a/src/Model/AvailabilitySlot.php b/src/Model/AvailabilitySlot.php index f994e9f..c6f8364 100644 --- a/src/Model/AvailabilitySlot.php +++ b/src/Model/AvailabilitySlot.php @@ -3,38 +3,38 @@ 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, - ) {} +class AvailabilitySlot { - 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 __construct( + public readonly int $instructorId, + public readonly string $startDt, + public readonly string $endDt, + public readonly bool $isBooked = false, + public readonly ?int $id = null, + ) {} - /** - * @return array