Files
chrnx-dev-wp/inc/show-meta.php
T
chrnxandClaude Opus 5 49047f1337 feat: link a podcast show to where it is heard
Releases already had platform links; shows had none, so the Podcasts
page could not point at Spotify or Apple. chrnx_show terms take a
Spotify, Apple Podcasts and RSS URL now, rendered above the latest
episode and only when a URL exists.

The platform helper takes its definitions as an argument rather than
always reading the release set: a podcast lives on Apple Podcasts, not
Apple Music, and labelling it wrong would send people to the wrong
service.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 20:21:01 -05:00

130 lines
3.8 KiB
PHP

<?php
/**
* Show (podcast programme) term metadata.
*
* @package CHRNX
*/
/**
* Return the platforms a show can be listened on.
*
* Shares the release icons but not their labels: a podcast lives on Apple
* Podcasts, not Apple Music.
*
* @return array<string,array<string,string>> Platform key to label and icon.
*/
function chrnx_get_show_platforms() {
$release = chrnx_get_release_platforms();
return array(
'spotify' => $release['spotify'],
'apple' => array(
'label' => __( 'Apple Podcasts', 'chrnx' ),
'icon' => $release['apple']['icon'],
),
'rss' => array(
'label' => __( 'RSS feed', 'chrnx' ),
'icon' => '<path d="M5 19a1 1 0 1 0 0-2 1 1 0 0 0 0 2"/><path d="M5 12a7 7 0 0 1 7 7"/><path d="M5 5a14 14 0 0 1 14 14"/>',
),
);
}
/**
* Return the show term meta keys and their sanitizers.
*
* @return array<string,string> Meta key to sanitize callback.
*/
function chrnx_get_show_meta_definitions() {
$definitions = array();
foreach ( array_keys( chrnx_get_show_platforms() ) as $platform ) {
$definitions[ 'chrnx_show_url_' . $platform ] = 'chrnx_sanitize_https_url';
}
return $definitions;
}
/**
* Register show term metadata.
*/
function chrnx_register_show_meta() {
foreach ( chrnx_get_show_meta_definitions() as $meta_key => $sanitize ) {
register_term_meta(
'chrnx_show',
$meta_key,
array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'sanitize_callback' => $sanitize,
'auth_callback' => 'chrnx_auth_release_meta',
)
);
}
}
add_action( 'init', 'chrnx_register_show_meta' );
/**
* Return the stored listening links for one show.
*
* @param int $term_id Show term ID.
* @return array<string,string> Platform key to URL, omitting empty ones.
*/
function chrnx_get_show_links( $term_id ) {
$links = array();
foreach ( array_keys( chrnx_get_show_platforms() ) as $platform ) {
$url = chrnx_sanitize_https_url( get_term_meta( $term_id, 'chrnx_show_url_' . $platform, true ) );
if ( '' !== $url ) {
$links[ $platform ] = $url;
}
}
return $links;
}
/**
* Render the show metadata fields on the term edit screen.
*
* @param WP_Term $term Show term.
*/
function chrnx_render_show_edit_fields( $term ) {
foreach ( chrnx_get_show_platforms() as $platform => $definition ) {
chrnx_print_release_field(
'chrnx_show_url_' . $platform,
/* translators: %s: listening platform name. */
sprintf( __( '%s URL', 'chrnx' ), $definition['label'] ),
(string) get_term_meta( $term->term_id, 'chrnx_show_url_' . $platform, true ),
'url',
__( 'HTTPS only. Leave empty to hide this icon.', 'chrnx' )
);
}
}
add_action( 'chrnx_show_edit_form_fields', 'chrnx_render_show_edit_fields' );
/**
* Persist show term metadata from the term edit screen.
*
* @param int $term_id Show term ID.
*/
function chrnx_save_show_meta( $term_id ) {
if ( ! chrnx_auth_release_meta() ) {
return;
}
// Core supplies and verifies the add/update term nonce on these screens.
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'update-tag_' . $term_id ) && ! wp_verify_nonce( $nonce, 'add-tag' ) ) {
return;
}
foreach ( chrnx_get_show_meta_definitions() as $meta_key => $sanitize ) {
if ( ! isset( $_POST[ $meta_key ] ) ) {
continue;
}
$value = call_user_func( $sanitize, wp_unslash( $_POST[ $meta_key ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by the mapped callback.
if ( '' === $value ) {
delete_term_meta( $term_id, $meta_key );
continue;
}
update_term_meta( $term_id, $meta_key, $value );
}
}
add_action( 'edited_chrnx_show', 'chrnx_save_show_meta' );
add_action( 'created_chrnx_show', 'chrnx_save_show_meta' );