2011-08-10 17:11:11 +00:00
< ? php
/**
* Functions for handling WordPress import to make it compatable with WooCommerce
*
* 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 .
*
* @ author WooThemes
* @ category Admin
2012-08-14 12:21:34 +00:00
* @ package WooCommerce / Admin / Import
* @ version 1.6 . 4
*/
2012-10-15 10:32:24 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2012-08-14 12:21:34 +00:00
/**
* When running the WP importer , ensure attributes exist .
*
* @ access public
* @ return void
2011-08-10 17:11:11 +00:00
*/
function woocommerce_import_start () {
global $wpdb ;
2012-08-14 12:21:34 +00:00
2012-03-07 20:12:19 +00:00
if ( ! isset ( $_POST [ 'import_id' ])) return ;
2012-04-19 15:53:46 +00:00
if ( ! class_exists ( 'WXR_Parser' )) return ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +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' ];
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
if ( $posts && sizeof ( $posts ) > 0 ) foreach ( $posts as $post ) :
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
if ( $post [ 'post_type' ] == 'product' ) :
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
if ( $post [ 'terms' ] && sizeof ( $post [ 'terms' ]) > 0 ) :
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
foreach ( $post [ 'terms' ] as $term ) :
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
$domain = $term [ 'domain' ];
2012-08-14 12:21:34 +00:00
2011-08-31 12:31:47 +00:00
if ( strstr ( $domain , 'pa_' )) :
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
// Make sure it exists!
if ( ! taxonomy_exists ( $domain )) :
2012-08-14 12:21:34 +00:00
2011-08-31 12:31:47 +00:00
$nicename = strtolower ( sanitize_title ( str_replace ( 'pa_' , '' , $domain )));
2012-08-14 12:21:34 +00:00
2012-10-16 14:46:21 +00:00
$exists_in_db = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT attribute_id FROM " . $wpdb -> prefix . " woocommerce_attribute_taxonomies WHERE attribute_name = %s; " , $nicename ) );
2012-08-14 12:21:34 +00:00
2011-09-11 13:28:15 +00:00
if ( ! $exists_in_db ) :
2012-08-14 12:21:34 +00:00
2011-09-11 13:28:15 +00:00
// Create the taxonomy
2012-10-09 14:57:02 +00:00
$wpdb -> insert ( $wpdb -> prefix . " woocommerce_attribute_taxonomies " , array ( 'attribute_name' => $nicename , 'attribute_type' => 'select' , 'attribute_orderby' => 'menu_order' ), array ( '%s' , '%s' , '%s' ) );
2012-08-14 12:21:34 +00:00
2011-09-11 13:28:15 +00:00
endif ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
// Register the taxonomy now so that the import works!
register_taxonomy ( $domain ,
array ( 'product' ),
array (
'hierarchical' => true ,
'show_ui' => false ,
'query_var' => true ,
2012-02-15 18:30:08 +00:00
'rewrite' => false ,
2011-08-10 17:11:11 +00:00
)
);
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endif ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endif ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endforeach ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endif ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endif ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endforeach ;
2012-08-14 12:21:34 +00:00
2011-08-10 17:11:11 +00:00
endif ;
}
add_action ( 'import_start' , 'woocommerce_import_start' );