Location: PHPKode > scripts > ColorShop > colorshop/admin/colorshop-admin-import.php
<?php
/**
 * Functions for handling WordPress import to make it compatable with ColorShop
 *
 * 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 		ColorVila
 * @category 	Admin
 * @package 	ColorShop/Admin/Import
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * When running the WP importer, ensure attributes exist.
 *
 * @access public
 * @return void
 */
function colorshop_import_start() {
	global $wpdb;

	if (!isset($_POST['import_id'])) return;
	if (!class_exists('WXR_Parser')) return;

	$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'];

		if ($posts && sizeof($posts)>0) foreach ($posts as $post) :

			if ($post['post_type']=='product') :

				if ($post['terms'] && sizeof($post['terms'])>0) :

					foreach ($post['terms'] as $term) :

						$domain = $term['domain'];

						if (strstr($domain, 'pa_')) :

							// Make sure it exists!
							if (!taxonomy_exists( $domain )) :

								$nicename = strtolower(sanitize_title(str_replace('pa_', '', $domain)));

								$exists_in_db = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_id FROM " . $wpdb->prefix . "colorshop_attribute_taxonomies WHERE attribute_name = %s;", $nicename ) );

								if (!$exists_in_db) :

									// Create the taxonomy
									$wpdb->insert( $wpdb->prefix . "colorshop_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'select', 'attribute_orderby' => 'menu_order' ), array( '%s', '%s', '%s' ) );

								endif;

								// Register the taxonomy now so that the import works!
								register_taxonomy( $domain,
							        apply_filters( 'colorshop_taxonomy_objects_' . $domain, array('product') ),
							        apply_filters( 'colorshop_taxonomy_args_' . $domain, array(
							            'hierarchical' => true,
							            'show_ui' => false,
							            'query_var' => true,
							            'rewrite' => false,
							        ) )
							    );

							endif;

						endif;

					endforeach;

				endif;

			endif;

		endforeach;

	endif;

}

add_action('import_start', 'colorshop_import_start');
Return current item: ColorShop