2013-07-24 16:01:36 +00:00
< ? php
/**
* Setup importers for WC data .
*
2014-11-30 06:52:32 +00:00
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin
2013-07-24 16:01:36 +00:00
* @ version 2.1 . 0
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-07-24 16:01:36 +00:00
if ( ! class_exists ( 'WC_Admin_Importers' ) ) :
/**
* WC_Admin_Importers Class
*/
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' ) );
}
/**
* Add menu items
*/
public function register_importers () {
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' ) );
}
/**
* Add menu item
*/
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
2013-07-24 18:55:02 +00:00
require '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
$id = ( int ) $_POST [ 'import_id' ];
$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 ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
if ( $post [ 'post_type' ] == 'product' ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
if ( $post [ 'terms' ] && sizeof ( $post [ 'terms' ] ) > 0 ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
foreach ( $post [ 'terms' ] as $term ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
$domain = $term [ 'domain' ];
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
if ( strstr ( $domain , 'pa_' ) ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
// Make sure it exists!
if ( ! taxonomy_exists ( $domain ) ) {
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
$nicename = strtolower ( sanitize_title ( str_replace ( 'pa_' , '' , $domain ) ) );
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
$exists_in_db = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT attribute_id FROM " . $wpdb -> prefix . " woocommerce_attribute_taxonomies WHERE attribute_name = %s; " , $nicename ) );
2013-07-24 16:01:36 +00:00
2014-11-30 06:52:32 +00:00
// Create the taxonomy
if ( ! $exists_in_db ) {
$wpdb -> insert ( $wpdb -> prefix . " woocommerce_attribute_taxonomies " , array ( 'attribute_name' => $nicename , 'attribute_type' => 'select' , 'attribute_orderby' => 'menu_order' ), array ( '%s' , '%s' , '%s' ) );
}
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 (
$domain ,
apply_filters ( 'woocommerce_taxonomy_objects_' . $domain , array ( 'product' ) ),
apply_filters ( 'woocommerce_taxonomy_args_' . $domain , array (
'hierarchical' => true ,
'show_ui' => false ,
'query_var' => true ,
'rewrite' => false ,
) )
);
}
2013-07-24 16:01:36 +00:00
}
}
}
}
}
}
}
}
}
endif ;
2014-09-20 19:52:30 +00:00
return new WC_Admin_Importers ();