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 / PostTemplate.php

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

985 lines 35.2 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\ParsedData;
9 use ImportWP\Common\Importer\TemplateInterface;
10 use ImportWP\Common\Model\ImporterModel;
11 use ImportWP\Common\Util\Logger;
12 use ImportWP\Container;
13 use ImportWP\EventHandler;
14
15 class PostTemplate extends Template implements TemplateInterface
16 {
17 protected $name = 'Post';
18 protected $mapper = 'post';
19 protected $field_map = [
20 'ID' => 'post.ID',
21 'post_title' => 'post.post_title',
22 'post_content' => 'post.post_content',
23 'post_excerpt' => 'post.post_excerpt',
24 'post_name' => 'post.post_name',
25 'post_status' => 'post.post_status',
26 'menu_order' => 'post.menu_order',
27 'post_password' => 'post.post_password',
28 'post_date' => 'post.post_date',
29 'comment_status' => 'post.comment_status',
30 'ping_status' => 'post.ping_status',
31 'post_parent' => 'post._parent.parent',
32 ];
33
34 protected $optional_fields = [
35 'ID',
36 'post_excerpt',
37 'post_name',
38 'post_status',
39 'menu_order',
40 'post_password',
41 'post_date',
42 'comment_status',
43 'ping_status'
44 ];
45
46 protected $_taxonomies = [];
47 protected $_attachments = [];
48
49 private $virtual_fields = [];
50
51 public function __construct(EventHandler $event_handler)
52 {
53 parent::__construct($event_handler);
54
55 $this->groups[] = 'post';
56 $this->groups[] = 'taxonomies';
57 $this->groups[] = 'attachments';
58
59 $this->default_template_options['post_type'] = 'post';
60
61 $this->field_options = array_merge($this->field_options, [
62 'post._parent.parent' => [$this, 'get_post_parent_options'],
63 'taxonomies.*.tax' => [$this, 'get_taxonomy_options'],
64 ]);
65 }
66
67 /**
68 * @param string $message
69 * @param int $id
70 * @param ParsedData $data
71 * @return $string
72 */
73 public function display_record_info($message, $id, $data)
74 {
75 $message = parent::display_record_info($message, $id, $data);
76
77 if (!empty($this->_taxonomies)) {
78 foreach ($this->_taxonomies as $tax => $terms) {
79 $message .= ', ' . $tax . ': (' . implode(', ', $terms) . ')';
80 }
81 }
82
83 if (!empty($this->_attachments)) {
84 $message .= ', Attachments: (' . implode(', ', $this->_attachments) . ')';
85 }
86
87 return $message;
88 }
89
90 public function register()
91 {
92 $groups = [];
93
94 // Post
95 $groups[] = $this->register_group('Post Fields', 'post', [
96 $this->register_field('ID', 'ID', [
97 'tooltip' => __('ID is only used to reference existing records', 'importwp')
98 ]),
99 $this->register_core_field('Title', 'post_title', [
100 'tooltip' => __('Title of the post.', 'importwp')
101 ]),
102 $this->register_core_field('Content', 'post_content', [
103 'tooltip' => __('Main WYSIWYG editor content of the post.', 'importwp')
104 ]),
105 $this->register_field('Excerpt', 'post_excerpt', [
106 'tooltip' => __('A custom short extract for the post.', 'importwp')
107 ]),
108 $this->register_field('Slug', 'post_name', [
109 'tooltip' => __('The slug is the user friendly and URL valid name of the post.', 'importwp')
110 ]),
111 $this->register_field('Status', 'post_status', [
112 'default' => 'publish',
113 'options' => [
114 ['value' => 'draft', 'label' => 'Draft'],
115 ['value' => 'publish', 'label' => 'Published'],
116 ['value' => 'pending', 'label' => 'Pending'],
117 ['value' => 'future', 'label' => 'Future'],
118 ['value' => 'private', 'label' => 'Private'],
119 ['value' => 'trash', 'label' => 'Trash']
120 ],
121 'tooltip' => __('Whether the post can accept comments. Accepts open or closed', 'importwp')
122 ]),
123 $this->register_group('Parent Settings', '_parent', [
124 $this->register_field('Parent', 'parent', [
125 'default' => '',
126 'options' => 'callback',
127 'tooltip' => __('Set this for the post it belongs to', 'importwp')
128 ]),
129 $this->register_field('Parent Field Type', '_parent_type', [
130 'default' => 'id',
131 'options' => [
132 ['value' => 'id', 'label' => 'ID'],
133 ['value' => 'slug', 'label' => 'Slug'],
134 ['value' => 'name', 'label' => 'Name'],
135 ['value' => 'column', 'label' => 'Reference Column'],
136 ],
137 'type' => 'select',
138 'tooltip' => __('Select how the parent field should be handled', 'importwp')
139 ]),
140 $this->register_field('Parent Reference Column', '_parent_ref', [
141 'condition' => ['_parent_type', '==', 'column'],
142 'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp')
143 ])
144 ]),
145 $this->register_field('Order', 'menu_order', [
146 'tooltip' => __('The order the post should be displayed in', 'importwp')
147 ]),
148 $this->register_group('Author Settings', '_author', [
149 $this->register_field('Author', 'post_author', [
150 'tooltip' => __('The user of who added this post', 'importwp')
151 ]),
152 $this->register_field('Author Field Type', '_author_type', [
153 'default' => 'id',
154 'options' => [
155 ['value' => 'id', 'label' => 'ID'],
156 ['value' => 'login', 'label' => 'Login'],
157 ['value' => 'email', 'label' => 'Email'],
158 ],
159 'tooltip' => __('Select how the author field should be handled', 'importwp')
160 ])
161 ]),
162 $this->register_field('Password', 'post_password', [
163 'tooltip' => __('The password to access the post', 'importwp')
164 ]),
165 $this->register_field('Date', 'post_date', [
166 'tooltip' => __('The date of the post , enter in the format "YYYY-MM-DD HH:ii:ss"', 'importwp')
167 ]),
168 $this->register_field('Allow Comments', 'comment_status', [
169 'options' => [
170 ['value' => '0', 'label' => 'Disabled'],
171 ['value' => '1', 'label' => 'Enabled']
172 ],
173 'default' => '0',
174 'tooltip' => __('Whether the post can accept comments', 'importwp')
175 ]),
176 $this->register_field('Allow Pingbacks', 'ping_status', [
177 'options' => [
178 ['value' => 'closed', 'label' => 'Closed'],
179 ['value' => 'open', 'label' => 'Open']
180 ],
181 'default' => 'closed',
182 'tooltip' => __('Whether the post can accept pings', 'importwp')
183 ])
184 ]);
185
186 // Taxonomies
187 $groups[] = $this->register_taxonomy_fields();
188
189 // Attachments
190 $groups[] = $this->register_attachment_fields();
191
192 return $groups;
193 }
194
195 public function register_taxonomy_fields()
196 {
197 return $this->register_group('Taxonomies', 'taxonomies', [
198 $this->register_field('Taxonomy', 'tax', [
199 'default' => 'category',
200 'options' => 'callback',
201 'tooltip' => __('Select the type of taxonomy you are importing to.', 'importwp')
202 ]),
203 $this->register_field('Terms', 'term', [
204 'tooltip' => __('Name of the taxonomy term or terms (entered as a comma seperated list).', 'importwp')
205 ]),
206 $this->register_group('Settings', 'settings', [
207 $this->register_field('Delimiter', '_delimiter', [
208 'type' => 'text',
209 'tooltip' => 'A single character used to seperate terms when listing multiple, Leave empty to use default: ,'
210 ]),
211 $this->register_field('Enable Hierarchy', '_hierarchy', [
212 'default' => 'no',
213 'options' => [
214 ['value' => 'no', 'label' => 'No'],
215 ['value' => 'yes', 'label' => 'Yes'],
216 ],
217 'type' => 'select'
218 ]),
219 $this->register_field('Hierarchy Character', '_hierarchy_character', [
220 'default' => '>',
221 'condition' => ['_hierarchy', '==', 'yes'],
222 ]),
223 $this->register_field('Append Terms', '_append', [
224 'default' => 'no',
225 'options' => [
226 ['value' => 'no', 'label' => 'No'],
227 ['value' => 'yes', 'label' => 'Yes'],
228 ],
229 'type' => 'select'
230 ]),
231 ], ['type' => 'settings'])
232 ], ['type' => 'repeatable', 'row_base' => true]);
233 }
234
235 public function register_settings()
236 {
237 }
238
239 public function register_options()
240 {
241 return [];
242 }
243
244 /**
245 * Alter fields before they are parsed
246 *
247 * @param array $fields
248 * @return array
249 */
250 public function field_map($fields)
251 {
252 if ($this->importer->isEnabledField('post._parent') && $fields['post._parent._parent_type'] === 'column' && !empty($fields["post._parent._parent_ref"])) {
253 $fields['_iwp_ref_post_parent'] = $fields["post._parent._parent_ref"];
254 $this->virtual_fields[] = '_iwp_ref_post_parent';
255 }
256 return $fields;
257 }
258
259 /**
260 * Process data before record is importer.
261 *
262 * Alter data that is passed to the mapper.
263 *
264 * @param ParsedData $data
265 * @return ParsedData
266 */
267 public function pre_process(ParsedData $data)
268 {
269 $data = parent::pre_process($data);
270
271 $post_field_map = [];
272 foreach ($this->field_map as $field_id => $field_map_key) {
273 $post_field_map[$field_id] = $data->getValue($field_map_key);
274 }
275
276 // remove fields that have not been set
277 foreach ($post_field_map as $field_key => $field_map) {
278
279 if ($field_map === false) {
280 unset($post_field_map[$field_key]);
281 continue;
282 }
283 }
284
285 foreach ($this->optional_fields as $optional_field) {
286 if (true !== $this->importer->isEnabledField('post.' . $optional_field)) {
287 unset($post_field_map[$optional_field]);
288 }
289 }
290
291 if (true !== $this->importer->isEnabledField('post._parent')) {
292 unset($post_field_map['post_parent']);
293 }
294
295 if (true !== $this->importer->isEnabledField('post._author')) {
296 unset($post_field_map['post_author']);
297 }
298
299 if (!empty($this->virtual_fields)) {
300 foreach ($this->virtual_fields as $virtual_field) {
301 $value = $data->getValue($virtual_field);
302 if (false !== $value) {
303 $post_field_map[$virtual_field] = $value;
304 }
305 }
306 }
307
308 if ($this->importer->isEnabledField('post._author')) {
309 $post_author = $data->getValue('post._author.post_author');
310 $post_author_type = $data->getValue('post._author._author_type');
311
312 $user_id = 0;
313
314 if ($post_author_type === 'id') {
315
316 $user = get_user_by('ID', $post_author);
317 if ($user) {
318 $user_id = intval($user->ID);
319 }
320 } elseif ($post_author_type === 'login') {
321
322 $user = get_user_by('login', $post_author);
323 if ($user) {
324 $user_id = intval($user->ID);
325 }
326 } elseif ($post_author_type === 'email') {
327
328 $user = get_user_by('email', $post_author);
329 if ($user) {
330 $user_id = intval($user->ID);
331 }
332 }
333
334 if ($user_id > 0) {
335 $post_field_map['post_author'] = $user_id;
336 } else {
337 $post_field_map['post_author'] = '';
338 }
339 }
340
341 // post_name is a unique field, so generate slug from title if no slug present
342 // if (!isset($post_field_map['post_name']) || empty($post_field_map['post_name'])) {
343 // $post_field_map['post_name'] = sanitize_title($post_field_map['post_title']);
344
345 // // set flag to say slug has been generated
346 // $data->add(['post_name' => 'yes'], '_generated');
347 // }
348
349 if ($this->importer->isEnabledField('post._parent') && isset($post_field_map['post_parent'])) {
350
351 $parent_id = 0;
352 $parent_field_type = $data->getValue('post._parent._parent_type');
353
354 if ($parent_field_type === 'name' || $parent_field_type === 'slug') {
355
356 // name or slug
357 $page = get_posts(array('name' => sanitize_title($post_field_map['post_parent']), 'post_type' => $this->importer->getSetting('post_type')));
358 if ($page) {
359 $parent_id = intval($page[0]->ID);
360 }
361 } elseif ($parent_field_type === 'id') {
362
363 // ID
364 $parent_id = intval($post_field_map['post_parent']);
365 } elseif ($parent_field_type === 'column') {
366
367 // reference column
368 $temp_id = $this->get_post_by_cf('post_parent', $post_field_map['post_parent']);
369 if (intval($temp_id > 0)) {
370 $parent_id = intval($temp_id);
371 }
372 }
373
374 if ($parent_id > 0) {
375 $post_field_map['post_parent'] = $parent_id;
376 }
377 }
378
379 foreach ($post_field_map as $key => $value) {
380 $post_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
381 }
382
383 $data->replace($post_field_map, 'default');
384
385 return $data;
386 }
387
388 /**
389 * Find existing post/page/custom by reference field
390 *
391 * @param string $field Reference Field Name
392 * @param string $value Value to check against reference
393 *
394 * @return bool
395 */
396 public function get_post_by_cf($field, $value)
397 {
398
399 $query = new \WP_Query(array(
400 'post_type' => $this->importer->getSetting('post_type'),
401 'posts_per_page' => 1,
402 'fields' => 'ids',
403 'cache_results' => false,
404 'update_post_meta_cache' => false,
405 'meta_query' => array(
406 array(
407 'key' => '_iwp_ref_' . $field,
408 'value' => $value
409 )
410 ),
411 'post_status' => 'any'
412 ));
413 if ($query->have_posts()) {
414 return $query->posts[0];
415 }
416 return false;
417 }
418
419 /**
420 * Process data after record is importer.
421 *
422 * Use data that is returned from the mapper.
423 *
424 * @param int $post_id
425 * @param ParsedData $data
426 * @return void
427 */
428 public function post_process($post_id, ParsedData $data)
429 {
430 /**
431 * @var Filesystem $filesystem
432 */
433 $filesystem = Container::getInstance()->get('filesystem');
434
435 /**
436 * @var Ftp $ftp
437 */
438 $ftp = Container::getInstance()->get('ftp');
439
440 /**
441 * @var Attachment $attachment
442 */
443 $attachment = Container::getInstance()->get('attachment');
444
445 $this->featured_set = false;
446 $this->process_taxonomies($post_id, $data);
447 $this->process_attachments($post_id, $data, $filesystem, $ftp, $attachment);
448
449 parent::post_process($post_id, $data);
450 }
451
452 /**
453 * Process taxonomy group
454 *
455 * TODO: Possibly move this into parser?
456 *
457 * @param int $post_id
458 * @param ParsedData $data
459 * @return void
460 */
461 public function process_taxonomies($post_id, $data)
462 {
463 // reset list of taxonomies
464 $this->_taxonomies = [];
465
466 $group = 'taxonomies';
467 $taxonomes_data = $data->getData($group);
468 $total_rows = isset($taxonomes_data[$group . '._index']) ? intval($taxonomes_data[$group . '._index']) : 0;
469 $base_delimiter = apply_filters('iwp/value_delimiter', ',');
470 $base_delimiter = apply_filters('iwp/taxonomy/value_delimiter', $base_delimiter);
471
472 $processed_taxonomies = [];
473 $term_hierarchy = [];
474 $term_hierarchy_enabled = [];
475 $term_append = [];
476
477 // Pre-Process taxonomy data
478 for ($i = 0; $i < $total_rows; $i++) {
479
480 $prefix = $group . '.' . $i . '.';
481
482 $sub_rows = [$taxonomes_data];
483 if (isset($taxonomes_data[$prefix . 'row_base']) && !empty($taxonomes_data[$prefix . 'row_base'])) {
484 $sub_group_id = $group . '.' . $i;
485 $sub_rows = $data->getData($sub_group_id);
486 }
487
488 foreach ($sub_rows as $row) {
489 $tax = isset($row[$prefix . 'tax']) ? $row[$prefix . 'tax'] : null;
490 $terms = isset($row[$prefix . 'term']) ? $row[$prefix . 'term'] : null;
491
492 $term_append[$tax] = isset($row[$prefix . 'settings._append']) && $row[$prefix . 'settings._append'] == 'yes';
493
494 $delimiter = apply_filters('iwp/taxonomy=' . $tax . '/value_delimiter', $base_delimiter);
495
496 $hierarchy_enabled = isset($row[$prefix . 'settings._hierarchy']) && $row[$prefix . 'settings._hierarchy'] === 'yes' ? true : false;
497 $hierarchy_character = isset($row[$prefix . 'settings._hierarchy_character']) ? $row[$prefix . 'settings._hierarchy_character'] : null;
498
499 $delimiter = isset($row[$prefix . 'settings._delimiter']) && strlen(trim($row[$prefix . 'settings._delimiter'])) === 1 ? trim($row[$prefix . 'settings._delimiter']) : $delimiter;
500
501 if (empty($hierarchy_character)) {
502 $hierarchy_enabled = false;
503 }
504
505 if (!is_null($tax) && !is_null($terms)) {
506 $terms = explode($delimiter, $terms);
507 foreach ($terms as $term) {
508
509 $term = trim($term);
510 $permission_key = 'taxonomy.' . $tax; //taxonomy.category | taxonomy.post_tag
511
512 if ($data->permission()) {
513 $allowed = $data->permission()->validate([$permission_key => ''], $data->getMethod(), $group);
514 $is_allowed = isset($allowed[$permission_key]) ? true : false;
515
516 if (!$is_allowed || empty($term)) {
517 continue;
518 }
519 }
520
521 // handle taxonomies
522 if (!isset($processed_taxonomies[$tax])) {
523 $processed_taxonomies[$tax] = [];
524 }
525
526 $processed_taxonomies[$tax][] = $term;
527
528 // Handle hierarchy
529 if (!isset($term_hierarchy[$tax])) {
530 $term_hierarchy[$tax] = [];
531 }
532
533 if ($hierarchy_enabled) {
534 $hierarchy_parts = explode($hierarchy_character, $term);
535 if (count($hierarchy_parts) > 0) {
536 $hierarchy_parts = array_filter(array_map('trim', $hierarchy_parts));
537 $term_hierarchy[$tax][] = $hierarchy_parts;
538 $term_hierarchy_enabled[$tax] = true;
539 }
540 } else {
541 $term_hierarchy[$tax][] = [$term];
542 }
543 }
544 }
545 }
546 }
547
548 // TODO: Process term hierarchy
549 foreach ($term_hierarchy as $processed_tax => $term_hierarchy_list) {
550
551 // clear existing taxonomies
552 if (!isset($term_append[$processed_tax]) || !$term_append[$processed_tax]) {
553 wp_set_object_terms($post_id, null, $processed_tax);
554 }
555
556 if (empty($term_hierarchy_list)) {
557 continue;
558 }
559
560 foreach ($term_hierarchy_list as $hierarchy_list) {
561
562 $prev_term = isset($term_hierarchy_enabled[$processed_tax]) ? 0 : null;
563
564 foreach ($hierarchy_list as $term) {
565 if (!isset($this->_taxonomies[$processed_tax])) {
566 $this->_taxonomies[$processed_tax] = [];
567 }
568
569 $term_result = $this->create_or_get_taxonomy_term($post_id, $processed_tax, $term, $prev_term);
570 if ($term_result) {
571 $prev_term = $term_result->term_id;
572 $this->_taxonomies[$processed_tax][] = $term_result->name;
573 }
574 }
575 }
576 }
577 }
578
579 private function create_or_get_taxonomy_term($post_id, $tax, $term, $parent)
580 {
581
582 if (is_null($parent)) {
583
584 // we do not care about parent, just fetch first
585 $tmp_term = get_term_by('name', $term, $tax);
586 if ($tmp_term) {
587 wp_set_object_terms($post_id, $tmp_term->term_id, $tax, true);
588 return $tmp_term;
589 }
590 } else {
591 $terms = get_terms([
592 'taxonomy' => $tax,
593 'name' => $term,
594 'hide_empty' => false
595 ]);
596 if (!empty($terms)) {
597 foreach ($terms as $tmp_term) {
598 if (intval($tmp_term->parent) === intval($parent)) {
599
600 // attach term to post
601 wp_set_object_terms($post_id, $tmp_term->term_id, $tax, true);
602 return $tmp_term;
603 }
604 }
605 }
606 }
607
608 // add term
609 $term_id = wp_insert_term($term, $tax, ['parent' => $parent]);
610 if (!is_wp_error($term_id)) {
611 wp_set_object_terms($post_id, $term_id['term_id'], $tax, true);
612 return get_term($term_id['term_id'], $tax);
613 }
614
615 return false;
616 }
617
618 /**
619 * Process attachments group
620 *
621 * TODO: Possibly move this into parser?
622 *
623 * @param int $post_id
624 * @param ParsedData $data
625 * @param Filesystem $filesystem
626 * @param FTP $ftp
627 * @param Attachment $attachment
628 * @return void
629 */
630 public function process_attachments($post_id, $data, $filesystem, $ftp, $attachment, $group = 'attachments')
631 {
632 // reset list of attachments
633 $this->_attachments = [];
634 $attachment_ids = [];
635
636 $attachment_data = $data->getData($group);
637 $total_rows = isset($attachment_data[$group . '._index']) ? intval($attachment_data[$group . '._index']) : 0;
638 $skipped = 0;
639
640 for ($i = 0; $i < $total_rows; $i++) {
641
642 $permission_key = $group . '.' . $i; //attachment.0 | attachment.1
643 if ($data->permission()) {
644 $allowed = $data->permission()->validate([$permission_key => ''], $data->getMethod(), $group);
645 $is_allowed = isset($allowed[$permission_key]) ? true : false;
646 if (!$is_allowed) {
647 $skipped++;
648 continue;
649 }
650 }
651
652 // Process Attachments
653 $row_prefix = $group . '.' . $i . '.';
654
655 $sub_rows = [$attachment_data];
656 if (isset($attachment_data[$row_prefix . 'row_base']) && !empty($attachment_data[$row_prefix . 'row_base'])) {
657 $sub_group_id = $group . '.' . $i;
658 $sub_rows = $data->getData($sub_group_id);
659 }
660
661 foreach ($sub_rows as $row) {
662 $ids = $this->process_attachment($post_id, $row, $row_prefix, $filesystem, $ftp, $attachment);
663 $attachment_ids = array_merge($attachment_ids, $ids);
664 }
665 }
666
667 if ($skipped == $total_rows) {
668 return false;
669 }
670
671 return $attachment_ids;
672 }
673
674 /**
675 * Get list of posts
676 *
677 * @param ImporterModel $importer_model
678 *
679 * @return array
680 */
681 public function get_post_parent_options($importer_model)
682 {
683 $query = new \WP_Query(array(
684 'post_type' => $importer_model->getSetting('post_type'),
685 'posts_per_page' => -1,
686 'cache_results' => false,
687 'update_post_meta_cache' => false,
688 ));
689
690 if (is_wp_error($query)) {
691 return $query;
692 }
693
694 $result = [];
695 foreach ($query->posts as $post) {
696 $result[] = [
697 'value' => '' . $post->ID,
698 'label' => $post->post_title
699 ];
700 }
701
702 return $result;
703 }
704
705 /**
706 * Get list taxonomies
707 *
708 * @param ImporterModel $importer_model
709 *
710 * @return array
711 */
712 public function get_taxonomy_options($importer_model)
713 {
714 $taxonomies = get_object_taxonomies($importer_model->getSetting('post_type'), 'objects');
715
716 if (empty($taxonomies)) {
717 return [];
718 }
719
720 $result = [];
721 /**
722 * @var \WP_Taxonomy[] $taxonomies
723 */
724 foreach ($taxonomies as $key => $taxonomy) {
725 $result[] = [
726 'value' => '' . $key,
727 'label' => $taxonomy->label
728 ];
729 }
730
731 return $result;
732 }
733
734 /**
735 * Convert fields/headings to data map
736 *
737 * @param mixed $fields
738 * @param ImporterModel $importer
739 * @return array
740 */
741 public function generate_field_map($fields, $importer)
742 {
743 $result = parent::generate_field_map($fields, $importer);
744 $map = $result['map'];
745 $enabled = $result['enabled'];
746
747 $taxonomies = [];
748 $attachments = [];
749 $author = [];
750 $parent = [];
751
752 foreach ($fields as $index => $field) {
753
754 if (preg_match('/^image\.(.*?)$/', $field, $matches) === 1) {
755
756 // Capture featured image.
757 // image.id,url, title, alt, caption, description
758 if (!isset($attachments['image'])) {
759 $attachments['image'] = [
760 'map' => []
761 ];
762 }
763
764 $attachments['image']['map'][$matches[1]] = sprintf('{%d}', $index);
765 $attachments['image']['map']['featured'] = 'yes';
766 } elseif (preg_match('/^tax_([a-zA-Z0-9_]+)\.(.*?)$/', $field, $matches) === 1) {
767
768 // Capture Taxonomies.
769 // tax_{taxonomy}.name,slug,id,hierarchy::id,hierarchy::name,hierarchy::slug
770
771 $taxonomy = $matches[1];
772 if (!isset($taxonomies[$taxonomy])) {
773 $taxonomies[$taxonomy] = [
774 'map' => []
775 ];
776 }
777
778 $taxonomies[$taxonomy]['map'][$matches[2]] = sprintf('{%d}', $index);
779 } elseif (preg_match('/^author\.(.*?)$/', $field, $matches) === 1) {
780
781 // Capture author.
782 // author.ID, author.user_login,author.user_nicename,author.user_email,author.user_url,author.display_name
783 if (!isset($author['map'])) {
784 $author['map'] = [];
785 }
786
787 $author['map'][$matches[1]] = sprintf('{%d}', $index);
788 } elseif (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) {
789
790 // Capture parent
791 // parent.id,parent.name,parent.slug
792 if (!isset($parent['map'])) {
793 $parent['map'] = [];
794 }
795
796 $parent['map'][$matches[1]] = sprintf('{%d}', $index);
797 } elseif (isset($this->field_map[$field])) {
798
799 // Handle core fields
800 $field_key = $this->field_map[$field];
801 $map[$field_key] = sprintf('{%d}', $index);
802
803 if (in_array($field, $this->optional_fields)) {
804 $enabled[] = $field_key;
805 }
806
807 if (in_array($field, ['post_status', 'comment_status', 'ping_status'])) {
808 $map[$field_key . '._enable_text'] = 'yes';
809 }
810 }
811 }
812
813 $taxonomies = apply_filters('iwp/importer/generate_field_map/taxonomies', $taxonomies, $fields, $importer);
814 $attachments = apply_filters('iwp/importer/generate_field_map/attachments', $attachments, $fields, $importer);
815 $author = apply_filters('iwp/importer/generate_field_map/author', $author, $fields, $importer);
816 $parent = apply_filters('iwp/importer/generate_field_map/parent', $parent, $fields, $importer);
817
818 if (!empty($taxonomies)) {
819
820 $taxonomy_counter = 0;
821 $defaults = [
822 'row_base' => '',
823 'tax' => '',
824 'term' => '',
825 'settings._append' => 'no',
826 'settings._delimiter' => '',
827 'settings._hierarchy' => 'no',
828 'settings._hierarchy_character' => '>',
829 ];
830
831 foreach ($taxonomies as $taxonomy => $taxonomy_data) {
832 $data = [];
833
834 if (isset($taxonomy_data['map']['hierarchy::name'])) {
835 $data['term'] = $taxonomy_data['map']['hierarchy::name'];
836 $data['settings._hierarchy'] = 'yes';
837 } elseif (isset($taxonomy_data['map']['hierarchy::slug'])) {
838 $data['term'] = $taxonomy_data['map']['hierarchy::slug'];
839 $data['settings._hierarchy'] = 'yes';
840 } elseif (isset($taxonomy_data['map']['name'])) {
841 $data['term'] = $taxonomy_data['map']['name'];
842 } elseif (isset($taxonomy_data['map']['slug'])) {
843 $data['term'] = $taxonomy_data['map']['slug'];
844 } else {
845 continue;
846 }
847
848 $data['tax'] = $taxonomy;
849
850 $data = wp_parse_args($data, $defaults);
851
852 $map = array_merge($map, array_reduce(array_keys($data), function ($carry, $key) use ($data, $taxonomy_counter) {
853 $carry[sprintf('taxonomies.%d.%s', $taxonomy_counter, $key)] = $data[$key];
854 return $carry;
855 }, []));
856
857 $taxonomy_counter++;
858 }
859
860 $map['taxonomies._index'] = $taxonomy_counter;
861 }
862
863 if (!empty($attachments)) {
864
865 $attachment_counter = 0;
866 $defaults = [
867 'row_base' => '',
868 'location' => '',
869 'settings._delimiter' => '',
870 'settings._download' => 'remote',
871 'settings._enable_image_hash' => 'yes',
872 'settings._featured' => 'no',
873 'settings._ftp_host' => '',
874 'settings._ftp_pass' => '',
875 'settings._ftp_path' => '',
876 'settings._ftp_user' => '',
877 'settings._local_url' => '',
878 'settings._meta._alt' => '',
879 'settings._meta._caption' => '',
880 'settings._meta._description' => '',
881 'settings._meta._enabled' => 'no',
882 'settings._meta._title' => '',
883 'settings._remote_url' => '',
884 ];
885
886 foreach ($attachments as $attachment_data) {
887
888 $data = [];
889
890 if (!isset($attachment_data['map']['url'])) {
891 continue;
892 }
893
894 // location
895 $data['location'] = $attachment_data['map']['url'];
896
897 if (isset($attachment_data['map']['featured'])) {
898 $data['settings._featured'] = $attachment_data['map']['featured'];
899 }
900
901 // Meta
902 if (isset($attachment_data['map']['title'])) {
903 $data['settings._meta._title'] = $attachment_data['map']['title'];
904 $data['settings._meta._enabled'] = 'yes';
905 }
906 if (isset($attachment_data['map']['alt'])) {
907 $data['settings._meta._alt'] = $attachment_data['map']['alt'];
908 $data['settings._meta._enabled'] = 'yes';
909 }
910 if (isset($attachment_data['map']['description'])) {
911 $data['settings._meta._description'] = $attachment_data['map']['description'];
912 $data['settings._meta._enabled'] = 'yes';
913 }
914 if (isset($attachment_data['map']['caption'])) {
915 $data['settings._meta._caption'] = $attachment_data['map']['caption'];
916 $data['settings._meta._enabled'] = 'yes';
917 }
918
919 $data = wp_parse_args($data, $defaults);
920
921 $map = array_merge($map, array_reduce(array_keys($data), function ($carry, $key) use ($data, $attachment_counter) {
922 $carry[sprintf('attachments.%d.%s', $attachment_counter, $key)] = $data[$key];
923 return $carry;
924 }, []));
925
926 $attachment_counter++;
927 }
928
929 $map['attachments._index'] = $attachment_counter;
930 }
931
932 if (!empty($author)) {
933
934 // TODO: enable field? this is a seperate field list
935 $enabled_key = 'post._author';
936 if (isset($author['map']['user_email'])) {
937
938 $enabled[] = $enabled_key;
939 $map['post._author.post_author'] = $author['map']['user_email'];
940 $map['post._author._author_type'] = 'email';
941 } elseif (isset($author['map']['user_login'])) {
942
943 $enabled[] = $enabled_key;
944 $map['post._author.post_author'] = $author['map']['user_login'];
945 $map['post._author._author_type'] = 'login';
946 } elseif (isset($author['map']['ID'])) {
947
948 $enabled[] = $enabled_key;
949 $map['post._author.post_author'] = $author['map']['ID'];
950 $map['post._author._author_type'] = 'id';
951 }
952 }
953
954 if (!empty($parent)) {
955
956 // parent.id,parent.name,parent.slug
957 $enabled_key = 'post._parent';
958 if (isset($parent['map']['name'])) {
959
960 $enabled[] = $enabled_key;
961 $map['post._parent.parent'] = $parent['map']['name'];
962 $map['post._parent.parent._enable_text'] = 'yes';
963 $map['post._parent._parent_type'] = 'name';
964 } elseif (isset($parent['map']['slug'])) {
965
966 $enabled[] = $enabled_key;
967 $map['post._parent.parent'] = $parent['map']['slug'];
968 $map['post._parent.parent._enable_text'] = 'yes';
969 $map['post._parent._parent_type'] = 'slug';
970 } elseif (isset($parent['map']['id'])) {
971
972 $enabled[] = $enabled_key;
973 $map['post._parent.parent'] = $parent['map']['name'];
974 $map['post._parent.parent._enable_text'] = 'yes';
975 $map['post._parent._parent_type'] = 'id';
976 }
977 }
978
979 return [
980 'map' => $map,
981 'enabled' => $enabled
982 ];
983 }
984 }
985