PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 2.9.1 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Exporter / File / CSVFile.php

CSVFile.php in Import WP – CSV & XML Import Export for WordPress trunk, at class/Common/Exporter/File/CSVFile.php

101 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Exporter\File;
4
5 use ImportWP\Common\Exporter\Mapper\MapperData;
6
7 class CSVFile extends File
8 {
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
17 public function start()
18 {
19 $fields = $this->exporter->getFields();
20
21 $this->columns = array_reduce($fields, function ($carry, $item) {
22 $carry[$this->getFieldLabel($item)] = isset($item['selection']) ? $item['selection'] : '';
23 return $carry;
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
33 // write headers
34 fputcsv($this->fh, array_keys($this->columns), $delimiter, $enclosure, $escape);
35
36 update_option('iwp_exporter_csv_config', [
37 'columns' => $this->columns
38 ]);
39 }
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
50 /**
51 * @param MapperData $mapper
52 * @return void
53 */
54 public function add($mapper)
55 {
56 $this->loadConfig();
57 $data = [];
58
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];
89 }
90
91 public function end()
92 {
93 fclose($this->fh);
94 }
95
96 public function get_file_name()
97 {
98 return $this->exporter->getId() . '.csv';
99 }
100 }
101