2013-07-24 16:01:36 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Setup importers for WC data
2013-07-24 16:01:36 +00:00
*
2014-11-30 06:52:32 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin
2015-11-27 14:28:20 +00:00
* @ version 2.5 . 0
2013-07-24 16:01:36 +00:00
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2015-11-27 14:28:20 +00:00
exit ;
2014-09-20 19:52:30 +00:00
}
2013-07-24 16:01:36 +00:00
2017-02-16 11:46:01 +00:00
if ( ! class_exists ( 'WC_Admin_Importers' , false ) ) :
2013-07-24 16:01:36 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Importers Class .
2013-07-24 16:01:36 +00:00
*/
class WC_Admin_Importers {
/**
* Hook in tabs .
*/
public function __construct () {
add_action ( 'admin_init' , array ( $this , 'register_importers' ) );
add_action ( 'import_start' , array ( $this , 'post_importer_compatibility' ) );
}
/**
2015-11-03 12:28:01 +00:00
* Add menu items .
2013-07-24 16:01:36 +00:00
*/
public function register_importers () {
2016-10-12 10:16:30 +00:00
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' ) );
2013-07-24 16:01:36 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Add menu item .
2013-07-24 16:01:36 +00:00
*/
public function tax_rates_importer () {
// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php' ;
if ( ! class_exists ( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php' ;
2014-11-30 06:52:32 +00:00
if ( file_exists ( $class_wp_importer ) ) {
2013-07-24 16:01:36 +00:00
require $class_wp_importer ;
2014-11-30 06:52:32 +00:00
}
2013-07-24 16:01:36 +00:00
}
// includes
2016-07-27 10:58:43 +00:00
require ( dirname ( __FILE__ ) . '/importers/class-wc-tax-rate-importer.php' );
2013-07-24 16:01:36 +00:00
// Dispatch
2013-07-24 18:55:02 +00:00
$importer = new WC_Tax_Rate_Importer ();
$importer -> dispatch ();
2013-07-24 16:01:36 +00:00
}
/**
* When running the WP importer , ensure attributes exist .
*
* WordPress import should work - however , it fails to import custom product attribute taxonomies .
* This code grabs the file before it is imported and ensures the taxonomies are created .
*/
public function post_importer_compatibility () {
global $wpdb ;
2014-11-30 06:52:32 +00:00
if ( empty ( $_POST [ 'import_id' ] ) || ! class_exists ( 'WXR_Parser' ) ) {
2013-07-24 16:01:36 +00:00
return ;
2014-11-30 06:52:32 +00:00
}
2013-07-24 16:01:36 +00:00
2015-11-27 14:28:20 +00:00
$id = absint ( $_POST [ 'import_id' ] );
2013-07-24 16:01:36 +00:00
$file = get_attached_file ( $id );
$parser = new WXR_Parser ();
$import_data = $parser -> parse ( $file );
if ( isset ( $import_data [ 'posts' ] ) ) {
$posts = $import_data [ 'posts' ];
2014-11-30 06:52:32 +00:00
if ( $posts && sizeof ( $posts ) > 0 ) {
foreach ( $posts as $post ) {
2015-11-27 14:28:20 +00:00
if ( 'product' === $post [ 'post_type' ] ) {
if ( ! empty ( $post [ 'terms' ] ) ) {
2014-11-30 06:52:32 +00:00
foreach ( $post [ 'terms' ] as $term ) {
2015-11-27 14:28:20 +00:00
if ( strstr ( $term [ 'domain' ], 'pa_' ) ) {
if ( ! taxonomy_exists ( $term [ 'domain' ] ) ) {
$attribute_name = wc_sanitize_taxonomy_name ( str_replace ( 'pa_' , '' , $term [ 'domain' ] ) );
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
// Create the taxonomy
2015-11-27 14:28:20 +00:00
if ( ! in_array ( $attribute_name , wc_get_attribute_taxonomies () ) ) {
$attribute = array (
'attribute_label' => $attribute_name ,
'attribute_name' => $attribute_name ,
'attribute_type' => 'select' ,
'attribute_orderby' => 'menu_order' ,
2016-08-27 01:46:45 +00:00
'attribute_public' => 0 ,
2015-11-27 14:28:20 +00:00
);
$wpdb -> insert ( $wpdb -> prefix . 'woocommerce_attribute_taxonomies' , $attribute );
delete_transient ( 'wc_attribute_taxonomies' );
2014-11-30 06:52:32 +00:00
}
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
// Register the taxonomy now so that the import works!
register_taxonomy (
2015-11-27 14:28:20 +00:00
$term [ 'domain' ],
apply_filters ( 'woocommerce_taxonomy_objects_' . $term [ 'domain' ], array ( 'product' ) ),
apply_filters ( 'woocommerce_taxonomy_args_' . $term [ 'domain' ], array (
2014-11-30 06:52:32 +00:00
'hierarchical' => true ,
2015-11-27 14:28:20 +00:00
'show_ui' => false ,
'query_var' => true ,
'rewrite' => false ,
2014-11-30 06:52:32 +00:00
) )
);
}
2013-07-24 16:01:36 +00:00
}
}
}
}
}
}
}
}
}
endif ;
2014-09-20 19:52:30 +00:00
return new WC_Admin_Importers ();