Files
rangerhq-radio/radio.php
T
ranger 774e7f9958 fix(0.3.1): My Radio layout polish + drop dark-auto
Real-screen review of v0.3.0 surfaced a contrast bug and several layout
issues. Quick patch.

- Drop @media prefers-color-scheme dark for theme=auto. WP admin has
  no native dark mode, so OS-dark made our light text sit on the still-
  white WP postbox = unreadable. `auto` now behaves as light; `dark`
  remains as an explicit choice.
- theme=dark now actually reads: the player surface itself goes dark
  (#1d2327 bg + subtle border + padding) so the light text has somewhere
  to land instead of fighting the white WP postbox.
- .radio-wrap max-width 880px — player no longer stretches edge-to-edge.
- Drop the .radio-intro max-width:720px cap so the intro fits one line.
- Volume slider fixed at flex:0 0 auto / width:220px — % label sits next
  to the slider instead of pinned to the far edge.
- Station dropdown capped at 360px (was width:100%).
- Play-button dashicon shrunk 18px → 14px so it sits on the button-text
  baseline instead of looking like a second row.

Files: radio.php, assets/css/radio.css, inc/about.php, CHANGELOG.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 23:11:06 +01:00

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.1
* 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.1' ); }
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 );
}
}