PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.5
Import WP – CSV & XML Import Export for WordPress v2.7.5
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.5, at class/Common/Importer/Template/PostTemplate.php

989 lines 35.4 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 (isset($post_field_map['post_date'])) {
292 $post_field_map['post_date_gmt'] = get_gmt_from_date($post_field_map['post_date']);
293 }
294
295 if (true !== $this->importer->isEnabledField('post._parent')) {
296 unset($post_field_map['post_parent']);
297 }
298
299 if (true !== $this->importer->isEnabledField('post._author')) {
300 unset($post_field_map['post_author']);
301 }
302
303 if (!empty($this->virtual_fields)) {
304 foreach ($this->virtual_fields as $virtual_field) {
305 $value = $data->getValue($virtual_field);
306 if (false !== $value) {
307 $post_field_map[$virtual_field] = $value;
308 }
309 }
310 }
311
312 if ($this->importer->isEnabledField('post._author')) {
313 $post_author = $data->getValue('post._author.post_author');
314 $post_author_type = $data->getValue('post._author._author_type');
315
316 $user_id = 0;
317
318 if ($post_author_type === 'id') {
319
320 $user = get_user_by('ID', $post_author);
321 if ($user) {
322 $user_id = intval($user->ID);
323 }
324 } elseif ($post_author_type === 'login') {
325
326 $user = get_user_by('login', $post_author);
327 if ($user) {
328 $user_id = intval($user->ID);
329 }
330 } elseif ($post_author_type === 'email') {
331
332 $user = get_user_by('email', $post_author);
333 if ($user) {
334 $user_id = intval($user->ID);
335 }
336 }
337
338 if ($user_id > 0) {
339 $post_field_map['post_author'] = $user_id;
340 } else {
341 $post_field_map['post_author'] = '';
342 }
343 }
344
345 // post_name is a unique field, so generate slug from title if no slug present
346 // if (!isset($post_field_map['post_name']) || empty($post_field_map['post_name'])) {
347 // $post_field_map['post_name'] = sanitize_title($post_field_map['post_title']);
348
349 // // set flag to say slug has been generated
350 // $data->add(['post_name' => 'yes'], '_generated');
351 // }
352
353 if ($this->importer->isEnabledField('post._parent') && isset($post_field_map['post_parent'])) {
354
355 $parent_id = 0;
356 $parent_field_type = $data->getValue('post._parent._parent_type');
357
358 if ($parent_field_type === 'name' || $parent_field_type === 'slug') {
359
360 // name or slug
361 $page = get_posts(array('name' => sanitize_title($post_field_map['post_parent']), 'post_type' => $this->importer->getSetting('post_type')));
362 if ($page) {
363 $parent_id = intval($page[0]->ID);
364 }
365 } elseif ($parent_field_type === 'id') {
366
367 // ID
368 $parent_id = intval($post_field_map['post_parent']);
369 } elseif ($parent_field_type === 'column') {
370
371 // reference column
372 $temp_id = $this->get_post_by_cf('post_parent', $post_field_map['post_parent']);
373 if (intval($temp_id > 0)) {
374 $parent_id = intval($temp_id);
375 }
376 }
377
378 if ($parent_id > 0) {
379 $post_field_map['post_parent'] = $parent_id;
380 }
381 }
382
383 foreach ($post_field_map as $key => $value) {
384 $post_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
385 }
386
387 $data->replace($post_field_map, 'default');
388
389 return $data;
390 }
391
392 /**
393 * Find existing post/page/custom by reference field
394 *
395 * @param string $field Reference Field Name
396 * @param string $value Value to check against reference
397 *
398 * @return bool
399 */
400 public function get_post_by_cf($field, $value)
401 {
402
403 $query = new \WP_Query(array(
404 'post_type' => $this->importer->getSetting('post_type'),
405 'posts_per_page' => 1,
406 'fields' => 'ids',
407 'cache_results' => false,
408 'update_post_meta_cache' => false,
409 'meta_query' => array(
410 array(
411 'key' => '_iwp_ref_' . $field,
412 'value' => $value
413 )
414 ),
415 'post_status' => 'any'
416 ));
417 if ($query->have_posts()) {
418 return $query->posts[0];
419 }
420 return false;
421 }
422
423 /**
424 * Process data after record is importer.
425 *
426 * Use data that is returned from the mapper.
427 *
428 * @param int $post_id
429 * @param ParsedData $data
430 * @return void
431 */
432 public function post_process($post_id, ParsedData $data)
433 {
434 /**
435 * @var Filesystem $filesystem
436 */
437 $filesystem = Container::getInstance()->get('filesystem');
438
439 /**
440 * @var Ftp $ftp
441 */
442 $ftp = Container::getInstance()->get('ftp');
443
444 /**
445 * @var Attachment $attachment
446 */
447 $attachment = Container::getInstance()->get('attachment');
448
449 $this->featured_set = false;
450 $this->process_taxonomies($post_id, $data);
451 $this->process_attachments($post_id, $data, $filesystem, $ftp, $attachment);
452
453 parent::post_process($post_id, $data);
454 }
455
456 /**
457 * Process taxonomy group
458 *
459 * TODO: Possibly move this into parser?
460 *
461 * @param int $post_id
462 * @param ParsedData $data
463 * @return void
464 */
465 public function process_taxonomies($post_id, $data)
466 {
467 // reset list of taxonomies
468 $this->_taxonomies = [];
469
470 $group = 'taxonomies';
471 $taxonomes_data = $data->getData($group);
472 $total_rows = isset($taxonomes_data[$group . '._index']) ? intval($taxonomes_data[$group . '._index']) : 0;
473 $base_delimiter = apply_filters('iwp/value_delimiter', ',');
474 $base_delimiter = apply_filters('iwp/taxonomy/value_delimiter', $base_delimiter);
475
476 $processed_taxonomies = [];
477 $term_hierarchy = [];
478 $term_hierarchy_enabled = [];
479 $term_append = [];
480
481 // Pre-Process taxonomy data
482 for ($i = 0; $i < $total_rows; $i++) {
483
484 $prefix = $group . '.' . $i . '.';
485
486 $sub_rows = [$taxonomes_data];
487 if (isset($taxonomes_data[$prefix . 'row_base']) && !empty($taxonomes_data[$prefix . 'row_base'])) {
488 $sub_group_id = $group . '.' . $i;
489 $sub_rows = $data->getData($sub_group_id);
490 }
491
492 foreach ($sub_rows as $row) {
493 $tax = isset($row[$prefix . 'tax']) ? $row[$prefix . 'tax'] : null;
494 $terms = isset($row[$prefix . 'term']) ? $row[$prefix . 'term'] : null;
495
496 $term_append[$tax] = isset($row[$prefix . 'settings._append']) && $row[$prefix . 'settings._append'] == 'yes';
497
498 $delimiter = apply_filters('iwp/taxonomy=' . $tax . '/value_delimiter', $base_delimiter);
499
500 $hierarchy_enabled = isset($row[$prefix . 'settings._hierarchy']) && $row[$prefix . 'settings._hierarchy'] === 'yes' ? true : false;
501 $hierarchy_character = isset($row[$prefix . 'settings._hierarchy_character']) ? $row[$prefix . 'settings._hierarchy_character'] : null;
502
503 $delimiter = isset($row[$prefix . 'settings._delimiter']) && strlen(trim($row[$prefix . 'settings._delimiter'])) === 1 ? trim($row[$prefix . 'settings._delimiter']) : $delimiter;
504
505 if (empty($hierarchy_character)) {
506 $hierarchy_enabled = false;
507 }
508
509 if (!is_null($tax) && !is_null($terms)) {
510 $terms = explode($delimiter, $terms);
511 foreach ($terms as $term) {
512
513 $term = trim($term);
514 $permission_key = 'taxonomy.' . $tax; //taxonomy.category | taxonomy.post_tag
515
516 if ($data->permission()) {
517 $allowed = $data->permission()->validate([$permission_key => ''], $data->getMethod(), $group);
518 $is_allowed = isset($allowed[$permission_key]) ? true : false;
519
520 if (!$is_allowed || empty($term)) {
521 continue;
522 }
523 }
524
525 // handle taxonomies
526 if (!isset($processed_taxonomies[$tax])) {
527 $processed_taxonomies[$tax] = [];
528 }
529
530 $processed_taxonomies[$tax][] = $term;
531
532 // Handle hierarchy
533 if (!isset($term_hierarchy[$tax])) {
534 $term_hierarchy[$tax] = [];
535 }
536
537 if ($hierarchy_enabled) {
538 $hierarchy_parts = explode($hierarchy_character, $term);
539 if (count($hierarchy_parts) > 0) {
540 $hierarchy_parts = array_filter(array_map('trim', $hierarchy_parts));
541 $term_hierarchy[$tax][] = $hierarchy_parts;
542 $term_hierarchy_enabled[$tax] = true;
543 }
544 } else {
545 $term_hierarchy[$tax][] = [$term];
546 }
547 }
548 }
549 }
550 }
551
552 // TODO: Process term hierarchy
553 foreach ($term_hierarchy as $processed_tax => $term_hierarchy_list) {
554
555 // clear existing taxonomies
556 if (!isset($term_append[$processed_tax]) || !$term_append[$processed_tax]) {
557 wp_set_object_terms($post_id, null, $processed_tax);
558 }
559
560 if (empty($term_hierarchy_list)) {
561 continue;
562 }
563
564 foreach ($term_hierarchy_list as $hierarchy_list) {
565
566 $prev_term = isset($term_hierarchy_enabled[$processed_tax]) ? 0 : null;
567
568 foreach ($hierarchy_list as $term) {
569 if (!isset($this->_taxonomies[$processed_tax])) {
570 $this->_taxonomies[$processed_tax] = [];
571 }
572
573 $term_result = $this->create_or_get_taxonomy_term($post_id, $processed_tax, $term, $prev_term);
574 if ($term_result) {
575 $prev_term = $term_result->term_id;
576 $this->_taxonomies[$processed_tax][] = $term_result->name;
577 }
578 }
579 }
580 }
581 }
582
583 private function create_or_get_taxonomy_term($post_id, $tax, $term, $parent)
584 {
585
586 if (is_null($parent)) {
587
588 // we do not care about parent, just fetch first
589 $tmp_term = get_term_by('name', $term, $tax);
590 if ($tmp_term) {
591 wp_set_object_terms($post_id, $tmp_term->term_id, $tax, true);
592 return $tmp_term;
593 }
594 } else {
595 $terms = get_terms([
596 'taxonomy' => $tax,
597 'name' => $term,
598 'hide_empty' => false
599 ]);
600 if (!empty($terms)) {
601 foreach ($terms as $tmp_term) {
602 if (intval($tmp_term->parent) === intval($parent)) {
603
604 // attach term to post
605 wp_set_object_terms($post_id, $tmp_term->term_id, $tax, true);
606 return $tmp_term;
607 }
608 }
609 }
610 }
611
612 // add term
613 $term_id = wp_insert_term($term, $tax, ['parent' => $parent]);
614 if (!is_wp_error($term_id)) {
615 wp_set_object_terms($post_id, $term_id['term_id'], $tax, true);
616 return get_term($term_id['term_id'], $tax);
617 }
618
619 return false;
620 }
621
622 /**
623 * Process attachments group
624 *
625 * TODO: Possibly move this into parser?
626 *
627 * @param int $post_id
628 * @param ParsedData $data
629 * @param Filesystem $filesystem
630 * @param FTP $ftp
631 * @param Attachment $attachment
632 * @return void
633 */
634 public function process_attachments($post_id, $data, $filesystem, $ftp, $attachment, $group = 'attachments')
635 {
636 // reset list of attachments
637 $this->_attachments = [];
638 $attachment_ids = [];
639
640 $attachment_data = $data->getData($group);
641 $total_rows = isset($attachment_data[$group . '._index']) ? intval($attachment_data[$group . '._index']) : 0;
642 $skipped = 0;
643
644 for ($i = 0; $i < $total_rows; $i++) {
645
646 $permission_key = $group . '.' . $i; //attachment.0 | attachment.1
647 if ($data->permission()) {
648 $allowed = $data->permission()->validate([$permission_key => ''], $data->getMethod(), $group);
649 $is_allowed = isset($allowed[$permission_key]) ? true : false;
650 if (!$is_allowed) {
651 $skipped++;
652 continue;
653 }
654 }
655
656 // Process Attachments
657 $row_prefix = $group . '.' . $i . '.';
658
659 $sub_rows = [$attachment_data];
660 if (isset($attachment_data[$row_prefix . 'row_base']) && !empty($attachment_data[$row_prefix . 'row_base'])) {
661 $sub_group_id = $group . '.' . $i;
662 $sub_rows = $data->getData($sub_group_id);
663 }
664
665 foreach ($sub_rows as $row) {
666 $ids = $this->process_attachment($post_id, $row, $row_prefix, $filesystem, $ftp, $attachment);
667 $attachment_ids = array_merge($attachment_ids, $ids);
668 }
669 }
670
671 if ($skipped == $total_rows) {
672 return false;
673 }
674
675 return $attachment_ids;
676 }
677
678 /**
679 * Get list of posts
680 *
681 * @param ImporterModel $importer_model
682 *
683 * @return array
684 */
685 public function get_post_parent_options($importer_model)
686 {
687 $query = new \WP_Query(array(
688 'post_type' => $importer_model->getSetting('post_type'),
689 'posts_per_page' => -1,
690 'cache_results' => false,
691 'update_post_meta_cache' => false,
692 ));
693
694 if (is_wp_error($query)) {
695 return $query;
696 }
697
698 $result = [];
699 foreach ($query->posts as $post) {
700 $result[] = [
701 'value' => '' . $post->ID,
702 'label' => $post->post_title
703 ];
704 }
705
706 return $result;
707 }
708
709 /**
710 * Get list taxonomies
711 *
712 * @param ImporterModel $importer_model
713 *
714 * @return array
715 */
716 public function get_taxonomy_options($importer_model)
717 {
718 $taxonomies = get_object_taxonomies($importer_model->getSetting('post_type'), 'objects');
719
720 if (empty($taxonomies)) {
721 return [];
722 }
723
724 $result = [];
725 /**
726 * @var \WP_Taxonomy[] $taxonomies
727 */
728 foreach ($taxonomies as $key => $taxonomy) {
729 $result[] = [
730 'value' => '' . $key,
731 'label' => $taxonomy->label
732 ];
733 }
734
735 return $result;
736 }
737
738 /**
739 * Convert fields/headings to data map
740 *
741 * @param mixed $fields
742 * @param ImporterModel $importer
743 * @return array
744 */
745 public function generate_field_map($fields, $importer)
746 {
747 $result = parent::generate_field_map($fields, $importer);
748 $map = $result['map'];
749 $enabled = $result['enabled'];
750
751 $taxonomies = [];
752 $attachments = [];
753 $author = [];
754 $parent = [];
755
756 foreach ($fields as $index => $field) {
757
758 if (preg_match('/^image\.(.*?)$/', $field, $matches) === 1) {
759
760 // Capture featured image.
761 // image.id,url, title, alt, caption, description
762 if (!isset($attachments['image'])) {
763 $attachments['image'] = [
764 'map' => []
765 ];
766 }
767
768 $attachments['image']['map'][$matches[1]] = sprintf('{%d}', $index);
769 $attachments['image']['map']['featured'] = 'yes';
770 } elseif (preg_match('/^tax_([a-zA-Z0-9_]+)\.(.*?)$/', $field, $matches) === 1) {
771
772 // Capture Taxonomies.
773 // tax_{taxonomy}.name,slug,id,hierarchy::id,hierarchy::name,hierarchy::slug
774
775 $taxonomy = $matches[1];
776 if (!isset($taxonomies[$taxonomy])) {
777 $taxonomies[$taxonomy] = [
778 'map' => []
779 ];
780 }
781
782 $taxonomies[$taxonomy]['map'][$matches[2]] = sprintf('{%d}', $index);
783 } elseif (preg_match('/^author\.(.*?)$/', $field, $matches) === 1) {
784
785 // Capture author.
786 // author.ID, author.user_login,author.user_nicename,author.user_email,author.user_url,author.display_name
787 if (!isset($author['map'])) {
788 $author['map'] = [];
789 }
790
791 $author['map'][$matches[1]] = sprintf('{%d}', $index);
792 } elseif (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) {
793
794 // Capture parent
795 // parent.id,parent.name,parent.slug
796 if (!isset($parent['map'])) {
797 $parent['map'] = [];
798 }
799
800 $parent['map'][$matches[1]] = sprintf('{%d}', $index);
801 } elseif (isset($this->field_map[$field])) {
802
803 // Handle core fields
804 $field_key = $this->field_map[$field];
805 $map[$field_key] = sprintf('{%d}', $index);
806
807 if (in_array($field, $this->optional_fields)) {
808 $enabled[] = $field_key;
809 }
810
811 if (in_array($field, ['post_status', 'comment_status', 'ping_status'])) {
812 $map[$field_key . '._enable_text'] = 'yes';
813 }
814 }
815 }
816
817 $taxonomies = apply_filters('iwp/importer/generate_field_map/taxonomies', $taxonomies, $fields, $importer);
818 $attachments = apply_filters('iwp/importer/generate_field_map/attachments', $attachments, $fields, $importer);
819 $author = apply_filters('iwp/importer/generate_field_map/author', $author, $fields, $importer);
820 $parent = apply_filters('iwp/importer/generate_field_map/parent', $parent, $fields, $importer);
821
822 if (!empty($taxonomies)) {
823
824 $taxonomy_counter = 0;
825 $defaults = [
826 'row_base' => '',
827 'tax' => '',
828 'term' => '',
829 'settings._append' => 'no',
830 'settings._delimiter' => '',
831 'settings._hierarchy' => 'no',
832 'settings._hierarchy_character' => '>',
833 ];
834
835 foreach ($taxonomies as $taxonomy => $taxonomy_data) {
836 $data = [];
837
838 if (isset($taxonomy_data['map']['hierarchy::name'])) {
839 $data['term'] = $taxonomy_data['map']['hierarchy::name'];
840 $data['settings._hierarchy'] = 'yes';
841 } elseif (isset($taxonomy_data['map']['hierarchy::slug'])) {
842 $data['term'] = $taxonomy_data['map']['hierarchy::slug'];
843 $data['settings._hierarchy'] = 'yes';
844 } elseif (isset($taxonomy_data['map']['name'])) {
845 $data['term'] = $taxonomy_data['map']['name'];
846 } elseif (isset($taxonomy_data['map']['slug'])) {
847 $data['term'] = $taxonomy_data['map']['slug'];
848 } else {
849 continue;
850 }
851
852 $data['tax'] = $taxonomy;
853
854 $data = wp_parse_args($data, $defaults);
855
856 $map = array_merge($map, array_reduce(array_keys($data), function ($carry, $key) use ($data, $taxonomy_counter) {
857 $carry[sprintf('taxonomies.%d.%s', $taxonomy_counter, $key)] = $data[$key];
858 return $carry;
859 }, []));
860
861 $taxonomy_counter++;
862 }
863
864 $map['taxonomies._index'] = $taxonomy_counter;
865 }
866
867 if (!empty($attachments)) {
868
869 $attachment_counter = 0;
870 $defaults = [
871 'row_base' => '',
872 'location' => '',
873 'settings._delimiter' => '',
874 'settings._download' => 'remote',
875 'settings._enable_image_hash' => 'yes',
876 'settings._featured' => 'no',
877 'settings._ftp_host' => '',
878 'settings._ftp_pass' => '',
879 'settings._ftp_path' => '',
880 'settings._ftp_user' => '',
881 'settings._local_url' => '',
882 'settings._meta._alt' => '',
883 'settings._meta._caption' => '',
884 'settings._meta._description' => '',
885 'settings._meta._enabled' => 'no',
886 'settings._meta._title' => '',
887 'settings._remote_url' => '',
888 ];
889
890 foreach ($attachments as $attachment_data) {
891
892 $data = [];
893
894 if (!isset($attachment_data['map']['url'])) {
895 continue;
896 }
897
898 // location
899 $data['location'] = $attachment_data['map']['url'];
900
901 if (isset($attachment_data['map']['featured'])) {
902 $data['settings._featured'] = $attachment_data['map']['featured'];
903 }
904
905 // Meta
906 if (isset($attachment_data['map']['title'])) {
907 $data['settings._meta._title'] = $attachment_data['map']['title'];
908 $data['settings._meta._enabled'] = 'yes';
909 }
910 if (isset($attachment_data['map']['alt'])) {
911 $data['settings._meta._alt'] = $attachment_data['map']['alt'];
912 $data['settings._meta._enabled'] = 'yes';
913 }
914 if (isset($attachment_data['map']['description'])) {
915 $data['settings._meta._description'] = $attachment_data['map']['description'];
916 $data['settings._meta._enabled'] = 'yes';
917 }
918 if (isset($attachment_data['map']['caption'])) {
919 $data['settings._meta._caption'] = $attachment_data['map']['caption'];
920 $data['settings._meta._enabled'] = 'yes';
921 }
922
923 $data = wp_parse_args($data, $defaults);
924
925 $map = array_merge($map, array_reduce(array_keys($data), function ($carry, $key) use ($data, $attachment_counter) {
926 $carry[sprintf('attachments.%d.%s', $attachment_counter, $key)] = $data[$key];
927 return $carry;
928 }, []));
929
930 $attachment_counter++;
931 }
932
933 $map['attachments._index'] = $attachment_counter;
934 }
935
936 if (!empty($author)) {
937
938 // TODO: enable field? this is a seperate field list
939 $enabled_key = 'post._author';
940 if (isset($author['map']['user_email'])) {
941
942 $enabled[] = $enabled_key;
943 $map['post._author.post_author'] = $author['map']['user_email'];
944 $map['post._author._author_type'] = 'email';
945 } elseif (isset($author['map']['user_login'])) {
946
947 $enabled[] = $enabled_key;
948 $map['post._author.post_author'] = $author['map']['user_login'];
949 $map['post._author._author_type'] = 'login';
950 } elseif (isset($author['map']['ID'])) {
951
952 $enabled[] = $enabled_key;
953 $map['post._author.post_author'] = $author['map']['ID'];
954 $map['post._author._author_type'] = 'id';
955 }
956 }
957
958 if (!empty($parent)) {
959
960 // parent.id,parent.name,parent.slug
961 $enabled_key = 'post._parent';
962 if (isset($parent['map']['name'])) {
963
964 $enabled[] = $enabled_key;
965 $map['post._parent.parent'] = $parent['map']['name'];
966 $map['post._parent.parent._enable_text'] = 'yes';
967 $map['post._parent._parent_type'] = 'name';
968 } elseif (isset($parent['map']['slug'])) {
969
970 $enabled[] = $enabled_key;
971 $map['post._parent.parent'] = $parent['map']['slug'];
972 $map['post._parent.parent._enable_text'] = 'yes';
973 $map['post._parent._parent_type'] = 'slug';
974 } elseif (isset($parent['map']['id'])) {
975
976 $enabled[] = $enabled_key;
977 $map['post._parent.parent'] = $parent['map']['name'];
978 $map['post._parent.parent._enable_text'] = 'yes';
979 $map['post._parent._parent_type'] = 'id';
980 }
981 }
982
983 return [
984 'map' => $map,
985 'enabled' => $enabled
986 ];
987 }
988 }
989