a22ddfb6d3
First release of a-radio — a free, focused SomaFM player for the
WordPress admin. Extracted-and-rebuilt from RangerPlex's RadioPlayer
component, simplified along the way (no Node CORS proxy needed; the
browser plays SomaFM streams directly).
Phase A deliverables — all in this commit:
PLAYER SURFACES
- Dashboard widget on WP Admin → Dashboard (compact: play/pause +
station select + volume).
- Dedicated admin page at WP Admin → Radio → My Radio (larger
layout, station genre badge, volume %% display).
- Both surfaces share the same JS — picking a station on one mirrors
to the other within the same admin session.
STATIONS
- 44 SomaFM stations grouped by 10 genres (Ambient / Electronic /
Lounge / Rock / Metal / Jazz / World / Reggae / Holiday / Specials).
- Ported verbatim from RangerPlex's RadioPlayer.tsx.
- Default station: Groove Salad (most popular SomaFM channel, safe
ambient/coding pick).
STATE
- Per-user state stored in user_meta under key `radio_state`:
{ station_id, volume, theme, last_played_at }.
- Settings page lets the user pick default station, default volume,
theme (auto/light/dark), and hide the dashboard widget.
- AJAX endpoint `radio_save_state` persists changes without a page
reload. Nonce-protected, capability-checked, only known keys
accepted, station validated against the list, volume clamped to
[0,1].
UPDATES
- Self-hosted updater wired to Gitea (ranger/a-radio) from commit 1.
- Direct port of Buddy's inc/updater.php with BUDDY_* → RADIO_* and
buddy_* → radio_* renames. Same 12h-success / 1h-error caching.
ARCHITECTURE
- No `wp` prefix (trademark policy).
- GPL v2+ public Gitea repo from day one.
- Vanilla JS only — no React, no webpack, no minified-only files.
- CSS-only animations, all assets local.
- Single H1 per admin page.
- Direct HTML5 <audio> playback (SomaFM has CORS headers; no PHP
audio proxy needed). This is the key simplification vs RangerPlex.
COMPLIANCE
- "Powered by SomaFM" credit displayed on both player surfaces with
link to somafm.com. About page invites users to donate to SomaFM
directly.
PHASE PROGRESSION (not in this commit)
- Phase B — Settings polish + retry on transient stream errors +
README.md formatted for WP.org submission.
- Phase C — Now-playing metadata via SomaFM's per-station song
history endpoint (this is where the RangerPlex proxy logic ports
to — server-side, for metadata not audio).
- Phase D — [ranger_radio] shortcode for frontend embedding.
- Phase E — Favorites system.
- Phase F — Multi-provider (Radio Paradise / NTS / KEXP / BBC).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
173 lines
6.0 KiB
PHP
173 lines
6.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.1.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.1.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__ ) ); }
|
|
|
|
// 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' ),
|
|
'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() );
|
|
}
|
|
|
|
/**
|
|
* 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 );
|
|
}
|
|
}
|