$spec ) { if ( null !== $spec['parent'] ) { continue; } $option_value = get_option( $name ); if ( ! $option_value ) { continue; } $export_data[] = array( 'option_name' => $name, 'option_value' => $option_value, ); foreach ( $schema as $sub_name => $sub_spec ) { if ( $sub_spec['parent'] !== $name || $sub_spec['site_specific'] ) { continue; } $sub_value = get_option( $sub_name ); if ( 'user_list' === $sub_spec['type'] ) { $logins = array(); foreach ( is_array( $sub_value ) ? $sub_value : array() as $user_id ) { $user_info = is_scalar( $user_id ) ? get_userdata( (int) $user_id ) : false; if ( $user_info ) { $logins[] = $user_info->user_login; } } $sub_value = $logins; } $export_data[] = array( 'option_name' => $sub_name, 'option_value' => $sub_value, ); } } $json = json_encode( $export_data ); header( 'Content-disposition: attachment; filename=agency-toolkit.json' ); header( 'Content-type: application/json' ); file_put_contents( 'php://output', $json ); exit; } add_action( 'init', 'inspry_toolkit_process_settings_export' ); // settings import file function inspry_toolkit_import_options_settings() { if ( ! isset( $_POST['agency_toolkit_import'] ) ) { return; } // Capability + nonce. Without the nonce this handler is reachable by CSRF // from any page, since it is hooked to init. if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! isset( $_POST['agency_toolkit_import_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['agency_toolkit_import_nonce'] ) ), 'agency_toolkit_import' ) ) { wp_die( esc_html__( 'Security check failed. Please go back and try again.', 'agency-toolkit' ) ); } // Make sure a file actually arrived before touching it // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- a server-side temp path, checked with is_uploaded_file(), never stored or output. if ( empty( $_FILES['import_file']['tmp_name'] ) || ! is_uploaded_file( $_FILES['import_file']['tmp_name'] ) ) { return; } // Get the file and decode JSON // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- the temp path verified just above. $filename = file_get_contents( $_FILES['import_file']['tmp_name'] ); $option_settings = json_decode( $filename ); if ( ! is_array( $option_settings ) ) { return; } /* * Only settings the schema knows, never the plugin's internal state or an * option some other plugin registered under the prefix. Site-specific * settings are skipped whatever the file says. Each value then goes through * the sanitize callback registered for it (options.php), as the settings * form's values do. */ $schema = inspry_toolkit_settings_schema(); // Loop through the options in the file foreach ( $option_settings as $option ) { if ( ! is_object( $option ) || ! isset( $option->option_name ) || ! is_string( $option->option_name ) ) { continue; } $option_name = $option->option_name; if ( ! isset( $schema[ $option_name ] ) || $schema[ $option_name ]['site_specific'] ) { continue; } $option_value = isset( $option->option_value ) ? $option->option_value : ''; if ( 'user_list' === $schema[ $option_name ]['type'] ) { // Exports carry user logins; resolve them to local IDs, admins only. $user_logins = is_array( $option_value ) ? $option_value : array(); $user_ids = array(); foreach ( $user_logins as $user_login ) { if ( ! is_string( $user_login ) ) { continue; } $user = get_user_by( 'login', $user_login ); if ( $user && in_array( 'administrator', (array) $user->roles, true ) ) { $user_ids[] = $user->ID; } } if ( empty( $user_ids ) ) { continue; } $option_value = $user_ids; } update_option( $option_name, $option_value ); } } add_action( 'init', 'inspry_toolkit_import_options_settings' );