fix: harden CHRNX editor lifecycle
This commit is contained in:
12 files changed
+512
-95
No files matched your search
+5
-2
@@ -32,7 +32,7 @@ function chrnx_enqueue_editor_assets() {
|
||||
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' ),
|
||||
array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' ),
|
||||
CHRNX_VERSION,
|
||||
true
|
||||
);
|
||||
@@ -40,9 +40,12 @@ function chrnx_enqueue_editor_assets() {
|
||||
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' ),
|
||||
array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' ),
|
||||
CHRNX_VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
wp_set_script_translations( 'chrnx-editor-taxonomies', 'chrnx', get_theme_file_path( 'languages' ) );
|
||||
wp_set_script_translations( 'chrnx-editor-podcast', 'chrnx', get_theme_file_path( 'languages' ) );
|
||||
}
|
||||
add_action( 'enqueue_block_editor_assets', 'chrnx_enqueue_editor_assets' );
|
||||
+15
-3
@@ -5,6 +5,9 @@
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
define( 'CHRNX_MAX_EPISODE_PARTICIPANTS', 50 );
|
||||
define( 'CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH', 100 );
|
||||
|
||||
/**
|
||||
* Sanitize an episode number.
|
||||
*
|
||||
@@ -39,7 +42,7 @@ function chrnx_sanitize_episode_duration( $value ) {
|
||||
* Sanitize participant names while preserving their order.
|
||||
*
|
||||
* @param mixed $value Submitted value.
|
||||
* @return string[] Sanitized participant names.
|
||||
* @return string[] Up to 50 sanitized names of up to 100 Unicode characters each.
|
||||
*/
|
||||
function chrnx_sanitize_episode_participants( $value ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
@@ -48,11 +51,16 @@ function chrnx_sanitize_episode_participants( $value ) {
|
||||
|
||||
$participants = array();
|
||||
foreach ( $value as $participant ) {
|
||||
if ( count( $participants ) >= CHRNX_MAX_EPISODE_PARTICIPANTS ) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ( ! is_scalar( $participant ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = trim( sanitize_text_field( (string) $participant ) );
|
||||
$name = mb_substr( $name, 0, CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH, 'UTF-8' );
|
||||
if ( '' !== $name ) {
|
||||
$participants[] = $name;
|
||||
}
|
||||
@@ -113,8 +121,12 @@ function chrnx_register_podcast_meta() {
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => 'chrnx_sanitize_episode_participants',
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array( 'type' => 'string' ),
|
||||
'type' => 'array',
|
||||
'maxItems' => CHRNX_MAX_EPISODE_PARTICIPANTS,
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
'maxLength' => CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH,
|
||||
),
|
||||
),
|
||||
),
|
||||
'canonical_episode_url' => array(
|
||||
|
||||
+139
-46
@@ -13,34 +13,34 @@
|
||||
function chrnx_get_editorial_taxonomies() {
|
||||
return array(
|
||||
'chrnx_topic' => array(
|
||||
'singular' => 'Topic',
|
||||
'plural' => 'Topics',
|
||||
'singular' => __( 'Topic', 'chrnx' ),
|
||||
'plural' => __( 'Topics', 'chrnx' ),
|
||||
'rest_base' => 'topic',
|
||||
'terms' => array(
|
||||
'research' => 'Research',
|
||||
'code' => 'Code',
|
||||
'literature' => 'Literature',
|
||||
'music' => 'Music',
|
||||
'podcasts' => 'Podcasts',
|
||||
'life' => 'Life',
|
||||
'gaming' => 'Gaming',
|
||||
'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',
|
||||
'plural' => 'Languages',
|
||||
'singular' => __( 'Language', 'chrnx' ),
|
||||
'plural' => __( 'Languages', 'chrnx' ),
|
||||
'rest_base' => 'language',
|
||||
'terms' => array(
|
||||
'en' => 'English',
|
||||
'es' => 'Español',
|
||||
'en' => __( 'English', 'chrnx' ),
|
||||
'es' => __( 'Español', 'chrnx' ),
|
||||
),
|
||||
),
|
||||
'chrnx_show' => array(
|
||||
'singular' => 'Show',
|
||||
'plural' => 'Shows',
|
||||
'singular' => __( 'Show', 'chrnx' ),
|
||||
'plural' => __( 'Shows', 'chrnx' ),
|
||||
'rest_base' => 'show',
|
||||
'terms' => array(
|
||||
'silicon-ranch' => 'Silicon Ranch',
|
||||
'silicon-ranch' => __( 'Silicon Ranch', 'chrnx' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -92,7 +92,10 @@ function chrnx_seed_editorial_terms() {
|
||||
|
||||
foreach ( $definition['terms'] as $slug => $name ) {
|
||||
if ( ! term_exists( $slug, $taxonomy_name ) ) {
|
||||
wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
|
||||
$result = wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
|
||||
if ( is_wp_error( $result ) || false === $result ) {
|
||||
chrnx_store_editorial_notice( '', 'vocabulary_setup_failed' );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,24 +103,94 @@ function chrnx_seed_editorial_terms() {
|
||||
add_action( 'after_switch_theme', 'chrnx_seed_editorial_terms' );
|
||||
|
||||
/**
|
||||
* Add or read request-local normalization notices.
|
||||
* Return the current user's editorial notice transient key.
|
||||
*
|
||||
* @param string $taxonomy_name Optional taxonomy name to add.
|
||||
* @return string[] Taxonomies normalized during this request.
|
||||
* @return string User-scoped transient key.
|
||||
*/
|
||||
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 );
|
||||
function chrnx_editorial_notice_key() {
|
||||
return 'chrnx_editorial_notices_' . get_current_user_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enforce one editorial term per taxonomy.
|
||||
* 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.
|
||||
@@ -127,8 +200,9 @@ function chrnx_editorial_taxonomy_notices( $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[ $taxonomy_name ] ) ) {
|
||||
if ( ! in_array( $taxonomy_name, $supported, true ) || ! empty( $normalizing[ $guard_key ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,32 +214,51 @@ function chrnx_normalize_editorial_taxonomy_terms( $object_id, $terms, $term_tax
|
||||
$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 );
|
||||
$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 a concise request-local normalization notice.
|
||||
* Display and consume concise user-scoped editorial notices.
|
||||
*/
|
||||
function chrnx_editorial_taxonomy_admin_notice() {
|
||||
$definitions = chrnx_get_editorial_taxonomies();
|
||||
|
||||
foreach ( chrnx_editorial_taxonomy_notices() as $taxonomy_name ) {
|
||||
if ( ! isset( $definitions[ $taxonomy_name ] ) ) {
|
||||
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;
|
||||
}
|
||||
|
||||
$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>
|
||||
<div class="notice <?php echo esc_attr( $class_name ); ?> is-dismissible"><p><?php echo esc_html( $message ); ?></p></div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user