- 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>
27 lines
1012 B
PHP
27 lines
1012 B
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Actions;
|
|
|
|
/** Queues the on-stream actions a completed donation triggers. */
|
|
final class Recorder {
|
|
|
|
/** Never throws. Idempotent: replays are ignored by the UNIQUE key on sg_action_events. */
|
|
public static function forDonation( int $donationId ): void {
|
|
try {
|
|
global $wpdb;
|
|
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT id, goal_id, base_minor, status FROM ' . $wpdb->prefix . 'sg_donations WHERE id = %d', $donationId ), ARRAY_A );
|
|
if ( ! is_array( $row ) || 'completed' !== ( $row['status'] ?? '' ) ) {
|
|
return;
|
|
}
|
|
$repo = new ActionRepository( $wpdb );
|
|
$matches = Matcher::match( (int) $row['base_minor'], $repo->enabledForGoal( (int) $row['goal_id'] ) );
|
|
if ( $matches ) {
|
|
$repo->recordEvents( $donationId, $matches );
|
|
}
|
|
} catch ( \Throwable $e ) {
|
|
error_log( 'StreamGoals: Recorder failed for donation ' . $donationId . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
|
}
|
|
}
|
|
}
|