Files
ranger 340cbb2487 v0.7.5 — WordPress.org slug rename: a-radio → rangerhq-radio
A third-party AI-driven naming check flagged `a-radio` as too
generic for the wp.org Plugin Directory (single common functional
word, no distinguishing prefix). The verdict was advisory but
defensible — short generic slugs are increasingly rejected as the
directory grows past 60K plugins. Fixing it preemptively is cheaper
than facing a rejection at submission time.

The new slug `rangerhq-radio` matches the public display name
"RangerHQ Radio" (unchanged) and lines up with the rest of the
RangerHQ plugin family: `rangerhq-spatial`, `rangerhq-glyph`,
now `rangerhq-radio`.

Changes (packaging only — no player behaviour change):
* Text Domain `a-radio` → `rangerhq-radio` across all 125 i18n
  call sites via in-place sed (esc_html__, _e, __, esc_attr_e
  and their friends). PHP lint clean post-rename.
* `Text Domain:` plugin header in radio.php line 15 → `rangerhq-radio`.
* `RADIO_GITEA_URL` constant value → new Gitea repo URL.
* README.md install link → new repo URL.
* readme.txt FAQ Gitea link → new repo URL.
* readme.txt Stable tag → 0.7.5.
* inc/about.php — v0.7.5 in "latest" slot; v0.7.4 demoted.

Unchanged deliberately (would have been pure churn):
* Plugin Name header "RangerHQ Radio" — already correct.
* Plugin URI.
* Internal constants `RADIO_*` — don't have to match slug.
* User-meta keys `radio_state` / `radio_history` /
  `radio_favourites` — renaming would orphan every existing
  user's settings on upgrade.
* HTML `data-radio-*` attributes — JS controller's element
  selectors in radio.js, not slug-related.
* CSS class names `radio-player`, `radio-about-*` — internal
  scoping.
* Main plugin file name `radio.php`.

Migration: existing Gitea-installed copies need their folder
renamed on disk (a-radio → rangerhq-radio) + reactivation in
WP admin. No data loss because all user-facing state lives in
user_meta under unchanged keys.

This commit is the file content for v0.7.5; the annotated tag
and push will follow the Gitea repo rename (ranger/a-radio →
ranger/rangerhq-radio) so the new tag lives in the new URL
space from the start.
2026-05-30 04:55:39 +01:00

259 lines
13 KiB
PHP

