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>
98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Radio state — per-user persistence for station choice + volume.
|
|
*
|
|
* Storage: per-user, in `wp_usermeta` under key `radio_state`. A small
|
|
* associative array. Picked user_meta over site-wide options so each
|
|
* admin remembers their own preferred station — matches Buddy's
|
|
* per-user model and the WordPress mental model.
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; }
|
|
|
|
const RADIO_META_KEY = 'radio_state';
|
|
|
|
/**
|
|
* Default state for a fresh listener. Used the first time a user
|
|
* visits a Radio-rendering page.
|
|
*
|
|
* @return array { station_id, volume, theme, last_played_at }
|
|
*/
|
|
function radio_default_state() {
|
|
return array(
|
|
'station_id' => radio_default_station_id(), // Groove Salad
|
|
'volume' => 0.6, // 60% — comfortable default
|
|
'theme' => 'auto', // auto = match site admin colour scheme
|
|
'last_played_at' => 0,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Fetch the current user's state, falling back to defaults and
|
|
* persisting them on first read so a baseline exists for AJAX patches.
|
|
*
|
|
* @param int $user_id Optional. Defaults to current user.
|
|
* @return array
|
|
*/
|
|
function radio_get_state( $user_id = 0 ) {
|
|
$user_id = $user_id ? (int) $user_id : get_current_user_id();
|
|
if ( ! $user_id ) { return radio_default_state(); }
|
|
|
|
$state = get_user_meta( $user_id, RADIO_META_KEY, true );
|
|
if ( ! is_array( $state ) || empty( $state ) ) {
|
|
$state = radio_default_state();
|
|
update_user_meta( $user_id, RADIO_META_KEY, $state );
|
|
}
|
|
|
|
// Merge any newly-introduced default keys forward without trampling
|
|
// existing user values.
|
|
$state = array_merge( radio_default_state(), $state );
|
|
|
|
// Validate station — if it's been removed from stations.php, fall
|
|
// back to the default rather than rendering a dead choice.
|
|
if ( ! radio_find_station( $state['station_id'] ) ) {
|
|
$state['station_id'] = radio_default_station_id();
|
|
}
|
|
|
|
// Clamp volume to [0, 1].
|
|
$state['volume'] = max( 0.0, min( 1.0, (float) $state['volume'] ) );
|
|
|
|
return $state;
|
|
}
|
|
|
|
/**
|
|
* Apply a partial update to the current user's state. Only keys in
|
|
* `radio_default_state()` are persisted; everything else is dropped.
|
|
*
|
|
* @param array $patch
|
|
* @param int $user_id Optional. Defaults to current user.
|
|
* @return array The new state.
|
|
*/
|
|
function radio_update_state( array $patch, $user_id = 0 ) {
|
|
$user_id = $user_id ? (int) $user_id : get_current_user_id();
|
|
if ( ! $user_id ) { return radio_default_state(); }
|
|
|
|
$state = radio_get_state( $user_id );
|
|
$allowed = array_keys( radio_default_state() );
|
|
$filtered = array_intersect_key( $patch, array_flip( $allowed ) );
|
|
|
|
if ( isset( $filtered['station_id'] ) ) {
|
|
$filtered['station_id'] = sanitize_key( $filtered['station_id'] );
|
|
if ( ! radio_find_station( $filtered['station_id'] ) ) {
|
|
unset( $filtered['station_id'] );
|
|
}
|
|
}
|
|
if ( isset( $filtered['volume'] ) ) {
|
|
$filtered['volume'] = max( 0.0, min( 1.0, (float) $filtered['volume'] ) );
|
|
}
|
|
if ( isset( $filtered['theme'] ) ) {
|
|
$filtered['theme'] = in_array( $filtered['theme'], array( 'auto', 'light', 'dark' ), true ) ? $filtered['theme'] : 'auto';
|
|
}
|
|
|
|
$new_state = array_merge( $state, $filtered );
|
|
$new_state['last_played_at'] = time();
|
|
|
|
update_user_meta( $user_id, RADIO_META_KEY, $new_state );
|
|
return $new_state;
|
|
}
|