- Overlay\Build id (plugin version + admin epoch) in X-SG-Build; overlay.js reloads once on mismatch (loop-guarded); 'Refresh all overlays now' - Media URLs carry ?v=<mtime> - Alert option 'Frameless' (schema v5): no box so transparent PNG/GIF/WebP and VP9-alpha WebM show through; admin preview with checkerboard and ffmpeg help Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
4.0 KiB
PHP
109 lines
4.0 KiB
PHP
<?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,v?:int,modified?:string} $attachment
|
|
* @return array{visual:?array{type:string,url:string},sound:?array{url:string},volume:int,duration_ms:int,message:?string,frameless:bool}|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;
|
|
$frameless = false;
|
|
$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'] );
|
|
$frameless = ! empty( $def['frameless'] );
|
|
}
|
|
}
|
|
$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,
|
|
'frameless' => $frameless,
|
|
);
|
|
}
|
|
|
|
/** @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 );
|
|
if ( null === $url ) {
|
|
return null;
|
|
}
|
|
// Cache-bust suffix from server-side data only (file mtime / post_modified_gmt supplied by the lookup).
|
|
$versioned = Media::safeUrl( Media::withVersion( $url, (int) ( $a['v'] ?? 0 ), isset( $a['modified'] ) && is_string( $a['modified'] ) ? $a['modified'] : null ) );
|
|
return array( 'url' => $versioned ?? $url, 'mime' => (string) $a['mime'] );
|
|
}
|
|
}
|