PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.1
Import WP – CSV & XML Import Export for WordPress v2.8.1
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 / Exporter / Mapper / CommentMapper.php

CommentMapper.php in Import WP – CSV & XML Import Export for WordPress 2.8.1, at class/Common/Exporter/Mapper/CommentMapper.php

130 lines 4.1 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\Exporter\Mapper;
4
5 use ImportWP\Common\Exporter\MapperInterface;
6
7 class CommentMapper extends AbstractMapper implements MapperInterface
8 {
9 private $post_type;
10
11 /**
12 * @var \WP_Comment_Query
13 */
14 private $query;
15
16 public function __construct($post_type)
17 {
18 $this->post_type = $post_type;
19 }
20
21 public function get_core_fields()
22 {
23 return array(
24 'comment_ID',
25 'comment_post_ID',
26 'comment_author',
27 'comment_author_email',
28 'comment_author_url',
29 'comment_author_IP',
30 'comment_date',
31 'comment_date_gmt',
32 'comment_content',
33 'comment_karma',
34 'comment_approved',
35 'comment_agent',
36 'comment_type',
37 'comment_parent',
38 'user_id',
39 );
40 }
41
42
43 public function get_fields()
44 {
45
46 /**
47 * @var \WPDB
48 */
49 global $wpdb;
50
51 $fields = [
52 'key' => 'main',
53 'label' => 'Comment',
54 'loop' => true,
55 'fields' => [],
56 'children' => [
57 'custom_fields' => [
58 'key' => 'custom_fields',
59 'label' => 'Custom Fields',
60 'loop' => true,
61 'loop_fields' => ['meta_key', 'meta_value'],
62 'fields' => [],
63 'children' => []
64 ]
65 ]
66
67 ];
68
69 $fields['fields'] = $this->get_core_fields();
70
71 // get comment custom fields
72 $meta_fields = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT cm.meta_key FROM " . $wpdb->comments . " as c INNER JOIN " . $wpdb->commentmeta . " as cm ON c.comment_ID = cm.comment_id WHERE c.comment_post_ID IN (SELECT ID from " . $wpdb->posts . " WHERE post_type=%s)", [$this->post_type]));
73 $custom_file_fields = apply_filters('iwp/exporter/comment/custom_file_id_fields', []);
74 foreach ($meta_fields as $field) {
75 $fields['children']['custom_fields']['fields'][] = $field;
76
77 if (in_array($field, $custom_file_fields)) {
78 $fields['children']['custom_fields']['fields'][] = $field . '::id';
79 $fields['children']['custom_fields']['fields'][] = $field . '::url';
80 $fields['children']['custom_fields']['fields'][] = $field . '::title';
81 $fields['children']['custom_fields']['fields'][] = $field . '::alt';
82 $fields['children']['custom_fields']['fields'][] = $field . '::caption';
83 $fields['children']['custom_fields']['fields'][] = $field . '::description';
84 }
85 }
86
87 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/comment/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type);
88
89 return $fields;
90 }
91
92 public function have_records($exporter_id)
93 {
94 $query_args = [];
95 $query_args = apply_filters('iwp/exporter/comment_query', $query_args);
96 $query_args = apply_filters(sprintf('iwp/exporter/%d/comment_query', $exporter_id), $query_args);
97 $query_args = apply_filters('iwp/exporter/comment_query/' . $this->post_type, $query_args);
98 $query_args = apply_filters(sprintf('iwp/exporter/%d/comment_query/%s', $exporter_id, $this->post_type), $query_args);
99
100 $query_args = wp_parse_args($query_args, [
101 'post_type' => $this->post_type,
102 'fields' => 'ids'
103 ]);
104
105 $this->query = new \WP_Comment_Query($query_args);
106 $this->items = $this->query->comments;
107
108 return $this->found_records() > 0;
109 }
110
111 public function found_records()
112 {
113 return count($this->items);
114 }
115
116 public function get_records()
117 {
118 return $this->items;
119 }
120
121 public function setup($i)
122 {
123 $this->record = get_comment($this->items[$i], ARRAY_A);
124 $this->record['custom_fields'] = get_comment_meta($this->record['comment_ID']);
125 $this->record = $this->modify_custom_field_data($this->record, 'comment');
126
127 return true;
128 }
129 }
130