balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

WXRImporter.php (4561B)


      1 <?php
      2 /**
      3  * WXR importer class used in the One Click Demo Import plugin.
      4  * Needed to extend the WXR_Importer class to get/set the importer protected variables,
      5  * for use in the multiple AJAX calls.
      6  *
      7  * @package ocdi
      8  */
      9 
     10 namespace OCDI;
     11 
     12 class WXRImporter extends \ProteusThemes\WPContentImporter2\WXRImporter {
     13     /**
     14      * Constructor method.
     15      *
     16      * @param array $options Importer options.
     17      */
     18     public function __construct($options = array()) {
     19         parent::__construct($options);
     20 
     21         // Set current user to $mapping variable.
     22         // Fixes the [WARNING] Could not find the author for ... log warning messages.
     23         $current_user_obj = wp_get_current_user();
     24         $this->mapping['user_slug'][$current_user_obj->user_login] = $current_user_obj->ID;
     25 
     26         // WooCommerce product attributes registration.
     27         if (class_exists('WooCommerce')) {
     28             add_filter('wxr_importer.pre_process.term', array($this, 'woocommerce_product_attributes_registration'), 10, 1);
     29         }
     30     }
     31 
     32     /**
     33      * Get all protected variables from the WXR_Importer needed for continuing the import.
     34      */
     35     public function get_importer_data() {
     36         return array(
     37             'mapping' => $this->mapping,
     38             'requires_remapping' => $this->requires_remapping,
     39             'exists' => $this->exists,
     40             'user_slug_override' => $this->user_slug_override,
     41             'url_remap' => $this->url_remap,
     42             'featured_images' => $this->featured_images,
     43         );
     44     }
     45 
     46     /**
     47      * Sets all protected variables from the WXR_Importer needed for continuing the import.
     48      *
     49      * @param array $data with set variables.
     50      */
     51     public function set_importer_data($data) {
     52         $this->mapping = empty($data['mapping']) ? array() : $data['mapping'];
     53         $this->requires_remapping = empty($data['requires_remapping']) ? array() : $data['requires_remapping'];
     54         $this->exists = empty($data['exists']) ? array() : $data['exists'];
     55         $this->user_slug_override = empty($data['user_slug_override']) ? array() : $data['user_slug_override'];
     56         $this->url_remap = empty($data['url_remap']) ? array() : $data['url_remap'];
     57         $this->featured_images = empty($data['featured_images']) ? array() : $data['featured_images'];
     58     }
     59 
     60     /**
     61      * Hook into the pre-process term filter of the content import and register the
     62      * custom WooCommerce product attributes, so that the terms can then be imported normally.
     63      *
     64      * This should probably be removed once the WP importer 2.0 support is added in WooCommerce.
     65      *
     66      * Fixes: [WARNING] Failed to import pa_size L warnings in content import.
     67      * Code from: woocommerce/includes/admin/class-wc-admin-importers.php (ver 2.6.9).
     68      *
     69      * Github issue: https://github.com/proteusthemes/one-click-demo-import/issues/71
     70      *
     71      * @param  array $date The term data to import.
     72      * @return array       The unchanged term data.
     73      */
     74     public function woocommerce_product_attributes_registration($data) {
     75         global $wpdb;
     76 
     77         if (strstr($data['taxonomy'], 'pa_')) {
     78             if (!taxonomy_exists($data['taxonomy'])) {
     79                 $attribute_name = wc_sanitize_taxonomy_name(str_replace('pa_', '', $data['taxonomy']));
     80 
     81                 // Create the taxonomy
     82                 if (!in_array($attribute_name, wc_get_attribute_taxonomies())) {
     83                     $attribute = array(
     84                         'attribute_label' => $attribute_name,
     85                         'attribute_name' => $attribute_name,
     86                         'attribute_type' => 'select',
     87                         'attribute_orderby' => 'menu_order',
     88                         'attribute_public' => 0,
     89                     );
     90                     $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute);
     91                     delete_transient('wc_attribute_taxonomies');
     92                 }
     93 
     94                 // Register the taxonomy now so that the import works!
     95                 register_taxonomy(
     96                     $data['taxonomy'],
     97                     apply_filters('woocommerce_taxonomy_objects_' . $data['taxonomy'], array('product')),
     98                     apply_filters('woocommerce_taxonomy_args_' . $data['taxonomy'], array(
     99                         'hierarchical' => true,
    100                         'show_ui' => false,
    101                         'query_var' => true,
    102                         'rewrite' => false,
    103                     ))
    104                 );
    105             }
    106         }
    107 
    108         return $data;
    109     }
    110 }