Files
rangerhq-radio/inc/history.php
T
ranger 09b61cc950 feat(0.7.0): WordPress.org submission prep — full Plugin Check clean
Ran the official Plugin Check (PCP) against v0.6.3 — surfaced 169
issues. This release closes all of them so the plugin is submission-
ready for the WordPress.org plugin directory.

Branding
  - Plugin Name renamed: "Radio" → "RangerHQ Radio". Removes the
    trademarked "SomaFM" from the plugin name surface (PCP
    trademarked_term). Lines up with the RangerHQ plugin family.
    SomaFM credited in Description + About as the data source.
    Folder/slug stays `a-radio` — no install path changes; existing
    user_meta keys (radio_state / radio_history / radio_favourites)
    untouched.
  - Text Domain header renamed: `radio` → `a-radio` (matches slug).
  - Requires at least bumped: 5.0 → 5.3 (matches wp_date() usage).
  - File docstring header dropped "SomaFM" from prominent line.

Code (mass-mechanical)
  - 134 i18n call sites rewritten from `'radio'` text domain to
    `'a-radio'` across 7 PHP files. Single sed pass on the unique
    pattern `, 'radio' )` — the 6 menu-slug `'radio'` references in
    add_*_page() were correctly left alone (those are URL slugs).

Security
  - 8 × MissingUnslash + 8 × InputNotSanitized in the v0.5.0 history
    endpoints (radio_ajax_log_track, radio_ajax_toggle_favourite).
    All four $_POST['artist|title|station|station_id'] access points
    are now wrapped sanitize_text_field( wp_unslash( $_POST['…'] ) )
    (or sanitize_key for station_id) at the access point.

Translator comments
  - 6 × printf / sprintf calls with placeholders now carry
    /* translators: ... */ comments.

Pop-out window refactor
  - Inline <link> stylesheets, <style> block, and <script> tag in
    radio_render_popout_page() replaced with wp_enqueue_style() +
    wp_enqueue_script() + wp_localize_script() registered before HTML
    output, then wp_print_styles() in <head> and wp_print_footer_
    scripts() at end of <body>.
  - Popup-specific CSS moved out of inline <style> and into radio.css
    under body.radio-popout scope so it only fires inside the popup.

Removed
  - .DS_Store files (root + assets/). PCP hidden_files.

Distribution
  - New readme.txt in proper WordPress.org format: Plugin headers,
    Contributors, Donate link, Tags, Requires-at-least, Tested-up-to,
    Stable Tag, Requires-PHP, License, Description, Installation,
    FAQ, Screenshots, Changelog, Upgrade Notice.

Compat
  - No behaviour change for users; user_meta preserved.
  - Displayed Plugin Name in Plugins → Installed changes from "Radio"
    to "RangerHQ Radio" — only visible difference on update.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-30 02:51:09 +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.', 'a-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', 'a-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', 'a-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', 'a-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.', 'a-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.', 'a-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…', 'a-radio' ); ?>" />
<select id="radio-history-station">
<option value=""><?php esc_html_e( 'All stations', 'a-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', 'a-radio' ); ?>
</button>
<?php endif; ?>
</div>
<table class="widefat radio-history-table">
<thead>
<tr>
<th class="when"><?php esc_html_e( 'When', 'a-radio' ); ?></th>
<th class="station"><?php esc_html_e( 'Station', 'a-radio' ); ?></th>
<th class="track"><?php esc_html_e( 'Artist — Title', 'a-radio' ); ?></th>
<th class="search"><?php esc_html_e( 'Search', 'a-radio' ); ?></th>
<th class="fav"><span class="screen-reader-text"><?php esc_html_e( 'Favourite', 'a-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', 'a-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', 'a-radio' ) : esc_attr__( 'Add to favourites', 'a-radio' ); ?>">
<?php echo $is_fav ? '★' : '☆'; ?>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php
}