示例#1
0
 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;
 }
示例#2
0
			<?php 
            foreach ($failed_product['reason'] as $failed_product_reason) {
                ?>
						<li><?php 
                echo $failed_product_reason;
                ?>
</li>
			<?php 
            }
            ?>
					</ul>
		<?php 
        }
        ?>
				</td>
			</tr>

	<?php 
    }
    ?>
		</tbody>
	</table>
<?php 
}
?>
	<p class="description text-align-right"><?php 
printf(__('Import took %s to complete.', 'woo_pi'), woo_pi_display_time_elapsed($import->start_time, $import->end_time));
?>
</p>
</div>
<!-- .inside -->