Files
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

179 lines
5.2 KiB
PHP

<?php
/**
* Track (song) post metadata.
*
* @package CHRNX
*/
/**
* Require permission to edit the post owning track metadata.
*
* @param bool $allowed Previously calculated permission.
* @param string $meta_key Metadata key.
* @param int $post_id Post ID.
* @return bool Whether the current user can edit the post.
*/
function chrnx_auth_track_meta( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
}
/**
* Return the track meta keys and their sanitizers.
*
* @return array<string,callable-string> Meta key to sanitize callback.
*/
function chrnx_get_track_meta_definitions() {
$definitions = array(
'chrnx_track_number' => 'absint',
'chrnx_track_audio' => 'absint',
'chrnx_track_description' => 'sanitize_textarea_field',
);
foreach ( array_keys( chrnx_get_release_platforms() ) as $platform ) {
$definitions[ 'chrnx_track_url_' . $platform ] = 'chrnx_sanitize_https_url';
}
return $definitions;
}
/**
* Register track metadata for posts.
*/
function chrnx_register_track_meta() {
foreach ( chrnx_get_track_meta_definitions() as $meta_key => $sanitize ) {
$is_number = in_array( $meta_key, array( 'chrnx_track_number', 'chrnx_track_audio' ), true );
register_post_meta(
'post',
$meta_key,
array(
'type' => $is_number ? 'integer' : 'string',
'single' => true,
'show_in_rest' => array(
'schema' => $is_number
? array(
'type' => 'integer',
'minimum' => 0,
)
: array( 'type' => 'string' ),
),
'sanitize_callback' => $sanitize,
'auth_callback' => 'chrnx_auth_track_meta',
)
);
}
}
add_action( 'init', 'chrnx_register_track_meta' );
/**
* Return the stored external links for one track.
*
* @param int $post_id Track post ID.
* @return array<string,string> Platform key to URL, omitting empty ones.
*/
function chrnx_get_track_links( $post_id ) {
$links = array();
foreach ( array_keys( chrnx_get_release_platforms() ) as $platform ) {
$url = chrnx_sanitize_https_url( get_post_meta( $post_id, 'chrnx_track_url_' . $platform, true ) );
if ( '' !== $url ) {
$links[ $platform ] = $url;
}
}
return $links;
}
/**
* Return the playable audio source for one track.
*
* @param int $post_id Track post ID.
* @return string Audio URL, or an empty string when nothing is attached.
*/
function chrnx_get_track_audio_url( $post_id ) {
$attachment_id = absint( get_post_meta( $post_id, 'chrnx_track_audio', true ) );
if ( 0 === $attachment_id || 'attachment' !== get_post_type( $attachment_id ) ) {
return '';
}
if ( 0 !== strpos( (string) get_post_mime_type( $attachment_id ), 'audio/' ) ) {
return '';
}
$url = wp_get_attachment_url( $attachment_id );
return is_string( $url ) ? $url : '';
}
/**
* Render the native mini player for one track, or nothing without audio.
*
* @param int $post_id Track post ID.
* @return string Escaped markup or an empty string.
*/
function chrnx_get_track_player_markup( $post_id ) {
$src = chrnx_get_track_audio_url( $post_id );
if ( '' === $src ) {
return '';
}
// ponytail: native <audio>, no player library. Controls are styled with CSS.
return sprintf(
'<audio class="chrnx-track-player" controls preload="none" src="%1$s" aria-label="%2$s"></audio>',
esc_url( $src ),
/* translators: %s: track title. */
esc_attr( sprintf( __( 'Play %s', 'chrnx' ), get_the_title( $post_id ) ) )
);
}
/**
* Return the release term assigned to one track.
*
* @param int $post_id Track post ID.
* @return WP_Term|null Release term or null.
*/
function chrnx_get_track_release( $post_id ) {
$terms = get_the_terms( $post_id, 'chrnx_release' );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return null;
}
$term = reset( $terms );
return $term instanceof WP_Term ? $term : null;
}
/**
* Return the ordered track IDs for one release.
*
* @param string $release_slug Release term slug.
* @param int $limit Maximum tracks, bounded to 60.
* @return int[] Track post IDs ordered by stored track number.
*/
function chrnx_get_release_track_ids( string $release_slug, int $limit = 30 ): array {
$slug = chrnx_validate_editorial_term_slug( $release_slug, 'chrnx_release' );
if ( '' === $slug ) {
return array();
}
$query = chrnx_run_eligible_query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => min( 60, max( 1, $limit ) ),
'orderby' => array(
'meta_value_num' => 'ASC',
'date' => 'ASC',
),
'meta_key' => 'chrnx_track_number', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Tracklist order is the feature.
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'fields' => 'ids',
'suppress_filters' => false,
'chrnx_require_nonempty_title' => true,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Scoped to one release.
array(
'taxonomy' => 'chrnx_release',
'field' => 'slug',
'terms' => $slug,
),
),
)
);
return array_values( array_map( 'absint', $query->posts ) );
}