$value ) { $formatted_value = ai4seo_make_nicer_setting_value( $value ); if ( $setting_value_has_named_keys ) { $formatted_values[] = htmlspecialchars( (string) $key, ENT_QUOTES, 'UTF-8' ) . ': ' . $formatted_value; } else { $formatted_values[] = $formatted_value; } } return implode( ', ', $formatted_values ); } elseif ( is_bool( $setting_value ) || is_null( $setting_value ) ) { return $setting_value ? esc_html__( 'Yes', 'ai-for-seo' ) : esc_html__( 'No', 'ai-for-seo' ); } else { return htmlspecialchars( (string) $setting_value, ENT_QUOTES, 'UTF-8' ); } } // ___________________________________________________________________________________________ \\ // === IMPORT VALUE HELPERS =================================================================== \\ /** * Converts disabled stored values into the active values submitted by UI checkboxes. * * @param array $available_values Available values on this website. * @param mixed $disabled_values Stored disabled values from the import file. * @param bool $use_integer_values Whether values should be compared as integers. * @return array Active values for save-anything. */ function ai4seo_get_active_selection_from_disabled_values( array $available_values, $disabled_values, bool $use_integer_values = false ): array { // Import files store disabled selections, while save-settings expects the active checkbox values. if ( ! is_array( $disabled_values ) ) { $disabled_values = $disabled_values ? array( $disabled_values ) : array(); } // Match the comparison type used by the target setting so array_diff mirrors save-settings normalization. if ( $use_integer_values ) { $available_values = array_map( 'intval', $available_values ); $disabled_values = array_map( 'intval', $disabled_values ); } else { $available_values = array_map( 'strval', $available_values ); $disabled_values = array_map( 'strval', $disabled_values ); } // Only current-site available values are kept, so stale imported entities stay dropped by the normal save path. $available_values = array_values( array_unique( $available_values ) ); $disabled_values = array_values( array_unique( $disabled_values ) ); return array_values( array_diff( $available_values, $disabled_values ) ); } /** * Converts disabled taxonomy terms from import storage into active UI checkbox selections. * * @param mixed $disabled_taxonomy_terms Stored disabled taxonomy terms from the import file. * @return array Active taxonomy terms for save-anything. */ function ai4seo_get_active_taxonomy_terms_from_disabled_taxonomy_terms( $disabled_taxonomy_terms ): array { // Invalid shapes have already failed validation; this fallback keeps helper calls defensive. if ( ! is_array( $disabled_taxonomy_terms ) ) { $disabled_taxonomy_terms = array(); } // Use the current site's supported terms so imports cannot resurrect deleted or unsupported terms. $ai4seo_supported_taxonomy_terms = ai4seo_get_supported_taxonomy_terms(); $ai4seo_active_taxonomy_terms = array(); foreach ( $ai4seo_supported_taxonomy_terms as $ai4seo_this_taxonomy_name => $ai4seo_this_supported_taxonomy ) { $ai4seo_supported_term_ids = array_map( 'intval', array_keys( $ai4seo_this_supported_taxonomy['terms'] ?? array() ) ); $ai4seo_disabled_term_ids = $disabled_taxonomy_terms[ $ai4seo_this_taxonomy_name ] ?? array(); $ai4seo_this_active_term_ids = ai4seo_get_active_selection_from_disabled_values( $ai4seo_supported_term_ids, $ai4seo_disabled_term_ids, true ); // Omitted taxonomy keys mean "no active terms" to the existing save-settings normalizer. if ( $ai4seo_this_active_term_ids ) { $ai4seo_active_taxonomy_terms[ $ai4seo_this_taxonomy_name ] = $ai4seo_this_active_term_ids; } } return $ai4seo_active_taxonomy_terms; } /** * Converts import values from stored setting format into save-anything's UI input format. * * @param array $setting_changes Setting changes keyed by setting name. * @return array Save-anything-compatible setting changes. */ function ai4seo_prepare_import_settings_for_save_anything( array $setting_changes ): array { foreach ( $setting_changes as $ai4seo_this_setting_name => $ai4seo_this_setting_value ) { // Only settings whose UI submits active selections need import-time conversion. switch ( $ai4seo_this_setting_name ) { case AI4SEO_SETTING_DISABLED_POST_TYPES: // Post type settings store disabled types but the settings page submits active types. $setting_changes[ $ai4seo_this_setting_name ] = ai4seo_get_active_selection_from_disabled_values( ai4seo_get_supported_post_types( false ), $ai4seo_this_setting_value ); break; case AI4SEO_SETTING_DISABLED_POST_AUTHORS: // Author settings use integer IDs, so convert by current available author IDs before save-anything. $setting_changes[ $ai4seo_this_setting_name ] = ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_post_authors() ), $ai4seo_this_setting_value, true ); break; case AI4SEO_SETTING_DISABLED_ATTACHMENT_POST_AUTHORS: // Media author settings follow the same active-checkbox contract as post authors. $setting_changes[ $ai4seo_this_setting_name ] = ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_attachment_post_authors() ), $ai4seo_this_setting_value, true ); break; case AI4SEO_SETTING_DISABLED_METADATA_WPML_LANGUAGES: case AI4SEO_SETTING_DISABLED_ATTACHMENT_ATTRIBUTES_WPML_LANGUAGES: // WPML language settings are saved through the shared active-language normalizer. $setting_changes[ $ai4seo_this_setting_name ] = ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_wpml_languages() ), $ai4seo_this_setting_value ); break; case AI4SEO_SETTING_DISABLED_TAXONOMY_TERMS: // Taxonomy terms are nested by taxonomy name and need the same disabled-to-active conversion per group. $setting_changes[ $ai4seo_this_setting_name ] = ai4seo_get_active_taxonomy_terms_from_disabled_taxonomy_terms( $ai4seo_this_setting_value ); break; } } return $setting_changes; } /** * Returns the preview label for settings whose stored name is inverted from the UI label. * * @param string $setting_name Setting name. * @return string Preview label. */ function ai4seo_get_import_setting_preview_name( string $setting_name ): string { if ( in_array( $setting_name, AI4SEO_NOTIFICATION_SETTINGS, true ) ) { return ai4seo_get_notification_preference_labels()[ $setting_name ]; } switch ( $setting_name ) { case AI4SEO_SETTING_DISABLED_POST_TYPES: return esc_html__( 'Active Post Types', 'ai-for-seo' ); case AI4SEO_SETTING_DISABLED_POST_AUTHORS: return esc_html__( 'Active Authors', 'ai-for-seo' ); case AI4SEO_SETTING_DISABLED_ATTACHMENT_POST_AUTHORS: return esc_html__( 'Active Media Authors', 'ai-for-seo' ); case AI4SEO_SETTING_DISABLED_METADATA_WPML_LANGUAGES: return esc_html__( 'Active Metadata WPML Languages', 'ai-for-seo' ); case AI4SEO_SETTING_DISABLED_ATTACHMENT_ATTRIBUTES_WPML_LANGUAGES: return esc_html__( 'Active Media WPML Languages', 'ai-for-seo' ); case AI4SEO_SETTING_DISABLED_TAXONOMY_TERMS: return esc_html__( 'Active Categories (taxonomy terms)', 'ai-for-seo' ); } return ai4seo_make_nicer_setting_name( $setting_name ); } /** * Converts stored disabled values into the active values admins expect in the import preview. * * @param string $setting_name Setting name. * @param mixed $setting_value Stored setting value. * @return mixed Preview setting value. */ function ai4seo_get_import_setting_preview_value( string $setting_name, $setting_value ) { switch ( $setting_name ) { case AI4SEO_SETTING_DISABLED_POST_TYPES: return ai4seo_get_active_selection_from_disabled_values( ai4seo_get_supported_post_types( false ), $setting_value ); case AI4SEO_SETTING_DISABLED_POST_AUTHORS: return ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_post_authors() ), $setting_value, true ); case AI4SEO_SETTING_DISABLED_ATTACHMENT_POST_AUTHORS: return ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_attachment_post_authors() ), $setting_value, true ); case AI4SEO_SETTING_DISABLED_METADATA_WPML_LANGUAGES: case AI4SEO_SETTING_DISABLED_ATTACHMENT_ATTRIBUTES_WPML_LANGUAGES: return ai4seo_get_active_selection_from_disabled_values( array_keys( ai4seo_get_available_wpml_languages() ), $setting_value ); case AI4SEO_SETTING_DISABLED_TAXONOMY_TERMS: return ai4seo_get_active_taxonomy_terms_from_disabled_taxonomy_terms( $setting_value ); } return $setting_value; } // ___________________________________________________________________________________________ \\ // === COMPATIBILITY MIGRATIONS =============================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ // Add missing prompt-slider settings when importing settings exported before those controls existed. $ai4seo_has_imported_prompt_slider_settings = ! empty( array_intersect_key( $ai4seo_new_settings_raw, AI4SEO_PROMPT_SLIDER_SETTING_STAGE_COUNTS ) ); $ai4seo_is_pre_240_settings_import = ( '' !== $ai4seo_import_plugin_version && version_compare( $ai4seo_import_plugin_version, '2.4.0', '<' ) ); $ai4seo_should_apply_prompt_slider_import_migration = in_array( 'settings', $ai4seo_import_categories, true ) && ( $ai4seo_is_pre_240_settings_import || ( '' === $ai4seo_import_plugin_version && ! $ai4seo_has_imported_prompt_slider_settings ) ); if ( $ai4seo_should_apply_prompt_slider_import_migration ) { $ai4seo_prompt_slider_setting_migration_values = ai4seo_get_prompt_slider_setting_pre_240_migration_values( $ai4seo_new_settings_raw ); foreach ( $ai4seo_prompt_slider_setting_migration_values as $ai4seo_this_setting_name => $ai4seo_this_setting_value ) { // Preserve explicit slider values from newer exports or manually edited import files. if ( array_key_exists( $ai4seo_this_setting_name, $ai4seo_new_settings_raw ) ) { continue; } $ai4seo_new_settings_raw[ $ai4seo_this_setting_name ] = $ai4seo_this_setting_value; } } // ___________________________________________________________________________________________ \\ // === FILTER BY CATEGORIES ================================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ $ai4seo_categorized_new_settings = array(); $ai4seo_got_unknown_category_entries = false; foreach ( $ai4seo_new_settings_raw as $ai4seo_this_setting_name => $ai4seo_this_setting_new_value ) { // can't import these settings ->. if ( in_array( $ai4seo_this_setting_name, AI4SEO_NOT_IMPORTABLE_SETTINGS, true ) ) { continue; } $ai4seo_this_setting_category = ai4seo_get_setting_category( $ai4seo_this_setting_name ); if ( in_array( $ai4seo_this_setting_category, $ai4seo_import_categories, true ) ) { $ai4seo_categorized_new_settings[ $ai4seo_this_setting_category ][ $ai4seo_this_setting_name ] = $ai4seo_this_setting_new_value; } if ( 'unknown' === $ai4seo_this_setting_category ) { $ai4seo_got_unknown_category_entries = true; } } // ___________________________________________________________________________________________ \\ // === VALIDATE ============================================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ $ai4seo_invalid_settings = array(); $ai4seo_current_settings_state = ai4seo_inspect_settings(); $ai4seo_not_selected_category_settings = array(); $ai4seo_validated_new_settings = array(); foreach ( $ai4seo_categorized_new_settings as $ai4seo_this_category => $ai4seo_this_settings ) { foreach ( $ai4seo_this_settings as $ai4seo_this_setting_name => $ai4seo_this_setting_new_value ) { // Get the current value from the settings. $ai4seo_current_value = $ai4seo_current_settings_state['settings'][ $ai4seo_this_setting_name ] ?? null; // Normalize imported boolean representations from current and older export formats. $ai4seo_this_setting_new_value = ai4seo_normalize_boolean_setting_value( $ai4seo_this_setting_name, $ai4seo_this_setting_new_value ); // convert the new value to the same type as the current value. if ( is_int( $ai4seo_current_value ) && is_numeric( $ai4seo_this_setting_new_value ) ) { $ai4seo_this_setting_new_value = (int) $ai4seo_this_setting_new_value; } elseif ( is_float( $ai4seo_current_value ) && is_numeric( $ai4seo_this_setting_new_value ) ) { $ai4seo_this_setting_new_value = (float) $ai4seo_this_setting_new_value; } // Custom-instruction imports use the same capping/cleanup path as settings saves before validation. $ai4seo_this_setting_new_value = ai4seo_normalize_custom_instructions_setting_value( $ai4seo_this_setting_name, $ai4seo_this_setting_new_value ); // Validate the normalized value so preview and execute mode agree on what can be imported. if ( ai4seo_validate_setting_value( $ai4seo_this_setting_name, $ai4seo_this_setting_new_value ) ) { // these settings are valid and can be imported. if ( in_array( $ai4seo_this_category, $ai4seo_import_categories, true ) ) { $ai4seo_validated_new_settings[ $ai4seo_this_setting_name ] = $ai4seo_this_setting_new_value; } else { // If the setting is valid but not in the selected categories, store it for later. $ai4seo_not_selected_category_settings[ $ai4seo_this_setting_name ] = $ai4seo_this_setting_new_value; } } else { $ai4seo_invalid_settings[ $ai4seo_this_setting_name ] = $ai4seo_this_setting_name; } } } $ai4seo_we_got_valid_settings = ! empty( $ai4seo_validated_new_settings ); // ___________________________________________________________________________________________ \\ // === COMPARE WITH OLD VALUE ================================================================ \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ $ai4seo_new_and_validated_changes = array(); foreach ( $ai4seo_validated_new_settings as $ai4seo_this_setting_name => $ai4seo_this_setting_new_value ) { // Get the current value from the settings. $ai4seo_current_value = $ai4seo_current_settings_state['settings'][ $ai4seo_this_setting_name ] ?? null; if ( isset( $ai4seo_current_settings_state['issues'][ $ai4seo_this_setting_name ] ) || $ai4seo_current_value !== $ai4seo_this_setting_new_value ) { // If the current value is different from the new value, prepare for comparison. $ai4seo_new_and_validated_changes[ $ai4seo_this_setting_name ] = array( 'current' => $ai4seo_current_value, 'new' => $ai4seo_this_setting_new_value, 'category' => ai4seo_get_setting_category( $ai4seo_this_setting_name ), ); } } $ai4seo_we_got_valid_changes = ! empty( $ai4seo_new_and_validated_changes ); // ___________________________________________________________________________________________ \\ // === PREVIEW ================================================================================ \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ if ( 'preview' === $ai4seo_import_mode ) { ai4seo_echo_wp_kses( ai4seo_get_settings_form_state_tag( $ai4seo_current_settings_state ) ); // HEADLINE. echo "