| @@ -8,8 +8,13 @@ | ||
| 8 | 8 | { |
| 9 | 9 | private $columns; |
| 10 | 10 | private $setup = false; |
| 11 | 11 | |
| 12 | + protected $default_delimiter = ","; | |
| 13 | + protected $default_enclosure = "\""; | |
| 14 | + protected $default_escape = "\\"; | |
| 15 | + protected $default_utf8_bom = false; | |
| 16 | + | |
| 12 | 17 | public function start() |
| 13 | 18 | { |
| 14 | 19 | $fields = $this->exporter->getFields(); |
| 15 | 20 | |
| @@ -17,12 +22,19 @@ | ||
| 17 | 22 | $carry[$this->getFieldLabel($item)] = isset($item['selection']) ? $item['selection'] : ''; |
| 18 | 23 | return $carry; |
| 19 | 24 | }, []); |
| 20 | 25 | |
| 26 | + list($delimiter, $enclosure, $escape, $utf8_bom) = $this->get_csv_settings(); | |
| 27 | + | |
| 28 | + // output uft8 BOM | |
| 29 | + if($utf8_bom){ | |
| 30 | + fwrite($this->fh, "\xEF\xBB\xBF"); | |
| 31 | + } | |
| 32 | + | |
| 21 | 33 | // write headers |
| 22 | - fputcsv($this->fh, array_keys($this->columns)); | |
| 34 | + fputcsv($this->fh, array_keys($this->columns), $delimiter, $enclosure, $escape); | |
| 23 | 35 | |
| 24 | - update_site_option('iwp_exporter_csv_config', [ | |
| 36 | + update_option('iwp_exporter_csv_config', [ | |
| 25 | 37 | 'columns' => $this->columns |
| 26 | 38 | ]); |
| 27 | 39 | } |
| 28 | 40 | |
| @@ -28,9 +40,9 @@ | ||
| 28 | 40 | |
| 29 | 41 | public function loadConfig() |
| 30 | 42 | { |
| 31 | 43 | if (!$this->setup) { |
| 32 | - $config = get_site_option('iwp_exporter_csv_config', []); | |
| 44 | + $config = get_option('iwp_exporter_csv_config', []); | |
| 33 | 45 | $this->columns = $config['columns']; |
| 34 | 46 | $this->setup = true; |
| 35 | 47 | } |
| 36 | 48 | } |
| @@ -43,8 +55,10 @@ | ||
| 43 | 55 | { |
| 44 | 56 | $this->loadConfig(); |
| 45 | 57 | $data = []; |
| 46 | 58 | |
| 59 | + list($delimiter, $enclosure, $escape) = $this->get_csv_settings(); | |
| 60 | + | |
| 47 | 61 | $max = $mapper->get_total_records(); |
| 48 | 62 | if ($max <= 0) { |
| 49 | 63 | $max = 1; |
| 50 | 64 | } |
| @@ -55,10 +69,24 @@ | ||
| 55 | 69 | $tmp = $mapper->get_value($item, $record[0]); |
| 56 | 70 | return is_array($tmp) ? implode(',', $tmp) : $tmp; |
| 57 | 71 | }, $this->columns); |
| 58 | 72 | |
| 59 | - fputcsv($this->fh, $data); | |
| 73 | + fputcsv($this->fh, $data, $delimiter, $enclosure, $escape); | |
| 60 | 74 | } |
| 75 | + } | |
| 76 | + | |
| 77 | + public function get_csv_settings() | |
| 78 | + { | |
| 79 | + $delimiter = $this->exporter->getFileSetting('delimiter', $this->default_delimiter); | |
| 80 | + $enclosure = $this->exporter->getFileSetting('enclosure', $this->default_enclosure); | |
| 81 | + $escape = $this->exporter->getFileSetting('escape', $this->default_escape); | |
| 82 | + $utf8_bom = $this->exporter->getFileSetting('utf8_bom', $this->default_utf8_bom); | |
| 83 | + | |
| 84 | + if ($enclosure === $escape) { | |
| 85 | + $escape = ''; | |
| 86 | + } | |
| 87 | + | |
| 88 | + return [$delimiter, $enclosure, $escape, $utf8_bom]; | |
| 61 | 89 | } |
| 62 | 90 | |
| 63 | 91 | public function end() |
| 64 | 92 | { |