CI / Coding Standards (pull_request) Successful in 51s
CI / No Debug Code (pull_request) Successful in 3s
CI / Tests (PHP 8.2) (pull_request) Successful in 50s
CI / Tests (PHP 8.3) (pull_request) Successful in 1m2s
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Tests (PHP 8.1) (pull_request) Successful in 53s
CI / PHPStan (pull_request) Successful in 1m24s
The booking block gains a loginPageId attribute choosing which page its logged-out "log in to book a lesson" link points to (default remains the WordPress login screen), and the student-login block gains a bookingPageId attribute controlling the logged-in "View available lessons" link and the post-login redirect target (default remains the current page). Both blocks also gain an autoRedirect toggle, off by default, that sends the visitor straight to the target page; block rendering starts after output, so the redirect runs on template_redirect by parsing the queried page's content for the block, with a self-target guard against redirect loops. The link targets are also available to the shortcodes as login_page_id/booking_page_id. Also fixes a pre-existing fatal: WordPress passes an empty string (not an array) to shortcode callbacks when a shortcode is used without attributes, so bare [us_booking] etc. threw a TypeError against the strictly-typed render(array $atts) methods. ShortcodeRegistrar now wraps each callback to normalize non-array attribute values. Closes #51 Co-Authored-By: Claude Fable 5 <[email protected]>
188 lines
6.5 KiB
PHP
188 lines
6.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Auth;
|
|
|
|
use Unsupervised\Schedular\Policy\AcceptanceRepository;
|
|
use Unsupervised\Schedular\Policy\Policy;
|
|
use Unsupervised\Schedular\Policy\PolicyAcceptance;
|
|
use Unsupervised\Schedular\Policy\PolicyRepository;
|
|
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
|
use Unsupervised\Schedular\Val;
|
|
|
|
class RegistrationPage {
|
|
|
|
public function __construct(
|
|
private InviteRepository $invites,
|
|
private PolicyRepository $policies,
|
|
private PolicyVersionRepository $versions,
|
|
private AcceptanceRepository $acceptances,
|
|
) {}
|
|
|
|
/**
|
|
* Renders the student registration shortcode output.
|
|
*
|
|
* @param array<int|string, mixed> $atts Shortcode attributes (unused — reserved for future options).
|
|
*/
|
|
public function render( array $atts ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
|
if ( is_user_logged_in() ) {
|
|
return '<p>' . esc_html__( 'You already have an account and are logged in.', 'unsupervised-schedular' ) . '</p>';
|
|
}
|
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- token identifies the invite; the form submit is nonce-checked below.
|
|
$token = sanitize_text_field( Val::string( wp_unslash( $_REQUEST['us_invite'] ?? '' ) ) );
|
|
// Only the token's hash is stored, so hash the submitted token for lookup.
|
|
$invite = '' !== $token ? $this->invites->findByToken( Invite::hashToken( $token ) ) : null;
|
|
|
|
$error = '';
|
|
$success = false;
|
|
|
|
if ( isset( $_POST['us_register'] ) && check_admin_referer( 'us_student_register' ) ) {
|
|
$result = $this->handleSubmit( $invite );
|
|
if ( true === $result ) {
|
|
$success = true;
|
|
} else {
|
|
$error = $result;
|
|
}
|
|
}
|
|
|
|
$policyForms = $this->signupPolicies();
|
|
$canRegister = null !== $invite && $invite->isAcceptable( current_time( 'mysql' ) );
|
|
|
|
ob_start();
|
|
include USC_PLUGIN_DIR . 'templates/frontend/register-page.php';
|
|
return (string) ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Redirect to the configured registration page when an invite token lands
|
|
* elsewhere (e.g. a link generated before the page was selected). Hooked on
|
|
* `template_redirect`.
|
|
*/
|
|
public function maybeRedirectToRegistrationPage(): void {
|
|
if ( is_admin() ) {
|
|
return;
|
|
}
|
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only token used only to build the redirect target.
|
|
$token = sanitize_text_field( Val::string( wp_unslash( $_GET['us_invite'] ?? '' ) ) );
|
|
if ( '' === $token ) {
|
|
return;
|
|
}
|
|
|
|
$pageId = Val::int( get_option( RegistrationController::OPTION_PAGE, 0 ) );
|
|
if ( $pageId <= 0 || is_page( $pageId ) ) {
|
|
return;
|
|
}
|
|
|
|
wp_safe_redirect( add_query_arg( 'us_invite', rawurlencode( $token ), (string) get_permalink( $pageId ) ) );
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Process the submitted registration. Returns true on success or an error
|
|
* message string on failure.
|
|
*/
|
|
private function handleSubmit( ?Invite $invite ): string|bool {
|
|
if ( null === $invite || ! $invite->isAcceptable( current_time( 'mysql' ) ) ) {
|
|
return esc_html__( 'This invitation is invalid, expired, or has already been used.', 'unsupervised-schedular' );
|
|
}
|
|
|
|
// The submit nonce is verified by the caller (render) before this runs.
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
|
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- passwords must not be sanitized.
|
|
$password = Val::string( wp_unslash( $_POST['password'] ?? '' ) );
|
|
$displayName = sanitize_text_field( Val::string( wp_unslash( $_POST['display_name'] ?? '' ) ) );
|
|
|
|
if ( strlen( $password ) < 8 ) {
|
|
return esc_html__( 'Please choose a password of at least 8 characters.', 'unsupervised-schedular' );
|
|
}
|
|
|
|
$policyForms = $this->signupPolicies();
|
|
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each element is coerced to a positive int in the array_map callback; slashes cannot survive integer coercion.
|
|
$accepted = array_map( static fn( mixed $v ): int => absint( Val::int( $v ) ), (array) ( $_POST['accept'] ?? [] ) );
|
|
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
|
|
|
foreach ( $policyForms as $form ) {
|
|
if ( ! in_array( (int) $form['version']->id, $accepted, true ) ) {
|
|
return esc_html__( 'You must accept all required policies to register.', 'unsupervised-schedular' );
|
|
}
|
|
}
|
|
|
|
if ( email_exists( $invite->email ) ) {
|
|
return esc_html__( 'An account already exists for this email.', 'unsupervised-schedular' );
|
|
}
|
|
|
|
$userId = wp_insert_user(
|
|
[
|
|
'user_login' => $invite->email,
|
|
'user_email' => $invite->email,
|
|
'user_pass' => $password,
|
|
'display_name' => '' !== $displayName ? $displayName : $invite->email,
|
|
'role' => $invite->role,
|
|
]
|
|
);
|
|
|
|
if ( is_wp_error( $userId ) ) {
|
|
return esc_html__( 'Could not create the account. Please contact the studio.', 'unsupervised-schedular' );
|
|
}
|
|
|
|
$this->recordAcceptances( $policyForms, (int) $userId );
|
|
$this->invites->markAccepted( (int) $invite->id, (int) $userId );
|
|
|
|
wp_set_current_user( (int) $userId );
|
|
wp_set_auth_cookie( (int) $userId );
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Record account-time acceptances for each signup policy version.
|
|
*
|
|
* @param list<array{policy: Policy, version: \Unsupervised\Schedular\Policy\PolicyVersion}> $policyForms
|
|
*/
|
|
private function recordAcceptances( array $policyForms, int $userId ): void {
|
|
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- IP is stored verbatim for audit.
|
|
$ip = sanitize_text_field( Val::string( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) ) );
|
|
|
|
foreach ( $policyForms as $form ) {
|
|
$this->acceptances->insert(
|
|
new PolicyAcceptance(
|
|
policyVersionId: (int) $form['version']->id,
|
|
studentId: $userId,
|
|
registrationType: PolicyAcceptance::REG_ACCOUNT,
|
|
registrationId: $userId,
|
|
ipAddress: '' !== $ip ? $ip : null,
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Signup-scoped policies that have a current published version.
|
|
*
|
|
* @return list<array{policy: Policy, version: \Unsupervised\Schedular\Policy\PolicyVersion}>
|
|
*/
|
|
private function signupPolicies(): array {
|
|
$out = [];
|
|
|
|
foreach ( $this->policies->findForScope( Policy::SCOPE_SIGNUP ) as $policy ) {
|
|
if ( null === $policy->currentVersionId ) {
|
|
continue;
|
|
}
|
|
|
|
$version = $this->versions->findById( $policy->currentVersionId );
|
|
if ( null === $version || ! $version->isPublished() ) {
|
|
continue;
|
|
}
|
|
|
|
$out[] = [
|
|
'policy' => $policy,
|
|
'version' => $version,
|
|
];
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
}
|