Meta key to sanitize callback. */ function chrnx_get_track_meta_definitions() { $definitions = array( 'chrnx_track_number' => 'absint', 'chrnx_track_audio' => 'absint', ); 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 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