- 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>
91 lines
3.3 KiB
PHP
91 lines
3.3 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Alerts;
|
|
|
|
/** $wpdb access to sg_alert_defs. */
|
|
final class DefRepository {
|
|
|
|
private object $db;
|
|
|
|
public function __construct( ?object $db = null ) {
|
|
$this->db = $db ?? $GLOBALS['wpdb'];
|
|
}
|
|
|
|
private function t(): string {
|
|
return $this->db->prefix . 'sg_alert_defs';
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private static function cast( array $r ): array {
|
|
return array(
|
|
'id' => (int) ( $r['id'] ?? 0 ),
|
|
'goal_id' => (int) ( $r['goal_id'] ?? 0 ),
|
|
'trigger_type' => 'reward' === ( $r['trigger_type'] ?? '' ) ? 'reward' : 'amount',
|
|
'min_minor' => (int) ( $r['min_minor'] ?? 0 ),
|
|
'action_id' => (int) ( $r['action_id'] ?? 0 ),
|
|
'title' => (string) ( $r['title'] ?? '' ),
|
|
'visual_id' => (int) ( $r['visual_id'] ?? 0 ),
|
|
'visual_type' => (string) ( $r['visual_type'] ?? '' ),
|
|
'sound_id' => (int) ( $r['sound_id'] ?? 0 ),
|
|
'volume' => (int) ( $r['volume'] ?? 80 ),
|
|
'duration_ms' => (int) ( $r['duration_ms'] ?? 6000 ),
|
|
'message' => (string) ( $r['message'] ?? '' ),
|
|
'frameless' => (int) ( $r['frameless'] ?? 0 ) ? 1 : 0,
|
|
'enabled' => (int) ( $r['enabled'] ?? 0 ),
|
|
'sort' => (int) ( $r['sort'] ?? 0 ),
|
|
'created_at' => (string) ( $r['created_at'] ?? '' ),
|
|
);
|
|
}
|
|
|
|
public function all(): array {
|
|
return array_map( array( self::class, 'cast' ), (array) $this->db->get_results( 'SELECT * FROM ' . $this->t() . ' ORDER BY sort ASC, id ASC', ARRAY_A ) );
|
|
}
|
|
|
|
/** All enabled definitions (one query); the Resolver filters by goal. */
|
|
public function enabled(): array {
|
|
return array_map( array( self::class, 'cast' ), (array) $this->db->get_results( 'SELECT * FROM ' . $this->t() . ' WHERE enabled = 1 ORDER BY sort ASC, id ASC', ARRAY_A ) );
|
|
}
|
|
|
|
public function find( int $id ): ?array {
|
|
$row = $this->db->get_row( $this->db->prepare( 'SELECT * FROM ' . $this->t() . ' WHERE id = %d', $id ), ARRAY_A );
|
|
return $row ? self::cast( $row ) : null;
|
|
}
|
|
|
|
public function nextSort(): int {
|
|
return (int) $this->db->get_var( 'SELECT COALESCE(MAX(sort),0)+10 FROM ' . $this->t() );
|
|
}
|
|
|
|
private const COLS = array(
|
|
'goal_id' => '%d', 'trigger_type' => '%s', 'min_minor' => '%d', 'action_id' => '%d', 'title' => '%s', 'visual_id' => '%d',
|
|
'visual_type' => '%s', 'sound_id' => '%d', 'volume' => '%d', 'duration_ms' => '%d', 'message' => '%s', 'sort' => '%d', 'frameless' => '%d',
|
|
);
|
|
|
|
public function create( array $d ): int {
|
|
$row = array();
|
|
foreach ( self::COLS as $k => $f ) {
|
|
$row[ $k ] = $d[ $k ] ?? ( '%d' === $f ? 0 : '' );
|
|
}
|
|
$row['enabled'] = 1;
|
|
$row['created_at'] = gmdate( 'Y-m-d H:i:s' );
|
|
$ok = $this->db->insert( $this->t(), $row, array_merge( array_values( self::COLS ), array( '%d', '%s' ) ) );
|
|
return $ok ? (int) $this->db->insert_id : 0;
|
|
}
|
|
|
|
public function update( int $id, array $d ): void {
|
|
$row = array();
|
|
foreach ( self::COLS as $k => $f ) {
|
|
$row[ $k ] = $d[ $k ] ?? ( '%d' === $f ? 0 : '' );
|
|
}
|
|
$this->db->update( $this->t(), $row, array( 'id' => $id ), array_values( self::COLS ), array( '%d' ) );
|
|
}
|
|
|
|
public function setEnabled( int $id, bool $on ): void {
|
|
$this->db->update( $this->t(), array( 'enabled' => $on ? 1 : 0 ), array( 'id' => $id ), array( '%d' ), array( '%d' ) );
|
|
}
|
|
|
|
public function delete( int $id ): void {
|
|
$this->db->delete( $this->t(), array( 'id' => $id ), array( '%d' ) );
|
|
}
|
|
}
|