266 lines
8.3 KiB
PHP
266 lines
8.3 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' ),
|
|
'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' ),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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 ) ) {
|
|
$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' );
|