goal id => title */ private static function goalTitles(): array { $out = array(); foreach ( Store::goals()->all() as $g ) { $out[ (int) $g['id'] ] = (string) $g['title']; } return $out; } /** Attachment lookup for validation: mime + size, null when missing. */ public static function attachmentInfo( int $id ): ?array { if ( $id <= 0 || 'attachment' !== get_post_type( $id ) ) { return null; } $path = get_attached_file( $id ); return array( 'mime' => (string) get_post_mime_type( $id ), 'size' => ( is_string( $path ) && is_file( $path ) ) ? (int) filesize( $path ) : 0, ); } /** * Media column parts for one alert. $existing maps attachment id => true for ids still in the Media Library. * * @param array $d * @param array $existing * @return array list of [label, missing] */ public static function mediaParts( array $d, array $existing ): array { $parts = array(); $vid = (int) ( $d['visual_id'] ?? 0 ); if ( $vid > 0 ) { $video = 'video' === ( $d['visual_type'] ?? '' ); $parts[] = isset( $existing[ $vid ] ) ? array( $video ? 'Video' : 'Picture/GIF', false ) : array( $video ? 'Video missing' : 'Picture missing', true ); } $sid = (int) ( $d['sound_id'] ?? 0 ); if ( $sid > 0 ) { $parts[] = isset( $existing[ $sid ] ) ? array( 'Sound', false ) : array( 'Sound missing', true ); } return $parts; } /** One query: which of the alerts' attachments still exist. @return array */ private static function existingAttachments( array $defs ): array { $ids = array(); foreach ( $defs as $d ) { foreach ( array( 'visual_id', 'sound_id' ) as $k ) { if ( (int) ( $d[ $k ] ?? 0 ) > 0 ) { $ids[ (int) $d[ $k ] ] = true; } } } if ( ! $ids ) { return array(); } $found = get_posts( array( 'post_type' => 'attachment', 'post_status' => 'any', 'post__in' => array_keys( $ids ), 'posts_per_page' => count( $ids ), 'fields' => 'ids', 'orderby' => 'none', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false ) ); return array_fill_keys( array_map( 'intval', $found ), true ); } /* ---------------------------------------------------------------- render */ public static function render(): void { Admin::open( 'Alerts' ); $edit = Admin::get_int( 'edit' ); if ( 'new' === Admin::get_str( 'view' ) || $edit ) { self::renderForm( $edit ); } else { self::renderHelp(); self::renderList(); } Admin::close(); } private static function renderHelp(): void { echo '

Make your alerts come alive

'; echo '

An alert is the pop-up on your stream when someone donates. Here you can give it a picture, a GIF or a video, and a sound. Pick the donation size (or a reward) that should trigger it.

'; echo '
    '; echo '
  • By amount: "Any donation of at least $X". If several match, the one with the highest $X wins.
  • '; echo '
  • By reward: plays when a donor triggers one of your rewards. A reward alert beats an amount alert.
  • '; echo '
  • Nothing set up? Alerts look and work exactly as before.
  • '; echo '
  • In OBS media plays automatically. In a normal browser tab you must click the page once before sound is allowed.
  • '; echo '
  • Pictures and GIFs stay for the time you choose. Videos play to the end, for at most 30 seconds.
  • '; echo '
  • Keep files small (under 25 MB). Big videos make OBS slow to load and can make your stream stutter.
  • '; echo '
'; } private static function renderList(): void { $cur = Admin::currency(); $defs = self::repo()->all(); $goals = self::goalTitles(); $rewards = array(); foreach ( ( new ActionRepository() )->all() as $a ) { $rewards[ $a['id'] ] = $a['title']; } echo '

Your alerts

'; echo '

Add an alert

'; if ( ! $defs ) { echo '

No custom alerts yet.

'; return; } echo Admin::tableOpen( array( 'Name', 'Plays when', 'Applies to', 'Media', 'Order', 'Actions' ), 'Your alerts' ); $existing = self::existingAttachments( $defs ); foreach ( $defs as $d ) { $id = (int) $d['id']; $when = 'reward' === $d['trigger_type'] ? 'Reward: ' . ( $rewards[ $d['action_id'] ] ?? 'a reward that was deleted' ) : 'Donation of at least ' . Format::money( (int) $d['min_minor'], $cur ); $media = array(); foreach ( self::mediaParts( $d, $existing ) as [ $mLabel, $mMissing ] ) { $media[] = $mMissing ? '' . esc_html( $mLabel ) . '' : esc_html( $mLabel ); } echo '' . Admin::td( 'Name' ) . '' . esc_html( $d['title'] ) . ''; echo Admin::td( 'Plays when' ) . esc_html( $when ) . ''; echo Admin::td( 'Applies to' ) . esc_html( 0 === $d['goal_id'] ? 'All goals' : ( $goals[ $d['goal_id'] ] ?? 'Goal #' . $d['goal_id'] ) ) . ''; echo Admin::td( 'Media' ) . ( $media ? implode( ', ', $media ) : 'Text only' ) . ''; echo Admin::td( 'Order' ) . (int) $d['sort'] . '' . Admin::td( 'Actions', 'sg-actions' ); echo 'Edit '; Admin::form( 'sg_alert_toggle', array( 'alert_id' => $id, 'enabled' => $d['enabled'] ? 0 : 1 ), 'sg-inline' ); echo ' '; Admin::form( 'sg_alert_delete', array( 'alert_id' => $id ), 'sg-inline sg-confirm' ); echo ''; echo ''; } echo Admin::tableClose() . ''; } private static function mediaField( string $name, string $label, string $kind, int $id, string $help ): void { $url = ''; $mime = ''; if ( $id > 0 && null !== self::attachmentInfo( $id ) ) { $url = (string) wp_get_attachment_url( $id ); $mime = (string) get_post_mime_type( $id ); } echo '' . esc_html( $label ) . ''; echo '
'; echo ''; echo '' . esc_html( '' !== $url ? wp_basename( $url ) : 'Nothing chosen' ) . ' '; echo ' '; echo ''; echo '

'; echo '

' . esc_html( $help ) . '

'; } private static function renderForm( int $id ): void { $d = $id ? self::repo()->find( $id ) : null; if ( $id && ! $d ) { echo '

That alert no longer exists. Back to alerts

'; return; } $cur = Admin::currency(); $goals = self::goalTitles(); $val = static fn( string $k, $default = '' ) => $d[ $k ] ?? $default; $trigger = (string) $val( 'trigger_type', 'amount' ); $rewards = array(); foreach ( ( new ActionRepository() )->all() as $a ) { if ( $a['enabled'] || (int) $a['id'] === (int) $val( 'action_id', 0 ) ) { $rewards[] = array( 'id' => $a['id'], 'title' => $a['title'], 'enabled' => (bool) $a['enabled'] ); } } $data = array( 'currency' => $cur, 'exp' => Money::exponent( $cur ), 'rewards' => $rewards, 'videoCap' => Media::VIDEO_CAP_MS, 'warnBytes' => Media::WARN_BYTES, 'allowed' => Media::MIMES, ); echo '

' . ( $d ? 'Edit alert' : 'Add an alert' ) . '

'; Admin::form( 'sg_alert_save', array( 'alert_id' => $id ), 'sga-form' ); echo ''; echo ''; echo ''; echo ''; self::mediaField( 'visual_id', 'Picture, GIF or video', 'visual', (int) $val( 'visual_id', 0 ), 'PNG, JPEG, GIF, WebP, MP4 or WebM. Optional. Under 25 MB works best; big videos can slow OBS down.' ); self::mediaField( 'sound_id', 'Sound', 'sound', (int) $val( 'sound_id', 0 ), 'MP3, OGG or WAV. Optional. If you also pick a video, the video is muted and this sound plays instead.' ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '

Preview

'; echo '

This is how the alert will look on stream. Press the button to play it (browsers only allow sound after you click).

'; echo '

'; echo '
'; echo '
'; echo '

Cancel

'; } /* -------------------------------------------------------------- handlers */ public static function handleSave(): void { Admin::guard( 'sg_alert_save' ); $post = Admin::post_raw(); $id = isset( $post['alert_id'] ) ? absint( $post['alert_id'] ) : 0; $back = self::url( $id ? array( 'edit' => $id ) : array( 'view' => 'new' ) ); $repo = self::repo(); if ( $id && ! $repo->find( $id ) ) { Admin::redirect( self::url(), 'error', 'That alert no longer exists.' ); } $enabled = array(); foreach ( ( new ActionRepository() )->all() as $a ) { if ( $a['enabled'] ) { $enabled[] = (int) $a['id']; } } $res = DefInput::validate( $post, self::minMinor(), Admin::currency(), array_keys( self::goalTitles() ), $enabled, array( self::class, 'attachmentInfo' ), $repo->nextSort() ); if ( null === $res['data'] ) { Admin::redirect( $back, 'error', (string) $res['error'] ); } if ( $id ) { $repo->update( $id, $res['data'] ); } else { $repo->create( $res['data'] ); } $msg = 'Alert saved.' . ( $res['warnings'] ? ' Heads up: ' . implode( ' ', $res['warnings'] ) : '' ); Admin::redirect( self::url(), $res['warnings'] ? 'warning' : 'success', $msg ); } public static function handleToggle(): void { Admin::guard( 'sg_alert_toggle' ); $post = Admin::post_raw(); self::repo()->setEnabled( isset( $post['alert_id'] ) ? absint( $post['alert_id'] ) : 0, ! empty( $post['enabled'] ) ); Admin::redirect( self::url(), 'success', 'Alert updated.' ); } public static function handleDelete(): void { Admin::guard( 'sg_alert_delete' ); $post = Admin::post_raw(); self::repo()->delete( isset( $post['alert_id'] ) ? absint( $post['alert_id'] ) : 0 ); Admin::redirect( self::url(), 'success', 'Alert deleted.' ); } }