PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.12
Import WP – CSV & XML Import Export for WordPress v2.7.12
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 / Model / ExporterModel.php

ExporterModel.php in Import WP – CSV & XML Import Export for WordPress 2.7.12, at class/Common/Model/ExporterModel.php

414 lines 11.1 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\Model;
4
5 use ImportWP\Common\Util\Logger;
6
7 class ExporterModel
8 {
9 /**
10 * @var int $id
11 */
12 protected $id;
13
14 /**
15 * @var string $name
16 */
17 protected $name;
18
19 /**
20 * @var string $type
21 */
22 protected $type;
23
24 /**
25 * @var string $file_type
26 */
27 protected $file_type;
28
29 /**
30 * @var string $unique_identifier
31 */
32 protected $unique_identifier;
33
34 /**
35 * @var array $fields
36 */
37 protected $fields;
38
39 /**
40 * @var array $filters
41 */
42 protected $filters;
43
44 /**
45 * @var array $cron
46 */
47 protected $cron;
48
49 /**
50 * @var string
51 */
52 protected $export_method = 'run';
53
54 /**
55 * @var bool
56 */
57 protected $debug;
58
59 public function __construct($data = null, $debug = false)
60 {
61 $this->debug = $debug;
62 $this->setup_data($data);
63 }
64
65 private function setup_data($data)
66 {
67
68 if (is_array($data)) {
69
70 // fetch data from array
71 $this->id = isset($data['id']) && intval($data['id']) > 0 ? intval($data['id']) : null;
72 $this->name = $data['name'];
73 $this->type = $data['type'];
74 $this->file_type = $data['file_type'];
75 $this->fields = $data['fields'];
76 $this->filters = $data['filters'];
77 $this->unique_identifier = $data['unique_identifier'];
78 $this->export_method = isset($data['export_method']) ? $data['export_method'] : 'run';
79 $this->cron = isset($data['cron']) ? $data['cron'] : [];
80 } elseif (!is_null($data)) {
81
82 $post = false;
83
84 if ($data instanceof \WP_Post) {
85
86 // fetch data from post
87 $post = $data;
88 } elseif (intval($data) > 0) {
89
90 // fetch data from id
91 $this->id = intval($data);
92 $post = get_post($this->id);
93 }
94
95 if ($post && $post->post_type === EWP_POST_TYPE) {
96
97 $json = maybe_unserialize($post->post_content, true);
98 $this->id = $post->ID;
99 $this->name = $post->post_title;
100 $this->type = $json['type'];
101 $this->fields = (array) $json['fields'];
102 $this->file_type = $json['file_type'];
103 $this->filters = $json['filters'];
104 $this->unique_identifier = $json['unique_identifier'];
105 $this->export_method = isset($json['export_method']) ? $json['export_method'] : 'run';
106 $this->cron = isset($json['cron']) ? $json['cron'] : [];
107 }
108 }
109
110 Logger::setId($this->getId());
111 }
112
113 public function data($view = 'public')
114 {
115 $result = array(
116 'id' => $this->id,
117 'name' => '' . $this->getName(),
118 'type' => '' . $this->type,
119 'file_type' => '' . $this->file_type,
120 'fields' => $this->fields,
121 'filters' => $this->filters,
122 'unique_identifier' => '' . $this->unique_identifier,
123 'export_method' => '' . $this->export_method,
124 'cron' => $this->cron
125 );
126
127 if (true === $this->debug) {
128 global $wpdb;
129 $content = $wpdb->get_var($wpdb->prepare("SELECT post_content from {$wpdb->posts} WHERE post_type='%s' AND ID=%d", EWP_POST_TYPE, $this->getId()));
130 $result['debug'] = [
131 'settings' => base64_encode($content),
132 ];
133 }
134
135 return $result;
136 }
137
138 public function save()
139 {
140 // Match what happens in wp-rest.
141 remove_filter('content_save_pre', 'wp_filter_post_kses');
142
143 $postarr = array(
144 'post_title' => $this->name,
145 'post_content' => serialize(array(
146 'type' => $this->type,
147 'fields' => (array) $this->fields,
148 'file_type' => $this->file_type,
149 'filters' => (array)$this->filters,
150 'unique_identifier' => '' . $this->unique_identifier,
151 'export_method' => '' . $this->export_method,
152 'cron' => $this->cron
153 )),
154 );
155
156 if (is_null($this->id)) {
157 $postarr['post_type'] = EWP_POST_TYPE;
158 $postarr['post_status'] = 'publish';
159
160 $result = wp_insert_post($postarr, true);
161 } else {
162 $postarr['ID'] = $this->id;
163 $result = wp_update_post($postarr, true);
164 }
165
166 // Match what happens in wp-rest.
167 add_filter('content_save_pre', 'wp_filter_post_kses');
168
169
170 if (!is_wp_error($result)) {
171 $this->setup_data($result);
172 }
173
174 return $result;
175 }
176
177 public function delete()
178 {
179 if (get_post_type($this->getId()) === EWP_POST_TYPE) {
180 wp_delete_post($this->getId(), true);
181 }
182 }
183
184 public function getId()
185 {
186 return $this->id;
187 }
188
189 public function getName()
190 {
191 return $this->name;
192 }
193
194 public function setName($name)
195 {
196 $this->name = $name;
197 }
198
199 /**
200 * @return string
201 */
202 public function getType()
203 {
204 return $this->type;
205 }
206
207 /**
208 * @param string $type
209 */
210 public function setType($type)
211 {
212 $this->type = $type;
213 }
214
215 /**
216 * @return string
217 */
218 public function getFileType()
219 {
220 return $this->file_type;
221 }
222
223 /**
224 * @param string $file_type
225 */
226 public function setFileType($file_type)
227 {
228 $this->file_type = $file_type;
229 }
230
231 /**
232 * @return array
233 */
234 public function getFields($public = false)
235 {
236 $output = $this->fields;
237
238 if (!$public) {
239 // nest files, make sure there is a main loop
240 if ($this->getFileType() !== 'csv' && !empty($output)) {
241 $found = false;
242 foreach ($output as $row) {
243 if (isset($row['loop']) && $row['loop'] == true && $row['loop'] == 'true' && $row['selection'] == 'main') {
244 $found = true;
245 }
246 }
247
248 if (!$found) {
249 foreach ($output as $i => $row) {
250 if ($output[$i]['parent'] == 0) {
251 $output[$i]['parent'] = 2;
252 } else {
253 $output[$i]['parent'] = intval($row['id']) + 2;
254 }
255
256 $output[$i]['id'] = intval($row['id']) + 2;
257 }
258
259 if ($this->getFileType() === 'xml') {
260 $output[] = ['id' => 2, 'parent' => 1, 'selection' => 'main', 'loop' => true, 'label' => 'record'];
261 $output[] = ['id' => 1, 'parent' => 0, 'selection' => '', 'loop' => false, 'label' => 'records'];
262 } elseif ($this->getFileType() === 'json') {
263 $output[] = ['id' => 2, 'parent' => 0, 'selection' => 'main', 'loop' => true, 'label' => 'records'];
264 }
265 }
266 }
267
268 // make sure there is unique identifier within the main loop / flat file
269
270 $unique_identifier = $this->getUniqueIdentifier();
271
272 $found = array_reduce($output, function ($carry, $item) use ($unique_identifier) {
273
274 if (isset($item['selection']) && $item['selection'] == $unique_identifier) {
275 return true;
276 }
277
278 return $carry;
279 }, false);
280
281 if (!$found) {
282 switch ($this->getFileType()) {
283 case 'csv':
284 $output[] = ['id' => -1, 'parent' => 0, 'selection' => $unique_identifier];
285 break;
286 default:
287 // search fields for main loop and insert.
288 $tmp = false;
289 foreach ($output as $row) {
290 if (isset($row['loop']) && $row['loop'] == true && $row['loop'] == 'true' && $row['selection'] == 'main') {
291 $tmp = ['id' => -1, 'parent' => $row['id'], 'selection' => $unique_identifier];
292 break;
293 }
294 }
295
296 if ($tmp) {
297 array_unshift($output, $tmp);
298 }
299 break;
300 }
301 }
302 }
303
304 return $output;
305 }
306
307 /**
308 * @param array $fields
309 */
310 public function setFields($fields)
311 {
312 $this->fields = $fields;
313 }
314
315 /**
316 * @return string
317 */
318 public function getUniqueIdentifier()
319 {
320 return $this->unique_identifier;
321 }
322
323 /**
324 * @param string $unique_identifier
325 */
326 public function setUniqueIdentifier($unique_identifier)
327 {
328 $this->unique_identifier = $unique_identifier;
329 }
330
331 /**
332 * @param array $filters
333 */
334 public function setFilters($filters)
335 {
336 $this->filters = $filters;
337 }
338
339 public function getFilters()
340 {
341 $output = [];
342 $filter_data = $this->filters;
343 if (!is_array($filter_data)) {
344 return $output;
345 }
346
347 $max_groups = isset($filter_data['filters._index']) ? intval($filter_data['filters._index']) : 0;
348 if ($max_groups <= 0) {
349 return $output;
350 }
351
352 for ($i = 0; $i < $max_groups; $i++) {
353 $group_data = [];
354 $max_rows = isset($filter_data["filters.{$i}._index"]) ? intval($filter_data["filters.{$i}._index"]) : 0;
355 if ($max_groups <= 0) {
356 continue;
357 }
358
359 for ($j = 0; $j < $max_rows; $j++) {
360
361 $group_data[] = [
362 'left' => isset($filter_data["filters.{$i}.{$j}.left"]) ? $filter_data["filters.{$i}.{$j}.left"] : "",
363 'condition' => isset($filter_data["filters.{$i}.{$j}.condition"]) ? $filter_data["filters.{$i}.{$j}.condition"] : "equal",
364 'right' => isset($filter_data["filters.{$i}.{$j}.right"]) ? $filter_data["filters.{$i}.{$j}.right"] : "",
365 ];
366 }
367
368 $output[] = $group_data;
369 }
370
371 return $output;
372 }
373
374 public function get_status()
375 {
376 clean_post_cache($this->getId());
377 $status = get_post_meta($this->getId(), '_ewp_status', true);
378 if (!$status) {
379 $status = $this->set_status('created');
380 }
381 return $status;
382 }
383
384 public function set_status($status, $counter = 0, $total = 0)
385 {
386
387 $progress = $total > 0 ? round(($counter / $total) * 100, 2) : 0;
388 $data = array('status' => $status, 'date' => date('Y-m-d H:i:s'), 'progress' => $progress, 'count' => $counter, 'total' => $total);
389 update_post_meta($this->getId(), '_ewp_status', $data);
390
391 return $data;
392 }
393
394 public function setExportMethod($method)
395 {
396 $this->export_method = $method;
397 }
398
399 public function getExportMethod()
400 {
401 return $this->export_method;
402 }
403
404 public function setCron($settings)
405 {
406 $this->cron = $settings;
407 }
408
409 public function getCron()
410 {
411 return $this->cron;
412 }
413 }
414