PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.7
Import WP – CSV & XML Import Export for WordPress v2.7.7
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 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Parser / AbstractParser.php

AbstractParser.php in Import WP – CSV & XML Import Export for WordPress 2.7.7, at class/Common/Importer/Parser/AbstractParser.php

269 lines 8.3 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\Importer\Parser;
4
5 use ImportWP\Common\Importer\FileInterface;
6
7 abstract class AbstractParser
8 {
9 /**
10 * @var \ImportWP\Common\Importer\FileInterface $file
11 */
12 protected $file;
13
14 protected $record_index;
15 protected $record;
16
17 /**
18 * Parser constructor.
19 *
20 * @param \ImportWP\Common\Importer\FileInterface $file
21 */
22 public function __construct(FileInterface $file)
23 {
24 $this->file = $file;
25 }
26
27 public function getRecord($record_index = 0)
28 {
29
30 if ($this->record_index !== $record_index) {
31 $this->record_index = $record_index;
32 $this->record = $this->file->getRecord($this->getRecordIndex());
33 $this->onRecordLoaded();
34 }
35
36 return $this;
37 }
38
39 public function getRecordIndex()
40 {
41 return $this->record_index;
42 }
43
44 abstract protected function onRecordLoaded();
45
46 public function queryGroup($group)
47 {
48 $output = [];
49
50 if (isset($group['fields'])) {
51
52 // find files matching ._mapped._index
53
54 $maps = [];
55 foreach ($group['fields'] as $field_key => $field_value) {
56
57 $matches = [];
58 if (
59 // post.post_name._mapped._index
60 // custom_fields.0.value._mapped._index
61 preg_match('/^(.*?)\._mapped\._index$/', $field_key, $matches) === 1 &&
62 // ignore custom_fields.0._mapped._index old style FieldMapper
63 preg_match('/^custom_fields\.(?:[0-9]+)\._mapped\._index$/', $field_key) === 0 &&
64 intval($field_value) > 0
65 ) {
66 $maps[$matches[1]] = $field_value;
67 } else {
68 $output[$field_key] = $this->query_string($field_value);
69 }
70 }
71 }
72
73 if (!empty($maps)) {
74 foreach ($maps as $field_key => $field_rows) {
75
76 $data = [];
77 $delimiter = false;
78
79 if (isset($output[$field_key . '._mapped._delimiter']) && !empty($output[$field_key . '._mapped._delimiter'])) {
80
81 // get delimiter from FieldMap delimiter field, this has priority
82 $delimiter = $output[$field_key . '._mapped._delimiter'];
83 } else {
84
85 // get delimiter from parent section settings. e.g. taxonomies and attachments
86 $lastPos = strrpos($field_key, '.');
87 if ($lastPos !== false) {
88 $tmp = substr($field_key, 0, $lastPos);
89 if (isset($output[$tmp . '.settings._delimiter'])) {
90 $delimiter = !empty($output[$tmp . '.settings._delimiter']) ? $output[$tmp . '.settings._delimiter'] : ',';
91 }
92 }
93 }
94
95 foreach ($output as $item_key => $item_value) {
96 $matches = [];
97 if (preg_match('/^' . $field_key . '\._mapped\.([0-9]+)\.(.*?)$/', $item_key, $matches) === 1) {
98
99 // row is to high
100 if (intval($matches[1]) >= intval($field_rows)) {
101
102 unset($output[$matches[0]]);
103 continue;
104 }
105
106 if (!isset($data[$matches[1]])) {
107 $data[$matches[1]] = [];
108 }
109
110 $data[$matches[1]][$matches[2]] = $item_value;
111
112 unset($output[$matches[0]]);
113 }
114 }
115
116 if (!empty($delimiter)) {
117
118 if (!empty($output[$field_key])) {
119 $field_parts = explode($delimiter, $output[$field_key]);
120 foreach ($field_parts as $k => $field_part) {
121 $field_parts[$k] = $this->map_field_data($field_part, array_values($data));
122 }
123 $output[$field_key] = implode($delimiter, $field_parts);
124 }
125 } else {
126 $output[$field_key] = $this->map_field_data($output[$field_key], array_values($data));
127 }
128 }
129 }
130
131 return $output;
132 }
133
134 /**
135 * Parse Query String for {} run query on them
136 *
137 * @param string $query
138 *
139 * @return string
140 */
141 public function query_string($query)
142 {
143
144 $output = preg_replace_callback('/{(.*?)}/', array($this, 'query_matches'), $query);
145 $output = $this->handle_custom_methods($output);
146
147 return $output;
148 }
149
150 public function query_matches($matches)
151 {
152 if (isset($matches[0]) && isset($matches[1])) {
153 return $this->query($matches[1]);
154 }
155
156 return '';
157 }
158
159 abstract public function query($query);
160
161 public function file()
162 {
163 return $this->file;
164 }
165
166 public function handle_custom_methods($input)
167 {
168 // m: Multiline modifier
169 // s: matches all characters including newlines
170 $input = preg_replace_callback('/\[([\w]+)\((.*?)\)\]/ms', function ($matches) {
171
172 $method = $matches[1];
173
174 $result = [];
175 $args = [];
176
177 // Dont split comma's if they are inside a double quote
178 if (preg_match_all('/(?:".*?"|[^",\s]+)(?=\s*,|\s*$)/s', $matches[2], $result) > 0) {
179 $args = $result[0];
180 foreach ($args as &$arg) {
181
182 // Strip commas from start and end of string
183 $arg = preg_replace('/^(\'(.*)\'|"(.*)")$/s', '$2$3', $arg);
184 }
185 }
186
187 if (is_callable($method)) {
188 return call_user_func_array($method, $args);
189 }
190
191 return $matches[0];
192 }, $input);
193 return $input;
194 }
195
196 public function map_field_data($input, $map)
197 {
198 foreach ($map as $map_data) {
199
200 $condition = isset($map_data['_condition']) && !empty($map_data['_condition']) ? $map_data['_condition'] : 'equal';
201 $key = isset($map_data['key']) ? $map_data['key'] : '';
202 $output = isset($map_data['value']) ? $map_data['value'] : '';
203
204 switch ($condition) {
205
206 case 'gt':
207 $left = intval($input);
208 $right = intval($key);
209 if ($left > $right) {
210 return $output;
211 }
212 break;
213 case 'gte':
214 $left = intval($input);
215 $right = intval($key);
216 if ($left >= $right) {
217 return $output;
218 }
219 break;
220 case 'lt':
221 $left = intval($input);
222 $right = intval($key);
223 if ($left < $right) {
224 return $output;
225 }
226 break;
227 case 'lte':
228 $left = intval($input);
229 $right = intval($key);
230 if ($left <= $right) {
231 return $output;
232 }
233 break;
234 case 'contains':
235 if (stripos($input, trim($key)) !== false) {
236 return $output;
237 }
238 break;
239 case 'in':
240 if (in_array($input, explode(',', $key))) {
241 return $output;
242 }
243 break;
244 case 'not-equal':
245 if (trim($key) !== trim($input)) {
246 return $output;
247 }
248 break;
249 case 'not-contains':
250 if (stripos($input, trim($key)) === false) {
251 return $output;
252 }
253 break;
254 case 'not-in':
255 if (!in_array($input, explode(',', $key))) {
256 return $output;
257 }
258 break;
259 default:
260 if (trim($key) === trim($input)) {
261 return $output;
262 }
263 break;
264 }
265 }
266 return $input;
267 }
268 }
269