1 <?php
2 3 4 5 6 7 8 9
10
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 if ( ! class_exists( 'WC_Admin_Importers' ) ) :
14
15 16 17
18 class WC_Admin_Importers {
19
20 21 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 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 37
38 public function tax_rates_importer() {
39
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
49 require 'importers/class-wc-tax-rate-importer.php';
50
51
52 $importer = new WC_Tax_Rate_Importer();
53 $importer->dispatch();
54 }
55
56 57 58 59 60 61 62 63 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
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
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
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();