PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.9
Import WP – CSV & XML Import Export for WordPress v2.7.9
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.7.9, at class/Common/Model/ImporterModel.php

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