Add OBS overlays (goal, timer, counter, alerts) with ETag polling
Align timer state key with rules engine (timer_ends_at). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7155396312
commit
65082fb2f5
10 files changed
+815
No files matched your search
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Overlay;
|
||||
|
||||
use StreamGoals\Rest\OverlayController;
|
||||
|
||||
/** Serves the transparent OBS browser-source page at /streamgoals-overlay/{token}/{widget}/. */
|
||||
final class Router {
|
||||
|
||||
public const RULES_VERSION = '1';
|
||||
private const OPTION = 'sg_overlay_rules_version';
|
||||
|
||||
public static function register(): void {
|
||||
add_action( 'init', array( self::class, 'add_rules' ) );
|
||||
add_filter( 'query_vars', array( self::class, 'query_vars' ) );
|
||||
add_action( 'template_redirect', array( self::class, 'render' ) );
|
||||
}
|
||||
|
||||
public static function add_rules(): void {
|
||||
add_rewrite_rule(
|
||||
'^streamgoals-overlay/([A-Za-z0-9]{16,128})/(goal|timer|counter|alerts)/?$',
|
||||
'index.php?sg_overlay_token=$matches[1]&sg_overlay_widget=$matches[2]',
|
||||
'top'
|
||||
);
|
||||
}
|
||||
|
||||
public static function query_vars( array $vars ): array {
|
||||
$vars[] = 'sg_overlay_token';
|
||||
$vars[] = 'sg_overlay_widget';
|
||||
return $vars;
|
||||
}
|
||||
|
||||
/** True when rewrite rules must be flushed (first run or rule set changed). Call mark_flushed() after flushing. */
|
||||
public static function flush_rules_needed(): bool {
|
||||
return get_option( self::OPTION ) !== self::RULES_VERSION;
|
||||
}
|
||||
|
||||
public static function mark_flushed(): void {
|
||||
update_option( self::OPTION, self::RULES_VERSION, false );
|
||||
}
|
||||
|
||||
public static function render(): void {
|
||||
$token = get_query_var( 'sg_overlay_token' );
|
||||
$widget = get_query_var( 'sg_overlay_widget' );
|
||||
if ( '' === $token || '' === $widget ) {
|
||||
return;
|
||||
}
|
||||
if ( ! is_string( $token ) || ! is_string( $widget ) || ! in_array( $widget, OverlayService::WIDGETS, true ) || ! OverlayController::verify_token( $token ) ) {
|
||||
global $wp_query;
|
||||
$wp_query->set_404();
|
||||
status_header( 404 );
|
||||
nocache_headers();
|
||||
return;
|
||||
}
|
||||
|
||||
// Only whitelisted, sanitized styling params ever reach the page.
|
||||
$style = Style::fromQuery( is_array( $_GET ) ? wp_unslash( $_GET ) : array() ); // phpcs:ignore WordPress.Security.NonceVerification
|
||||
$file = dirname( __DIR__, 2 ) . '/streamgoals-donate.php';
|
||||
$asset = static fn( string $f ): string => esc_url( add_query_arg( 'v', (string) @filemtime( dirname( __DIR__, 2 ) . '/assets/overlay/' . $f ), plugins_url( 'assets/overlay/' . $f, $file ) ) );
|
||||
$endpoint = rest_url( 'streamgoals/v1/overlay/' . rawurlencode( $token ) . '/' . $widget );
|
||||
|
||||
nocache_headers();
|
||||
header( 'Content-Type: text/html; charset=utf-8' );
|
||||
header( 'X-Robots-Tag: noindex, nofollow' );
|
||||
header( 'Referrer-Policy: no-referrer' );
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="en" style="<?php echo esc_attr( Style::inline( $style['vars'] ) ); ?>">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noindex,nofollow">
|
||||
<title>StreamGoals overlay</title>
|
||||
<link rel="stylesheet" href="<?php echo $asset( 'overlay.css' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>">
|
||||
</head>
|
||||
<body class="sg-theme-<?php echo esc_attr( $style['theme'] ); ?> sg-w-<?php echo esc_attr( $widget ); ?>" data-widget="<?php echo esc_attr( $widget ); ?>" data-endpoint="<?php echo esc_url( $endpoint ); ?>" data-poll="2500">
|
||||
<main id="sg-root" aria-live="polite"></main>
|
||||
<script src="<?php echo $asset( 'overlay.js' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>"></script>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user