Files
rangerhq-radio/inc/settings.php
T
ranger a22ddfb6d3 chore: initial commit — Radio v0.1.0 (Phase A complete)
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>
2026-05-26 09:40:23 +01:00

131 lines
5.9 KiB
PHP

<?php
/**
* Radio — Settings page.
*
* Lets the user pick default station + volume + theme + dashboard
* widget opt-out. Renders the Updates panel from `updater.php` at the
* bottom.
*/
if ( ! defined( 'ABSPATH' ) ) { exit; }
function radio_render_settings_page() {
if ( ! current_user_can( 'read' ) ) {
wp_die( esc_html__( 'You do not have permission to view this page.', 'radio' ) );
}
// Handle form submission.
if ( isset( $_POST['radio_settings_submit'] )
&& check_admin_referer( 'radio_save_settings', 'radio_settings_nonce' ) ) {
$patch = array();
if ( isset( $_POST['default_station'] ) ) {
$patch['station_id'] = sanitize_key( wp_unslash( $_POST['default_station'] ) );
}
if ( isset( $_POST['default_volume'] ) ) {
$patch['volume'] = max( 0.0, min( 1.0, ( (float) $_POST['default_volume'] ) / 100.0 ) );
}
if ( isset( $_POST['theme'] ) ) {
$patch['theme'] = sanitize_key( wp_unslash( $_POST['theme'] ) );
}
radio_update_state( $patch );
// Dashboard-widget opt-out is stored in state too (extra key).
$hide_widget = ! empty( $_POST['hide_dashboard_widget'] );
$user_id = get_current_user_id();
$state = radio_get_state( $user_id );
$state['hide_dashboard_widget'] = $hide_widget ? 1 : 0;
update_user_meta( $user_id, RADIO_META_KEY, $state );
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'radio' ) . '</p></div>';
}
$state = radio_get_state();
$stations = radio_get_stations_grouped();
$hide_widget = ! empty( $state['hide_dashboard_widget'] );
?>
<div class="wrap radio-settings-wrap">
<h1><?php esc_html_e( 'Radio — Settings', 'radio' ); ?></h1>
<form method="post" action="">
<?php wp_nonce_field( 'radio_save_settings', 'radio_settings_nonce' ); ?>
<table class="form-table" role="presentation">
<tr>
<th scope="row">
<label for="default_station"><?php esc_html_e( 'Default station', 'radio' ); ?></label>
</th>
<td>
<select id="default_station" name="default_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'] ); ?>" <?php selected( $entry['id'], $state['station_id'] ); ?>>
<?php echo esc_html( $entry['name'] ); ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
<p class="description">
<?php esc_html_e( 'The station that loads when you open Radio in a fresh tab.', 'radio' ); ?>
</p>
</td>
</tr>
<tr>
<th scope="row">
<label for="default_volume"><?php esc_html_e( 'Default volume', 'radio' ); ?></label>
</th>
<td>
<input type="range" id="default_volume" name="default_volume" min="0" max="100" value="<?php echo esc_attr( (int) round( $state['volume'] * 100 ) ); ?>" oninput="document.getElementById('default_volume_label').textContent = this.value + '%'">
<span id="default_volume_label"><?php echo esc_html( (int) round( $state['volume'] * 100 ) ); ?>%</span>
</td>
</tr>
<tr>
<th scope="row">
<label for="theme"><?php esc_html_e( 'Theme', 'radio' ); ?></label>
</th>
<td>
<select id="theme" name="theme">
<option value="auto" <?php selected( $state['theme'], 'auto' ); ?>><?php esc_html_e( 'Auto (match WP admin colour scheme)', 'radio' ); ?></option>
<option value="light" <?php selected( $state['theme'], 'light' ); ?>><?php esc_html_e( 'Light', 'radio' ); ?></option>
<option value="dark" <?php selected( $state['theme'], 'dark' ); ?>><?php esc_html_e( 'Dark', 'radio' ); ?></option>
</select>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Dashboard widget', 'radio' ); ?>
</th>
<td>
<label>
<input type="checkbox" name="hide_dashboard_widget" value="1" <?php checked( $hide_widget ); ?>>
<?php esc_html_e( 'Hide the Radio widget from the WordPress Dashboard', 'radio' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'When checked, Radio is only accessible from the dedicated admin page (WP Admin → Radio → My Radio).', 'radio' ); ?>
</p>
</td>
</tr>
</table>
<?php submit_button( __( 'Save Changes', 'radio' ), 'primary', 'radio_settings_submit' ); ?>
</form>
<?php
// Updates panel — only manage_options users see it.
if ( current_user_can( 'manage_options' ) ) {
radio_render_updates_panel();
}
?>
</div>
<?php
}