/** * Get preview for sample CSV data * * @since 3.0.0 */ public function get_preview() { check_ajax_referer('get-csv-preview', 'security'); $delimiter = isset($_GET['delimiter']) ? $_GET['delimiter'] : ','; $file = isset($_GET['file']) ? $_GET['file'] : ''; if (!$file) { wp_die(); } list($data, $headers) = WC_CSV_Import_Suite_Parser::parse_sample_data($file, $delimiter); $data = array(1 => $headers) + $data; echo WC_CSV_Import_Suite_Parser::generate_html_rows($data); wp_die(); }
/** * Manages the separate stages of the CSV import process * * This method may be called either before any output is sent to the buffer * or when some output has already been sent. The first case should only * be used for handling POST requests in the CSV import process, and visitor * should be redirected to a idempotent page after processing the request. * * The importer class uses the 'redirect after post' pattern - and all * subclasses must also follow the same pattern. * This is to make sure reloading a screen during import does not alter the * import progress in any way. * * 1. Display introductory text and source select * 2. Handle the physical upload/sideload of the source * 3. Detect delimiter, display preview and import options * 4. Display column mapper * 5. Kick-off parsing & importing from the input source * * @since 3.0.0 */ public function dispatch() { // prevent dispatching more than once if ($this->has_dispatched) { return; } $this->has_dispatched = true; $step = isset($_GET['step']) ? (int) $_GET['step'] : 0; $type = isset($_GET['import']) ? sanitize_key($_GET['import']) : null; $source = isset($_GET['source']) ? sanitize_key($_GET['source']) : null; $action = isset($_REQUEST['action']) ? trim($_REQUEST['action']) : null; $file = isset($_REQUEST['file']) ? trim($_REQUEST['file']) : null; $job_id = isset($_REQUEST['job_id']) ? trim($_REQUEST['job_id']) : null; if (!$action) { $this->header(); } // non-idempotent steps - action/POST request handlers if ($action) { switch ($action) { // handle source upload/sideload case 'upload': check_admin_referer('import-upload'); $file_path = $this->handle_upload($type, $source); if ($file_path) { $redirect_to = admin_url('admin.php?import=' . $type . '&step=2&file=' . urlencode($file_path)); wp_safe_redirect($redirect_to); exit; } break; // kick-off parsing & import // kick-off parsing & import case 'kickoff': check_admin_referer('import-woocommerce'); if ($file) { $bytes = filesize($file); /** * Filter CSV import options * * @since 3.0.0 * @param array $options * @param string $file Path to CSV file * @param string $type Import type */ $options = apply_filters('wc_csv_import_suite_import_options', (array) $_REQUEST['options'], $file, $type); // Setting default options will ensure that these options are set // even if they're unchecked $default_options = array('merge' => false, 'dry_run' => false, 'insert_non_matching' => false); $options = wp_parse_args($options, $default_options); $job_data = array('type' => $type, 'file' => $file, 'bytes' => $bytes); $this->start_background_import($job_data, $options); } break; case 'run_live': check_admin_referer('import-woocommerce'); if ($job_id) { $results = get_option('wc_csv_import_suite_background_import_job_' . $job_id); if ($results) { $job = json_decode($results, true); if ('completed' == $job['status']) { $options = $job['options']; $options['dry_run'] = false; $job_data = $job['data']; } $this->start_background_import($job_data, $options); } } break; } } else { switch ($step) { // 0. greeting and import source options form case 0: // render job import progress if ($job_id) { $this->render_import_progress($_GET['job_id']); } else { $this->render_import_source_options(); } break; // 1. display file upload / url / copy-paste input form // 1. display file upload / url / copy-paste input form case 1: $this->render_source_input_form($type, $source); break; // 2. detect delimiter and render additional options & preview // 2. detect delimiter and render additional options & preview case 2: if ($file) { $sample = WC_CSV_Import_Suite_Parser::get_sample($file); $delimiter = $this->guess_delimiter($sample); list($data, $headers) = WC_CSV_Import_Suite_Parser::parse_sample_data($file, $delimiter); $data = array(1 => $headers) + $data; $this->render_import_options($data, $delimiter); } break; // 3. display column mapper // 3. display column mapper case 3: if ($file) { $options = (array) $_REQUEST['options']; list($data, $raw_headers) = WC_CSV_Import_Suite_Parser::parse_sample_data($file, $options['delimiter'], 3); $this->render_column_mapper($data, $options, $raw_headers); } break; } } if (!$action) { $this->footer(); } }