PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.2
Import WP – CSV & XML Import Export for WordPress v2.15.2
2.15.2 2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 All 145 releases
← All changes | class/Common/Exporter/File/CSVFile.php +60 -7 2.7.6 → 2.15.2 View file →
@@ -6,8 +6,15 @@
6 6
7 7 class CSVFile extends File
8 8 {
9 9 private $columns;
10 + private $setup = false;
11 +
12 + protected $default_delimiter = ",";
13 + protected $default_enclosure = "\"";
14 + protected $default_escape = "\\";
15 + protected $default_utf8_bom = false;
16 +
10 17 public function start()
11 18 {
12 19 $fields = $this->exporter->getFields();
13 20
@@ -14,12 +21,33 @@
14 21 $this->columns = array_reduce($fields, function ($carry, $item) {
15 22 $carry[$this->getFieldLabel($item)] = isset($item['selection']) ? $item['selection'] : '';
16 23 return $carry;
17 24 }, []);
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 +
18 33 // write headers
19 - fputcsv($this->fh, array_keys($this->columns));
34 + fputcsv($this->fh, array_keys($this->columns), $delimiter, $enclosure, $escape);
35 +
36 + update_option('iwp_exporter_csv_config', [
37 + 'columns' => $this->columns
38 + ]);
20 39 }
21 40
41 + public function loadConfig()
42 + {
43 + if (!$this->setup) {
44 + $config = get_option('iwp_exporter_csv_config', []);
45 + $this->columns = $config['columns'];
46 + $this->setup = true;
47 + }
48 + }
49 +
22 50 /**
23 51 * @param MapperData $mapper
24 52 * @return void
25 53 */
@@ -24,16 +52,41 @@
24 52 * @return void
25 53 */
26 54 public function add($mapper)
27 55 {
56 + $this->loadConfig();
28 57 $data = [];
29 - $data = array_map(function ($item) use ($mapper) {
30 - $record = $mapper->data([]);
31 - $tmp = $mapper->get_value($item, $record[0]);
32 - return is_array($tmp) ? implode(',', $tmp) : $tmp;
33 - }, $this->columns);
34 58
35 - fputcsv($this->fh, $data);
59 + list($delimiter, $enclosure, $escape) = $this->get_csv_settings();
60 +
61 + $max = $mapper->get_total_records();
62 + if ($max <= 0) {
63 + $max = 1;
64 + }
65 + for ($i = 0; $i < $max; $i++) {
66 +
67 + $data = array_map(function ($item) use ($mapper, $i) {
68 + $record = $mapper->data([], $i);
69 + $tmp = $mapper->get_value($item, $record[0]);
70 + return is_array($tmp) ? implode(',', $tmp) : $tmp;
71 + }, $this->columns);
72 +
73 + fputcsv($this->fh, $data, $delimiter, $enclosure, $escape);
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];
36 89 }
37 90
38 91 public function end()
39 92 {