- Actions\Matcher (exact / every), ActionRepository, Recorder (idempotent via unique key) - Admin: Rewards screen with live preview, queue with Mark done, menu badge - Donate form shows actions as payment buttons; overlay 'actions' widget; alerts list triggered actions - Installer DB_VERSION 2 (sg_actions, sg_action_events) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
220 lines
11 KiB
PHP
220 lines
11 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Admin;
|
|
|
|
use StreamGoals\Actions\ActionRepository;
|
|
use StreamGoals\Actions\Matcher;
|
|
use StreamGoals\Core\Money;
|
|
use StreamGoals\Core\Settings;
|
|
|
|
/** "Rewards": the menu of on-stream actions, and the queue of triggered ones. */
|
|
final class ActionsPage {
|
|
|
|
public const PAGE = 'streamgoals-rewards';
|
|
|
|
private static function repo(): ActionRepository {
|
|
return new ActionRepository();
|
|
}
|
|
|
|
public static function minMinor(): int {
|
|
return Settings::effectiveMin( (int) Settings::get( 'min_donation_minor', 0 ) );
|
|
}
|
|
|
|
public static function queuedCount(): int {
|
|
try {
|
|
return self::repo()->queuedCount();
|
|
} catch ( \Throwable ) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private static function url( array $args = array() ): string {
|
|
return Admin::url( self::PAGE, $args );
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- render */
|
|
|
|
public static function render(): void {
|
|
Admin::open( 'Rewards' );
|
|
$edit = Admin::get_int( 'edit' );
|
|
if ( 'new' === Admin::get_str( 'view' ) || $edit ) {
|
|
self::renderForm( $edit );
|
|
} else {
|
|
self::renderQueue();
|
|
self::renderMenu();
|
|
}
|
|
Admin::close();
|
|
}
|
|
|
|
private static function renderQueue(): void {
|
|
$cur = Admin::currency();
|
|
$repo = self::repo();
|
|
$queued = $repo->queued( 200 );
|
|
echo '<div class="sg-card"><h2>Waiting to be done (' . count( $queued ) . ')</h2>';
|
|
if ( ! $queued ) {
|
|
echo '<p>Nothing in the queue. When a donation triggers a reward, it shows up here.</p>';
|
|
} else {
|
|
echo '<table class="widefat striped sg-table"><thead><tr><th>Donor</th><th>Amount</th><th>Reward</th><th>Time</th><th>Message</th><th></th></tr></thead><tbody>';
|
|
foreach ( $queued as $e ) {
|
|
echo '<tr><td>' . esc_html( Format::donorName( $e ) ) . '</td>';
|
|
echo '<td>' . esc_html( Format::money( (int) ( $e['base_minor'] ?? 0 ), Format::currency( $e['currency'] ?? $cur ) ) ) . '</td>';
|
|
echo '<td><strong>' . esc_html( (string) $e['title'] ) . '</strong> × ' . (int) $e['times'] . '</td>';
|
|
echo '<td>' . esc_html( self::when( (string) $e['created_at'] ) ) . '</td>';
|
|
echo '<td>' . esc_html( (string) ( $e['message'] ?? '' ) ) . '</td><td>';
|
|
Admin::form( 'sg_event_done', array( 'event_id' => (int) $e['id'] ), 'sg-inline' );
|
|
echo '<button class="button button-primary">Mark done</button></form></td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
Admin::form( 'sg_event_done_all' );
|
|
echo '<p><button class="button">Mark all done</button></p></form>';
|
|
}
|
|
$done = $repo->recentDone( 15 );
|
|
if ( $done ) {
|
|
echo '<h3>Recently done</h3><ul class="sg-preview-list">';
|
|
foreach ( $done as $e ) {
|
|
echo '<li>' . esc_html( Format::donorName( $e ) . ': ' . $e['title'] . ' x ' . (int) $e['times'] . ' (' . self::when( (string) ( $e['done_at'] ?? '' ) ) . ')' ) . '</li>';
|
|
}
|
|
echo '</ul>';
|
|
}
|
|
echo '</div>';
|
|
}
|
|
|
|
private static function when( string $utc ): string {
|
|
return '' === $utc ? '' : get_date_from_gmt( $utc, 'Y-m-d H:i' );
|
|
}
|
|
|
|
/** @return array<int,string> goal id => title */
|
|
private static function goalTitles(): array {
|
|
$out = array();
|
|
foreach ( Store::goals()->all() as $g ) {
|
|
$out[ (int) $g['id'] ] = (string) $g['title'];
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
private static function renderMenu(): void {
|
|
$cur = Admin::currency();
|
|
$actions = self::repo()->all();
|
|
$goals = self::goalTitles();
|
|
echo '<div class="sg-card"><h2>Your reward menu</h2>';
|
|
echo '<p class="description">Donors pick a reward on the donation form (or just donate the amount). Rewards must be at least ' . esc_html( Format::money( self::minMinor(), $cur ) ) . ', the minimum donation.</p>';
|
|
echo '<p><a class="button button-primary" href="' . esc_url( self::url( array( 'view' => 'new' ) ) ) . '">Add a reward</a></p>';
|
|
if ( ! $actions ) {
|
|
echo '<p>No rewards yet.</p></div>';
|
|
return;
|
|
}
|
|
echo '<table class="widefat striped sg-table"><thead><tr><th>Reward</th><th>Amount</th><th>Triggers</th><th>Applies to</th><th>Order</th><th>Actions</th></tr></thead><tbody>';
|
|
foreach ( $actions as $a ) {
|
|
$id = (int) $a['id'];
|
|
echo '<tr' . ( $a['enabled'] ? '' : ' class="sg-off"' ) . '><td><strong>' . esc_html( $a['title'] ) . '</strong>';
|
|
if ( '' !== $a['description'] ) {
|
|
echo '<br><span class="description">' . esc_html( $a['description'] ) . '</span>';
|
|
}
|
|
echo '</td><td>' . esc_html( Format::money( $a['amount_minor'], $cur ) ) . '</td>';
|
|
echo '<td>' . esc_html( Matcher::EVERY === $a['mode'] ? 'Every time it adds up' : 'Exactly this amount' ) . '</td>';
|
|
echo '<td>' . esc_html( 0 === $a['goal_id'] ? 'All goals' : ( $goals[ $a['goal_id'] ] ?? 'Goal #' . $a['goal_id'] ) ) . '</td>';
|
|
echo '<td>' . (int) $a['sort'] . '</td><td class="sg-actions">';
|
|
echo '<a class="button" href="' . esc_url( self::url( array( 'edit' => $id ) ) ) . '">Edit</a> ';
|
|
Admin::form( 'sg_action_toggle', array( 'action_id' => $id, 'enabled' => $a['enabled'] ? 0 : 1 ), 'sg-inline' );
|
|
echo '<button class="button">' . ( $a['enabled'] ? 'Turn off' : 'Turn on' ) . '</button></form> ';
|
|
Admin::form( 'sg_action_delete', array( 'action_id' => $id ), 'sg-inline sg-confirm' );
|
|
echo '<button class="button button-link-delete" data-confirm="Delete this reward? Rewards already waiting in the queue stay there.">Delete</button></form>';
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table></div>';
|
|
}
|
|
|
|
private static function renderForm( int $id ): void {
|
|
$a = $id ? self::repo()->find( $id ) : null;
|
|
if ( $id && ! $a ) {
|
|
echo '<p>That reward no longer exists. <a href="' . esc_url( self::url() ) . '">Back to rewards</a></p>';
|
|
return;
|
|
}
|
|
$cur = Admin::currency();
|
|
$goals = self::goalTitles();
|
|
$val = static fn( string $k, $d = '' ) => $a[ $k ] ?? $d;
|
|
$mode = (string) $val( 'mode', Matcher::EXACT );
|
|
$data = array(
|
|
'currency' => $cur,
|
|
'exp' => Money::exponent( $cur ),
|
|
'editId' => $id,
|
|
'actions' => array_values( array_map(
|
|
static fn( array $r ): array => array( 'id' => $r['id'], 'goal_id' => $r['goal_id'], 'title' => $r['title'], 'amount_minor' => $r['amount_minor'], 'mode' => $r['mode'] ),
|
|
array_filter( self::repo()->all(), static fn( array $r ): bool => (bool) $r['enabled'] && $r['id'] !== $id )
|
|
) ),
|
|
);
|
|
|
|
echo '<h2>' . ( $a ? 'Edit reward' : 'Add a reward' ) . '</h2>';
|
|
Admin::form( 'sg_action_save', array( 'action_id' => $id ), 'sg-action-form' );
|
|
echo '<table class="form-table" role="presentation">';
|
|
echo '<tr><th scope="row"><label for="sg_a_title">Title</label></th><td><input type="text" required maxlength="120" id="sg_a_title" name="title" class="regular-text" value="' . esc_attr( (string) $val( 'title' ) ) . '"><p class="description">What you do on stream, for example "Eat a bean". Up to 120 characters.</p></td></tr>';
|
|
echo '<tr><th scope="row"><label for="sg_a_desc">Description</label></th><td><input type="text" maxlength="255" id="sg_a_desc" name="description" class="large-text" value="' . esc_attr( (string) $val( 'description' ) ) . '"><p class="description">Optional. A short note shown to donors.</p></td></tr>';
|
|
echo '<tr><th scope="row"><label for="sg_a_amount">Amount (' . esc_html( $cur ) . ')</label></th><td><input type="text" required inputmode="decimal" id="sg_a_amount" name="amount" class="small-text" value="' . esc_attr( $a ? Format::decimal( (int) $a['amount_minor'], $cur ) : '' ) . '"><p class="description">At least ' . esc_html( Format::money( self::minMinor(), $cur ) ) . ' (your minimum donation). The amount before any fee the donor chooses to cover.</p></td></tr>';
|
|
echo '<tr><th scope="row"><label for="sg_a_goal">Applies to</label></th><td><select id="sg_a_goal" name="goal_id"><option value="0"' . selected( (int) $val( 'goal_id', 0 ), 0, false ) . '>All goals</option>';
|
|
foreach ( $goals as $gid => $title ) {
|
|
echo '<option value="' . (int) $gid . '"' . selected( (int) $val( 'goal_id', 0 ), $gid, false ) . '>' . esc_html( $title ) . '</option>';
|
|
}
|
|
echo '</select></td></tr>';
|
|
echo '<tr><th scope="row">Triggers</th><td>';
|
|
echo '<label><input type="radio" name="mode" value="exact"' . checked( $mode, Matcher::EXACT, false ) . '> Exactly this amount</label><br>';
|
|
echo '<label><input type="radio" name="mode" value="every"' . checked( $mode, Matcher::EVERY, false ) . '> Every time the donation adds up to this amount (a $10 donation triggers a $5 reward twice)</label></td></tr>';
|
|
echo '<tr><th scope="row"><label for="sg_a_sort">Order</label></th><td><input type="number" min="0" max="99999" id="sg_a_sort" name="sort" class="small-text" value="' . ( $a ? (int) $a['sort'] : '' ) . '"><p class="description">Lower numbers are listed first. Leave empty to put it last.</p></td></tr>';
|
|
echo '</table>';
|
|
echo '<div class="sg-card"><h3>What would a donation do?</h3><label>Donation amount (' . esc_html( $cur ) . ') <input type="text" id="sg-action-try" class="small-text" inputmode="decimal"></label>';
|
|
echo '<p id="sg-action-preview" class="sg-form-preview" aria-live="polite"></p>';
|
|
echo '<script type="application/json" id="sg-actions-data">' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . '</script></div>';
|
|
echo '<p><button class="button button-primary">' . ( $a ? 'Save reward' : 'Add reward' ) . '</button> <a class="button" href="' . esc_url( self::url() ) . '">Cancel</a></p></form>';
|
|
}
|
|
|
|
/* -------------------------------------------------------------- handlers */
|
|
|
|
public static function handleSave(): void {
|
|
Admin::guard( 'sg_action_save' );
|
|
$post = Admin::post_raw();
|
|
$id = isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0;
|
|
$back = self::url( $id ? array( 'edit' => $id ) : array( 'view' => 'new' ) );
|
|
$repo = self::repo();
|
|
if ( $id && ! $repo->find( $id ) ) {
|
|
Admin::redirect( self::url(), 'error', 'That reward no longer exists.' );
|
|
}
|
|
$res = ActionInput::validate( $post, self::minMinor(), Admin::currency(), array_keys( self::goalTitles() ), $repo->nextSort() );
|
|
if ( null === $res['data'] ) {
|
|
Admin::redirect( $back, 'error', (string) $res['error'] );
|
|
}
|
|
if ( $id ) {
|
|
$repo->update( $id, $res['data'] );
|
|
} else {
|
|
$repo->create( $res['data'] );
|
|
}
|
|
Admin::redirect( self::url(), 'success', 'Reward saved.' );
|
|
}
|
|
|
|
public static function handleToggle(): void {
|
|
Admin::guard( 'sg_action_toggle' );
|
|
$post = Admin::post_raw();
|
|
self::repo()->setEnabled( isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0, ! empty( $post['enabled'] ) );
|
|
Admin::redirect( self::url(), 'success', 'Reward updated.' );
|
|
}
|
|
|
|
public static function handleDelete(): void {
|
|
Admin::guard( 'sg_action_delete' );
|
|
$post = Admin::post_raw();
|
|
self::repo()->delete( isset( $post['action_id'] ) ? absint( $post['action_id'] ) : 0 );
|
|
Admin::redirect( self::url(), 'success', 'Reward deleted.' );
|
|
}
|
|
|
|
public static function handleDone(): void {
|
|
Admin::guard( 'sg_event_done' );
|
|
$post = Admin::post_raw();
|
|
self::repo()->markDone( isset( $post['event_id'] ) ? absint( $post['event_id'] ) : 0 );
|
|
Admin::redirect( self::url(), 'success', 'Marked as done.' );
|
|
}
|
|
|
|
public static function handleDoneAll(): void {
|
|
Admin::guard( 'sg_event_done_all' );
|
|
$n = self::repo()->markAllDone();
|
|
Admin::redirect( self::url(), 'success', $n . ( 1 === $n ? ' reward' : ' rewards' ) . ' marked as done.' );
|
|
}
|
|
}
|