[ 'mode' => self::MODE_ALL ], ]; /** * Register WordPress hooks. */ public static function init(): void { add_action( 'admin_init', [ self::class, 'register' ] ); add_action( 'admin_enqueue_scripts', [ self::class, 'enqueue_assets' ] ); } /** * Enqueue the preselect progressive-disclosure script on the settings page. * * @since 7.0.0 * * @param string $hook Current admin page hook. */ public static function enqueue_assets( $hook ): void { if ( 'settings_page_' . Settings::PAGE_SLUG !== $hook ) { return; } wp_enqueue_script( 'beyondwords-settings--preselect', BEYONDWORDS__PLUGIN_URI . 'src/settings/preselect.js', [], BEYONDWORDS__PLUGIN_VERSION, true ); } /** * Register the option and settings field. */ public static function register(): void { register_setting( Tabs::SETTINGS_GROUP_PREFERENCES, self::OPTION_NAME, [ 'type' => 'object', 'default' => self::DEFAULT_VALUE, 'sanitize_callback' => [ self::class, 'sanitize' ], ] ); add_settings_field( 'beyondwords-preselect', __( 'Preselect ‘Generate audio’', 'speechkit' ), [ self::class, 'render' ], Tabs::PAGE_PREFERENCES, Tabs::SECTION_PREFERENCES ); } /** * Read the current preselect map. * * @return array */ public static function get(): array { $preselect = get_option( self::OPTION_NAME, self::DEFAULT_VALUE ); return is_array( $preselect ) ? $preselect : []; } /** * Resolve the preselect mode for a post type. * * Tolerant of pre-7.0.0 shapes so behaviour is correct before the migration * runs: `'1'` reads as `all`, a non-empty taxonomy array as `terms`. * * @param string $post_type Post type slug. * @param array|null $preselect Pre-loaded option, to avoid an extra `get_option()`. * * @return string One of `off`, `all`, `terms`. */ public static function get_mode( string $post_type, ?array $preselect = null ): string { if ( null === $preselect ) { $preselect = self::get(); } if ( ! array_key_exists( $post_type, $preselect ) ) { return self::MODE_OFF; } $value = $preselect[ $post_type ]; // Legacy whole-post-type flag. if ( '1' === $value || 1 === $value || true === $value ) { return self::MODE_ALL; } if ( is_array( $value ) ) { if ( isset( $value['mode'] ) ) { return in_array( $value['mode'], [ self::MODE_ALL, self::MODE_TERMS ], true ) ? $value['mode'] : self::MODE_OFF; } // Legacy term-gated shape: [ taxonomy => [ term ids ] ]. return empty( $value ) ? self::MODE_OFF : self::MODE_TERMS; } return self::MODE_OFF; } /** * The selected term IDs for a post type, keyed by taxonomy. * * Reads both the new (`terms` key) and legacy (bare taxonomy array) shapes. * Returns `[]` for any mode other than `terms`. * * @param string $post_type Post type slug. * @param array|null $preselect Pre-loaded option. * * @return array Map of taxonomy slug to term IDs. */ public static function get_selected_terms( string $post_type, ?array $preselect = null ): array { if ( null === $preselect ) { $preselect = self::get(); } if ( ! isset( $preselect[ $post_type ] ) || ! is_array( $preselect[ $post_type ] ) ) { return []; } $value = $preselect[ $post_type ]; if ( isset( $value['mode'] ) ) { if ( self::MODE_TERMS !== $value['mode'] || ! isset( $value['terms'] ) || ! is_array( $value['terms'] ) ) { return []; } $raw = $value['terms']; } else { // Legacy [ taxonomy => [ term ids ] ]. $raw = $value; } $terms = []; foreach ( $raw as $taxonomy => $ids ) { if ( ! is_array( $ids ) ) { continue; } $clean = array_values( array_filter( array_map( 'intval', $ids ) ) ); if ( ! empty( $clean ) ) { $terms[ (string) $taxonomy ] = $clean; } } return $terms; } /** * Whether "Generate audio" should be preselected for a given post. * * In `terms` mode a post matches when it has at least one listed term (OR * across taxonomies); unregistered/detached taxonomies are skipped, never fatal. * * @param \WP_Post|int $post Post object or ID. */ public static function should_preselect_for_post( $post ): bool { $post_type = get_post_type( $post ); if ( ! $post_type ) { return false; } // get()'s DEFAULT_VALUE fallback applies in every context (REST, cron), // keeping the server's decision in step with the editor's derived toggle. $preselect = self::get(); $mode = self::get_mode( $post_type, $preselect ); if ( self::MODE_ALL === $mode ) { return true; } if ( self::MODE_TERMS !== $mode ) { return false; } $selected = self::get_selected_terms( $post_type, $preselect ); if ( empty( $selected ) ) { return false; } $post_type_object_taxonomies = get_object_taxonomies( $post_type ); foreach ( $selected as $taxonomy => $term_ids ) { if ( ! taxonomy_exists( $taxonomy ) || ! in_array( $taxonomy, $post_type_object_taxonomies, true ) ) { continue; } $post_terms = get_the_terms( $post, $taxonomy ); if ( empty( $post_terms ) || is_wp_error( $post_terms ) ) { continue; } $post_term_ids = array_map( 'intval', wp_list_pluck( $post_terms, 'term_id' ) ); if ( array_intersect( $term_ids, $post_term_ids ) ) { return true; } } return false; } /** * Sanitise the submitted preselect map. * * Merge-preserve: only post types/taxonomies rendered this request are read * from the submission, so a save never wipes a deactivated plugin's config. * * @param mixed $value Raw submitted value. * * @return array */ public static function sanitize( $value ): array { // Merge from the RAW stored option — get()'s DEFAULT_VALUE fallback // would leak `post => all` into a fresh save. $raw = get_option( self::OPTION_NAME ); $existing = is_array( $raw ) ? $raw : []; if ( ! is_array( $value ) ) { return $existing; } $clean = $existing; foreach ( Utils::get_compatible_post_types() as $post_type ) { $submitted = ( isset( $value[ $post_type ] ) && is_array( $value[ $post_type ] ) ) ? $value[ $post_type ] : []; if ( empty( $submitted['enabled'] ) ) { unset( $clean[ $post_type ] ); continue; } $has_taxonomies = ! empty( self::get_hierarchical_taxonomy_names( $post_type ) ); // "All" wins over any ticked terms; with no hierarchical taxonomies // the whole-post-type option is the only choice. if ( ! $has_taxonomies || ! empty( $submitted['all'] ) ) { $clean[ $post_type ] = [ 'mode' => self::MODE_ALL ]; continue; } $clean[ $post_type ] = [ 'mode' => self::MODE_TERMS, 'terms' => self::sanitize_terms( $post_type, $submitted, $existing ), ]; } return $clean; } /** * Sanitise one post type's term map, merge-preserving unrendered taxonomies. * * @param string $post_type Post type slug. * @param array $submitted Submitted value for this post type. * @param array $existing Full stored option (for preservation). * * @return array */ private static function sanitize_terms( string $post_type, array $submitted, array $existing ): array { $rendered_taxonomies = self::get_hierarchical_taxonomy_names( $post_type ); $submitted_terms = ( isset( $submitted['terms'] ) && is_array( $submitted['terms'] ) ) ? $submitted['terms'] : []; $terms = []; // Preserve stored terms for taxonomies that weren't rendered this request. foreach ( self::get_selected_terms( $post_type, $existing ) as $taxonomy => $ids ) { if ( ! in_array( $taxonomy, $rendered_taxonomies, true ) ) { $terms[ $taxonomy ] = $ids; } } // Take rendered taxonomies' terms from the submission (authoritative). foreach ( $rendered_taxonomies as $taxonomy ) { if ( ! isset( $submitted_terms[ $taxonomy ] ) || ! is_array( $submitted_terms[ $taxonomy ] ) ) { continue; } $ids = array_values( array_filter( array_map( 'intval', $submitted_terms[ $taxonomy ] ) ) ); if ( ! empty( $ids ) ) { $terms[ $taxonomy ] = $ids; } } return $terms; } /** * Render the per-post-type controls. */ public static function render(): void { $post_types = Utils::get_compatible_post_types(); if ( empty( $post_types ) ) { ?>

$preselect Stored option. */ private static function render_post_type( $post_type_object, array $preselect ): void { $post_type = $post_type_object->name; $mode = self::get_mode( $post_type, $preselect ); $selected_terms = self::get_selected_terms( $post_type, $preselect ); $base = self::OPTION_NAME . '[' . $post_type . ']'; $taxonomies = self::get_hierarchical_taxonomies( $post_type ); $enabled = ( self::MODE_OFF !== $mode ); $is_all = ( self::MODE_TERMS !== $mode ); // 'all' (or the default for a fresh enable). $show_terms = ( $enabled && ! $is_all ); ?>

label ); ?>

name ] ?? []; $name = $base . '[terms][' . $taxonomy->name . '][]'; self::render_term_tree( $taxonomy, $name, $ids ); ?>
$taxonomy->name, 'hide_empty' => false, ] ); if ( empty( $terms ) || is_wp_error( $terms ) ) { return; } $by_parent = []; foreach ( $terms as $term ) { $by_parent[ (int) $term->parent ][] = $term; } self::render_term_branch( $by_parent, 0, $name, $selected_ids ); } /** * Recursively render one branch of a term tree. * * @param array $by_parent Terms grouped by parent ID. * @param int $parent_id Parent term ID (0 for top level). * @param string $name Checkbox `name` attribute. * @param int[] $selected_ids Selected term IDs. */ private static function render_term_branch( array $by_parent, int $parent_id, string $name, array $selected_ids ): void { if ( empty( $by_parent[ $parent_id ] ) ) { return; } ?>
  • term_id, $name, $selected_ids ); ?>
hierarchical && $taxonomy->show_ui; } ) ); } /** * Names of the hierarchical taxonomies for a post type. * * @param string $post_type Post type slug. * * @return string[] */ private static function get_hierarchical_taxonomy_names( string $post_type ): array { return array_values( wp_list_pluck( self::get_hierarchical_taxonomies( $post_type ), 'name' ) ); } }