1 <?php
  2 /**
  3  * Setup importers for WC data.
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Importers' ) ) :
 14 
 15 /**
 16  * WC_Admin_Importers Class
 17  */
 18 class WC_Admin_Importers {
 19 
 20     /**
 21      * Hook in tabs.
 22      */
 23     public function __construct() {
 24         add_action( 'admin_init', array( $this, 'register_importers' ) );
 25         add_action( 'import_start', array( $this, 'post_importer_compatibility' ) );
 26     }
 27 
 28     /**
 29      * Add menu items
 30      */
 31     public function register_importers() {
 32         register_importer( 'woocommerce_tax_rate_csv', __( 'WooCommerce Tax Rates (CSV)', 'woocommerce' ), __( 'Import <strong>tax rates</strong> to your store via a csv file.', 'woocommerce'), array( $this, 'tax_rates_importer' ) );
 33     }
 34 
 35     /**
 36      * Add menu item
 37      */
 38     public function tax_rates_importer() {
 39         // Load Importer API
 40         require_once ABSPATH . 'wp-admin/includes/import.php';
 41 
 42         if ( ! class_exists( 'WP_Importer' ) ) {
 43             $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
 44             if ( file_exists( $class_wp_importer ) )
 45                 require $class_wp_importer;
 46         }
 47 
 48         // includes
 49         require 'importers/class-wc-tax-rate-importer.php';
 50 
 51         // Dispatch
 52         $importer = new WC_Tax_Rate_Importer();
 53         $importer->dispatch();
 54     }
 55 
 56     /**
 57      * When running the WP importer, ensure attributes exist.
 58      *
 59      * WordPress import should work - however, it fails to import custom product attribute taxonomies.
 60      * This code grabs the file before it is imported and ensures the taxonomies are created.
 61      *
 62      * @access public
 63      * @return void
 64      */
 65     public function post_importer_compatibility() {
 66         global $wpdb;
 67 
 68         if ( empty( $_POST['import_id'] ) || ! class_exists( 'WXR_Parser' ) )
 69             return;
 70 
 71         $id          = (int) $_POST['import_id'];
 72         $file        = get_attached_file( $id );
 73         $parser      = new WXR_Parser();
 74         $import_data = $parser->parse( $file );
 75 
 76         if ( isset( $import_data['posts'] ) ) {
 77             $posts = $import_data['posts'];
 78 
 79             if ( $posts && sizeof( $posts ) > 0 ) foreach ( $posts as $post ) {
 80 
 81                 if ( $post['post_type'] == 'product' ) {
 82 
 83                     if ( $post['terms'] && sizeof( $post['terms'] ) > 0 ) {
 84 
 85                         foreach ( $post['terms'] as $term ) {
 86 
 87                             $domain = $term['domain'];
 88 
 89                             if ( strstr( $domain, 'pa_' ) ) {
 90 
 91                                 // Make sure it exists!
 92                                 if ( ! taxonomy_exists( $domain ) ) {
 93 
 94                                     $nicename = strtolower( sanitize_title( str_replace( 'pa_', '', $domain ) ) );
 95 
 96                                     $exists_in_db = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_id FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $nicename ) );
 97 
 98                                     // Create the taxonomy
 99                                     if ( ! $exists_in_db )
100                                         $wpdb->insert( $wpdb->prefix . "woocommerce_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'select', 'attribute_orderby' => 'menu_order' ), array( '%s', '%s', '%s' ) );
101 
102                                     // Register the taxonomy now so that the import works!
103                                     register_taxonomy( $domain,
104                                         apply_filters( 'woocommerce_taxonomy_objects_' . $domain, array('product') ),
105                                         apply_filters( 'woocommerce_taxonomy_args_' . $domain, array(
106                                             'hierarchical' => true,
107                                             'show_ui' => false,
108                                             'query_var' => true,
109                                             'rewrite' => false,
110                                         ) )
111                                     );
112                                 }
113                             }
114                         }
115                     }
116                 }
117             }
118         }
119     }
120 }
121 
122 endif;
123 
124 return new WC_Admin_Importers();
WooCommerce API documentation generated by ApiGen 2.8.0