/**
  * Get data from filters, after the theme has loaded and instantiate the importer.
  */
 public function setup_plugin_with_filter_data()
 {
     // Get info of import data files and filter it.
     $this->import_files = OCDI_Helpers::validate_import_file_info(apply_filters('pt-ocdi/import_files', array()));
     // Importer options array.
     $importer_options = apply_filters('pt-ocdi/importer_options', array('fetch_attachments' => true));
     // Logger options for the logger used in the importer.
     $logger_options = apply_filters('pt-ocdi/logger_options', array('logger_min_level' => 'warning'));
     // Configure logger instance and set it to the importer.
     $this->logger = new OCDI_Logger();
     $this->logger->min_level = $logger_options['logger_min_level'];
     // Create importer instance with proper parameters.
     $this->importer = new OCDI_Importer($importer_options, $this->logger);
 }
 /**
  * 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);
     }
 }
 function test_helper_validate_import_file_info()
 {
     // Test empty array input
     $import_files = array();
     $expected_output = array();
     $this->assertEquals($expected_output, OCDI_Helpers::validate_import_file_info($import_files));
     // Test valid array
     $import_files = array(array('import_file_name' => 'Demo Import 1', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets.json'), array('import_file_name' => 'Demo Import 2', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content2.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets2.json'));
     $expected_output = array(array('import_file_name' => 'Demo Import 1', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets.json'), array('import_file_name' => 'Demo Import 2', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content2.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets2.json'));
     $this->assertEquals($expected_output, OCDI_Helpers::validate_import_file_info($import_files));
     // Test valid array with one invalid item
     $import_files = array(array('import_file_name' => 'Demo Import 1', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets.json'), array('import_file_title' => 'Invalid Demo import', 'import_file_link' => 'http://www.your_domain.com/ocdi/invalid-demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/invalid-widgets.json'), array('import_file_name' => 'Demo Import 2', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content2.xml'));
     $expected_output = array(array('import_file_name' => 'Demo Import 1', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/widgets.json'), array('import_file_name' => 'Demo Import 2', 'import_file_url' => 'http://www.your_domain.com/ocdi/demo-content2.xml'));
     $this->assertEquals($expected_output, OCDI_Helpers::validate_import_file_info($import_files));
     // Test invalid array
     $import_files = array(array('import_file_title' => 'Invalid Demo import', 'import_file_link' => 'http://www.your_domain.com/ocdi/invalid-demo-content.xml', 'import_widget_file_url' => 'http://www.your_domain.com/ocdi/invalid-widgets.json'), array('import_file_title' => 'Invalid Demo import', 'import_file_url' => 'http://www.your_domain.com/ocdi/invalid-demo-content.xml'), array('import_file_name' => 'Invalid Demo import', 'import_file_link' => 'http://www.your_domain.com/ocdi/invalid-demo-content.xml'));
     $expected_output = array();
     $this->assertEquals($expected_output, OCDI_Helpers::validate_import_file_info($import_files));
 }