PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.3
Import WP – CSV & XML Import Export for WordPress v2.7.3
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 / Template / Template.php

Template.php in Import WP – CSV & XML Import Export for WordPress 2.7.3, at class/Common/Importer/Template/Template.php

851 lines 30.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\Importer\Template;
4
5 use ImportWP\Common\Attachment\Attachment;
6 use ImportWP\Common\Filesystem\Filesystem;
7 use ImportWP\Common\Ftp\Ftp;
8 use ImportWP\Common\Importer\File\XMLFile;
9 use ImportWP\Common\Importer\ParsedData;
10 use ImportWP\Common\Model\ImporterModel;
11 use ImportWP\Common\Util\Logger;
12 use ImportWP\Container;
13 use ImportWP\EventHandler;
14
15 class Template extends AbstractTemplate
16 {
17 /**
18 * Template name
19 *
20 * @var string
21 */
22 protected $name;
23 /**
24 * Mapper name
25 *
26 * @var string
27 */
28 protected $mapper;
29 /**
30 * @var ImporterModel
31 */
32 protected $importer;
33
34 /**
35 * Registered data groups
36 *
37 * @var string[]
38 */
39 protected $groups;
40
41 /**
42 * List of field option callbacks
43 *
44 * @var array
45 */
46 protected $field_options = [];
47
48 protected $default_template_options = [];
49
50 /**
51 * @var \WP_Error[] $errors
52 */
53 protected $errors = [];
54
55 /**
56 * @var EventHandler $event_handler
57 */
58 private $event_handler;
59
60 /**
61 * @var boolean
62 */
63 private $featured_set = false;
64
65 public function __construct(EventHandler $event_handler)
66 {
67 $this->event_handler = $event_handler;
68 $this->event_handler->run('template.init', [$this]);
69
70 $this->groups = $this->event_handler->run('template.data_groups', [$this->groups, $this]);
71
72 // Register field options callbacks
73 $this->field_options = [
74 '*.row_base' => [$this, 'get_row_base']
75 ];
76 $this->field_options = $this->event_handler->run('template.field_option_callbacks', [$this->field_options, $this]);
77 }
78
79 /**
80 * Get xml record base
81 *
82 * @param ImporterModel $importer_model
83 * @return array
84 */
85 public function get_row_base($importer_model)
86 {
87 /**
88 * @var ImporterManager $importer_manager
89 */
90 $importer_manager = Container::getInstance()->get('importer_manager');
91 $tmp = false;
92 $results = [];
93
94 if ($importer_model->getParser() !== 'xml') {
95 return $results;
96 }
97
98 $base_path = $importer_model->getFileSetting('base_path');
99 $nodes = $importer_model->getFileSetting('nodes');
100
101 $config = $importer_manager->get_config($importer_model->getId(), $tmp);
102
103 $filePath = $importer_model->getFile();
104 $file = new XMLFile($filePath, $config);
105 $file->setRecordPath($base_path);
106 $nodes = $file->get_node_list();
107
108 foreach ($nodes as $node) {
109 if (strpos($node, $base_path) !== 0) {
110 continue;
111 }
112
113 $sub_node = substr($node, strlen($base_path));
114
115 $results[] = [
116 'value' => $sub_node,
117 'label' => $sub_node
118 ];
119 }
120
121 return $results;
122 }
123
124 public function get_name()
125 {
126 return $this->name;
127 }
128
129 public function get_mapper()
130 {
131 return $this->mapper;
132 }
133
134 public function get_importer()
135 {
136 return $this->importer;
137 }
138
139 public function get_fields(ImporterModel $importer = null)
140 {
141 $fields = $this->register();
142 $fields = $this->event_handler->run('template.fields', [$fields, $this, $importer]);
143 return $fields;
144 }
145
146 public function register()
147 {
148 return [];
149 }
150
151 public function register_hooks(ImporterModel $importer)
152 {
153 $this->importer = $importer;
154
155 add_filter('iwp/importer/before_mapper', array($this, 'pre_process'));
156 add_filter('iwp/status/record_inserted', [$this, 'display_record_info'], 10, 3);
157 add_filter('iwp/status/record_updated', [$this, 'display_record_info'], 10, 3);
158 }
159
160 public function unregister_hooks()
161 {
162 remove_filter('iwp/importer/before_mapper', array($this, 'pre_process'));
163 remove_filter('iwp/status/record_inserted', [$this, 'display_record_info'], 10, 3);
164 remove_filter('iwp/status/record_updated', [$this, 'display_record_info'], 10, 3);
165 }
166
167 /**
168 * @param string $message
169 * @param int $id
170 * @param ParsedData $data
171 * @return $string
172 */
173 public function display_record_info($message, $id, $data)
174 {
175
176 $fields = $data->getData();
177 if (!empty($fields)) {
178 $message .= ' (' . implode(', ', array_keys($fields)) . ')';
179 }
180
181 if (!empty($this->errors)) {
182 $errors = [];
183 foreach ($this->errors as $error) {
184 $errors[] = $error->get_error_message();
185 }
186
187 $message .= ', Errors: (' . implode(', ', $errors) . ')';
188 }
189
190 $this->errors = [];
191
192 return $message;
193 }
194
195 public function register_group($label, $key, $fields, $args = [])
196 {
197 if (isset($args['row_base']) && true === $args['row_base']) {
198
199 $fields = array_merge(
200 [$this->register_field('Repeater Node', 'row_base', [
201 'type' => 'text',
202 'options' => 'callback',
203 'tooltip' => 'Select the path to the parent node containing values for this record, The preview when selecting data for each record should show a single record. Please note changing this will require updating references in this record.'
204 ])],
205 $fields
206 );
207 }
208
209 $tmp = [
210 'id' => $key,
211 'heading' => $label,
212 'type' => isset($args['type']) ? $args['type'] : 'group',
213 'fields' => $fields
214 ];
215
216 if (isset($args['condition']) && !empty($args['condition'])) {
217 $tmp['condition'] = $args['condition'];
218 }
219
220 return $tmp;
221 }
222
223 public function register_core_field($label, $key, $args = [])
224 {
225 $args['core'] = true;
226 return $this->register_field($label, $key, $args);
227 }
228
229 public function register_field($label, $key, $args = [])
230 {
231 $data = [
232 'id' => $key,
233 'label' => $label,
234 'type' => isset($args['type']) ? $args['type'] : 'field',
235 'core' => isset($args['core']) ? $args['core'] : false,
236 'tooltip' => isset($args['tooltip']) ? $args['tooltip'] : false
237 ];
238
239 if (isset($args['options'])) {
240 $data['options'] = $args['options'];
241 }
242
243 if (isset($args['default'])) {
244 $data['default'] = $args['default'];
245 }
246
247 if (isset($args['condition'])) {
248 $data['condition'] = $args['condition'];
249 }
250
251 return $data;
252 }
253
254 public function _register_attachment_setting_fields($display_conditions = [], $extra_fields = [])
255 {
256 return $this->register_group('Settings', 'settings', array_merge($extra_fields, [
257
258 $this->register_field('Is Featured?', '_featured', [
259 'default' => 'no',
260 'options' => [
261 ['value' => 'no', 'label' => 'No'],
262 ['value' => 'yes', 'label' => 'Yes'],
263 ],
264 'tooltip' => __('Is the attachment the featured image for the current post.', 'importwp')
265 ]),
266 $this->register_field('Download', '_download', [
267 'default' => 'remote',
268 'options' => [
269 ['value' => 'remote', 'label' => 'Remote URL'],
270 ['value' => 'ftp', 'label' => 'FTP'],
271 ['value' => 'local', 'label' => 'Local Filesystem'],
272 ['value' => 'media', 'label' => 'Media Library'],
273 ],
274 'tooltip' => __('Select how the attachment is being downloaded.', 'importwp')
275 ]),
276 $this->register_field('Host', '_ftp_host', [
277 'condition' => ['_download', '==', 'ftp'],
278 'tooltip' => __('Enter the FTP hostname', 'importwp')
279 ]),
280 $this->register_field('Username', '_ftp_user', [
281 'condition' => ['_download', '==', 'ftp'],
282 'tooltip' => __('Enter the FTP username', 'importwp')
283 ]),
284 $this->register_field('Password', '_ftp_pass', [
285 'condition' => ['_download', '==', 'ftp'],
286 'tooltip' => __('Enter the FTP password', 'importwp')
287 ]),
288 $this->register_field('Path', '_ftp_path', [
289 'condition' => ['_download', '==', 'ftp'],
290 'tooltip' => __('Enter the FTP base path, this is prefixed onto the Location field, leave empty to be ignore', 'importwp')
291 ]),
292 $this->register_field('Base URL', '_remote_url', [
293 'condition' => ['_download', '==', 'remote'],
294 'tooltip' => __('Enter the base url, this is prefixed onto the Location field, leave empty to be ignore', 'importwp')
295 ]),
296 $this->register_field('Base URL', '_local_url', [
297 'condition' => ['_download', '==', 'local'],
298 'tooltip' => __('Enter the base path from this servers root file system, this is prefixed onto the Location field, leave empty to be ignore', 'importwp')
299 ]),
300
301 $this->register_field('Permissions', '_enable_image_hash', [
302 'condition' => ['_download', '!=', 'media'],
303 'default' => 'yes',
304 'options' => [
305 ['value' => 'no', 'label' => 'Always download new files'],
306 ['value' => 'yes', 'label' => 'Search media library before downloading new files'],
307 ],
308 'tooltip' => __('Enable to stop duplicate images by searching the media library before downloading new files.', 'importwp')
309 ]),
310 $this->register_field('Delimiter', '_delimiter', [
311 'type' => 'text',
312 'tooltip' => 'A single character used to seperate attachments when listing multiple, Leave empty to use default: ,'
313 ]),
314 $this->register_group('Attachment Meta', '_meta', [
315 $this->register_field('Enable Meta', '_enabled', [
316 'default' => 'no',
317 'options' => [
318 ['value' => 'no', 'label' => 'No'],
319 ['value' => 'yes', 'label' => 'Yes'],
320 ],
321 'type' => 'select',
322 'tooltip' => __('Enable/Disable the fields to import attachment meta data.', 'importwp')
323 ]),
324 $this->register_field('Alt Text', '_alt', [
325 'condition' => ['_enabled', '==', 'yes'],
326 'tooltip' => __('Image attachment alt text.', 'importwp'),
327 ]),
328 $this->register_field('Title Text', '_title', [
329 'condition' => ['_enabled', '==', 'yes'],
330 'tooltip' => __('Attachments title text.', 'importwp')
331 ]),
332 $this->register_field('Caption Text', '_caption', [
333 'condition' => ['_enabled', '==', 'yes'],
334 'tooltip' => __('Image attachments caption text.', 'importwp')
335 ]),
336 $this->register_field('Description Text', '_description', [
337 'condition' => ['_enabled', '==', 'yes'],
338 'tooltip' => __('Attachments description text.', 'importwp')
339 ])
340 ]),
341 ]), ['type' => 'settings', 'condition' => $display_conditions]);
342 }
343
344 public function register_attachment_fields($label = 'Attachments', $name = 'attachments', $field_label = 'Location', $group_args = null)
345 {
346 if (is_null($group_args)) {
347 $group_args = ['type' => 'repeatable', 'row_base' => true];
348 }
349 return $this->register_group($label, $name, [
350 $this->register_field($field_label, 'location', [
351 'tooltip' => __('The source location of the file being attached.', 'importwp')
352 ]),
353 $this->_register_attachment_setting_fields()
354 ], $group_args);
355 }
356
357 public function get_field_options($field_name, $id)
358 {
359 $callback = false;
360 foreach ($this->field_options as $callback_field_name => $temp_callback) {
361 if (strpos($callback_field_name, '*') !== false) {
362
363 $callback_field_name = str_replace('.', '\.', $callback_field_name);
364
365 $pattern = str_replace('*', '[\S]+', $callback_field_name);
366 if (1 !== preg_match("/^{$pattern}/i", $field_name)) {
367 continue;
368 }
369
370 $callback = $temp_callback;
371 break;
372 }
373
374 if ($field_name !== $callback_field_name) {
375 continue;
376 }
377
378 $callback = $temp_callback;
379 break;
380 }
381
382 if (!$callback) {
383 return false;
384 }
385
386 if (!method_exists($callback[0], $callback[1])) {
387 return false;
388 }
389
390 if (!is_callable($callback)) {
391 return false;
392 }
393
394 return call_user_func_array($callback, [$id]);
395 }
396
397 public function get_default_template_options()
398 {
399 return $this->default_template_options;
400 }
401
402 /**
403 * Alter fields before they are parsed
404 *
405 * @param array $fields
406 * @return array
407 */
408 public function field_map($fields)
409 {
410 return $fields;
411 }
412
413 public function config_field_map($field_map)
414 {
415 $template_fields = $this->field_map($field_map);
416
417 $field_groups = [
418 'default' => [
419 'id' => 'default',
420 'fields' => []
421 ]
422 ];
423
424 foreach ($template_fields as $key => $value) {
425
426 if (empty($value) || preg_match('/\.row_base$/', $key) !== 1) {
427 continue;
428 }
429
430 $group_id = substr($key, 0, strlen($key) - strlen('.row_base'));
431
432 $field_groups[$group_id] = [
433 'id' => $group_id,
434 'base' => $value,
435 'fields' => []
436 ];
437 }
438
439 foreach ($template_fields as $field_id => $field_value) {
440
441 $found = false;
442
443 foreach ($field_groups as $group_prefix => $group_settings) {
444 if (false === strpos($field_id, $group_prefix) || preg_match('/\.row_base$/', $field_id) === 1) {
445 continue;
446 }
447
448 $field_groups[$group_prefix]['fields'][$field_id] = $field_value;
449 $found = true;
450 }
451
452 if (false === $found) {
453 $field_groups['default']['fields'][$field_id] = $field_value;
454 }
455 }
456
457 return $field_groups;
458 }
459
460 /**
461 * Process data before record is importer.
462 *
463 * Alter data that is passed to the mapper.
464 *
465 * @param ParsedData $data
466 * @return ParsedData
467 */
468 public function pre_process(ParsedData $data)
469 {
470 $data = $this->pre_process_groups($data);
471 $this->event_handler->run('template.pre_process', [$data, $this->importer, $this]);
472 return $data;
473 }
474
475 public function process($post_id, ParsedData $data, ImporterModel $importer_model)
476 {
477 $this->featured_set = false;
478 $this->event_handler->run('template.process', [$post_id, $data, $importer_model, $this]);
479 }
480
481 /**
482 * Process data after record is importer.
483 *
484 * Use data that is returned from the mapper.
485 *
486 * @param int $post_id
487 * @param ParsedData $data
488 * @return void
489 */
490 public function post_process($post_id, ParsedData $data)
491 {
492 $this->event_handler->run('template.post_process', [$post_id, $data, $this]);
493 }
494
495 public function pre_process_groups(ParsedData $data)
496 {
497 // Allows virtual groups to be registered
498 $this->groups = $this->event_handler->run('template.pre_process_groups', [$this->groups, $data, $this]);
499
500 $map = $data->getData('default');
501 foreach ($this->groups as $group) {
502 $group_map = [];
503
504 foreach ($map as $field_key => $fields_map) {
505 if (preg_match('/^' . $group . '\.(.*?)$/', $field_key) === 1) {
506 $group_map[$field_key] = $fields_map;
507 }
508 }
509
510 $data->replace($group_map, $group);
511 }
512
513 return $data;
514 }
515
516 /**
517 * Process attachment fields
518 *
519 * @param int $post_id
520 * @param array $row
521 * @param string $row_prefix
522 * @param Filesystem $filesystem
523 * @param Ftp $ftp
524 * @param Attachment $attachment
525 */
526 public function process_attachment($post_id, $row, $row_prefix, $filesystem, $ftp, $attachment)
527 {
528 $delimiter = apply_filters('iwp/value_delimiter', ',');
529 $delimiter = apply_filters('iwp/attachment/value_delimiter', $delimiter);
530
531 if (isset($row[$row_prefix . 'settings._delimiter'])) {
532
533 if (strlen(trim($row[$row_prefix . 'settings._delimiter'])) === 1) {
534 $delimiter = trim($row[$row_prefix . 'settings._delimiter']);
535 }
536 } elseif (isset($row[$row_prefix . '_delimiter'])) {
537
538 if (strlen(trim($row[$row_prefix . '_delimiter'])) === 1) {
539 $delimiter = trim($row[$row_prefix . '_delimiter']);
540 }
541 }
542
543 $meta_delimiter = apply_filters('iwp/attachment/meta_delimiter', $delimiter);
544
545 $locations = isset($row[$row_prefix . 'location']) ? $row[$row_prefix . 'location'] : null;
546 $location_parts = explode($delimiter, $locations);
547 $location_parts = array_filter(array_map('trim', $location_parts));
548
549 if (isset($row[$row_prefix . 'settings._meta._title'])) {
550 $attachment_titles = explode($meta_delimiter, $row[$row_prefix . 'settings._meta._title']);
551 } elseif ($row[$row_prefix . '_meta._title']) {
552 $attachment_titles = explode($meta_delimiter, $row[$row_prefix . '_meta._title']);
553 } else {
554 $attachment_titles = null;
555 }
556
557 if (isset($row[$row_prefix . 'settings._meta._alt'])) {
558 $attachment_alts = explode($meta_delimiter, $row[$row_prefix . 'settings._meta._alt']);
559 } elseif (isset($row[$row_prefix . '_meta._alt'])) {
560 $attachment_alts = explode($meta_delimiter, $row[$row_prefix . '_meta._alt']);
561 } else {
562 $attachment_alts = null;
563 }
564
565 if (isset($row[$row_prefix . 'settings._meta._caption'])) {
566 $attachment_captions = explode($meta_delimiter, $row[$row_prefix . 'settings._meta._caption']);
567 } elseif (isset($row[$row_prefix . '_meta._caption'])) {
568 $attachment_captions = explode($meta_delimiter, $row[$row_prefix . '_meta._caption']);
569 } else {
570 $attachment_captions = null;
571 }
572
573 if (isset($row[$row_prefix . 'settings._meta._description'])) {
574 $attachment_descriptions = explode($meta_delimiter, $row[$row_prefix . 'settings._meta._description']);
575 } elseif (isset($row[$row_prefix . '_meta._description'])) {
576 $attachment_descriptions = explode($meta_delimiter, $row[$row_prefix . '_meta._description']);
577 } else {
578 $attachment_descriptions = null;
579 }
580
581 if (isset($row[$row_prefix . 'settings._enable_image_hash'])) {
582 $attachment_enable_image_hash = $row[$row_prefix . 'settings._enable_image_hash'];
583 } elseif (isset($row[$row_prefix . '_enable_image_hash'])) {
584 $attachment_enable_image_hash = $row[$row_prefix . '_enable_image_hash'];
585 } else {
586 $attachment_enable_image_hash = 'yes';
587 }
588
589 $attachment_ids = [];
590 $location_counter = 0;
591 foreach ($location_parts as $location) {
592
593 if (empty($location)) {
594 continue;
595 }
596
597 if (isset($row[$row_prefix . 'settings._download'])) {
598 $download = $row[$row_prefix . 'settings._download'];
599 } elseif (isset($row[$row_prefix . '_download'])) {
600 $download = $row[$row_prefix . '_download'];
601 } else {
602 $download = null;
603 }
604
605 if (isset($row[$row_prefix . 'settings._featured'])) {
606 $featured = $row[$row_prefix . 'settings._featured'];
607 } elseif (isset($row[$row_prefix . '_featured'])) {
608 $featured = $row[$row_prefix . '_featured'];
609 } else {
610 $featured = null;
611 }
612
613
614 $source = null;
615 $result = false;
616 $attachment_id = null;
617 $attachment_salt = '';
618
619 $location = trim($location);
620
621 switch ($download) {
622 case 'remote':
623
624 if (isset($row[$row_prefix . 'settings._remote_url'])) {
625 $base_url = $row[$row_prefix . 'settings._remote_url'];
626 } elseif (isset($row[$row_prefix . '_remote_url'])) {
627 $base_url = $row[$row_prefix . '_remote_url'];
628 } else {
629 $base_url = null;
630 }
631
632 // check if file hash is already stored
633 $source = $base_url . $location;
634 if (empty($source)) {
635 continue 2;
636 }
637
638 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
639
640 $attachment_id = 0;
641 if ($attachment_enable_image_hash == 'yes') {
642 $attachment_id = $attachment->get_attachment_by_hash($source);
643 }
644
645 if ($attachment_id <= 0) {
646 Logger::write(__CLASS__ . '::process__attachments -remote=' . $source . ' -filename=' . $custom_filename);
647 $result = $filesystem->download_file($source, null, null, $custom_filename);
648 }
649 break;
650 case 'ftp':
651
652 if (isset($row[$row_prefix . 'settings._ftp_user'])) {
653 $ftp_user = $row[$row_prefix . 'settings._ftp_user'];
654 } elseif (isset($row[$row_prefix . '_ftp_user'])) {
655 $ftp_user = $row[$row_prefix . '_ftp_user'];
656 } else {
657 $ftp_user = null;
658 }
659
660 if (isset($row[$row_prefix . 'settings._ftp_host'])) {
661 $ftp_host = $row[$row_prefix . 'settings._ftp_host'];
662 } elseif (isset($row[$row_prefix . '_ftp_host'])) {
663 $ftp_host = $row[$row_prefix . '_ftp_host'];
664 } else {
665 $ftp_host = null;
666 }
667
668 if (isset($row[$row_prefix . 'settings._ftp_pass'])) {
669 $ftp_pass = $row[$row_prefix . 'settings._ftp_pass'];
670 } elseif (isset($row[$row_prefix . '_ftp_pass'])) {
671 $ftp_pass = $row[$row_prefix . '_ftp_pass'];
672 } else {
673 $ftp_pass = null;
674 }
675
676 if (isset($row[$row_prefix . 'settings._ftp_path'])) {
677 $base_url = $row[$row_prefix . 'settings._ftp_path'];
678 } elseif (isset($row[$row_prefix . '_ftp_path'])) {
679 $base_url = $row[$row_prefix . '_ftp_path'];
680 } else {
681 $base_url = null;
682 }
683
684 // check if file hash is already stored
685 $source = $base_url . $location;
686 if (empty($source)) {
687 continue 2;
688 }
689
690 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
691
692 $attachment_id = 0;
693 if ($attachment_enable_image_hash == 'yes') {
694 $attachment_id = $attachment->get_attachment_by_hash($source);
695 }
696
697 if ($attachment_id <= 0) {
698 Logger::write(__CLASS__ . '::process__attachments -ftp=' . $source . ' -filename=' . $custom_filename);
699 $result = $ftp->download_file($source, $ftp_host, $ftp_user, $ftp_pass, $custom_filename);
700 }
701 break;
702 case 'local':
703
704 if (isset($row[$row_prefix . 'settings._local_url'])) {
705 $base_url = $row[$row_prefix . 'settings._local_url'];
706 } elseif (isset($row[$row_prefix . '_local_url'])) {
707 $base_url = $row[$row_prefix . '_local_url'];
708 } else {
709 $base_url = null;
710 }
711
712 // check if file hash is already stored
713 $source = $base_url . $location;
714 if (empty($source)) {
715 continue 2;
716 }
717
718 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
719 $attachment_salt = file_exists($source) ? md5_file($source) : '';
720 $attachment_id = 0;
721 if ($attachment_enable_image_hash == 'yes') {
722 $attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt);
723 }
724 if ($attachment_id <= 0) {
725 Logger::write(__CLASS__ . '::process__attachments -local=' . $source . ' -filename=' . $custom_filename);
726 $result = $filesystem->copy_file($source, null, $custom_filename);
727 }
728 break;
729 case 'media':
730 $source = $location;
731 if (empty($source)) {
732 continue 2;
733 }
734
735 $attachment_id = $attachment->attachment_partial_url_to_postid($source);
736 Logger::write(__CLASS__ . '::process__attachments -media=' . $source);
737
738 break;
739 }
740
741 if (isset($row[$row_prefix . 'settings._meta._enabled'])) {
742 $meta_enabled = $row[$row_prefix . 'settings._meta._enabled'] === 'yes' ? true : false;
743 } elseif (isset($row[$row_prefix . '_meta._enabled'])) {
744 $meta_enabled = $row[$row_prefix . '_meta._enabled'] === 'yes' ? true : false;
745 } else {
746 $meta_enabled = false;
747 }
748
749 // insert attachment
750 if ($attachment_id <= 0) {
751
752 if (is_wp_error($result)) {
753 Logger::write(__CLASS__ . '::process__attachments -error=' . $result->get_error_message());
754 $this->errors[] = $result;
755 continue;
756 }
757
758 if (!$result) {
759 continue;
760 }
761
762 $attachment_args = [];
763 if ($meta_enabled) {
764 $attachment_args['title'] = isset($attachment_titles[$location_counter]) ? $attachment_titles[$location_counter] : null;
765 $attachment_args['alt'] = isset($attachment_alts[$location_counter]) ? $attachment_alts[$location_counter] : null;
766 $attachment_args['caption'] = isset($attachment_captions[$location_counter]) ? $attachment_captions[$location_counter] : null;
767 $attachment_args['description'] = isset($attachment_descriptions[$location_counter]) ? $attachment_descriptions[$location_counter] : null;;
768 }
769
770 $attachment_id = $attachment->insert_attachment($post_id, $result['dest'], $result['mime'], $attachment_args);
771 if (is_wp_error($attachment_id)) {
772 Logger::write(__CLASS__ . '::process__attachments -error=' . $attachment_id->get_error_message());
773 continue;
774 }
775
776 $attachment->generate_image_sizes($attachment_id, $result['dest']);
777 $attachment->store_attachment_hash($attachment_id, $source, $attachment_salt);
778 } else {
779 // Update existing attachment meta
780 if ($meta_enabled) {
781 $post_data = [];
782
783 if (isset($attachment_titles[$location_counter])) {
784 $post_data['post_title'] = $attachment_titles[$location_counter];
785 }
786
787 if (isset($attachment_descriptions[$location_counter])) {
788 $post_data['post_content'] = $attachment_descriptions[$location_counter];
789 }
790
791 if (isset($attachment_captions[$location_counter])) {
792 $post_data['post_excerpt'] = $attachment_captions[$location_counter];
793 }
794
795 if (!empty($post_data)) {
796 $post_data['ID'] = $attachment_id;
797 wp_update_post($post_data);
798 }
799
800 if (isset($attachment_alts[$location_counter])) {
801 update_post_meta($attachment_id, '_wp_attachment_image_alt', $attachment_alts[$location_counter]);
802 }
803 }
804 }
805
806 $attachment_ids[] = $attachment_id;
807 $attachment_url = wp_get_attachment_url($attachment_id);
808 $this->_attachments[] = $attachment_url;
809
810 Logger::write(__CLASS__ . '::process__attachments -id=' . $attachment_id . ' -url=' . $attachment_url);
811
812 // set featured
813 if ('yes' === $featured && false === $this->featured_set) {
814 update_post_meta($post_id, '_thumbnail_id', $attachment_id);
815 $this->featured_set = true;
816 }
817
818 $location_counter++;
819 }
820
821 return $attachment_ids;
822 }
823
824 /**
825 * Add error message
826 *
827 * @param string|\WP_Error $error
828 * @return void
829 */
830 public function add_error($error)
831 {
832 if (!is_wp_error($error)) {
833 $error = new \WP_Error('IWP_TEMP_ERR', $error);
834 }
835
836 $this->errors[] = $error;
837 }
838
839 /**
840 * Convert fields/headings to data map
841 *
842 * @param mixed $fields
843 * @param ImporterModel $importer
844 * @return array
845 */
846 public function generate_field_map($fields, $importer)
847 {
848 return ['enabled' => [], 'map' => []];
849 }
850 }
851