Files
chrnx-dev-wp/inc/release-meta.php
T
chrnx 76917decd0 feat(editorial): widen sections and add local track pages
Unify the editorial canvas across section pages, keep release tracks out of Journal, and give each song a listening surface with audio, metadata, platform links, sharing, and optional featured art. Document and seed the local The No Names fixture without changing production data.
2026-08-27 20:24:39 -05:00

260 lines
8.7 KiB
PHP

<?php
/**
* Release (album) term metadata and shared platform links.
*
* @package CHRNX
*/
/**
* Return the supported streaming platforms in display order.
*
* @return array<string,array<string,string>> Platform key to label and icon path data.
*/
function chrnx_get_release_platforms() {
return array(
'suno' => array(
'label' => __( 'Suno', 'chrnx' ),
'icon' => '<path d="M12 3a9 9 0 1 0 9 9"/><path d="M8 12a4 4 0 1 0 4-4"/>',
),
'spotify' => array(
'label' => __( 'Spotify', 'chrnx' ),
'icon' => '<circle cx="12" cy="12" r="9"/><path d="M7.5 9.5c3-.8 6.2-.5 8.8 1M8 12.6c2.4-.6 5-.4 7.2.9M8.6 15.6c1.9-.5 4-.3 5.7.7"/>',
),
'apple' => array(
'label' => __( 'Apple Music', 'chrnx' ),
'icon' => '<path d="M9 18V6.5l9-1.5V16"/><circle cx="7" cy="18" r="2"/><circle cx="16" cy="16" r="2"/>',
),
);
}
/**
* Sanitize an external listening URL and require HTTPS.
*
* @param mixed $value Submitted value.
* @return string Valid HTTPS URL or an empty string.
*/
function chrnx_sanitize_https_url( $value ) {
if ( ! is_string( $value ) ) {
return '';
}
$url = esc_url_raw( trim( $value ) );
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
$host = wp_parse_url( $url, PHP_URL_HOST );
return '' !== $url && 'https' === $scheme && ! empty( $host ) ? $url : '';
}
/**
* Return the release term meta keys and their sanitizers.
*
* @return array<string,callable-string> Meta key to sanitize callback.
*/
function chrnx_get_release_meta_definitions() {
$definitions = array(
'chrnx_release_cover' => 'absint',
'chrnx_release_status' => 'sanitize_text_field',
);
foreach ( array_keys( chrnx_get_release_platforms() ) as $platform ) {
$definitions[ 'chrnx_release_url_' . $platform ] = 'chrnx_sanitize_https_url';
}
return $definitions;
}
/**
* Require permission to manage release term metadata.
*
* @return bool Whether the current user can manage categories.
*/
function chrnx_auth_release_meta() {
return current_user_can( 'manage_categories' );
}
/**
* Register release term metadata.
*/
function chrnx_register_release_meta() {
foreach ( chrnx_get_release_meta_definitions() as $meta_key => $sanitize ) {
register_term_meta(
'chrnx_release',
$meta_key,
array(
'type' => 'chrnx_release_cover' === $meta_key ? 'integer' : 'string',
'single' => true,
'show_in_rest' => true,
'sanitize_callback' => $sanitize,
'auth_callback' => 'chrnx_auth_release_meta',
)
);
}
}
add_action( 'init', 'chrnx_register_release_meta' );
/**
* Return the stored external links for one release term.
*
* @param int $term_id Release term ID.
* @return array<string,string> Platform key to URL, omitting empty ones.
*/
function chrnx_get_release_links( $term_id ) {
$links = array();
foreach ( array_keys( chrnx_get_release_platforms() ) as $platform ) {
$url = chrnx_sanitize_https_url( get_term_meta( $term_id, 'chrnx_release_url_' . $platform, true ) );
if ( '' !== $url ) {
$links[ $platform ] = $url;
}
}
return $links;
}
/**
* Render a row of platform links, or nothing when none are stored.
*
* @param array<string,string> $links Platform key to URL.
* @param string $class_name Wrapper class.
* @param array<string,array>|null $platforms Platform definitions, defaulting to releases.
* @return string Escaped markup or an empty string.
*/
function chrnx_get_platform_links_markup( array $links, $class_name = 'chrnx-platform-links', $platforms = null ) {
$platforms = is_array( $platforms ) ? $platforms : chrnx_get_release_platforms();
$items = '';
foreach ( $links as $platform => $url ) {
if ( ! isset( $platforms[ $platform ] ) ) {
continue;
}
$items .= sprintf(
// The icon path data is a fixed literal, never user input.
'<li><a class="%1$s__link" href="%2$s" target="_blank" rel="noopener noreferrer external"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">%3$s</svg><span class="%1$s__label">%4$s</span></a></li>',
esc_attr( $class_name ),
esc_url( $url ),
$platforms[ $platform ]['icon'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html( $platforms[ $platform ]['label'] )
);
}
if ( '' === $items ) {
return '';
}
return '<ul class="' . esc_attr( $class_name ) . '">' . $items . '</ul>';
}
/**
* Print one release meta field row for the term edit screen.
*
* @param string $meta_key Meta key.
* @param string $label Field label.
* @param string $value Current value.
* @param string $type Input type.
* @param string $help Optional description.
*/
function chrnx_print_release_field( $meta_key, $label, $value, $type = 'url', $help = '' ) {
printf(
'<tr class="form-field"><th scope="row"><label for="%1$s">%2$s</label></th><td><input name="%1$s" id="%1$s" type="%3$s" value="%4$s" class="regular-text"%5$s>%6$s</td></tr>',
esc_attr( $meta_key ),
esc_html( $label ),
esc_attr( $type ),
esc_attr( $value ),
'chrnx_release_cover' === $meta_key ? ' data-chrnx-release-cover' : '',
'' !== $help ? '<p class="description">' . esc_html( $help ) . '</p>' : ''
);
}
/**
* Render the release metadata fields on the term edit screen.
*
* @param WP_Term $term Release term.
*/
function chrnx_render_release_edit_fields( $term ) {
$cover_id = absint( get_term_meta( $term->term_id, 'chrnx_release_cover', true ) );
chrnx_print_release_field(
'chrnx_release_cover',
__( 'Cover image ID', 'chrnx' ),
(string) ( $cover_id > 0 ? $cover_id : '' ),
'number',
__( 'Media library attachment ID for the album artwork.', 'chrnx' )
);
echo '<tr class="form-field"><th scope="row"></th><td>';
echo '<button type="button" class="button" data-chrnx-release-cover-pick>' . esc_html__( 'Select cover', 'chrnx' ) . '</button>';
echo '<div data-chrnx-release-cover-preview>';
if ( $cover_id > 0 ) {
echo wp_get_attachment_image( $cover_id, 'medium' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core returns escaped markup.
}
echo '</div></td></tr>';
chrnx_print_release_field(
'chrnx_release_status',
__( 'Status', 'chrnx' ),
(string) get_term_meta( $term->term_id, 'chrnx_release_status', true ),
'text',
__( 'Shown under the title, for example: 13-track debut album — in production.', 'chrnx' )
);
foreach ( chrnx_get_release_platforms() as $platform => $definition ) {
chrnx_print_release_field(
'chrnx_release_url_' . $platform,
/* translators: %s: streaming platform name. */
sprintf( __( '%s URL', 'chrnx' ), $definition['label'] ),
(string) get_term_meta( $term->term_id, 'chrnx_release_url_' . $platform, true ),
'url',
__( 'HTTPS only. Leave empty to hide this icon.', 'chrnx' )
);
}
}
add_action( 'chrnx_release_edit_form_fields', 'chrnx_render_release_edit_fields' );
/**
* Persist release term metadata from the term edit screen.
*
* @param int $term_id Release term ID.
*/
function chrnx_save_release_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_release_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 || 0 === $value ) {
delete_term_meta( $term_id, $meta_key );
continue;
}
update_term_meta( $term_id, $meta_key, $value );
}
}
add_action( 'edited_chrnx_release', 'chrnx_save_release_meta' );
add_action( 'created_chrnx_release', 'chrnx_save_release_meta' );
/**
* Enqueue the media picker only on the release term screen.
*
* @param string $hook_suffix Current admin page.
*/
function chrnx_enqueue_release_admin_assets( $hook_suffix ) {
$taxonomy = isset( $_GET['taxonomy'] ) ? sanitize_key( wp_unslash( $_GET['taxonomy'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen routing.
if ( ! in_array( $hook_suffix, array( 'term.php', 'edit-tags.php' ), true ) || 'chrnx_release' !== $taxonomy ) {
return;
}
wp_enqueue_media();
wp_enqueue_script(
'chrnx-admin-release',
get_theme_file_uri( 'assets/js/admin-release.js' ),
array( 'media-editor' ),
CHRNX_VERSION,
true
);
}
add_action( 'admin_enqueue_scripts', 'chrnx_enqueue_release_admin_assets' );