2026-11-15 10:26:03 -08:00
<? php
declare ( strict_types = 1 );
namespace StreamGoals\Admin ;
use StreamGoals\Core\Settings ;
2026-11-15 13:41:18 -08:00
use StreamGoals\Overlay\Build ;
2026-11-15 10:26:03 -08:00
use StreamGoals\Overlay\Style ;
use StreamGoals\Rest\OverlayController ;
/** Overlay URLs, style builder, live preview, token regeneration, and test donations. */
final class OverlaysPage {
public const WIDGETS = array (
'goal' => array ( 'Goal bar' , 800 , 120 , 'Progress bar towards your active goal.' ),
'timer' => array ( 'Subathon timer' , 400 , 120 , 'Countdown that grows with donations (needs an "Add time" rule).' ),
'counter' => array ( 'Counter' , 400 , 120 , 'A running count (needs a "Counter" rule).' ),
'alerts' => array ( 'Donation alerts' , 600 , 300 , 'Pop-up alert for each new donation.' ),
2026-11-15 12:36:28 -08:00
'actions' => array ( 'Rewards menu' , 500 , 400 , 'Lists your donation actions (e.g. "$5: Eat a bean") for the active goal. Shows nothing if you have none.' ),
2026-11-15 10:26:03 -08:00
);
/** @return array{url:string,base:string,sep:string} */
public static function widgetUrl ( string $widget ) : array {
2026-11-15 14:07:44 -08:00
$token = OverlayController :: ensure_token ();
2026-11-15 10:26:03 -08:00
if ( get_option ( 'permalink_structure' ) ) {
$base = home_url ( '/streamgoals-overlay/' . rawurlencode ( $token ) . '/' . rawurlencode ( $widget ) . '/' );
return array ( 'url' => $base , 'base' => $base , 'sep' => '?' );
}
$base = add_query_arg ( array ( 'sg_overlay_token' => $token , 'sg_overlay_widget' => $widget ), home_url ( '/' ) );
return array ( 'url' => $base , 'base' => $base , 'sep' => '&' );
}
/** Used by the wizard and this screen: one copy-able link per widget. */
public static function urlList ( bool $withPreview = false ) : void {
foreach ( self :: WIDGETS as $w => [ $label , $width , $height , $desc ] ) {
$u = self :: widgetUrl ( $w );
echo '<div class="sg-overlay-card" data-widget="' . esc_attr ( $w ) . '" data-base="' . esc_attr ( $u [ 'base' ] ) . '" data-sep="' . esc_attr ( $u [ 'sep' ] ) . '" data-w="' . ( int ) $width . '" data-h="' . ( int ) $height . '">' ;
echo '<h3>' . esc_html ( $label ) . '</h3><p class="description">' . esc_html ( $desc ) . ' OBS size: <strong>' . ( int ) $width . ' x ' . ( int ) $height . '</strong></p>' ;
echo '<div class="sg-copy"><input type="text" readonly class="large-text code sg-url" value="' . esc_attr ( $u [ 'url' ] ) . '" aria-label="' . esc_attr ( $label . ' overlay URL' ) . '"> <button type="button" class="button" data-sg-copy>Copy</button></div>' ;
if ( $withPreview ) {
echo '<p><button type="button" class="button" data-sg-preview>Show live preview</button></p>' ;
echo '<div class="sg-preview" hidden><iframe title="' . esc_attr ( $label . ' preview' ) . '" loading="lazy" style="width:' . ( int ) $width . 'px;height:' . ( int ) $height . 'px"></iframe></div>' ;
}
echo '</div>' ;
}
if ( ! get_option ( 'permalink_structure' ) ) {
echo '<p class="description">Your site uses "Plain" permalinks, so the links use the longer form. They work the same.</p>' ;
}
}
public static function render () : void {
Admin :: open ( 'Overlays' );
$fonts = array ( 'sans' => 'Clean sans-serif' , 'serif' => 'Serif' , 'mono' => 'Monospace' , 'rounded' => 'Rounded' , 'condensed' => 'Condensed' );
echo '<p class="sg-lead">Paste these links into OBS as Browser Sources. Use the style builder to change how they look: all links update as you go.</p>' ;
echo '<div class="sg-card"><h2>Style</h2><form id="sg-style" onsubmit="return false"><div class="sg-grid">' ;
echo '<label>Theme<select name="theme">' ;
foreach ( Style :: THEMES as $t ) {
echo '<option value="' . esc_attr ( $t ) . '">' . esc_html ( ucfirst ( $t ) ) . '</option>' ;
}
echo '</select></label>' ;
echo '<label>Font<select name="font">' ;
foreach ( $fonts as $k => $l ) {
echo '<option value="' . esc_attr ( $k ) . '">' . esc_html ( $l ) . '</option>' ;
}
echo '<option value="__custom">A font installed on my PC...</option></select></label>' ;
echo '<label class="sg-custom-font" hidden>Font name<input type="text" name="font_custom" maxlength="40" placeholder="Bebas Neue"></label>' ;
echo '<label>Size (50% - 300%)<input type="number" name="scale" min="0.5" max="3" step="0.1" value="1"></label>' ;
foreach ( array ( 'color' => 'Text color' , 'accent' => 'Accent color' , 'accent2' => 'Second accent' , 'track' => 'Bar background' , 'bg' => 'Panel background' ) as $k => $l ) {
echo '<label class="sg-color">' . esc_html ( $l ) . '<span><input type="checkbox" name="use_' . esc_attr ( $k ) . '" aria-label="Use custom ' . esc_attr ( $l ) . '"> <input type="color" name="' . esc_attr ( $k ) . '" value="#ffffff"></span></label>' ;
}
echo '</div><p class="description">Tick a box to override that color; unticked colors come from the theme.</p></form></div>' ;
echo '<div class="sg-card"><h2>Links</h2>' ;
self :: urlList ( true );
echo '</div>' ;
2026-11-15 13:41:18 -08:00
echo '<div class="sg-card"><h2>Refresh overlays</h2>' ;
Admin :: form ( 'sg_refresh_overlays' , array (), 'sg-inline' );
echo '<button type="submit" class="button">Refresh all overlays now</button></form>' ;
echo '<p class="description">Reloads every open OBS source. Done automatically after a plugin update.</p></div>' ;
2026-11-15 10:26:03 -08:00
echo '<div class="sg-card"><h2>Send a test donation</h2>' ;
echo '<p>Shows a fake donation on the <strong>Donation alerts</strong> overlay so you can check your OBS setup. Open the alerts preview above (or have OBS running) first, then press the button; the alert appears within a few seconds.</p>' ;
echo '<p class="description"><strong>How tests are kept out of your numbers:</strong> test donations are not attached to any goal, so they never count towards raised totals, timers or counters. They are excluded from the CSV export, and marked <em>TEST</em> in the Donations list. Test donations are kept in the database until you delete them below.</p>' ;
Admin :: form ( 'sg_test_donation' , array (), 'sg-inline' );
echo '<label>Amount (' . esc_html ( Admin :: currency () ) . ') <input type="text" name="amount" class="small-text" value="' . esc_attr ( 0 === \StreamGoals\Core\Money :: exponent ( Admin :: currency () ) ? '500' : '5.00' ) . '"></label> ' ;
echo '<button type="submit" class="button button-primary">Send test donation</button></form> ' ;
Admin :: form ( 'sg_delete_tests' , array (), 'sg-inline' );
echo '<button type="submit" class="button">Delete all test donations</button></form></div>' ;
echo '<div class="sg-card"><h2>Security</h2><p>The links contain a secret token. If a link was shown on stream or shared by mistake, make new links. The old ones stop working immediately and you will need to paste the new ones into OBS.</p>' ;
Admin :: form ( 'sg_regen_token' , array (), 'sg-confirm' );
echo '<button type="submit" class="button button-secondary" data-confirm="Make new overlay links? Your current links will stop working and you must update OBS.">Regenerate token</button></form></div>' ;
Admin :: close ();
}
public static function handleTestDonation () : void {
Admin :: guard ( 'sg_test_donation' );
$cur = Admin :: currency ();
$amt = Input :: amountToMinor ( Admin :: post_str ( 'amount' , 30 ), $cur );
if ( null === $amt || $amt <= 0 || $amt > 99999999 ) {
Admin :: redirect ( Admin :: url ( 'streamgoals-overlays' ), 'error' , 'Please enter a valid test amount, for example 5.00.' );
}
$id = Store :: insertTestDonation ( $amt , $cur );
Admin :: redirect ( Admin :: url ( 'streamgoals-overlays' ), $id ? 'success' : 'error' , $id ? 'Test donation sent. Watch your alerts overlay.' : 'The test donation could not be saved.' );
}
public static function handleDeleteTests () : void {
Admin :: guard ( 'sg_delete_tests' );
$n = Store :: deleteTestDonations ();
Admin :: redirect ( Admin :: url ( 'streamgoals-overlays' ), 'success' , $n . ' test donation(s) deleted.' );
}
2026-11-15 13:41:18 -08:00
public static function handleRefreshAll () : void {
Admin :: guard ( 'sg_refresh_overlays' );
Build :: bump ();
Admin :: redirect ( Admin :: url ( 'streamgoals-overlays' ), 'success' , 'All open overlays will reload within a few seconds.' );
}
2026-11-15 10:26:03 -08:00
public static function handleRegenerate () : void {
Admin :: guard ( 'sg_regen_token' );
Settings :: update ( array ( 'overlay_token' => bin2hex ( random_bytes ( 16 ) ) ) );
Admin :: redirect ( Admin :: url ( 'streamgoals-overlays' ), 'success' , 'New overlay links created. Update them in OBS.' );
}
}