Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
- Alerts\Resolver/Media/Template/AlertMedia/DefInput/DefRepository; Alerts admin screen with Media Library picker and live preview - Overlay: media resolved server-side from attachment IDs (MIME whitelist), playback with timeouts and autoplay-failure fallbacks - Workflow: pick the next free minute if the version tag already exists (two pushes in one minute failed 'Create release') Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1201db175b
commit
2c16a9ebbb
22 files changed
+1591
-22
No files matched your search
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Alerts;
|
||||
|
||||
/**
|
||||
* Builds the public `media` block for one alert. URLs only ever come from the $attachment callback
|
||||
* (WordPress attachment lookup); the DB row contributes ids, never URLs.
|
||||
*/
|
||||
final class AlertMedia {
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $row Donation row (goal_id, base_minor).
|
||||
* @param array<int,array<string,mixed>> $defs Raw alert definitions.
|
||||
* @param array<int,array{action_id:int,amount_minor:int,title:string}> $triggered Rewards this donation triggered.
|
||||
* @param callable(int):?array{url:string,mime:string} $attachment
|
||||
* @return array{visual:?array{type:string,url:string},sound:?array{url:string},volume:int,duration_ms:int,message:?string}|null
|
||||
*/
|
||||
public static function build( array $row, string $name, string $amountDisplay, array $defs, array $triggered, callable $attachment ): ?array {
|
||||
if ( ! $defs ) {
|
||||
return null;
|
||||
}
|
||||
$ids = array();
|
||||
$amounts = array();
|
||||
foreach ( $triggered as $t ) {
|
||||
$aid = (int) ( $t['action_id'] ?? 0 );
|
||||
$ids[] = $aid;
|
||||
$amounts[ $aid ] = (int) ( $t['amount_minor'] ?? 0 );
|
||||
}
|
||||
$def = Resolver::pick( (int) ( $row['goal_id'] ?? 0 ), (int) ( $row['base_minor'] ?? 0 ), $ids, $defs, $amounts );
|
||||
if ( null === $def ) {
|
||||
return null;
|
||||
}
|
||||
return self::fromDef( $def, $name, $amountDisplay, $triggered, $attachment );
|
||||
}
|
||||
|
||||
/** @return array<string,mixed>|null */
|
||||
public static function fromDef( array $def, string $name, string $amountDisplay, array $triggered, callable $attachment ): ?array {
|
||||
$visual = null;
|
||||
$vid = (int) ( $def['visual_id'] ?? 0 );
|
||||
if ( $vid > 0 ) {
|
||||
$a = self::lookup( $attachment, $vid );
|
||||
$type = $a ? Media::visualType( $a['mime'] ) : null;
|
||||
if ( $a && $type ) {
|
||||
$visual = array( 'type' => $type, 'url' => $a['url'] );
|
||||
}
|
||||
}
|
||||
$sound = null;
|
||||
$sid = (int) ( $def['sound_id'] ?? 0 );
|
||||
if ( $sid > 0 ) {
|
||||
$a = self::lookup( $attachment, $sid );
|
||||
if ( $a && Media::isSound( $a['mime'] ) ) {
|
||||
$sound = array( 'url' => $a['url'] );
|
||||
}
|
||||
}
|
||||
$reward = '';
|
||||
$best = -1;
|
||||
foreach ( $triggered as $t ) {
|
||||
$amt = (int) ( $t['amount_minor'] ?? 0 );
|
||||
if ( 'reward' === ( $def['trigger_type'] ?? '' ) ) {
|
||||
if ( (int) ( $t['action_id'] ?? 0 ) === (int) $def['action_id'] ) {
|
||||
$reward = (string) ( $t['title'] ?? '' );
|
||||
break;
|
||||
}
|
||||
} elseif ( $amt > $best ) {
|
||||
$best = $amt;
|
||||
$reward = (string) ( $t['title'] ?? '' );
|
||||
}
|
||||
}
|
||||
$msg = Template::render( (string) ( $def['message'] ?? '' ), $name, $amountDisplay, $reward );
|
||||
if ( null === $visual && null === $sound && '' === $msg ) {
|
||||
return null; // nothing usable left: behave exactly as a plain alert
|
||||
}
|
||||
$duration = Media::clampDurationMs( $def['duration_ms'] ?? Media::DEFAULT_MS );
|
||||
if ( $visual && 'video' === $visual['type'] ) {
|
||||
$duration = Media::VIDEO_CAP_MS;
|
||||
}
|
||||
return array(
|
||||
'visual' => $visual,
|
||||
'sound' => $sound,
|
||||
'volume' => Media::clampVolume( $def['volume'] ?? 80 ),
|
||||
'duration_ms' => $duration,
|
||||
'message' => '' === $msg ? null : $msg,
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array{url:string,mime:string}|null */
|
||||
private static function lookup( callable $attachment, int $id ): ?array {
|
||||
try {
|
||||
$a = $attachment( $id );
|
||||
} catch ( \Throwable ) {
|
||||
return null;
|
||||
}
|
||||
if ( ! is_array( $a ) || ! Media::isAllowed( $a['mime'] ?? null ) ) {
|
||||
return null;
|
||||
}
|
||||
$url = Media::safeUrl( $a['url'] ?? null );
|
||||
return null === $url ? null : array( 'url' => $url, 'mime' => (string) $a['mime'] );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user