public function run_import() { global $woocsv_product; /** * Are we starting for the first time, than create a batch and continue, else just pick uo the batch code and start where you left */ do_action('woocsv_start_import'); if (empty($_POST['batch_code'])) { //create a new batch $batch_code = woocsv_batches::create(); if ($batch_code) { //get max time we have and set the block size $max_execution_time = @ini_get('max_execution_time'); if ($max_execution_time == 0) { $max_execution_time = 30; } $block_size = $this->get_blocksize(); $data = array('filename' => $_POST['filename'], 'row' => 0, 'block_size' => $block_size, 'header_name' => $_POST['header_name'], 'seperator' => $_POST['seperator'], 'total_rows' => (int) $_POST['total_rows'], 'start_date' => time(), 'max_execution_time' => $max_execution_time); woocsv_batches::update($batch_code, $data); } else { //@todo die nice } //and get the batch $batch = woocsv_batches::get_batch($batch_code); } else { $batch_code = $_POST['batch_code']; $batch = woocsv_batches::get_batch($_POST['batch_code']); } //lets check if we are done? if ($batch['row'] >= $batch['total_rows']) { $batch['end_date'] = time(); $batch['status'] = 'done'; woocsv_batches::update($batch_code, $batch); do_action('woocsv_after_import_finished'); //@todo DIE NICE $this->die_nicer($batch_code, $batch); } // do we need to skip the first line? if ($this->get_skip_first_line() == 1 && $batch['row'] == 0) { $batch['row'] = 1; } //get the from and till $from = $batch['row']; $till = $batch['row'] + $batch['block_size'] < $batch['total_rows'] ? $batch['row'] + $batch['block_size'] : $batch['total_rows']; //get the lines $lines = $this->get_lines_from_file($batch['filename'], $from, $till, $batch['seperator']); //get the header $header = $this->get_header_from_name($batch['header_name']); //turn stuff off if (function_exists('wp_suspend_cache_invalidation')) { wp_suspend_cache_invalidation(true); } if (function_exists('wp_defer_term_counting ')) { wp_defer_term_counting(true); } $time_started = microtime(true); //loop over the lines and fill,pase and save the lines foreach ($lines['lines'] as $line) { //reset time ever time around @set_time_limit(0); //new one and fill in the header and the raw data $woocsv_product = new woocsv_import_product(); $woocsv_product->header = $header; $woocsv_product->raw_data = $line; //fill it, parse it and save it $woocsv_product->fill_in_data(); $this->write_to_log($batch_code, '-----> row: ' . $batch['row']); $woocsv_product->parse_data(); $this->write_to_log($batch_code, $woocsv_product->log); $woocsv_product->save(); $this->write_to_log($batch_code, $woocsv_product->log); //write tot log if debug is on if ($this->get_debug() == 0) { $this->write_to_log($batch_code, '-----> product dump'); $this->write_to_log($batch_code, $woocsv_product); } //close log $this->write_to_log($batch_code, '-----> end row: ' . $batch['row']); $this->write_to_log($batch_code, ''); //goto the next row $batch['row']++; //delete transionts if (function_exists('wc_delete_product_transients')) { wc_delete_product_transients($woocsv_product->body['ID']); } } $time_finished = microtime(true); if (!$this->get_blocksize()) { $time_factor = ceil(($batch['max_execution_time'] - ($time_finished - $time_started)) / $batch['max_execution_time'] * 100); switch ($time_factor) { case $time_factor > 90: $block_size = 10; break; case $time_factor > 50: $block_size = 5; break; case $time_factor > 10: $block_size = 1; break; default: $block_size = 0; } $batch['block_size'] += $block_size; woocsv_batches::update($batch_code, $batch); } $this->die_nicer($batch_code, $batch); }
public function import() { global $woocsv_import; //what page to load? /* ! HEADER */ if (isset($_FILES['csvfile']) && $_FILES['csvfile']['error'] != 0) { echo '<div class="error"><p>'; echo __('Something went wrong during the file upload, make sure the file is not to big!', 'woocommerce-csvimport'); echo '</p></div>'; } //save header if (isset($_POST['action']) && $_POST['action'] === 'save_header_preview' && check_admin_referer('save_header_preview', 'save_header_preview')) { $this->save_header(); include dirname(__FILE__) . '/partials/woocsv-import-admin-header.php'; return; } //preview the header if (isset($_POST['action']) && $_POST['action'] === 'start_header_preview' && @is_uploaded_file($_FILES['csvfile']['tmp_name']) && check_admin_referer('upload_header_file', 'upload_header_file')) { include dirname(__FILE__) . '/partials/woocsv-import-admin-header-preview.php'; return; } //header page if (isset($_GET['page'], $_GET['tab']) && $_GET['page'] == 'woocsv_import' && $_GET['tab'] == 'headers' || empty($woocsv_import->headers)) { include dirname(__FILE__) . '/partials/woocsv-import-admin-header.php'; return; } /* ! IMPORT */ //preview the import if (isset($_POST['action']) && $_POST['action'] === 'start_import_preview' && @is_uploaded_file($_FILES['csvfile']['tmp_name']) && check_admin_referer('upload_import_file', 'upload_import_file')) { // @since 3.1.0 //@todo create batch allready! //set the active header $header = $woocsv_import->headers[$_POST['header_name']]; $header_name = $_POST['header_name']; update_option('woocsv_header', $woocsv_import->headers[$_POST['header_name']]); $woocsv_import->header = $header; include dirname(__FILE__) . '/partials/woocsv-import-admin-import-preview.php'; return; } //we are creating a batch if (isset($_POST['action']) && $_POST['action'] === 'create_batch' && check_admin_referer('create_batch', 'create_batch')) { //create a new batch $new_batch_code = woocsv_batches::create(); if ($new_batch_code) { $data = array('filename' => $_POST['filename'], 'row' => 0, 'header_name' => $_POST['header_name'], 'speperator' => $_POST['seperator'], 'total_rows' => $_POST['total_rows']); woocsv_batches::update($new_batch_code, $data); } } //else just load the regular import screen include dirname(__FILE__) . '/partials/woocsv-import-admin-import.php'; }