'ai-content-signals', 'version' => AYUDAWP_ACS_VERSION, 'exported' => gmdate( 'Y-m-d H:i:s' ) . ' UTC', 'settings' => $settings, ); $filename = 'ai-content-signals-' . gmdate( 'Y-m-d' ) . '.json'; nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=' . $filename ); header( 'Expires: 0' ); echo wp_json_encode( $export_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); exit; } add_action( 'admin_init', 'ayudawp_acs_handle_export' ); /** * Handle settings import * * Processes an uploaded JSON file and restores plugin settings. */ function ayudawp_acs_handle_import() { if ( ! isset( $_POST['ayudawp_acs_import'] ) || '1' !== $_POST['ayudawp_acs_import'] ) { return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to import settings.', 'ai-content-signals' ) ); } if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'ayudawp_acs_import' ) ) { wp_die( esc_html__( 'Security check failed.', 'ai-content-signals' ) ); } if ( ! isset( $_FILES['ayudawp_acs_import_file']['error'] ) || UPLOAD_ERR_OK !== $_FILES['ayudawp_acs_import_file']['error'] ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'No file uploaded or upload error occurred.', 'ai-content-signals' ), 'error' ); return; } // Validate file type. $file_name = isset( $_FILES['ayudawp_acs_import_file']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['ayudawp_acs_import_file']['name'] ) ) : ''; $file_type = wp_check_filetype( $file_name, array( 'json' => 'application/json' ) ); if ( 'json' !== $file_type['ext'] ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Invalid file type. Please upload a JSON file.', 'ai-content-signals' ), 'error' ); return; } // Read file contents. $tmp_name = isset( $_FILES['ayudawp_acs_import_file']['tmp_name'] ) ? sanitize_text_field( $_FILES['ayudawp_acs_import_file']['tmp_name'] ) : ''; if ( empty( $tmp_name ) ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Could not read the uploaded file.', 'ai-content-signals' ), 'error' ); return; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $file_contents = file_get_contents( $tmp_name ); if ( false === $file_contents ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Could not read the uploaded file.', 'ai-content-signals' ), 'error' ); return; } $import_data = json_decode( $file_contents, true ); if ( null === $import_data || ! is_array( $import_data ) ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Invalid JSON file. Could not parse the file contents.', 'ai-content-signals' ), 'error' ); return; } // Validate it's an AI Content Signals export. if ( ! isset( $import_data['plugin'] ) || 'ai-content-signals' !== $import_data['plugin'] ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Invalid export file. This does not appear to be an AI Content Signals configuration file.', 'ai-content-signals' ), 'error' ); return; } if ( ! isset( $import_data['settings'] ) || ! is_array( $import_data['settings'] ) ) { add_settings_error( 'ayudawp_acs', 'import_error', esc_html__( 'Invalid export file. No settings data found.', 'ai-content-signals' ), 'error' ); return; } // Sanitize imported settings through the existing sanitize callback. $sanitized_settings = ayudawp_acs_sanitize_settings( $import_data['settings'] ); // Update settings. update_option( 'ayudawp_acs_settings', $sanitized_settings ); // Redirect to avoid form resubmission and show success notice. set_transient( 'ayudawp_acs_notice', 'imported', 30 ); wp_safe_redirect( admin_url( 'options-general.php?page=ai-content-signals' ) ); exit; } add_action( 'admin_init', 'ayudawp_acs_handle_import' ); /** * Handle restore defaults * * Resets all plugin settings to their default values. */ function ayudawp_acs_handle_restore_defaults() { if ( ! isset( $_POST['ayudawp_acs_restore_defaults'] ) || '1' !== $_POST['ayudawp_acs_restore_defaults'] ) { return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to restore settings.', 'ai-content-signals' ) ); } if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'ayudawp_acs_restore_defaults' ) ) { wp_die( esc_html__( 'Security check failed.', 'ai-content-signals' ) ); } $current = get_option( 'ayudawp_acs_settings', array() ); $defaults = array( 'global_search' => 'yes', 'global_ai_input' => 'yes', 'global_ai_train' => 'yes', 'include_legal_text' => 'yes', // Carried over rather than reset where the checkbox is not shown, for // the reason documented in ayudawp_acs_sanitize_physical_robots_toggle(). 'create_physical_robots' => ayudawp_acs_sanitize_physical_robots_toggle( array(), is_array( $current ) ? $current : array() ), 'emit_meta_tags' => 'no', 'emit_http_headers' => 'no', 'emit_tdmrep' => 'no', 'bot_settings' => array(), 'custom_bots' => array(), ); update_option( 'ayudawp_acs_settings', $defaults ); set_transient( 'ayudawp_acs_notice', 'restored', 30 ); wp_safe_redirect( admin_url( 'options-general.php?page=ai-content-signals' ) ); exit; } add_action( 'admin_init', 'ayudawp_acs_handle_restore_defaults' ); /** * Display admin notices for import/restore actions */ function ayudawp_acs_action_notices() { $screen = get_current_screen(); if ( ! $screen || 'settings_page_ai-content-signals' !== $screen->id ) { return; } $notice = get_transient( 'ayudawp_acs_notice' ); if ( ! $notice ) { return; } delete_transient( 'ayudawp_acs_notice' ); if ( 'imported' === $notice ) { echo '
'; esc_html_e( 'Settings imported successfully.', 'ai-content-signals' ); echo '
'; esc_html_e( 'Settings restored to defaults.', 'ai-content-signals' ); echo '