feat: add CHRNX editorial metadata

This commit is contained in:
chrnx committed 2026-08-11 14:35:51 -05:00
1 parent ab305c98d1
commit e031b753cd
10 files changed
+1127 -1

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* Block editor assets.
*
* @package CHRNX
*/
/**
* Enqueue CHRNX controls only in the post block editor.
*/
function chrnx_enqueue_editor_assets() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || 'post' !== $screen->base || 'post' !== $screen->post_type || ! $screen->is_block_editor() ) {
return;
}
wp_enqueue_script(
'chrnx-editor-taxonomies',
get_theme_file_uri( 'assets/js/editor-taxonomies.js' ),
array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-plugins' ),
CHRNX_VERSION,
true
);
wp_enqueue_script(
'chrnx-editor-podcast',
get_theme_file_uri( 'assets/js/editor-podcast.js' ),
array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-plugins' ),
CHRNX_VERSION,
true
);
}
add_action( 'enqueue_block_editor_assets', 'chrnx_enqueue_editor_assets' );
+149
View File
@@ -0,0 +1,149 @@
<?php
/**
* Podcast post metadata.
*
* @package CHRNX
*/
/**
* Sanitize an episode number.
*
* @param mixed $value Submitted value.
* @return int Nonnegative episode number.
*/
function chrnx_sanitize_episode_number( $value ) {
return max( 0, intval( $value ) );
}
/**
* Sanitize a duration display string.
*
* @param mixed $value Submitted value.
* @return string Valid duration or an empty string.
*/
function chrnx_sanitize_episode_duration( $value ) {
if ( ! is_string( $value ) ) {
return '';
}
$value = trim( $value );
$sanitized = sanitize_text_field( $value );
if ( $value !== $sanitized || ! preg_match( '/^\d+:[0-5]\d(?::[0-5]\d)?$/', $sanitized ) ) {
return '';
}
return $sanitized;
}
/**
* Sanitize participant names while preserving their order.
*
* @param mixed $value Submitted value.
* @return string[] Sanitized participant names.
*/
function chrnx_sanitize_episode_participants( $value ) {
if ( ! is_array( $value ) ) {
return array();
}
$participants = array();
foreach ( $value as $participant ) {
if ( ! is_scalar( $participant ) ) {
continue;
}
$name = trim( sanitize_text_field( (string) $participant ) );
if ( '' !== $name ) {
$participants[] = $name;
}
}
return $participants;
}
/**
* Sanitize a canonical episode URL and require HTTPS.
*
* @param mixed $value Submitted value.
* @return string Valid HTTPS URL or an empty string.
*/
function chrnx_sanitize_canonical_episode_url( $value ) {
if ( ! is_string( $value ) ) {
return '';
}
$value = trim( $value );
if ( ! filter_var( $value, FILTER_VALIDATE_URL ) ) {
return '';
}
$url = esc_url_raw( $value, array( 'https' ) );
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
$host = wp_parse_url( $url, PHP_URL_HOST );
return 'https' === $scheme && ! empty( $host ) ? $url : '';
}
/**
* Require permission to edit the post owning podcast 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_podcast_meta( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
}
/**
* Register podcast metadata for posts.
*/
function chrnx_register_podcast_meta() {
$definitions = array(
'chrnx_episode_number' => array(
'type' => 'integer',
'sanitize_callback' => 'chrnx_sanitize_episode_number',
'schema' => array(
'type' => 'integer',
'minimum' => 0,
),
),
'chrnx_episode_duration' => array(
'type' => 'string',
'sanitize_callback' => 'chrnx_sanitize_episode_duration',
'schema' => array( 'type' => 'string' ),
),
'chrnx_episode_participants' => array(
'type' => 'array',
'sanitize_callback' => 'chrnx_sanitize_episode_participants',
'schema' => array(
'type' => 'array',
'items' => array( 'type' => 'string' ),
),
),
'canonical_episode_url' => array(
'type' => 'string',
'sanitize_callback' => 'chrnx_sanitize_canonical_episode_url',
'schema' => array(
'type' => 'string',
'format' => 'uri',
),
),
);
foreach ( $definitions as $meta_key => $definition ) {
register_post_meta(
'post',
$meta_key,
array(
'type' => $definition['type'],
'single' => true,
'show_in_rest' => array( 'schema' => $definition['schema'] ),
'sanitize_callback' => $definition['sanitize_callback'],
'auth_callback' => 'chrnx_auth_podcast_meta',
)
);
}
}
add_action( 'init', 'chrnx_register_podcast_meta' );
+172
View File
@@ -0,0 +1,172 @@
<?php
/**
* Editorial taxonomies.
*
* @package CHRNX
*/
/**
* Return the supported editorial taxonomy definitions.
*
* @return array<string,array<string,mixed>> Taxonomy definitions.
*/
function chrnx_get_editorial_taxonomies() {
return array(
'chrnx_topic' => array(
'singular' => 'Topic',
'plural' => 'Topics',
'rest_base' => 'topic',
'terms' => array(
'research' => 'Research',
'code' => 'Code',
'literature' => 'Literature',
'music' => 'Music',
'podcasts' => 'Podcasts',
'life' => 'Life',
'gaming' => 'Gaming',
),
),
'chrnx_language' => array(
'singular' => 'Language',
'plural' => 'Languages',
'rest_base' => 'language',
'terms' => array(
'en' => 'English',
'es' => 'Español',
),
),
'chrnx_show' => array(
'singular' => 'Show',
'plural' => 'Shows',
'rest_base' => 'show',
'terms' => array(
'silicon-ranch' => 'Silicon Ranch',
),
),
);
}
/**
* Register one editorial taxonomy with the shared contract.
*
* @param string $taxonomy_name Taxonomy name.
* @param array<string,mixed> $definition Taxonomy definition.
*/
function chrnx_register_editorial_taxonomy( $taxonomy_name, $definition ) {
register_taxonomy(
$taxonomy_name,
array( 'post' ),
array(
'labels' => array(
'name' => $definition['plural'],
'singular_name' => $definition['singular'],
),
'public' => true,
'hierarchical' => false,
'show_in_rest' => true,
'rest_base' => $definition['rest_base'],
'rewrite' => array( 'slug' => $definition['rest_base'] ),
'show_admin_column' => true,
)
);
}
/**
* Register all editorial taxonomies.
*/
function chrnx_register_editorial_taxonomies() {
foreach ( chrnx_get_editorial_taxonomies() as $taxonomy_name => $definition ) {
chrnx_register_editorial_taxonomy( $taxonomy_name, $definition );
}
}
add_action( 'init', 'chrnx_register_editorial_taxonomies' );
/**
* Insert initial editorial terms when the theme is activated.
*/
function chrnx_seed_editorial_terms() {
foreach ( chrnx_get_editorial_taxonomies() as $taxonomy_name => $definition ) {
if ( ! taxonomy_exists( $taxonomy_name ) ) {
chrnx_register_editorial_taxonomy( $taxonomy_name, $definition );
}
foreach ( $definition['terms'] as $slug => $name ) {
if ( ! term_exists( $slug, $taxonomy_name ) ) {
wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
}
}
}
}
add_action( 'after_switch_theme', 'chrnx_seed_editorial_terms' );
/**
* Add or read request-local normalization notices.
*
* @param string $taxonomy_name Optional taxonomy name to add.
* @return string[] Taxonomies normalized during this request.
*/
function chrnx_editorial_taxonomy_notices( $taxonomy_name = '' ) {
static $notices = array();
$supported = array_keys( chrnx_get_editorial_taxonomies() );
if ( in_array( $taxonomy_name, $supported, true ) ) {
$notices[ $taxonomy_name ] = $taxonomy_name;
}
return array_values( $notices );
}
/**
* Enforce one editorial term per taxonomy.
*
* @param int $object_id Object ID.
* @param mixed $terms Assigned terms.
* @param int[] $term_taxonomy_ids Term taxonomy IDs.
* @param string $taxonomy_name Taxonomy name.
*/
function chrnx_normalize_editorial_taxonomy_terms( $object_id, $terms, $term_taxonomy_ids, $taxonomy_name ) {
static $normalizing = array();
$supported = array_keys( chrnx_get_editorial_taxonomies() );
if ( ! in_array( $taxonomy_name, $supported, true ) || ! empty( $normalizing[ $taxonomy_name ] ) ) {
return;
}
$term_ids = wp_get_object_terms( $object_id, $taxonomy_name, array( 'fields' => 'ids' ) );
if ( is_wp_error( $term_ids ) || count( $term_ids ) <= 1 ) {
return;
}
$term_ids = array_map( 'absint', $term_ids );
sort( $term_ids, SORT_NUMERIC );
$normalizing[ $taxonomy_name ] = true;
wp_set_object_terms( $object_id, array( $term_ids[0] ), $taxonomy_name, false );
unset( $normalizing[ $taxonomy_name ] );
chrnx_editorial_taxonomy_notices( $taxonomy_name );
}
add_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10, 4 );
/**
* Display a concise request-local normalization notice.
*/
function chrnx_editorial_taxonomy_admin_notice() {
$definitions = chrnx_get_editorial_taxonomies();
foreach ( chrnx_editorial_taxonomy_notices() as $taxonomy_name ) {
if ( ! isset( $definitions[ $taxonomy_name ] ) ) {
continue;
}
$message = sprintf(
/* translators: %s: taxonomy singular label. */
__( '%s supports one value. The lowest existing term was kept.', 'chrnx' ),
$definitions[ $taxonomy_name ]['singular']
);
?>
<div class="notice notice-info is-dismissible"><p><?php echo esc_html( $message ); ?></p></div>
<?php
}
}
add_action( 'admin_notices', 'chrnx_editorial_taxonomy_admin_notice' );