<?php
/**
* Radio — track history + favourites (v0.5.0).
*
* Storage: two per-user `wp_usermeta` keys, separate from `radio_state`
* so frequent track logging doesn't rewrite the whole state blob.
* radio_history — capped FIFO list of recently played tracks
* radio_favourites — uncapped list of user-starred tracks
*
* Entry shape:
* array(
* 'artist' => string,
* 'title' => string,
* 'station' => string, // display name e.g. "DEF CON Radio"
* 'station_id' => string, // e.g. "soma-defcon"
* 'at' => int, // unix timestamp
* )
*/
if ( ! defined( 'ABSPATH' ) ) { exit; }
const RADIO_HISTORY_KEY = 'radio_history';
const RADIO_FAVOURITES_KEY = 'radio_favourites';
const RADIO_HISTORY_CAP = 500;
/** Current user's track history (oldest first). */
function radio_get_history( $user_id = 0 ) {
$user_id = $user_id ? (int) $user_id : get_current_user_id();
if ( ! $user_id ) { return array(); }
$h = get_user_meta( $user_id, RADIO_HISTORY_KEY, true );
return is_array( $h ) ? $h : array();
}
/** Current user's favourited tracks (oldest first). */
function radio_get_favourites( $user_id = 0 ) {
$user_id = $user_id ? (int) $user_id : get_current_user_id();
if ( ! $user_id ) { return array(); }
$f = get_user_meta( $user_id, RADIO_FAVOURITES_KEY, true );
return is_array( $f ) ? $f : array();
}
/** Normalise raw POSTed track data; returns null on junk input. */
function radio_sanitize_entry( $entry ) {
if ( ! is_array( $entry ) ) { return null; }
$artist = isset( $entry['artist'] ) ? sanitize_text_field( wp_unslash( $entry['artist'] ) ) : '';
$title = isset( $entry['title'] ) ? sanitize_text_field( wp_unslash( $entry['title'] ) ) : '';
$station = isset( $entry['station'] ) ? sanitize_text_field( wp_unslash( $entry['station'] ) ) : '';
$station_id = isset( $entry['station_id'] ) ? sanitize_key( wp_unslash( $entry['station_id'] ) ) : '';
if ( ! $artist || ! $title ) { return null; }
if ( strtolower( $artist ) === '(unknown)' ) { return null; } // SomaFM promo / dead-air placeholder
return array(
'artist' => mb_substr( $artist, 0, 200 ),
'title' => mb_substr( $title, 0, 200 ),
'station' => mb_substr( $station, 0, 100 ),
'station_id' => $station_id,
'at' => time(),
);
}
/** Dedup signature — artist|title|station_id, lowercased + trimmed. */
function radio_entry_signature( $entry ) {
$a = isset( $entry['artist'] ) ? $entry['artist'] : '';
$t = isset( $entry['title'] ) ? $entry['title'] : '';
$s = isset( $entry['station_id'] ) ? $entry['station_id'] : '';
return strtolower( trim( $a . '|' . $t . '|' . $s ) );
}
/** Append a track to the user's history, deduped against the last entry
* and capped at RADIO_HISTORY_CAP. Returns true if appended. */
function radio_log_track( $entry, $user_id = 0 ) {
$user_id = $user_id ? (int) $user_id : get_current_user_id();
if ( ! $user_id ) { return false; }
$clean = radio_sanitize_entry( $entry );
if ( ! $clean ) { return false; }
$history = radio_get_history( $user_id );
if ( ! empty( $history ) ) {
$last_sig = radio_entry_signature( $history[ count( $history ) - 1 ] );
if ( radio_entry_signature( $clean ) === $last_sig ) { return false; }
}
$history[] = $clean;
if ( count( $history ) > RADIO_HISTORY_CAP ) {
$history = array_slice( $history, -RADIO_HISTORY_CAP );
}
update_user_meta( $user_id, RADIO_HISTORY_KEY, $history );
return true;
}
/** Toggle whether an entry is favourited. Returns the new state
* (true = now favourited, false = now unfavourited). */
function radio_toggle_favourite( $entry, $user_id = 0 ) {
$user_id = $user_id ? (int) $user_id : get_current_user_id();
if ( ! $user_id ) { return false; }
$clean = radio_sanitize_entry( $entry );
if ( ! $clean ) { return false; }
$sig = radio_entry_signature( $clean );
$favs = radio_get_favourites( $user_id );
$found = -1;
foreach ( $favs as $i => $f ) {
if ( radio_entry_signature( $f ) === $sig ) { $found = $i; break; }
}
if ( $found >= 0 ) {
array_splice( $favs, $found, 1 );
update_user_meta( $user_id, RADIO_FAVOURITES_KEY, $favs );
return false;
}
$favs[] = $clean;
update_user_meta( $user_id, RADIO_FAVOURITES_KEY, $favs );
return true;
}
/** Clear the user's history (favourites preserved). */
function radio_clear_history_all( $user_id = 0 ) {
$user_id = $user_id ? (int) $user_id : get_current_user_id();
if ( ! $user_id ) { return false; }
delete_user_meta( $user_id, RADIO_HISTORY_KEY );
return true;
}
/** URL-build helpers for the four search providers. */
function radio_search_urls( $artist, $title ) {
$enc = rawurlencode( trim( $artist . ' ' . $title ) );
return array(
'spotify' => 'https://open.spotify.com/search/' . $enc,
'youtube' => 'https://www.youtube.com/results?search_query=' . $enc,
'apple' => 'https://music.apple.com/search?term=' . $enc,
'bandcamp' => 'https://bandcamp.com/search?q=' . $enc,
);
}
/** Render the History admin page (tabs: History / Favourites). */
function radio_render_history_page() {
if ( ! current_user_can( 'read' ) ) {
wp_die( esc_html__( 'You do not have permission to view this page.', 'rangerhq-radio' ) );
}
$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'history'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- tab-switch only, no state change
if ( ! in_array( $tab, array( 'history', 'favourites' ), true ) ) { $tab = 'history'; }
$all_history = radio_get_history();
$all_favourites = radio_get_favourites();
$entries = ( $tab === 'favourites' ) ? $all_favourites : $all_history;
$entries = array_reverse( $entries ); // newest first
// Set of favourite signatures for fast lookup in the row render.
$fav_sigs = array();
foreach ( $all_favourites as $f ) { $fav_sigs[ radio_entry_signature( $f ) ] = true; }
// Stations present in the current tab → filter dropdown options.
$stations_in_list = array();
foreach ( $entries as $e ) {
if ( ! empty( $e['station_id'] ) && ! isset( $stations_in_list[ $e['station_id'] ] ) ) {
$stations_in_list[ $e['station_id'] ] = $e['station'];
}
}
asort( $stations_in_list );
$base_url = admin_url( 'admin.php?page=radio-history' );
$hist_url = $base_url . '&tab=history';
$fav_url = $base_url . '&tab=favourites';
$nonce = wp_create_nonce( 'radio_history' );
?>
<div class="wrap radio-history-wrap">
<h1><?php esc_html_e( 'Radio — Track history', 'rangerhq-radio' ); ?></h1>
<h2 class="nav-tab-wrapper">
<a href="<?php echo esc_url( $hist_url ); ?>" class="nav-tab <?php echo $tab === 'history' ? 'nav-tab-active' : ''; ?>">
<?php esc_html_e( 'History', 'rangerhq-radio' ); ?>
<span class="radio-tab-count">(<?php echo (int) count( $all_history ); ?>)</span>
</a>
<a href="<?php echo esc_url( $fav_url ); ?>" class="nav-tab <?php echo $tab === 'favourites' ? 'nav-tab-active' : ''; ?>">
★ <?php esc_html_e( 'Favourites', 'rangerhq-radio' ); ?>
<span class="radio-tab-count">(<?php echo (int) count( $all_favourites ); ?>)</span>
</a>
</h2>
<?php if ( empty( $entries ) ) : ?>
<p class="radio-history-empty">
<?php if ( $tab === 'favourites' ) : ?>
<?php esc_html_e( 'No favourites yet — star a track on the History tab to save it here.', 'rangerhq-radio' ); ?>
<?php else : ?>
<?php esc_html_e( 'No tracks logged yet. Play some music in the Radio player — tracks will appear here as they play.', 'rangerhq-radio' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
<div class="radio-history-toolbar">
<input type="search" id="radio-history-search" placeholder="<?php esc_attr_e( 'Filter by artist or title…', 'rangerhq-radio' ); ?>" />
<select id="radio-history-station">
<option value=""><?php esc_html_e( 'All stations', 'rangerhq-radio' ); ?></option>
<?php foreach ( $stations_in_list as $sid => $sname ) : ?>
<option value="<?php echo esc_attr( $sid ); ?>"><?php echo esc_html( $sname ); ?></option>
<?php endforeach; ?>
</select>
<?php if ( $tab === 'history' ) : ?>
<button type="button" id="radio-history-clear" class="button radio-history-clear" data-nonce="<?php echo esc_attr( $nonce ); ?>">
🗑 <?php esc_html_e( 'Clear history', 'rangerhq-radio' ); ?>
</button>
<?php endif; ?>
</div>
<table class="widefat radio-history-table">
<thead>
<tr>
<th class="when"><?php esc_html_e( 'When', 'rangerhq-radio' ); ?></th>
<th class="station"><?php esc_html_e( 'Station', 'rangerhq-radio' ); ?></th>
<th class="track"><?php esc_html_e( 'Artist — Title', 'rangerhq-radio' ); ?></th>
<th class="search"><?php esc_html_e( 'Search', 'rangerhq-radio' ); ?></th>
<th class="fav"><span class="screen-reader-text"><?php esc_html_e( 'Favourite', 'rangerhq-radio' ); ?></span>★</th>
</tr>
</thead>
<tbody>
<?php foreach ( $entries as $e ) :
$sig = radio_entry_signature( $e );
$is_fav = isset( $fav_sigs[ $sig ] );
$search = radio_search_urls( $e['artist'], $e['title'] );
$ago = human_time_diff( (int) $e['at'], time() );
?>
<tr class="radio-history-row" data-station-id="<?php echo esc_attr( $e['station_id'] ); ?>" data-search="<?php echo esc_attr( strtolower( $e['artist'] . ' ' . $e['title'] ) ); ?>">
<td class="when" title="<?php echo esc_attr( wp_date( 'j M Y, H:i', (int) $e['at'] ) ); ?>">
<?php
/* translators: %s = human-readable time difference, e.g. "2 minutes" */
printf( esc_html__( '%s ago', 'rangerhq-radio' ), esc_html( $ago ) );
?>
</td>
<td class="station"><?php echo esc_html( $e['station'] ); ?></td>
<td class="track">
<strong><?php echo esc_html( $e['artist'] ); ?></strong> &mdash; <?php echo esc_html( $e['title'] ); ?>
</td>
<td class="search">
<a href="<?php echo esc_url( $search['spotify'] ); ?>" target="_blank" rel="noopener" class="radio-search-link radio-search-link--spotify">Spotify</a>
<a href="<?php echo esc_url( $search['youtube'] ); ?>" target="_blank" rel="noopener" class="radio-search-link radio-search-link--youtube">YouTube</a>
<a href="<?php echo esc_url( $search['apple'] ); ?>" target="_blank" rel="noopener" class="radio-search-link radio-search-link--apple">Apple</a>
<a href="<?php echo esc_url( $search['bandcamp'] ); ?>" target="_blank" rel="noopener" class="radio-search-link radio-search-link--bandcamp">Bandcamp</a>
</td>
<td class="fav">
<button type="button"
class="radio-fav-btn <?php echo $is_fav ? 'is-fav' : ''; ?>"
data-artist="<?php echo esc_attr( $e['artist'] ); ?>"
data-title="<?php echo esc_attr( $e['title'] ); ?>"
data-station="<?php echo esc_attr( $e['station'] ); ?>"
data-station-id="<?php echo esc_attr( $e['station_id'] ); ?>"
data-nonce="<?php echo esc_attr( $nonce ); ?>"
aria-label="<?php echo $is_fav ? esc_attr__( 'Remove from favourites', 'rangerhq-radio' ) : esc_attr__( 'Add to favourites', 'rangerhq-radio' ); ?>">
<?php echo $is_fav ? '★' : '☆'; ?>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php
}