Files
unsupervised-scheduler/src/Auth/InviteRepository.php
T
thatguygriffandClaude Opus 4.8 a281935811
CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 49s
CI / No Debug Code (pull_request) Successful in 2s
CI / PHPStan (pull_request) Successful in 2m49s
CI / Coding Standards (pull_request) Successful in 2m55s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m40s
CI / Build Plugin Zip (pull_request) Skipped
Add invite-only group classes
Group classes can now be marked invite-only (us_offerings.access_mode).
Invite-only classes are hidden from the public catalog and reachable only
when the instructor lets someone in via one of three paths, managed from
My Lessons -> My Group Classes:

- Add students directly: enrols them now with a pending payment.
- Make available: grants registered students access to self-enrol through
  the normal paid flow (multi-select, emailed a notice).
- Invite by email: tokenised registration invite tied to the class for a
  non-account address; after they register the class becomes enrollable.
  Reuses an existing pending invite instead of sending a second link.

New us_group_access table records grants; GET /offerings merges granted
invite-only classes for the caller; enrolment requires a grant
(403 invite_required) and flips it to enrolled on success.

composer test (487), composer lint, composer cs all pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-23 13:51:02 -03:00

113 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Auth;
class InviteRepository {
private string $table;
public function __construct( private \wpdb $db ) {
$this->table = $db->prefix . 'us_invites';
}
/**
* Persist an invite. Returns the new row id, or 0 when the insert failed —
* callers must not hand out a registration link for an unstored token.
*/
public function insert( Invite $invite ): int {
$result = $this->db->insert(
$this->table,
[
'email' => $invite->email,
'token' => $invite->token,
'role' => $invite->role,
'kind' => $invite->kind,
'offering_id' => $invite->offeringId,
'status' => $invite->status,
'invited_by' => $invite->invitedBy,
'accepted_user_id' => $invite->acceptedUserId,
'created_at' => current_time( 'mysql' ),
'accepted_at' => $invite->acceptedAt,
'expires_at' => $invite->expiresAt,
],
[ '%s', '%s', '%s', '%s', '%d', '%s', '%d', '%d', '%s', '%s', '%s' ]
);
return false === $result ? 0 : $this->db->insert_id;
}
public function findByToken( string $token ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare( 'SELECT * FROM %i WHERE token = %s', $this->table, $token )
);
return $row ? Invite::fromRow( $row ) : null;
}
public function findById( int $id ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare( 'SELECT * FROM %i WHERE id = %d', $this->table, $id )
);
return $row ? Invite::fromRow( $row ) : null;
}
/**
* The most recent pending invite for an email, if any.
*/
public function findPendingByEmail( string $email ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare(
'SELECT * FROM %i WHERE email = %s AND status = %s ORDER BY id DESC LIMIT 1',
$this->table,
$email,
Invite::STATUS_PENDING
)
);
return $row ? Invite::fromRow( $row ) : null;
}
/**
* All invites awaiting acceptance, newest first.
*
* @return list<Invite>
*/
public function findPending(): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE status = %s ORDER BY created_at DESC',
$this->table,
Invite::STATUS_PENDING
)
);
return array_map( Invite::fromRow( ... ), $rows ?? [] );
}
public function markAccepted( int $id, int $userId ): bool {
return false !== $this->db->update(
$this->table,
[
'status' => Invite::STATUS_ACCEPTED,
'accepted_user_id' => $userId,
'accepted_at' => current_time( 'mysql' ),
],
[ 'id' => $id ],
[ '%s', '%d', '%s' ],
[ '%d' ]
);
}
public function revoke( int $id ): bool {
return false !== $this->db->update(
$this->table,
[ 'status' => Invite::STATUS_REVOKED ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
}