f5feca7dfa
Two-layer "this is playing right now" visual:
(1) CSS dancing bars — four tiny vertical bars next to the "Now Playing"
label, staggered `@keyframes` pulse while audio plays. Pure CSS, no
JS dependency, tints to the user's WP admin colour scheme via
var(--wp-admin-theme-color). Driven by a single `.is-playing` class
on `.radio-player` toggled from the existing play/pause/error
handlers. Always works.
(2) Web Audio frequency visualizer (progressive upgrade) — on first play,
builds AudioContext + AnalyserNode + canvas drawing pipeline. When
the analyser starts returning real (non-zero) data, hides the bars
and shows the canvas with live frequency bars. Falls back to bars
if AudioContext is unavailable, createMediaElementSource throws, or
the analyser returns all-zeros for >2s (CORS silently blocking).
State machine on player._vizState: no-webaudio / init-failed /
cors-blocked / ok.
`<audio>` element gained `crossorigin="anonymous"` so Web Audio can read
the stream data (SomaFM serves the CORS headers).
Files: radio.php (version), inc/admin-page.php + inc/dashboard-widget.php
(.radio-player__indicator with .radio-player__bars + canvas; crossorigin
on audio), assets/css/radio.css (indicator, bars, radio-bars-dance
keyframes, canvas size), assets/js/radio.js (tryVisualizer,
startVizLoop, stopVizLoop; play/pause/error handlers wire the loop and
toggle is-playing), inc/about.php (history entry).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
109 lines
5.7 KiB
PHP
109 lines
5.7 KiB
PHP
<?php
|
|
/**
|
|
* Radio — main admin page (WP Admin → Radio → My Radio).
|
|
*
|
|
* Uses WP-native postbox structure so the player feels like part of
|
|
* WordPress, not a third-party widget. The dashboard widget shares
|
|
* the same internal markup via assets/js/radio.js binding to every
|
|
* .radio-player on the page.
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; }
|
|
|
|
function radio_render_main_page() {
|
|
if ( ! current_user_can( 'read' ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to view this page.', 'radio' ) );
|
|
}
|
|
|
|
$state = radio_get_state();
|
|
$station = radio_find_station( $state['station_id'] );
|
|
$stations = radio_get_stations_grouped();
|
|
$count = count( radio_get_stations_flat() );
|
|
?>
|
|
<div class="wrap radio-wrap">
|
|
<h1><?php esc_html_e( 'Radio', 'radio' ); ?></h1>
|
|
|
|
<p class="radio-intro">
|
|
<?php
|
|
printf(
|
|
/* translators: %d = number of stations */
|
|
esc_html__( 'A tab of background music for your WordPress admin. %d hand-curated SomaFM stations across 10 genres — free, no ads, no tracking.', 'radio' ),
|
|
(int) $count
|
|
);
|
|
?>
|
|
</p>
|
|
|
|
<div class="postbox">
|
|
<div class="postbox-header">
|
|
<h2 class="hndle"><?php esc_html_e( 'Player', 'radio' ); ?></h2>
|
|
</div>
|
|
<div class="inside">
|
|
<div class="radio-player" data-radio-surface="main">
|
|
|
|
<div class="radio-player__now">
|
|
<span class="radio-player__indicator" aria-hidden="true">
|
|
<span class="radio-player__bars"><span></span><span></span><span></span><span></span></span>
|
|
<canvas class="radio-player__viz" data-radio-viz hidden></canvas>
|
|
</span>
|
|
<span class="radio-player__label"><?php esc_html_e( 'Now Playing', 'radio' ); ?></span>
|
|
<span class="radio-player__station-name" data-radio-name><?php echo esc_html( $station['name'] ); ?></span>
|
|
<span class="radio-player__station-genre" data-radio-genre><?php echo esc_html( $station['genre'] ); ?></span>
|
|
<p class="radio-player__station-desc" data-radio-desc><?php echo esc_html( $station['description'] ); ?></p>
|
|
<p class="radio-player__track" data-radio-track hidden></p>
|
|
</div>
|
|
|
|
<div class="radio-player__controls">
|
|
<button type="button" class="button button-primary radio-player__play" data-radio-play>
|
|
<span class="radio-player__play-glyph" data-radio-play-glyph aria-hidden="true">▶</span>
|
|
<span data-radio-play-label><?php esc_html_e( 'Play', 'radio' ); ?></span>
|
|
</button>
|
|
|
|
<div class="radio-player__volume">
|
|
<button type="button" class="radio-player__mute" data-radio-mute aria-label="<?php esc_attr_e( 'Mute', 'radio' ); ?>">
|
|
<span class="dashicons dashicons-controls-volumeon" aria-hidden="true"></span>
|
|
</button>
|
|
<input type="range" min="0" max="100" value="<?php echo esc_attr( (int) round( $state['volume'] * 100 ) ); ?>" data-radio-volume aria-label="<?php esc_attr_e( 'Volume', 'radio' ); ?>">
|
|
<span class="radio-player__volume-pct" data-radio-volume-pct><?php echo esc_html( (int) round( $state['volume'] * 100 ) ); ?>%</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="radio-player__station-select">
|
|
<label for="radio-station-main"><?php esc_html_e( 'Station', 'radio' ); ?></label>
|
|
<select id="radio-station-main" data-radio-station>
|
|
<?php foreach ( $stations as $genre => $entries ) :
|
|
if ( empty( $entries ) ) { continue; }
|
|
?>
|
|
<optgroup label="<?php echo esc_attr( $genre ); ?>">
|
|
<?php foreach ( $entries as $entry ) : ?>
|
|
<option value="<?php echo esc_attr( $entry['id'] ); ?>" data-url="<?php echo esc_attr( $entry['url'] ); ?>" data-desc="<?php echo esc_attr( $entry['description'] ); ?>" data-genre="<?php echo esc_attr( $entry['genre'] ); ?>" <?php selected( $entry['id'], $state['station_id'] ); ?>>
|
|
<?php echo esc_html( $entry['name'] ); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</optgroup>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="radio-player__error" data-radio-error hidden></div>
|
|
|
|
<audio data-radio-audio preload="none" crossorigin="anonymous"></audio>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<p class="radio-player__credit radio-player__credit--main description">
|
|
<?php
|
|
printf(
|
|
wp_kses(
|
|
/* translators: %s = link to somafm.com */
|
|
__( 'Stations and streams provided by %s — an independent, listener-supported, commercial-free internet radio network. Please consider supporting them.', 'radio' ),
|
|
array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true ) )
|
|
),
|
|
'<a href="https://somafm.com/" target="_blank" rel="noopener">SomaFM</a>'
|
|
);
|
|
?>
|
|
</p>
|
|
</div>
|
|
<?php
|
|
}
|