Projects were hardcoded h3 elements in a pattern: no URL, no feed, no search, uneditable without touching PHP. They follow the podcast model now, a chrnx_topic term plus posts. Adds a chrnx_project_featured flag with an editor toggle so the stage lists chosen projects first and fills with the most recent. Sticky was already spoken for by the home featured stories, so the choice needed its own signal. journal-stream gains a pinned topic attribute, reusing the archive filter, pagination and empty state it already had. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
287 lines
9.1 KiB
PHP
287 lines
9.1 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' ),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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' );
|
|
|
|
/**
|
|
* 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' );
|