Add donation actions (rewards): menu editor + queue, donate-form buttons, overlay menu widget, alert lines
- 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>
This commit is contained in:
1 parent
c62ac06b27
commit
b0b5012698
25 files changed
+1227
-7
No files matched your search
@@ -126,6 +126,7 @@ final class Payloads {
|
||||
'currency' => $cur,
|
||||
'message' => (string) ( $r['message'] ?? '' ),
|
||||
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
|
||||
'actions' => self::actionEvents( $r['actions'] ?? array() ),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -158,6 +159,70 @@ final class Payloads {
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** Display-safe single line: no tags, control chars or runs of whitespace; capped at $max characters. */
|
||||
public static function text( mixed $v, int $max ): string {
|
||||
if ( ! is_scalar( $v ) ) {
|
||||
return '';
|
||||
}
|
||||
$s = strip_tags( (string) $v );
|
||||
$s = (string) preg_replace( '/[\x00-\x1F\x7F\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2066}-\x{2069}\x{FEFF}]+/u', ' ', $s );
|
||||
$s = trim( (string) preg_replace( '/\s+/u', ' ', $s ) );
|
||||
return mb_substr( $s, 0, $max );
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered actions for one alert: [ {title, times} ]. At most 5, titles capped at 80 chars, times clamped to 1..999.
|
||||
*
|
||||
* @param mixed $events
|
||||
* @return array<int,array{title:string,times:int}>
|
||||
*/
|
||||
public static function actionEvents( mixed $events ): array {
|
||||
$out = array();
|
||||
foreach ( is_array( $events ) ? $events : array() as $e ) {
|
||||
if ( ! is_array( $e ) ) {
|
||||
continue;
|
||||
}
|
||||
$title = self::text( $e['title'] ?? '', 80 );
|
||||
if ( '' === $title ) {
|
||||
continue;
|
||||
}
|
||||
$out[] = array( 'title' => $title, 'times' => max( 1, min( 999, (int) ( $e['times'] ?? 1 ) ) ) );
|
||||
if ( count( $out ) >= 5 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The on-stream reward menu for the active goal. Empty list means "render nothing".
|
||||
*
|
||||
* @param array<string,mixed>|null $goal
|
||||
* @param array<int,array<string,mixed>> $rows Raw sg_actions rows, already ordered.
|
||||
*/
|
||||
public static function actionsMenu( ?array $goal, array $rows ): array {
|
||||
$cur = strtoupper( (string) ( $goal['currency'] ?? 'USD' ) );
|
||||
$list = array();
|
||||
foreach ( $goal ? $rows : array() as $r ) {
|
||||
$amount = (int) ( $r['amount_minor'] ?? 0 );
|
||||
$title = self::text( $r['title'] ?? '', 120 );
|
||||
if ( $amount <= 0 || '' === $title ) {
|
||||
continue;
|
||||
}
|
||||
$list[] = array(
|
||||
'title' => $title,
|
||||
'description' => self::text( $r['description'] ?? '', 255 ),
|
||||
'amount_minor' => $amount,
|
||||
'amount_display' => self::money( $amount, $cur ),
|
||||
'mode' => 'every' === ( $r['mode'] ?? '' ) ? 'every' : 'exact',
|
||||
);
|
||||
if ( count( $list ) >= 50 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array( 'active' => (bool) $list, 'currency' => $cur, 'actions' => $list );
|
||||
}
|
||||
|
||||
/** Strong ETag over the payload, ignoring the volatile server_now so unchanged data yields 304s. */
|
||||
public static function etag( array $payload ): string {
|
||||
unset( $payload['server_now'] );
|
||||
|
||||
Reference in new issue
Block a user