function woo_pi_ajax_product_importer() { if (isset($_POST['step'])) { global $import; @ini_set('memory_limit', WP_MAX_MEMORY_LIMIT); ob_start(); // Split the CSV data from the main transient if ($_POST['step'] != 'prepare_data') { $import = get_transient(WOO_PI_PREFIX . '_import'); if (is_object($import)) { if (isset($_POST['settings']) && !is_string($_POST['settings'])) { foreach ($_POST['settings'] as $key => $value) { if (is_array($value)) { foreach ($value as $value_key => $value_value) { if (!is_array($value_value)) { $value[$value_key] = stripslashes($value_value); } } $import->{$key} = $value; } else { $import->{$key} = stripslashes($value); } } } // Merge the split transients into the $import global if (isset($import->headers)) { $args = array('generate_categories', 'generate_tags', 'prepare_product_import', 'save_product'); foreach ($import->headers as $header) { // Exclude $import->csv_category and $import->csv_brand for most of the import if (in_array($header, array('category', 'brand'))) { if (in_array($_POST['step'], $args)) { $import->{'csv_' . $header} = get_transient(WOO_PI_PREFIX . '_csv_' . $header); } } else { $import->{'csv_' . $header} = get_transient(WOO_PI_PREFIX . '_csv_' . $header); } } } } else { $import = new stdClass(); $import->cancel_import = true; $troubleshooting_url = 'http://www.visser.com.au/woocommerce/documentation/plugins/product-importer-deluxe/usage/'; $import->failed_import = sprintf(__('Your CSV contained special characters that WordPress and Product Importer Deluxe could not filter. %s', 'woo_pi'), __('Need help?', 'woo_pi') . ' ' . $troubleshooting_url); } } $timeout = 0; if (isset($import->timeout)) { $timeout = $import->timeout; } if (!ini_get('safe_mode')) { @set_time_limit($timeout); } switch ($_POST['step']) { case 'prepare_data': $import = new stdClass(); $import->start_time = time(); $import->cancel_import = false; $import->failed_import = ''; $import->log = ''; $import->timeout = isset($_POST['timeout']) ? $_POST['timeout'] : false; $import->delimiter = $_POST['delimiter']; $import->category_separator = $_POST['category_separator']; $import->parent_child_delimiter = $_POST['parent_child_delimiter']; $import->delete_file = woo_pi_get_option('delete_file', 0); $import->import_method = isset($_POST['import_method']) ? $_POST['import_method'] : 'new'; $import->advanced_log = isset($_POST['advanced_log']) ? (int) $_POST['advanced_log'] : 0; $import->log .= '<br />' . sprintf(__('Import method: %s', 'woo_pi'), $import->import_method); woo_pi_prepare_data('prepare_data'); if ($import->advanced_log) { $import->log .= "<br />" . __('Validating required columns...', 'woo_pi'); } if (!$import->cancel_import) { woo_pi_prepare_columns(); } $import->skip_first = $_POST['skip_first']; $import->log .= "<br />" . __('Product columns have been grouped', 'woo_pi'); $import->log .= "<br /><br />" . __('Generating Categories...', 'woo_pi'); $import->loading_text = __('Generating Categories...', 'woo_pi'); break; case 'generate_categories': $import->products_added = 0; $import->products_deleted = 0; $import->products_failed = 0; // Category generation if (in_array($import->import_method, array('new')) && isset($import->csv_category)) { woo_pi_generate_categories(); } else { $import->log .= "<br />" . __('Categories skipped', 'woo_pi'); } $import->log .= "<br /><br />" . __('Generating Tags...', 'woo_pi'); $import->loading_text = __('Generating Tags...', 'woo_pi'); break; case 'generate_tags': // Tag generation if (in_array($import->import_method, array('new')) && isset($import->csv_tag)) { woo_pi_generate_tags(); } else { $import->log .= "<br />" . __('Tags skipped', 'woo_pi'); } if ($import->import_method == 'new') { $import->log .= "<br /><br />" . __('Generating Products...', 'woo_pi'); } else { if ($import->import_method == 'delete') { $import->log .= "<br /><br />" . __('Checking for matching Products to delete...', 'woo_pi'); } } $import->loading_text = __('Importing Products...', 'woo_pi'); break; case 'prepare_product_import': global $import, $product; if ($import->advanced_log) { $import->log .= "<br />>>> " . __('Including non-essential reporting in this import log', 'woo_pi'); } if ($import->skip_first) { $import->i = 1; $import->log .= "<br />>>> " . __('Skipping import of first CSV row', 'woo_pi'); } else { $import->i = 0; $import->log .= "<br />>>> " . __('Starting import at first CSV row', 'woo_pi'); } $import->failed_products = array(); $i = $import->i; woo_pi_prepare_product($i); // This runs once as part of the import preparation $import->active_product = $product; if ($import->import_method == 'delete') { if (!empty($product->name)) { $import->log .= "<br />>>> " . sprintf(__('Searching for %s...', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>> " . sprintf(__('Searching for (no title) - SKU: %s...', 'woo_pi'), $product->sku); } $import->loading_text = sprintf(__('Searching for Product %d of %d...', 'woo_pi'), $i, $import->skip_first ? $import->rows - 1 : $import->rows); } else { if (!empty($product->name)) { $import->log .= "<br />>>> " . sprintf(__('Importing %s...', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>> " . sprintf(__('Importing (no title) - SKU: %s...', 'woo_pi'), $product->sku); } $import->loading_text = sprintf(__('Importing Product %d of %d...', 'woo_pi'), $i, $import->skip_first ? $import->rows - 1 : $import->rows); } break; case 'save_product': global $import, $product; $i = $_POST['i']; if ($import->active_product) { if (!isset($product)) { $product = new stdClass(); } foreach ($import->active_product as $key => $value) { $product->{$key} = $value; } } $import->product_start_time = microtime(true); if (in_array($import->import_method, array('new'))) { // Build Categories woo_pi_process_categories(); // Build Tags woo_pi_process_tags(); } // Check for duplicate SKU woo_pi_duplicate_product_exists(); woo_pi_validate_product(); if ($product->fail_requirements) { if ($import->advanced_log) { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, see Import Report for full explanation. Reason: %s', 'woo_pi'), $product->failed_reason); } else { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, reason: %s', 'woo_pi'), $product->failed_reason); } $import->products_failed++; } else { if ($import->import_method == 'delete' && $product->duplicate_exists) { woo_pi_delete_product(); } else { if (in_array($import->import_method, array('new')) && !$product->duplicate_exists) { woo_pi_create_product(); } } if ($import->import_method == 'delete') { if ($product->deleted) { if (!empty($product->name)) { $import->log .= "<br />>>>>>> " . sprintf(__('%s successfully deleted', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>>>>> " . sprintf(__('(no title) - SKU: %s successfully deleted', 'woo_pi'), $product->sku); } } else { if ($import->advanced_log) { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, see Import Report for full explanation. Reason: %s', 'woo_pi'), $product->failed_reason); } else { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, reason: %s', 'woo_pi'), $product->failed_reason); } } } else { if ($product->imported) { if ($import->import_method == 'new') { if (!empty($product->name)) { $import->log .= "<br />>>>>>> " . sprintf(__('%s successfully imported', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>>>>> " . sprintf(__('(no title) - SKU: %s successfully imported', 'woo_pi'), $product->sku); } } } else { if ($import->advanced_log) { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, see Import Report for full explanation. Reason: %s', 'woo_pi'), $product->failed_reason); } else { $import->log .= "<br />>>>>>> " . sprintf(__('Skipping Product, reason: %s', 'woo_pi'), $product->failed_reason); } } } } $import->product_end_time = microtime(true); $import->product_min_time = isset($import->product_min_time) ? $import->product_min_time : $import->product_end_time - $import->product_start_time; $import->product_max_time = isset($import->product_max_time) ? $import->product_max_time : $import->product_end_time - $import->product_start_time; // Update minimum product import time if it is shorter than the last if ($import->product_end_time - $import->product_start_time < $import->product_min_time) { $import->product_min_time = $import->product_end_time - $import->product_start_time; } // Update maximum product import time if it is longer than the last if ($import->product_end_time - $import->product_start_time > $import->product_max_time) { $import->product_max_time = $import->product_end_time - $import->product_start_time; } // All import rows have been processed if ($i + 1 == $import->rows) { if ($import->import_method == 'new') { $import->log .= "<br />" . __('Products have been generated', 'woo_pi'); } else { if ($import->import_method == 'delete') { $import->log .= "<br />" . __('Products have been deleted', 'woo_pi'); } } $import->log .= "<br /><br />" . __('Cleaning up...', 'woo_pi'); $import->loading_text = __('Cleaning up...', 'woo_pi'); } else { unset($import->active_product); woo_pi_prepare_product($i + 1); // This runs for each additional Product imported $import->active_product = $product; if ($import->import_method == 'delete') { if (!empty($product->name)) { $import->log .= "<br />>>> " . sprintf(__('Searching for %s...', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>> " . sprintf(__('Searching for (no title) - SKU: %s...', 'woo_pi'), $product->sku); } $import->loading_text = sprintf(__('Searching for Product %d of %d...', 'woo_pi'), $i + 1, $import->skip_first ? $import->rows - 1 : $import->rows); } else { if (!empty($product->name)) { $import->log .= "<br />>>> " . sprintf(__('Importing %s...', 'woo_pi'), $product->name); } else { $import->log .= "<br />>>> " . sprintf(__('Importing (no title) - SKU: %s...', 'woo_pi'), $product->sku); } $import->loading_text = sprintf(__('Importing Product %d of %d...', 'woo_pi'), $i + 1, $import->skip_first ? $import->rows - 1 : $import->rows); } } break; case 'clean_up': global $wpdb, $product; // Organise Categories if (isset($import->csv_category)) { $term_taxonomy = 'product_cat'; $import->log .= "<br />>>> " . __('Organise Categories', 'woo_pi'); } // Organise Tags if (isset($import->csv_tag)) { $import->log .= "<br />>>> " . __('Organise Tags', 'woo_pi'); } $import->log .= "<br />" . __('Clean up has completed', 'woo_pi'); $import->end_time = time(); // Post-import Product details if ($import->advanced_log) { $import->log .= "<br /><br />" . __('Import summary', 'woo_pi'); if (in_array($import->import_method, array('new', 'merge'))) { $import->log .= "<br />>>> " . sprintf(__('%d Products added', 'woo_pi'), $import->products_added); } else { if ($import->import_method == 'delete') { $import->log .= "<br />>>> " . sprintf(__('%d Products deleted', 'woo_pi'), $import->products_deleted); } } $import->log .= "<br />>>> " . sprintf(__('%d Products skipped', 'woo_pi'), $import->products_failed); $import->log .= "<br />>>> " . sprintf(__('Import took %s to complete', 'woo_pi'), woo_pi_display_time_elapsed($import->start_time, $import->end_time)); $import->log .= "<br />>>> " . sprintf(__('Fastest Product took < %s to process', 'woo_pi'), woo_pi_display_time_elapsed(time(), strtotime(sprintf('+%d seconds', $import->product_min_time)))); $import->log .= "<br />>>> " . sprintf(__('Slowest Product took > %s to process', 'woo_pi'), woo_pi_display_time_elapsed(time(), strtotime(sprintf('+%d seconds', $import->product_max_time)))); } $import->log .= "<br /><br />" . __('Import complete!', 'woo_pi'); $import->loading_text = __('Completed', 'woo_pi'); break; } // Clear transients if (function_exists('wc_delete_product_transients')) { wc_delete_product_transients(); } $import->step = $_POST['step']; $import->errors = ob_get_clean(); // Encode our transients in UTF-8 before storing them add_filter('pre_set_transient_woo_pi_import', 'woo_pi_filter_set_transient'); // Split the import data from the main transient if (isset($import->headers)) { foreach ($import->headers as $header) { if (isset($import->{'csv_' . $header})) { $response = set_transient(WOO_PI_PREFIX . '_csv_' . $header, $import->{'csv_' . $header}); // Check if the Transient was saved if (is_wp_error($response)) { error_log(sprintf(__('[product-importer] Could not save the import data Transient for the column %s', 'woo_pi'), $header)); } unset($import->{'csv_' . $header}); } } } $response = set_transient(WOO_PI_PREFIX . '_import', $import); // Check if the Transient was saved if (is_wp_error($response)) { error_log('[product-importer] Could not save the import Transient prior to starting AJAX import engine', 'woo_pi'); } $return = array(); if (isset($import->log)) { $return['log'] = $import->log; } if (isset($import->rows)) { $return['rows'] = $import->rows; } if (isset($import->skip_first)) { $return['skip_first'] = $import->skip_first; } if (isset($import->loading_text)) { $return['loading_text'] = $import->loading_text; } if (isset($import->cancel_import)) { $return['cancel_import'] = $import->cancel_import; } if (isset($import->failed_import)) { $return['failed_import'] = $import->failed_import; } if (isset($i)) { $return['i'] = $i; } if (isset($import->next)) { $return['next'] = $import->next; } if (isset($import->html)) { $return['html'] = $import->html; } if (isset($import->step)) { $return['step'] = $import->step; } @array_map('utf8_encode', $return); header("Content-type: application/json"); echo json_encode($return); } die; }
function woo_pi_html_page() { global $import; $action = woo_get_action(); $title = __('Product Importer', 'woo_pi'); if (in_array($action, array('upload', 'save')) && !$import->cancel_import) { if ($file = woo_pi_get_option('csv')) { $title .= ': <em>' . basename($file) . '</em>'; } } $troubleshooting_url = 'http://www.visser.com.au/woocommerce/documentation/plugins/product-importer-deluxe/usage/'; $woo_pd_url = 'http://www.visser.com.au/woocommerce/plugins/product-importer-deluxe/'; $woo_pd_link = sprintf('<a href="%s" target="_blank">' . __('Product Importer Deluxe', 'woo_pi') . '</a>', $woo_pd_url); woo_pi_template_header($title); woo_pi_support_donate(); woo_pi_upload_directories(); switch ($action) { case 'upload': if (isset($import->file)) { $file = $import->file; } else { $file = array('size' => 0); } /* if( $file['size'] == 0 ) { $message = __( '', 'woo_pi' ); woo_pi_admin_notice( '' ); $import->cancel_import = true; } */ // Display the opening Import tab if the import fails if ($import->cancel_import) { woo_pi_manage_form(); return; } $upload_dir = wp_upload_dir(); if ($file) { woo_pi_prepare_data(); $i = 0; $products = woo_pi_return_product_count(); // Override the default import method if no Products exist if (!$products) { $import->import_method = 'new'; } $import->options = woo_pi_product_fields(); $import->options_size = count($import->options); $first_row = array(); $second_row = array(); if (isset($import->lines)) { // Detect character encoding and compare to selected file encoding $auto_encoding = mb_detect_encoding($import->raw); if ($auto_encoding !== false) { if (strtolower($auto_encoding) != strtolower($import->encoding)) { woo_pi_update_option('encoding', $auto_encoding); $message = sprintf(__('It seems the character encoding provided under General Settings on the Settings tab - <code>%s</code> - didn\'t match this import file so we automatically detected the character encoding for you to <code>%s</code>.', 'woo_pi'), $import->encoding, $auto_encoding); // Force the message to the screen as we are post-init woo_pi_admin_notice_html($message); } } $first_row = str_getcsv($import->lines[0], $import->delimiter); $import->columns = count($first_row); // If we only detect a single column then the delimiter may be wrong if ($import->columns == 1) { $auto_delimiter = woo_pi_detect_file_delimiter((string) $first_row[0]); if ($import->delimiter != $auto_delimiter) { $import->delimiter = $auto_delimiter; $first_row = str_getcsv($import->lines[0], $import->delimiter); $import->columns = count($first_row); // If the column count is unchanged then the CSV either has only a single column (which won't work) or we've failed our job $priority = 'updated'; if ($import->columns > 1) { $message = sprintf(__('It seems the field delimiter provided under Import Options - <code>%s</code> - didn\'t match this CSV so we automatically detected the CSV delimiter for you to <code>%s</code>.', 'woo_pi'), woo_pi_get_option('delimiter', ','), $auto_delimiter); woo_pi_update_option('delimiter', $import->delimiter); } else { $priority = 'error'; $message = __('It seems either this CSV has only a single column or we were unable to automatically detect the CSV delimiter.', 'woo_pi') . ' <a href="' . $troubleshooting_url . '" target="_blank">' . __('Need help?', 'woo_pi') . '</a>'; } // Force the message to the screen as we are post-init woo_pi_admin_notice_html($message, $priority); } unset($auto_delimiter); } $second_row = str_getcsv($import->lines[1], $import->delimiter); unset($import->lines); } foreach ($first_row as $key => $cell) { for ($k = 0; $k < $import->options_size; $k++) { if (woo_pi_format_column($import->options[$k]['label']) == woo_pi_format_column($cell)) { $import->skip_first = true; break; } } if (!isset($second_row[$key])) { $second_row[$key] = ''; } } include_once WOO_PI_PATH . 'templates/admin/import_upload.php'; } break; case 'save': // Display the opening Import tab if the import fails if ($import->cancel_import == false) { include_once WOO_PI_PATH . 'templates/admin/import_save.php'; } else { woo_pi_manage_form(); return; } break; default: woo_pi_manage_form(); break; } woo_pi_template_footer(); }