403 ) ); } check_admin_referer( 'aiou_export_settings' ); // The tabs ticked on the export form; all of them when none were sent. $chosen = isset( $_POST['aiou_export_tabs'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['aiou_export_tabs'] ) ) : array(); $data = array(); if ( ! $chosen && ! empty( $_POST['aiou_export_choose'] ) ) { $this->redirect_with_notice( ToolsScreen::url( ImportExportScreen::TAB ), 'error', __( 'Choose at least one tab to export.', 'all-in-one-utilities' ) ); } foreach ( $this->registry->get_tabs() as $tab_id => $tab ) { if ( ! $chosen || in_array( $tab_id, $chosen, true ) ) { $data[ $tab_id ] = $this->registry->get_values( $tab_id ); } } $payload = array( 'plugin' => 'all-in-one-utilities', 'version' => AIOU_VERSION, 'exported_at' => gmdate( 'c' ), 'data' => $data, ); nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="all-in-one-utilities-settings-' . gmdate( 'Y-m-d' ) . '.json"' ); echo wp_json_encode( $payload, JSON_PRETTY_PRINT ); exit; } /** * Validate and apply an uploaded settings export. * * @return void */ public function handle_import(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'aiou_import_settings' ); $redirect = ToolsScreen::url( ImportExportScreen::TAB ); if ( empty( $_FILES['aiou_import_file']['tmp_name'] ) || ! is_uploaded_file( $_FILES['aiou_import_file']['tmp_name'] ) ) { $this->redirect_with_notice( $redirect, 'error', __( 'Please choose a file to import.', 'all-in-one-utilities' ) ); } $file = $_FILES['aiou_import_file']; if ( ! empty( $file['error'] ) ) { $this->redirect_with_notice( $redirect, 'error', __( 'The upload failed. Please try again.', 'all-in-one-utilities' ) ); } if ( $file['size'] > self::MAX_UPLOAD_BYTES ) { $this->redirect_with_notice( $redirect, 'error', __( 'The file is too large.', 'all-in-one-utilities' ) ); } $contents = file_get_contents( $file['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- reading a just-uploaded temp file, not a remote URL. $json = is_string( $contents ) ? json_decode( $contents, true ) : null; if ( ! is_array( $json ) || empty( $json['data'] ) || ! is_array( $json['data'] ) || ( $json['plugin'] ?? '' ) !== 'all-in-one-utilities' ) { $this->redirect_with_notice( $redirect, 'error', __( 'That file is not a valid All-in-One Utilities export.', 'all-in-one-utilities' ) ); } $imported = 0; foreach ( $json['data'] as $tab_id => $values ) { $tab_id = sanitize_key( (string) $tab_id ); if ( ! $this->registry->has_tab( $tab_id ) || ! is_array( $values ) ) { continue; } $sanitized = $this->registry->sanitize( $tab_id, $values ); update_option( $this->registry->option_name( $tab_id ), $sanitized ); ++$imported; } if ( ! $imported ) { $this->redirect_with_notice( $redirect, 'error', __( 'Nothing recognizable was found to import.', 'all-in-one-utilities' ) ); } $this->redirect_with_notice( $redirect, 'success', sprintf( /* translators: %d: number of settings tabs imported */ _n( 'Imported settings for %d tab.', 'Imported settings for %d tabs.', $imported, 'all-in-one-utilities' ), $imported ) ); } /** * Store a one-time notice and redirect back to the Import/Export tab. * * @param string $redirect Redirect URL. * @param string $type 'success' or 'error'. * @param string $message Notice text. * @return never */ private function redirect_with_notice( string $redirect, string $type, string $message ) { set_transient( 'aiou_import_export_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, ), MINUTE_IN_SECONDS ); wp_safe_redirect( $redirect ); exit; } }