Exemple #1
0
 /**
  * Singleton
  */
 public static function instance()
 {
     if (!isset(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * Import settings from file .json
  * @return bool|void
  * @author Julien Maury
  */
 public function import_settings()
 {
     if (empty($_POST['action']) || 'import_facetwp_settings' !== $_POST['action']) {
         return;
     }
     if (!current_user_can('manage_options')) {
         return;
     }
     if (!wp_verify_nonce($_POST['import_facetwp_nonce'], 'import_facetwp_nonce')) {
         return;
     }
     $extension = end(explode('.', $_FILES['import_file']['name']));
     if ('json' !== $extension) {
         wp_die(__('Please upload a valid .json file'));
     }
     $import_file = $_FILES['import_file']['tmp_name'];
     if (empty($import_file)) {
         wp_die(__('Please upload a file to import'));
     }
     $settings = FacetWP::instance()->helper->settings;
     $import_code = (array) json_decode(file_get_contents($import_file), true);
     $status = array('imported' => array(), 'skipped' => array());
     $overwrite = !empty($_POST['import_facetwp_overwrite']) ? (int) $_POST['import_facetwp_overwrite'] : 0;
     foreach ($import_code as $object_type => $object_items) {
         foreach ($object_items as $object_item) {
             $is_match = false;
             foreach ($settings[$object_type] as $key => $settings_item) {
                 if ($object_item['name'] == $settings_item['name']) {
                     if ($overwrite) {
                         $settings[$object_type][$key] = $object_item;
                         $status['imported'][] = $object_item['label'];
                     } else {
                         $status['skipped'][] = $object_item['label'];
                     }
                     $is_match = true;
                     break;
                 }
             }
             if (!$is_match) {
                 $settings[$object_type][] = $object_item;
                 $status['imported'][] = $object_item['label'];
             }
         }
     }
     if (!empty($settings)) {
         update_option('facetwp_settings', json_encode($settings));
     }
     wp_safe_redirect(add_query_arg('page', 'facetwp-export-import', admin_url('tools.php')));
     exit;
 }