$config ) { add_filter( "theme_mod_{$setting_id}", function( $value ) use ( $setting_id, $config ) { return bloghash_maybe_migrate_select_ids( $value, $setting_id, $config ); }, 20 ); } } add_action( 'after_setup_theme', 'bloghash_register_select_id_migration_filters', 20 ); /** * Map select settings to migration rules. * * @since 1.0.28 * @return array */ function bloghash_get_select_id_migration_map() { return array( 'bloghash_ticker_category' => array( 'type' => 'term', 'taxonomy' => 'category' ), 'bloghash_hero_slider_category' => array( 'type' => 'term', 'taxonomy' => 'category' ), 'bloghash_popular_post_category' => array( 'type' => 'term', 'taxonomy' => 'category' ), 'bloghash_popular_post_post' => array( 'type' => 'post', 'post_type' => 'post' ), 'bloghash_editors_choice_category' => array( 'type' => 'term', 'taxonomy' => 'category' ), 'bloghash_editors_choice_post' => array( 'type' => 'post', 'post_type' => 'post' ), 'bloghash_pyml_category' => array( 'type' => 'term', 'taxonomy' => 'category' ), ); } /** * Migrate legacy slug selections to IDs for select controls. * * @since 1.0.28 * @param mixed $value Current setting value. * @param string $setting_id Setting ID. * @param array $config Migration config. * @return mixed */ function bloghash_maybe_migrate_select_ids( $value, $setting_id, $config ) { if ( empty( $value ) ) { return $value; } $original_value = $value; if ( is_string( $value ) ) { $value = array_filter( array_map( 'trim', explode( ',', $value ) ) ); } elseif ( ! is_array( $value ) ) { $value = array( $value ); } $converted = array(); $needs_update = ! is_array( $original_value ); foreach ( $value as $item ) { $item_string = is_scalar( $item ) ? (string) $item : ''; if ( '' !== $item_string && ctype_digit( $item_string ) ) { $converted[] = (int) $item_string; continue; } $needs_update = true; $candidates = array( $item_string ); if ( 'term' === $config['type'] ) { foreach ( $candidates as $candidate ) { $term = get_term_by( 'slug', $candidate, $config['taxonomy'] ); if ( $term && ! is_wp_error( $term ) ) { $converted[] = (int) $term->term_id; continue 2; } } foreach ( $candidates as $candidate ) { $term = get_term_by( 'name', $candidate, $config['taxonomy'] ); if ( $term && ! is_wp_error( $term ) ) { $converted[] = (int) $term->term_id; continue 2; } } } elseif ( 'post' === $config['type'] ) { foreach ( $candidates as $candidate ) { $post = get_page_by_path( $candidate, OBJECT, $config['post_type'] ); if ( $post ) { $converted[] = (int) $post->ID; continue 2; } } } } $converted = array_values( array_unique( array_filter( $converted ) ) ); if ( empty( $converted ) ) { if ( $needs_update ) { bloghash()->options->set( $setting_id, array() ); } return array(); } if ( $needs_update || $converted !== $original_value ) { bloghash()->options->set( $setting_id, $converted ); } return $converted; }