/** * Process import file - this parses the widget data and returns it. * * @param string $file path to json file. * @return object $data decoded JSON string */ private function process_import_file($file) { // File exists? if (!file_exists($file)) { return new WP_Error('widget_import_file_not_found', __('Widget import file could not be found.', 'pt-ocdi')); } // Get file contents and decode. $data = OCDI_Helpers::data_from_file($file); // Return from this function if there was an error. if (is_wp_error($data)) { return $data; } return json_decode($data); }
/** * Imports uploaded mods and calls WordPress core customize_save actions so * themes that hook into them can act before mods are saved to the database. * * Update: WP core customize_save actions were removed, because of some errors. * * @since 1.1.1 * @param string $import_file_path Path to the import file. * @return void|WP_Error */ public static function import_customizer_options($import_file_path) { // Setup global vars. global $wp_customize; // Setup internal vars. $template = get_template(); // Make sure we have an import file. if (!file_exists($import_file_path)) { return new WP_Error('missing_cutomizer_import_file', sprintf(esc_html__('The customizer import file is missing! File path: %s', 'pt-ocdi'), $import_file_path)); } // Get the upload data. $raw = OCDI_Helpers::data_from_file($import_file_path); // Make sure we got the data. if (is_wp_error($raw)) { return $raw; } $data = unserialize($raw); // Data checks. if (!is_array($data) && (!isset($data['template']) || !isset($data['mods']))) { return new WP_Error('customizer_import_data_error', esc_html__('The customizer import file is not in a correct format. Please make sure to use the correct customizer import file.', 'pt-ocdi')); } if ($data['template'] !== $template) { return new WP_Error('customizer_import_wrong_theme', esc_html__('The customizer import file is not suitable for current theme. You can only import customizer settings for the same theme or a child theme.', 'pt-ocdi')); } // Import images. if (apply_filters('pt-ocdi/customizer_import_images', true)) { $data['mods'] = self::import_customizer_images($data['mods']); } // Import custom options. if (isset($data['options'])) { // Require modified customizer options class. if (!class_exists('WP_Customize_Setting')) { require_once ABSPATH . 'wp-includes/class-wp-customize-setting.php'; } require PT_OCDI_PATH . 'inc/class-ocdi-customizer-option.php'; foreach ($data['options'] as $option_key => $option_value) { $option = new OCDI_Customizer_Option($wp_customize, $option_key, array('default' => '', 'type' => 'option', 'capability' => 'edit_theme_options')); $option->import($option_value); } } // Loop through the mods. foreach ($data['mods'] as $key => $val) { // Save the mod. set_theme_mod($key, $val); } }