/** * Display pre-import options */ public function import_options() { $j = 0; if ($this->id) { $file = get_attached_file($this->id); } else { if ($this->file_url_import_enabled) { $file = ABSPATH . $this->file_url; } else { return; } } // Set locale $enc = wc_pcsvis_is_first_row_encoded_in($file, 'UTF-8, ISO-8859-1'); if ($enc) { setlocale(LC_ALL, 'en_US.' . $enc); } @ini_set('auto_detect_line_endings', true); // Get headers if (($handle = fopen($file, "r")) !== FALSE) { $row = $raw_headers = array(); $header = fgetcsv($handle, 0, $this->delimiter); while (($postmeta = fgetcsv($handle, 0, $this->delimiter)) !== FALSE) { foreach ($header as $key => $heading) { if (!$heading) { continue; } $s_heading = strtolower($heading); $row[$s_heading] = isset($postmeta[$key]) ? $this->format_data_from_csv($postmeta[$key], $enc) : ''; $raw_headers[$s_heading] = $heading; } break; } fclose($handle); } $merge = !empty($_GET['merge']) && $_GET['merge'] ? 1 : 0; $taxonomies = get_taxonomies('', 'names'); include 'views/html-import-options.php'; }
/** * Parse the data * @param string $file [description] * @param string $delimiter [description] * @param array $mapping [description] * @param integer $start_pos [description] * @param integer $end_pos [description] * @return array */ public function parse_data($file, $delimiter, $mapping, $start_pos = 0, $end_pos = null) { // Set locale $enc = wc_pcsvis_is_first_row_encoded_in($file, 'UTF-8, ISO-8859-1'); if ($enc) { setlocale(LC_ALL, 'en_US.' . $enc); } @ini_set('auto_detect_line_endings', true); $parsed_data = array(); $raw_headers = array(); // Put all CSV data into an associative array if (($handle = fopen($file, "r")) !== FALSE) { $header = fgetcsv($handle, 0, $delimiter); if ($start_pos != 0) { fseek($handle, $start_pos); } while (($postmeta = fgetcsv($handle, 0, $delimiter)) !== FALSE) { $row = array(); foreach ($header as $key => $heading) { // Heading is the lowercase version of the column name $s_heading = strtolower($heading); // Check if this heading is being mapped to a different field if (isset($mapping[$s_heading])) { if ($mapping[$s_heading] == 'import_as_meta') { $s_heading = 'meta:' . $s_heading; } elseif ($mapping[$s_heading] == 'import_as_images') { $s_heading = 'images'; } else { $s_heading = esc_attr($mapping[$s_heading]); } } if ($s_heading == '') { continue; } // Add the heading to the parsed data $row[$s_heading] = isset($postmeta[$key]) ? $this->format_data_from_csv($postmeta[$key], $enc) : ''; // Raw Headers stores the actual column name in the CSV $raw_headers[$s_heading] = $heading; } $parsed_data[] = $row; unset($postmeta, $row); $position = ftell($handle); if ($end_pos && $position >= $end_pos) { break; } } fclose($handle); } return array($parsed_data, $raw_headers, $position); }