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

333 lines
10 KiB
PHP

<?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', 'chrnx' ),
'plural' => __( 'Topics', 'chrnx' ),
'rest_base' => 'topic',
'terms' => array(
'research' => __( 'Research', 'chrnx' ),
'code' => __( 'Code', 'chrnx' ),
'projects' => __( 'Projects', 'chrnx' ),
'literature' => __( 'Literature', 'chrnx' ),
'music' => __( 'Music', 'chrnx' ),
'podcasts' => __( 'Podcasts', 'chrnx' ),
'life' => __( 'Life', 'chrnx' ),
'gaming' => __( 'Gaming', 'chrnx' ),
),
),
'chrnx_language' => array(
'singular' => __( 'Language', 'chrnx' ),
'plural' => __( 'Languages', 'chrnx' ),
'rest_base' => 'language',
'terms' => array(
'en' => __( 'English', 'chrnx' ),
'es' => __( 'Español', 'chrnx' ),
),
),
'chrnx_show' => array(
'singular' => __( 'Show', 'chrnx' ),
'plural' => __( 'Shows', 'chrnx' ),
'rest_base' => 'show',
'terms' => array(
'silicon-ranch' => __( 'Silicon Ranch', 'chrnx' ),
),
),
'chrnx_release' => array(
'singular' => __( 'Release', 'chrnx' ),
'plural' => __( 'Releases', 'chrnx' ),
'rest_base' => 'release',
'terms' => array(
'el-hermoso-caos' => __( 'El Hermoso Caos que habita en mi cabeza', 'chrnx' ),
'ando-bien-arriba' => __( 'Ando Bien Arriba', 'chrnx' ),
'aqui-se-rompio-una-jerga' => __( 'Aquí se Rompió una Jerga', 'chrnx' ),
'side-b' => __( 'Side B', 'chrnx' ),
),
),
'chrnx_project' => array(
'singular' => __( 'Project', 'chrnx' ),
'plural' => __( 'Projects', 'chrnx' ),
'rest_base' => 'project',
'terms' => array(
'green-tea' => __( 'Green Tea', 'chrnx' ),
'ailed' => __( 'Ailed', 'chrnx' ),
),
),
);
}
/**
* 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 ) {
/*
* term_exists() reads a cache that is not always warm during theme
* activation, which let a second activation insert duplicates with
* a "-2" slug. Ask the taxonomy directly, by slug and by name.
*/
if ( get_term_by( 'slug', $slug, $taxonomy_name ) instanceof WP_Term ) {
continue;
}
if ( get_term_by( 'name', $name, $taxonomy_name ) instanceof WP_Term ) {
continue;
}
$result = wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
if ( is_wp_error( $result ) || false === $result ) {
chrnx_store_editorial_notice( '', 'vocabulary_setup_failed' );
}
}
}
}
add_action( 'after_switch_theme', 'chrnx_seed_editorial_terms' );
/**
* Retire the legacy category archives.
*
* chrnx_topic is the editorial taxonomy. Category archives listed the same
* posts at a second indexable URL, so both competed for the same queries.
* A same-slug topic keeps that equity; everything else folds into the Journal.
*
* ponytail: matching by slug instead of a hand-kept category-to-topic map.
*/
function chrnx_redirect_legacy_category_archives() {
if ( ! is_category() ) {
return;
}
$category = get_queried_object();
$target = '';
if ( $category instanceof WP_Term ) {
$topic = get_term_by( 'slug', $category->slug, 'chrnx_topic' );
if ( $topic instanceof WP_Term && $topic->count > 0 ) {
$link = get_term_link( $topic );
$target = is_wp_error( $link ) ? '' : $link;
}
}
if ( '' === $target ) {
$target = chrnx_get_full_journal_url();
}
if ( '' === $target ) {
return;
}
wp_safe_redirect( $target, 301 );
exit;
}
add_action( 'template_redirect', 'chrnx_redirect_legacy_category_archives' );
/**
* Return the current user's editorial notice transient key.
*
* @return string User-scoped transient key.
*/
function chrnx_editorial_notice_key() {
return 'chrnx_editorial_notices_' . get_current_user_id();
}
/**
* Store a generic, allowlisted editorial notice for the current user.
*
* @param string $taxonomy_name Supported taxonomy or empty for setup status.
* @param string $status Allowlisted status.
*/
function chrnx_store_editorial_notice( $taxonomy_name, $status ) {
$supported_taxonomies = array_keys( chrnx_get_editorial_taxonomies() );
$allowed_statuses = array( 'normalized', 'normalization_failed', 'vocabulary_setup_failed' );
if ( ! in_array( $status, $allowed_statuses, true ) ) {
return;
}
if ( 'vocabulary_setup_failed' === $status ) {
$taxonomy_name = '';
} elseif ( ! in_array( $taxonomy_name, $supported_taxonomies, true ) ) {
return;
}
$key = chrnx_editorial_notice_key();
$notices = get_transient( $key );
if ( ! is_array( $notices ) ) {
$notices = array();
}
$notice_key = $status . ':' . $taxonomy_name;
$notices[ $notice_key ] = array(
'status' => $status,
'taxonomy' => $taxonomy_name,
);
set_transient( $key, $notices, 5 * MINUTE_IN_SECONDS );
}
/**
* Consume valid notices for the current user.
*
* @return array<int,array<string,string>> Valid one-time notices.
*/
function chrnx_consume_editorial_notices() {
$key = chrnx_editorial_notice_key();
$notices = get_transient( $key );
delete_transient( $key );
if ( ! is_array( $notices ) ) {
return array();
}
$supported_taxonomies = array_keys( chrnx_get_editorial_taxonomies() );
$allowed_statuses = array( 'normalized', 'normalization_failed', 'vocabulary_setup_failed' );
$valid = array();
foreach ( $notices as $notice ) {
if ( ! is_array( $notice ) || ! isset( $notice['status'], $notice['taxonomy'] ) ) {
continue;
}
if ( ! in_array( $notice['status'], $allowed_statuses, true ) ) {
continue;
}
if ( 'vocabulary_setup_failed' !== $notice['status'] && ! in_array( $notice['taxonomy'], $supported_taxonomies, true ) ) {
continue;
}
$valid[] = $notice;
}
return $valid;
}
/**
* Record a corrective assignment outcome without exposing its details.
*
* @param string $taxonomy_name Supported taxonomy.
* @param array|WP_Error|false $result Corrective assignment result.
*/
function chrnx_record_editorial_normalization_result( $taxonomy_name, $result ) {
$status = is_array( $result ) ? 'normalized' : 'normalization_failed';
chrnx_store_editorial_notice( $taxonomy_name, $status );
}
/**
* Enforce one editorial term per taxonomy and object.
*
* @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() );
$guard_key = $taxonomy_name . ':' . absint( $object_id );
if ( ! in_array( $taxonomy_name, $supported, true ) || ! empty( $normalizing[ $guard_key ] ) ) {
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[ $guard_key ] = true;
try {
$result = wp_set_object_terms( $object_id, array( $term_ids[0] ), $taxonomy_name, false );
chrnx_record_editorial_normalization_result( $taxonomy_name, $result );
} catch ( Throwable $exception ) {
chrnx_store_editorial_notice( $taxonomy_name, 'normalization_failed' );
throw $exception;
} finally {
unset( $normalizing[ $guard_key ] );
}
}
add_action( 'set_object_terms', 'chrnx_normalize_editorial_taxonomy_terms', 10, 4 );
/**
* Display and consume concise user-scoped editorial notices.
*/
function chrnx_editorial_taxonomy_admin_notice() {
$definitions = chrnx_get_editorial_taxonomies();
foreach ( chrnx_consume_editorial_notices() as $notice ) {
$status = $notice['status'];
$taxonomy_name = $notice['taxonomy'];
$class_name = 'notice-info';
if ( 'vocabulary_setup_failed' === $status ) {
$message = __( 'Editorial vocabulary setup was incomplete. Please reactivate the theme or contact an administrator.', 'chrnx' );
$class_name = 'notice-error';
} elseif ( isset( $definitions[ $taxonomy_name ] ) && 'normalization_failed' === $status ) {
$message = sprintf(
/* translators: %s: taxonomy singular label. */
__( 'The %s value could not be normalized. Please try again.', 'chrnx' ),
$definitions[ $taxonomy_name ]['singular']
);
$class_name = 'notice-error';
} elseif ( isset( $definitions[ $taxonomy_name ] ) && 'normalized' === $status ) {
$message = sprintf(
/* translators: %s: taxonomy singular label. */
__( '%s supports one value. The lowest existing term was kept.', 'chrnx' ),
$definitions[ $taxonomy_name ]['singular']
);
} else {
continue;
}
?>
<div class="notice <?php echo esc_attr( $class_name ); ?> is-dismissible"><p><?php echo esc_html( $message ); ?></p></div>
<?php
}
}
add_action( 'admin_notices', 'chrnx_editorial_taxonomy_admin_notice' );