c5a4b28b29
Closes the gaps from a UI review of v0.2.0. Added - Mute toggle: speaker icon is now a button; remembers prior volume. - MediaSession API: OS media keys / headphone buttons / lock-screen widget play/pause the radio. Metadata exposes station + SomaFM + genre. - Current-track display: polls https://somafm.com/songs/{code}.json every 30s while playing; shown as `♪ Title — Artist` under the description. Best-effort — silently hidden if CORS-blocked / unreachable. Fixed (2nd-look) - Dark theme now actually renders. v0.2.0 saved the dropdown but had no CSS — add `admin_body_class` filter + `radio-theme-{auto,light,dark}` CSS for the player + about-cards. `auto` follows OS prefers-color-scheme. - Settings-page volume slider: removed inline `oninput`; wired in radio.js via `bindSettingsSlider()`. Cleaner under strict CSP. - Save errors surface as a transient notice instead of being swallowed. - Gitea changelog URL moved into `RADIO_GITEA_URL` constant. - Genre badge restyled as an inline pill (was using `margin-left: auto` which wrapped poorly on narrow widget widths). Files - radio.php (version, constant, strings, body-class filter) - inc/about.php (use constant, add 0.3.0 history entry) - inc/settings.php (drop inline oninput) - inc/admin-page.php + inc/dashboard-widget.php (mute button, track slot) - assets/css/radio.css (pill, mute, track, dark-theme rules) - assets/js/radio.js (rewrite: mute, MediaSession, track polling, settings slider, save-error surfacing) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
192 lines
7.0 KiB
PHP
192 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Radio — a free SomaFM player for your WordPress dashboard
|
|
*
|
|
* Plugin Name: Radio
|
|
* Plugin URI: https://icanhelp.ie/radio
|
|
* Description: A small, focused, free radio player for your WordPress admin. 44 SomaFM stations grouped by 10 genres — ambient, electronic, lounge, rock, metal, jazz, world, reggae, holiday, specials. Plays via HTML5 audio; volume + station choice persist per-user.
|
|
* Version: 0.3.0
|
|
* Requires at least: 5.0
|
|
* Requires PHP: 7.4
|
|
* Author: David Keane
|
|
* Author URI: https://rangersmyth.xyz/
|
|
* License: GPL v2 or later
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
|
* Text Domain: radio
|
|
*
|
|
* @package Radio
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; }
|
|
|
|
// Plugin coordinates.
|
|
if ( ! defined( 'RADIO_VERSION' ) ) { define( 'RADIO_VERSION', '0.3.0' ); }
|
|
if ( ! defined( 'RADIO_FILE' ) ) { define( 'RADIO_FILE', __FILE__ ); }
|
|
if ( ! defined( 'RADIO_PATH' ) ) { define( 'RADIO_PATH', plugin_dir_path( __FILE__ ) ); }
|
|
if ( ! defined( 'RADIO_URL' ) ) { define( 'RADIO_URL', plugin_dir_url( __FILE__ ) ); }
|
|
if ( ! defined( 'RADIO_BASENAME' ) ) { define( 'RADIO_BASENAME', plugin_basename( __FILE__ ) ); }
|
|
if ( ! defined( 'RADIO_GITEA_URL' ) ) { define( 'RADIO_GITEA_URL', 'https://git.davidtkeane.com/ranger/a-radio' ); }
|
|
|
|
// Includes — each file owns one concern.
|
|
require_once RADIO_PATH . 'inc/stations.php'; // the 44-station array + genre helpers
|
|
require_once RADIO_PATH . 'inc/state.php'; // user_meta storage for station/volume choice
|
|
require_once RADIO_PATH . 'inc/dashboard-widget.php'; // the compact mini-player on WP Dashboard
|
|
require_once RADIO_PATH . 'inc/admin-page.php'; // dedicated Radio admin page (larger player)
|
|
require_once RADIO_PATH . 'inc/about.php'; // About page
|
|
require_once RADIO_PATH . 'inc/settings.php'; // Settings page
|
|
require_once RADIO_PATH . 'inc/updater.php'; // self-hosted update checker against Gitea
|
|
|
|
/**
|
|
* Admin menu registration. Radio gets its own top-level menu — having
|
|
* the player one click away matters more than tucking it into Tools or
|
|
* Settings. Icon is dashicons-format-audio for direct visual match.
|
|
*/
|
|
add_action( 'admin_menu', 'radio_register_admin_menu' );
|
|
function radio_register_admin_menu() {
|
|
add_menu_page(
|
|
__( 'Radio', 'radio' ),
|
|
__( 'Radio', 'radio' ),
|
|
'read', // any logged-in user with read access has their own radio
|
|
'radio',
|
|
'radio_render_main_page',
|
|
'dashicons-format-audio',
|
|
72 // sits below Comments, above plugin entries
|
|
);
|
|
|
|
// "My Radio" — main landing submenu. Same slug as parent so clicking
|
|
// either entry lands on the same page. Empty callback so only the
|
|
// parent renderer fires (lesson learned from Buddy / Logbook duplicate-
|
|
// form gotcha).
|
|
add_submenu_page(
|
|
'radio',
|
|
__( 'My Radio', 'radio' ),
|
|
__( 'My Radio', 'radio' ),
|
|
'read',
|
|
'radio',
|
|
''
|
|
);
|
|
|
|
add_submenu_page(
|
|
'radio',
|
|
__( 'Settings', 'radio' ),
|
|
__( 'Settings', 'radio' ),
|
|
'manage_options',
|
|
'radio-settings',
|
|
'radio_render_settings_page'
|
|
);
|
|
|
|
add_submenu_page(
|
|
'radio',
|
|
__( 'About', 'radio' ),
|
|
__( 'About', 'radio' ),
|
|
'read',
|
|
'radio-about',
|
|
'radio_render_about_page'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Enqueue Radio's CSS + JS on its own admin pages and on the main
|
|
* dashboard (where the mini-player widget lives).
|
|
*/
|
|
add_action( 'admin_enqueue_scripts', 'radio_enqueue_admin_assets' );
|
|
function radio_enqueue_admin_assets( $hook ) {
|
|
$radio_hooks = array(
|
|
'index.php', // WP Dashboard (the widget lives here)
|
|
'toplevel_page_radio', // Radio main page
|
|
'radio_page_radio-settings', // Settings
|
|
'radio_page_radio-about', // About
|
|
);
|
|
if ( ! in_array( $hook, $radio_hooks, true ) ) { return; }
|
|
|
|
wp_enqueue_style(
|
|
'radio-admin',
|
|
RADIO_URL . 'assets/css/radio.css',
|
|
array(),
|
|
RADIO_VERSION
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'radio-admin',
|
|
RADIO_URL . 'assets/js/radio.js',
|
|
array(),
|
|
RADIO_VERSION,
|
|
true
|
|
);
|
|
|
|
// Pass per-user state + station list + AJAX URL to JS.
|
|
wp_localize_script( 'radio-admin', 'RadioPlugin', array(
|
|
'state' => radio_get_state(),
|
|
'stations' => radio_get_stations_flat(),
|
|
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
|
'nonce' => wp_create_nonce( 'radio_save_state' ),
|
|
'strings' => array(
|
|
'play' => __( 'Play', 'radio' ),
|
|
'pause' => __( 'Pause', 'radio' ),
|
|
'loading' => __( 'Loading…', 'radio' ),
|
|
'error' => __( 'Stream error — try another station.', 'radio' ),
|
|
'saveError' => __( 'Preferences not saved — check your connection.', 'radio' ),
|
|
'mute' => __( 'Mute', 'radio' ),
|
|
'unmute' => __( 'Unmute', 'radio' ),
|
|
'nowPlaying' => __( 'Now Playing', 'radio' ),
|
|
'volume' => __( 'Volume', 'radio' ),
|
|
'station' => __( 'Station', 'radio' ),
|
|
),
|
|
) );
|
|
}
|
|
|
|
/**
|
|
* AJAX endpoint for saving per-user state (station / volume changes).
|
|
* Avoids a full page reload on every interaction.
|
|
*/
|
|
add_action( 'wp_ajax_radio_save_state', 'radio_ajax_save_state' );
|
|
function radio_ajax_save_state() {
|
|
if ( ! is_user_logged_in() ) {
|
|
wp_send_json_error( 'Not logged in.', 401 );
|
|
}
|
|
check_ajax_referer( 'radio_save_state', 'nonce' );
|
|
|
|
$patch = array();
|
|
if ( isset( $_POST['station_id'] ) ) {
|
|
$patch['station_id'] = sanitize_key( wp_unslash( $_POST['station_id'] ) );
|
|
}
|
|
if ( isset( $_POST['volume'] ) ) {
|
|
$patch['volume'] = max( 0.0, min( 1.0, (float) $_POST['volume'] ) );
|
|
}
|
|
|
|
if ( empty( $patch ) ) {
|
|
wp_send_json_error( 'Nothing to save.', 400 );
|
|
}
|
|
|
|
radio_update_state( $patch );
|
|
wp_send_json_success( radio_get_state() );
|
|
}
|
|
|
|
/**
|
|
* Surface the user's theme choice (auto/light/dark) to CSS as a body
|
|
* class. `radio-theme-dark` forces dark; `radio-theme-auto` lets the
|
|
* CSS respect `prefers-color-scheme`; `radio-theme-light` is the
|
|
* default no-op.
|
|
*/
|
|
add_filter( 'admin_body_class', 'radio_admin_body_class' );
|
|
function radio_admin_body_class( $classes ) {
|
|
if ( ! is_user_logged_in() ) { return $classes; }
|
|
$state = radio_get_state();
|
|
$theme = isset( $state['theme'] ) ? $state['theme'] : 'auto';
|
|
if ( ! in_array( $theme, array( 'auto', 'light', 'dark' ), true ) ) { $theme = 'auto'; }
|
|
return $classes . ' radio-theme-' . $theme;
|
|
}
|
|
|
|
/**
|
|
* Activation hook: ensure the version option is set so the updater can
|
|
* track it.
|
|
*/
|
|
register_activation_hook( __FILE__, 'radio_on_activate' );
|
|
function radio_on_activate() {
|
|
if ( false === get_option( 'radio_version' ) ) {
|
|
add_option( 'radio_version', RADIO_VERSION );
|
|
} else {
|
|
update_option( 'radio_version', RADIO_VERSION );
|
|
}
|
|
}
|