PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
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 / ImporterModel.php

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

708 lines 19.0 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 ImporterModel
8 {
9 /**
10 * @var int $id
11 */
12 protected $id;
13
14 /**
15 * @var int $user_id
16 */
17 protected $user_id;
18
19 /**
20 * @var string $name
21 */
22 protected $name;
23
24 /**
25 * @var string $template
26 */
27 protected $template;
28
29 /**
30 * @var string $template_type
31 */
32 protected $template_type;
33
34 /**
35 * @var string $parser
36 */
37 protected $parser;
38
39 /**
40 * @var string $mapper
41 */
42 protected $mapper;
43
44 /**
45 * @var string $datasource
46 */
47 protected $datasource;
48
49 /**
50 * @var array $datasource_settings
51 */
52 protected $datasource_settings;
53
54 /**
55 * @var string $file
56 */
57 protected $file;
58
59 /**
60 * @var int $file_id
61 */
62 protected $file_id;
63
64 /**
65 * @var array $file_settings
66 */
67 protected $file_settings;
68
69 /**
70 * @var array $map
71 */
72 protected $map;
73
74 /**
75 * @var array $enabled
76 */
77 protected $enabled;
78
79 /**
80 * @var array $permissions
81 */
82 protected $permissions;
83
84 /**
85 * @var int $max_row
86 */
87 protected $max_row;
88
89 /**
90 * @var int $start_row
91 */
92 protected $start_row;
93
94 /**
95 * Importer general settings
96 *
97 * @var array
98 */
99 protected $settings = [];
100
101 /**
102 * @var int
103 */
104 protected $version;
105 protected $version_latest = 2;
106
107 /**
108 * @var bool
109 */
110 protected $debug;
111
112 public function __construct($data = null, $debug = false)
113 {
114 $this->debug = $debug;
115 $this->setup_data($data);
116 }
117
118 private function setup_data($data)
119 {
120
121 if (is_array($data)) {
122
123 // fetch data from array
124 $this->id = isset($data['id']) && intval($data['id']) > 0 ? intval($data['id']) : null;
125 $this->name = $data['name'];
126 $this->template = isset($data['template']) ? $data['template'] : null;
127 $this->template_type = isset($data['template_type']) ? $data['template_type'] : null;
128 $this->parser = isset($data['parser']) ? $data['parser'] : null;
129 $this->mapper = isset($data['mapper']) ? $data['mapper'] : null;
130 $this->map = isset($data['map']) ? $data['map'] : null;
131 $this->enabled = isset($data['enabled']) ? $data['enabled'] : null;
132 $this->file_id = isset($data['file'], $data['file']['id']) ? $data['file']['id'] : null;
133 if ($this->file_id) {
134 $this->file = $this->get_attached_file($this->file_id);
135 }
136 $this->file_settings = array_merge($this->getDetaultFileSettings(), isset($data['file'], $data['file']['settings']) ? $data['file']['settings'] : array());
137 $this->datasource = isset($data['datasource'], $data['datasource']['type']) ? $data['datasource']['type'] : null;
138 $this->datasource_settings = isset($data['datasource'], $data['datasource']['settings']) ? $data['datasource']['settings'] : null;
139 $this->permissions = isset($data['permissions']) ? $data['permissions'] : null;
140 $this->start_row = isset($data['settings'], $data['settings']['start_row']) ? $data['settings']['start_row'] : null;
141 $this->max_row = isset($data['settings'], $data['settings']['max_row']) ? $data['settings']['max_row'] : null;
142 $this->settings = isset($data['settings']) ? $data['settings'] : [];
143 $this->version = isset($data['version']) ? $data['version'] : 0;
144 } elseif (!is_null($data)) {
145
146 $post = false;
147
148 if ($data instanceof \WP_Post) {
149
150 // fetch data from post
151 $post = $data;
152 } elseif (intval($data) > 0) {
153
154 // fetch data from id
155 $this->id = intval($data);
156 $post = get_post($this->id);
157 }
158
159 if ($post && $post->post_type === IWP_POST_TYPE) {
160
161 // Fix broken importer issue.
162 $json = maybe_unserialize($post->post_content, true);
163 if (!is_array($json)) {
164 $json = [];
165 }
166
167 $this->id = $post->ID;
168 $this->name = $post->post_title;
169 $this->user_id = $post->post_author;
170
171 $this->template = isset($json['template']) ? $json['template'] : null;
172 $this->template_type = isset($json['template_type']) ? $json['template_type'] : null;
173 $this->parser = isset($json['parser']) ? $json['parser'] : null;
174 $this->mapper = isset($json['mapper']) ? $json['mapper'] : null;
175 $this->map = isset($json['map']) ? $json['map'] : [];
176 $this->enabled = isset($json['enabled']) ? $json['enabled'] : [];
177 $this->file_id = isset($json['file'], $json['file']['id']) ? intval($json['file']['id']) : 0;
178 if ($this->file_id > 0) {
179 $this->file = $this->get_attached_file($this->file_id);
180 }
181
182 if (!isset($json['file']['settings']) || !is_array($json['file']['settings'])) {
183 $json['file']['settings'] = [];
184 }
185
186 $this->file_settings = array_merge($this->getDetaultFileSettings(), $json['file']['settings']);
187
188 $this->datasource = isset($json['datasource'], $json['datasource']['type']) ? $json['datasource']['type'] : null;
189 $this->datasource_settings = isset($json['datasource'], $json['datasource']['settings']) ? $json['datasource']['settings'] : [];
190
191 $this->permissions = isset($json['permissions']) ? $json['permissions'] : [];
192
193 $this->start_row = isset($json['settings'], $json['settings']['start_row']) ? $json['settings']['start_row'] : null;
194 $this->max_row = isset($json['settings'], $json['settings']['max_row']) ? $json['settings']['max_row'] : null;
195 $this->settings = isset($json['settings']) ? $json['settings'] : [];
196 $this->version = isset($json['version']) ? $json['version'] : 0;
197 }
198 }
199
200 Logger::setId($this->getId());
201 }
202
203 public function data($view = 'public')
204 {
205 $files = $this->getFiles();
206 $file = $this->getFile();
207 if ($view === 'public' && false === $this->debug) {
208
209 if ($file) {
210 $file = basename($file);
211 }
212
213 foreach ($files as &$tmp_file) {
214 $tmp_file = basename($tmp_file) . ' - (Added: ' . date(get_option('date_format') . ' \a\t ' . get_option('time_format'), filemtime($tmp_file)) . ')';
215 }
216 } else {
217 foreach ($files as &$tmp_file) {
218
219 $tmp_file .= ' - (Added: ' . date(get_option('date_format') . ' \a\t ' . get_option('time_format'), filemtime($tmp_file)) . ')';
220 }
221 }
222
223 $file_data = [
224 'id' => $this->getFileId(),
225 'src' => $file,
226 'settings' => $this->file_settings
227 ];
228
229 $datasource_data = [
230 'type' => $this->getDatasource(),
231 'settings' => (object) $this->getDatasourceSettings()
232 ];
233
234 $settings = array_merge($this->settings, [
235 'start_row' => $this->start_row,
236 'max_row' => $this->max_row,
237 ]);
238
239 $result = array(
240 'id' => $this->id,
241 'name' => $this->getName(),
242 'template' => $this->template,
243 'template_type' => $this->template_type,
244 'parser' => $this->getParser(),
245 'cron' => [],
246 'file' => $file_data,
247 'files' => (object) $files,
248 'datasource' => $datasource_data,
249 'map' => (object) $this->map,
250 'enabled' => (object) $this->enabled,
251 'permissions' => (object) $this->getPermissions(),
252 'settings' => (object) $settings,
253 'version' => $this->version,
254 );
255
256 if (true === $this->debug) {
257 global $wpdb;
258 $content = $wpdb->get_var($wpdb->prepare("SELECT post_content from {$wpdb->posts} WHERE post_type='%s' AND ID=%d", IWP_POST_TYPE, $this->getId()));
259 $result['debug'] = [
260 'settings' => base64_encode($content),
261 // 'log' => file_get_contents(Logger::getLogFile($this->getId()))
262 ];
263 }
264
265 return $result;
266 }
267
268 public function save()
269 {
270 // Match what happens in wp-rest.
271 remove_filter('content_save_pre', 'wp_filter_post_kses');
272
273 $settings = array_merge($this->settings, [
274 'start_row' => $this->start_row,
275 'max_row' => $this->max_row,
276 ]);
277
278 $post_content = array(
279 'template' => $this->template,
280 'template_type' => $this->template_type,
281 'file' => [
282 'id' => $this->file_id,
283 'settings' => $this->file_settings
284 ],
285 'datasource' => [
286 'type' => $this->datasource,
287 'settings' => $this->getDatasourceSettings()
288 ],
289 'parser' => $this->parser,
290 'map' => $this->map,
291 'enabled' => $this->enabled,
292 'permissions' => $this->permissions,
293 'settings' => $settings,
294 'version' => $this->version
295 );
296
297 if (is_null($this->id)) {
298
299 if ($this->template === 'jet-engine-cct') {
300
301 // do not force the new permissions interface.
302
303 } else {
304
305 // set defaults on new importers
306 $post_content['version'] = $this->version_latest;
307 $post_content['settings']['unique_identifier_type'] = 'field';
308 }
309 }
310
311 $postarr = array(
312 'post_title' => $this->name,
313 'post_content' => wp_slash(serialize($post_content)),
314 );
315
316 if (is_null($this->id)) {
317 $postarr['post_type'] = IWP_POST_TYPE;
318 $postarr['post_status'] = 'publish';
319
320 $result = wp_insert_post($postarr, true);
321 } else {
322 $postarr['ID'] = $this->id;
323 $result = wp_update_post($postarr, true);
324 }
325
326 // Match what happens in wp-rest.
327 add_filter('content_save_pre', 'wp_filter_post_kses');
328
329
330 if (!is_wp_error($result)) {
331 $this->setup_data($result);
332 }
333
334 return $result;
335 }
336
337 public function delete()
338 {
339 if (get_post_type($this->getId()) === IWP_POST_TYPE) {
340 wp_delete_post($this->getId(), true);
341 }
342 }
343
344 public function getAllowedFileTypes()
345 {
346
347 $parser = $this->getParser();
348 $allowed_file_types = apply_filters('iwp/importer/datasource/allowed_file_types', ['xml', 'csv'], $this);
349
350 if (!is_null($parser)) {
351 $allowed_file_types = [$parser];
352 }
353
354 return $allowed_file_types;
355 }
356
357 public function setDatasource($datasource)
358 {
359 $this->datasource = $datasource;
360 }
361
362 public function setDatasourceSetting($key, $value)
363 {
364 $this->datasource_settings[$key] = $value;
365 }
366
367 public function getSetting($key)
368 {
369 return isset($this->settings[$key]) ? $this->settings[$key] : null;
370 }
371
372 public function setSetting($key, $value)
373 {
374 $this->settings[$key] = $value;
375 }
376
377 public function setFileId($file_id)
378 {
379 $this->file_id = $file_id;
380 $this->file = $this->get_attached_file($this->file_id);
381
382 if (is_null($this->file_settings)) {
383 $this->file_settings = $this->getDetaultFileSettings();
384 }
385 }
386
387 public function getDetaultFileSettings()
388 {
389 switch ($this->getParser()) {
390 case 'xml':
391 return [
392 'base_path' => null,
393 'setup' => false
394 ];
395 break;
396 case 'csv':
397 return [
398 'enclosure' => '"',
399 'delimiter' => ',',
400 'escape' => '\\',
401 'show_headings' => true,
402 'setup' => false
403 ];
404 break;
405 }
406
407 return [];
408 }
409
410 public function setFile($file_id, $settings)
411 {
412 $this->file_id = $file_id;
413 $this->file = $this->get_attached_file($this->file_id);
414 $this->file_settings = array_merge($this->getDetaultFileSettings(), $settings);
415 }
416
417 public function setMaxRow($max)
418 {
419 $this->max_row = $max;
420 }
421
422 public function setStartRow($start)
423 {
424 $this->start_row = $start;
425 }
426
427 public function setName($name)
428 {
429 $this->name = $name;
430 }
431
432 public function setParser($parser)
433 {
434 $this->parser = $parser;
435 }
436
437 public function setTemplate($template, $template_type = '')
438 {
439 $this->template = $template;
440 $this->template_type = $template_type;
441 }
442
443 public function getDatasource()
444 {
445 return $this->datasource;
446 }
447
448 public function getDatasourceSettings()
449 {
450 return $this->datasource_settings;
451 }
452
453 public function getDatasourceSetting($key)
454 {
455 return isset($this->datasource_settings[$key]) ? $this->datasource_settings[$key] : null;
456 }
457
458 public function getFile()
459 {
460 return $this->file;
461 }
462
463 public function getFileId()
464 {
465 return $this->file_id;
466 }
467
468 public function getFiles()
469 {
470 $files = [];
471
472 if (!$this->getId()) {
473 return $files;
474 }
475
476 global $wpdb;
477 $query = "SELECT meta_key, meta_value
478 FROM {$wpdb->postmeta}
479 WHERE post_id={$this->getId()} AND meta_key
480 LIKE '\_importer\_file\_%'";
481
482 $results = $wpdb->get_results($query, ARRAY_A);
483
484 foreach ($results as $result) {
485 $key = $result['meta_key'];
486 $id = intval(str_replace('_importer_file_', '', $key));
487 $value = $result['meta_value'];
488 if (file_exists($value)) {
489 $files[$id] = $value;
490 }
491 }
492
493 return $files;
494 }
495
496 public function get_attached_file($post_id)
497 {
498 $files = $this->getFiles();
499 return isset($files[$post_id]) ? $files[$post_id] : false;
500 }
501
502 /**
503 * Set the limit of importer files
504 *
505 * @param integer $limit The number of files to keep, 0 to keep all files.
506 * @return void
507 */
508 public function limit_importer_files($limit = 0)
509 {
510
511 $limit = intval($limit);
512 if ($limit <= -1) {
513 return;
514 }
515
516 $files = $this->getFiles();
517 if (empty($files) || count($files) <= $limit) {
518 return;
519 }
520
521 ksort($files);
522
523 $file_ids = array_keys($files);
524
525 for ($i = 0; $i < count($files) - $limit; $i++) {
526
527 $id = $file_ids[$i];
528 $path = $files[$id];
529
530 if ($id === $this->getFileId()) {
531 continue;
532 }
533
534 if (!file_exists($path) || unlink($path)) {
535 global $wpdb;
536 $query = "DELETE FROM {$wpdb->postmeta} WHERE post_id={$this->getId()} AND meta_key = '_importer_file_{$id}'";
537 $result = $wpdb->query($query);
538 }
539 }
540 }
541
542 public function getId()
543 {
544 return $this->id;
545 }
546
547 public function getMaxRow()
548 {
549 return $this->max_row;
550 }
551
552 public function getStartRow()
553 {
554 return $this->start_row;
555 }
556
557 public function getName()
558 {
559 return $this->name;
560 }
561
562 public function getParser()
563 {
564 return $this->parser;
565 }
566
567 public function getStatus()
568 {
569 clean_post_cache($this->id);
570 $post = get_post($this->id);
571 return (array) maybe_unserialize($post->post_excerpt);
572 }
573
574 public function getStatusId()
575 {
576 return get_post_meta($this->getId(), '_iwp_session', true);
577 }
578
579 public function getTemplate()
580 {
581 return $this->template;
582 }
583
584 public function getFileSettings()
585 {
586 return $this->file_settings;
587 }
588
589 public function getFileSetting($key, $default = null)
590 {
591 return isset($this->file_settings[$key]) ? $this->file_settings[$key] : $default;
592 }
593
594 public function setFileSetting($key, $value)
595 {
596 $this->file_settings[$key] = $value;
597 }
598
599 public function getMap()
600 {
601 return $this->map;
602 }
603
604 public function setMap($key, $value)
605 {
606 $this->map[$key] = $value;
607 }
608
609 public function getEnabled()
610 {
611 return $this->enabled;
612 }
613
614 public function setEnabled($key, $value = true)
615 {
616 if ($value === true) {
617 $this->enabled[$key] = $value;
618 } elseif (isset($this->enabled[$key])) {
619 unset($this->enabled[$key]);
620 }
621 }
622
623 public function isEnabledField($key)
624 {
625 return isset($this->enabled[$key]) && $this->enabled[$key] === true ? true : false;
626 }
627
628 public function getPermission($key)
629 {
630 return isset($this->permissions[$key]) ? $this->permissions[$key] : false;
631 }
632
633 public function getPermissions()
634 {
635 return $this->permissions;
636 }
637
638 public function setPermission($key, $settings)
639 {
640 $this->permissions[$key] = $settings;
641 }
642
643 public function getFilters()
644 {
645 $output = [];
646 $filter_data = $this->getSetting('filters');
647 if (!is_array($filter_data)) {
648 return $output;
649 }
650
651 $max_groups = isset($filter_data['filters._index']) ? intval($filter_data['filters._index']) : 0;
652 if ($max_groups <= 0) {
653 return $output;
654 }
655
656 for ($i = 0; $i < $max_groups; $i++) {
657 $group_data = [];
658 $max_rows = isset($filter_data["filters.{$i}._index"]) ? intval($filter_data["filters.{$i}._index"]) : 0;
659 if ($max_groups <= 0) {
660 continue;
661 }
662
663 for ($j = 0; $j < $max_rows; $j++) {
664
665 $group_data[] = [
666 'left' => isset($filter_data["filters.{$i}.{$j}.left"]) ? $filter_data["filters.{$i}.{$j}.left"] : "",
667 'condition' => isset($filter_data["filters.{$i}.{$j}.condition"]) ? $filter_data["filters.{$i}.{$j}.condition"] : "equal",
668 'right' => isset($filter_data["filters.{$i}.{$j}.right"]) ? $filter_data["filters.{$i}.{$j}.right"] : "",
669 ];
670 }
671
672 $output[] = $group_data;
673 }
674
675 return $output;
676 }
677
678 public function getUserId()
679 {
680 return intval($this->user_id) > 0 ? intval($this->user_id) : false;
681 }
682
683 public function getVersion()
684 {
685 return $this->version;
686 }
687
688 public function get_iwp_reference_meta_key()
689 {
690 return '_iwp_ref_uid';
691 }
692
693 public function has_custom_unique_identifier()
694 {
695 return $this->getSetting('unique_identifier_type') === 'custom';
696 }
697
698 public function has_field_unique_identifier()
699 {
700 return $this->getSetting('unique_identifier_type') === 'field';
701 }
702
703 public function has_legacy_unique_identifier()
704 {
705 return !in_array($this->getSetting('unique_identifier_type'), ['custom', 'field']);
706 }
707 }
708