parent = $framework; add_filter( 'eof_sections', array( $this, 'port_section' ) ); add_action( 'wp_ajax_eof_download_options-' . $this->parent->configs['opt_name'], array( $this, 'download_options' ) ); add_action( 'admin_init', array( $this, 'run' ) ); add_filter( 'upload_mimes', array( $this, 'custom_upload_mimes' ) ); } /** * Register the settings-page actions. */ public function run() { if ( isset( $this->parent->configs['options_page_id'] ) ) { add_action( 'load-' . $this->parent->configs['options_page_id'], array( $this, 'upload_import_file' ) ); } } /** * Append the import and export section. * * @param array $sections Configured settings sections. */ public function port_section( $sections ) { $sections['eof_port'] = array( 'title' => __( 'Import/Export', 'adz-world' ), 'id' => 'eof_port', 'type' => 'blank', 'callback' => array( $this, 'display' ), 'priority' => 41, ); return $sections; } /** * Display the settings import and export forms. */ public function display() { $output = ''; ?>
.json file generated by this plugin.', 'adz-world' ) ); ?>
.json file for your current option settings.', 'adz-world' ) ); ?>
parent->configs['opt_name'] . '&nonce=' . wp_create_nonce( 'eof-export' ) ); ?>
parent->configs['page_permissions'] ) ) { return; } // Check nonce for security since form was posted. if ( ! empty( $_POST ) && ! empty( $_FILES['eof_import_file'] ) && check_admin_referer( 'eof_import', 'eof_import_nonce' ) ) { // Uploaded file. $uploaded_file = array( 'name' => isset( $_FILES['eof_import_file']['name'] ) ? sanitize_file_name( $_FILES['eof_import_file']['name'] ) : '', 'type' => isset( $_FILES['eof_import_file']['type'] ) ? sanitize_mime_type( $_FILES['eof_import_file']['type'] ) : '', 'tmp_name' => isset( $_FILES['eof_import_file']['tmp_name'] ) ? sanitize_text_field( $_FILES['eof_import_file']['tmp_name'] ) : '', 'error' => isset( $_FILES['eof_import_file']['error'] ) ? absint( $_FILES['eof_import_file']['error'] ) : UPLOAD_ERR_NO_FILE, 'size' => isset( $_FILES['eof_import_file']['size'] ) ? absint( $_FILES['eof_import_file']['size'] ) : 0, ); // Check file type // This will also fire if no file uploaded. $filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false ); if ( 'json' !== $filetype['ext'] ) { wp_die( wp_kses_post( __( 'You must upload a .json file generated by this plugin.', 'adz-world' ) ), '', array( 'back_link' => true ) ); } // Check and move file to uploads dir, get file data // Will show die with WP errors if necessary (file too large, quota exceeded, etc.). $overrides = array( 'test_form' => false ); $file_data = wp_handle_upload( $uploaded_file, $overrides ); if ( isset( $file_data['error'] ) ) { wp_die( esc_html( $file_data['error'] ), '', array( 'back_link' => true ) ); } // Process import file. $this->process_import_file( $file_data ); } } /** * Read and remove the local uploaded backup. * * @param array $file_data Uploaded file information. */ public function process_import_file( $file_data ) { if ( ! current_user_can( $this->parent->configs['page_permissions'] ) ) { wp_die( esc_html__( 'Permission to import settings is required.', 'adz-world' ), '', array( 'response' => 403 ) ); } $uploads = wp_upload_dir(); $base = realpath( $uploads['basedir'] ); $file = isset( $file_data['file'] ) && is_string( $file_data['file'] ) ? realpath( $file_data['file'] ) : false; if ( ! $base || ! $file || 0 !== strpos( $file, $base . DIRECTORY_SEPARATOR ) || ! is_readable( $file ) ) { wp_die( esc_html__( 'The local import file could not be read.', 'adz-world' ), '', array( 'back_link' => true ) ); } global $wp_filesystem; if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $contents = WP_Filesystem() ? $wp_filesystem->get_contents( $file ) : false; wp_delete_file( $file ); if ( false === $contents ) { wp_die( esc_html__( 'Reading the settings import failed.', 'adz-world' ), '', array( 'back_link' => true ) ); } $data = json_decode( $contents, true ); if ( JSON_ERROR_NONE !== json_last_error() ) { wp_die( esc_html__( 'The settings import is not valid JSON.', 'adz-world' ), '', array( 'back_link' => true ) ); } $result = $this->import_data( $data ); if ( is_wp_error( $result ) ) { add_settings_error( 'eof-' . $this->parent->configs['opt_name'] . '-notices', $result->get_error_code(), $result->get_error_message() ); } } /** * Validate and import configured option values. * * @param array $data Imported option values. */ public function import_data( $data ) { if ( ! current_user_can( $this->parent->configs['page_permissions'] ) ) { return new WP_Error( 'eof_import_forbidden', __( 'Permission to import settings is required.', 'adz-world' ) ); } if ( ! is_array( $data ) || ! isset( $data['eof-backup'] ) || ! in_array( $data['eof-backup'], array( '1', 1 ), true ) ) { return new WP_Error( 'eof_import_invalid', __( 'The file does not contain a settings backup generated by this plugin.', 'adz-world' ) ); } unset( $data['eof-backup'] ); $before = count( get_settings_errors() ); $new_options = $this->parent->eof_settings->sanitize_callback( $data ); $messages = array_slice( get_settings_errors(), $before ); foreach ( $messages as $message ) { if ( 'error' === $message['type'] ) { return new WP_Error( 'eof_import_invalid', __( 'The imported settings failed validation. Existing settings were preserved.', 'adz-world' ) ); } } if ( get_option( $this->parent->configs['opt_name'] ) !== $new_options && ! update_option( $this->parent->configs['opt_name'], $new_options ) ) { return new WP_Error( 'eof_import_storage', __( 'The imported settings could not be stored.', 'adz-world' ) ); } $this->parent->refresh(); return true; } /** * Authorize and download the current settings backup. */ public function download_options() { if ( ! current_user_can( $this->parent->configs['page_permissions'] ) ) { wp_die( esc_html__( 'Permission to export settings is required.', 'adz-world' ), '', array( 'response' => 403 ) ); } $nonce = isset( $_GET['nonce'] ) && is_string( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'eof-export' ) ) { wp_die( esc_html__( 'The settings export request could not be verified.', 'adz-world' ), '', array( 'response' => 403 ) ); } $backup_options = $this->parent->options; $backup_options['eof-backup'] = '1'; header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( 'eof_options_' . $this->parent->configs['opt_name'] . '_backup_' . gmdate( 'd-m-Y' ) . '.json' ) . '"' ); nocache_headers(); wp_send_json( $backup_options ); } } endif;