Files
wp-donations/includes/Rest/OverlayController.php
T
shadowdaoandClaude Sonnet 5.5 b0b5012698
Test and Release / test (push) Successful in 1m43s
Test and Release / release (push) Successful in 7s
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>
2026-11-15 12:36:28 -08:00

71 lines
2.7 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Rest;
use StreamGoals\Core\Settings;
use StreamGoals\Overlay\OverlayService;
use StreamGoals\Overlay\Payloads;
use StreamGoals\Overlay\Router;
use StreamGoals\Overlay\WpdbDataSource;
/** GET streamgoals/v1/overlay/{token}/{widget} — public-safe, ETag'd JSON for OBS polling. */
final class OverlayController {
public static function register(): void {
add_action( 'rest_api_init', array( self::class, 'routes' ) );
Router::register();
}
public static function routes(): void {
register_rest_route( 'streamgoals/v1', '/overlay/(?P<token>[A-Za-z0-9]{16,128})/(?P<widget>goal|timer|counter|alerts|actions)', array(
'methods' => 'GET',
'callback' => array( self::class, 'handle' ),
'permission_callback' => '__return_true', // capability is the unguessable token, checked in handle()
'args' => array(
'since' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint' ),
'recent' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 1, 'sanitize_callback' => 'absint' ),
),
) );
}
/** Lazily generates the token (32 hex chars) on first use. */
public static function token(): string {
$token = (string) Settings::get( 'overlay_token', '' );
if ( strlen( $token ) < 32 ) {
$token = bin2hex( random_bytes( 16 ) );
Settings::update( array( 'overlay_token' => $token ) );
}
return $token;
}
public static function verify_token( string $given ): bool {
return hash_equals( self::token(), $given );
}
public static function handle( \WP_REST_Request $req ): \WP_REST_Response|\WP_Error {
if ( ! self::verify_token( (string) $req['token'] ) ) {
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
}
$since = $req->has_param( 'since' ) && '' !== (string) $req->get_param( 'since' ) ? absint( $req->get_param( 'since' ) ) : null;
$recent = $req->has_param( 'recent' ) && 1 === absint( $req->get_param( 'recent' ) );
$payload = ( new OverlayService( new WpdbDataSource() ) )->payload( (string) $req['widget'], time(), $since, $recent );
if ( null === $payload ) {
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
}
$etag = Payloads::etag( $payload );
$headers = array( 'ETag' => $etag, 'Cache-Control' => 'private, max-age=1, must-revalidate', 'X-Robots-Tag' => 'noindex' );
$inm = (string) $req->get_header( 'if_none_match' );
if ( '' !== $inm && in_array( $etag, array_map( 'trim', explode( ',', str_replace( 'W/', '', $inm ) ) ), true ) ) {
$res = new \WP_REST_Response( null, 304 );
} else {
$res = new \WP_REST_Response( $payload, 200 );
}
foreach ( $headers as $k => $v ) {
$res->header( $k, $v );
}
return $res;
}
}