173 lines
4.9 KiB
PHP
173 lines
4.9 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',
|
|
'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' );
|