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

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

344 lines 9.9 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\Mapper;
4
5 use ImportWP\Common\Importer\Exception\MapperException;
6 use ImportWP\Common\Importer\MapperInterface;
7 use ImportWP\Common\Importer\ParsedData;
8 use ImportWP\Common\Util\Logger;
9
10 class CommentMapper extends AbstractMapper implements MapperInterface
11 {
12 protected $_core_fields = [
13 'comment_ID',
14 'comment_agent',
15 'comment_approved',
16 'comment_author',
17 'comment_author_email',
18 'comment_author_IP',
19 'comment_author_url',
20 'comment_content',
21 'comment_date',
22 'comment_date_gmt',
23 'comment_karma',
24 'comment_parent',
25 'comment_post_ID',
26 'comment_type',
27 'user_id',
28 ];
29
30 public function setup()
31 {
32 }
33
34 public function teardown()
35 {
36 }
37
38 public function exists(ParsedData $data)
39 {
40 list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
41
42 $query_args = [
43 'fields' => 'ids',
44 'update_comment_meta_cache' => false,
45 'update_comment_post_cache' => false,
46 'no_found_rows' => true,
47 ];
48
49 $unique_field_found = false;
50
51 if (!$has_unique_field) {
52 foreach ($unique_fields as $field) {
53
54 // check all groups for a unique value
55 $unique_value = $this->find_unique_field_in_data($data, $field);
56
57 if (!empty($unique_value)) {
58 $has_unique_field = true;
59
60 if (in_array($field, $this->_core_fields, true)) {
61
62 switch ($field) {
63 case 'comment_ID':
64 $query_args['comment__in'] = [intval($unique_value)];
65 break;
66 }
67 } else {
68 $meta_args[] = array(
69 'key' => $field,
70 'value' => $unique_value
71 );
72 }
73 $unique_field_found = $field;
74 $this->set_unique_identifier_settings($unique_field_found, $unique_value);
75 break;
76 }
77 }
78 }
79
80 if (!$has_unique_field) {
81 throw new MapperException(__("No Unique fields present.", 'jc-importer'));
82 }
83
84 if (!empty($meta_args)) {
85 $query_args['meta_query'] = $meta_args;
86 }
87
88 $query_args = apply_filters('iwp/importer/mapper/comment_exists_query', $query_args);
89 Logger::debug("CommentMapper::exists -query=" . wp_json_encode($query_args));
90 $query = new \WP_Comment_Query($query_args);
91
92 // $query->found_comments doesnt work when using field ids
93 if (count($query->comments) > 1) {
94 throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->comments)));
95 }
96
97 if (count($query->comments) == 1) {
98 $this->ID = $query->comments[0];
99 return $this->ID;
100 }
101
102 return false;
103 }
104
105 /**
106 * Sort fields into comment and meta array
107 *
108 * @param array $fields list of fields
109 * @param array $post comment_data pointer array
110 * @param array $meta comment_meta pointer array
111 *
112 * @return void
113 */
114 function sortFields($fields = array(), &$comment = array(), &$meta = array())
115 {
116
117 foreach ($fields as $id => $value) {
118
119 if (in_array($id, $this->_core_fields, true)) {
120
121 // comment field
122 $comment[$id] = $value;
123 } else {
124
125 // meta field
126 $meta[$id] = $value;
127 }
128 }
129 }
130
131 public function insert(ParsedData $data)
132 {
133 $fields = $data->getData('default');
134
135 $comment = array();
136 $meta = array();
137
138 // we dont import the comment_ID
139 if (isset($fields['comment_ID'])) {
140 unset($fields['comment_ID']);
141 }
142
143 $this->sortFields($fields, $comment, $meta);
144
145 Logger::debug('CommentMapper::insert -wp_insert_comment=' . wp_json_encode($comment));
146 $this->ID = wp_insert_comment($comment);
147 if (!$this->ID) {
148 throw new MapperException(__("Unable to insert comment", 'jc-importer'));
149 }
150
151 $this->template->process($this->ID, $data, $this->importer);
152
153 $meta = array_merge($meta, $data->getData('custom_fields'));
154 Logger::debug('CommentMapper::insert -meta=' . wp_json_encode($meta));
155
156 // create meta
157 if ($this->ID && !empty($meta)) {
158 foreach ($meta as $key => $value) {
159 if (is_array($value)) {
160 $this->clear_custom_field($this->ID, $key);
161 foreach ($value as $v) {
162 $this->add_custom_field($this->ID, $key, $v);
163 }
164 } else {
165 $this->update_custom_field($this->ID, $key, $value);
166 }
167 }
168 }
169
170 $this->add_version_tag();
171 $this->add_reference_tag($data);
172 $this->template->post_process($this->ID, $data);
173
174 clean_comment_cache($this->ID);
175
176 return $this->ID;
177 }
178
179 public function update(ParsedData $data)
180 {
181 $fields = $data->getData('default');
182
183 $comment = array();
184 $meta = array();
185
186 // we dont import the comment_ID
187 if (isset($fields['comment_ID'])) {
188 unset($fields['comment_ID']);
189 }
190
191 $this->sortFields($fields, $comment, $meta);
192 if (!$this->ID) {
193 return false;
194 }
195
196 Logger::debug('CommentMapper::update -wp_update_comment=' . wp_json_encode($comment));
197
198 if (!empty($comment)) {
199 $comment['comment_ID'] = $this->ID;
200 $result = wp_update_comment($comment, true);
201 if (is_wp_error($result)) {
202 throw new MapperException($result->get_error_message());
203 }
204 }
205
206 $this->template->process($this->ID, $data, $this->importer);
207
208 // update post meta
209 $meta = array_merge($meta, $data->getData('custom_fields'));
210 Logger::debug('CommentMapper::update -meta=' . wp_json_encode($meta));
211
212 if (!empty($meta)) {
213 foreach ($meta as $key => $value) {
214 if (is_array($value)) {
215 $this->clear_custom_field($this->ID, $key);
216 foreach ($value as $v) {
217 $this->add_custom_field($this->ID, $key, $v);
218 }
219 } else {
220 $this->update_custom_field($this->ID, $key, $value);
221 }
222 }
223 }
224
225 $this->add_version_tag();
226 $this->add_reference_tag($data);
227 $this->template->post_process($this->ID, $data);
228
229 clean_comment_cache($this->ID);
230
231 return $this->ID;
232 }
233
234 public function get_objects_for_removal()
235 {
236 if ($this->is_session_tag_enabled()) {
237 return $this->get_ids_without_session_tag('comment');
238 } else {
239
240 $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
241 if (empty($import_ids)) {
242 return false;
243 }
244
245 $meta_query = [];
246 foreach ($import_ids as $import_id) {
247 $meta_query[] = [
248 'key' => '_iwp_session_' . $import_id,
249 'value' => $this->importer->getStatusId(),
250 'compare' => '!='
251 ];
252 }
253
254 if (count($meta_query) > 1) {
255 $meta_query['relation'] = 'AND';
256 }
257
258 $q = new \WP_Comment_Query(array(
259 'meta_query' => $meta_query,
260 'fields' => 'ids',
261 'update_comment_meta_cache' => false,
262 'update_comment_post_cache' => false,
263 'no_found_rows' => true,
264 ));
265
266 if ($q->have_posts()) {
267 return $q->posts;
268 }
269 }
270
271 return false;
272 }
273
274 public function delete($id)
275 {
276 wp_delete_comment($id);
277 $this->remove_session_tag($id, 'comment');
278 }
279
280 /**
281 * Add custom field, allow for multiple records using the same key
282 *
283 * @param int $id
284 * @param string $key
285 * @param string $value
286 * @return void
287 */
288 public function add_custom_field($id, $key, $value)
289 {
290 // Stop double serialization
291 if (is_serialized($value)) {
292 $value = unserialize($value);
293 }
294
295 add_comment_meta($id, $key, $value);
296 }
297
298 public function get_custom_field($id, $key = '', $single = false)
299 {
300 return get_comment_meta($id, $key, $single);
301 }
302
303 public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
304 {
305
306 $old_value = get_comment_meta($id, $key, true);
307
308 // Stop double serialization
309 if (is_serialized($value)) {
310 $value = unserialize($value);
311 }
312
313 // check if new value
314 if ($old_value === $value) {
315 return;
316 }
317
318 if ($value !== '' && '' == $old_value) {
319 add_comment_meta($id, $key, $value, $unique);
320 } elseif ($value !== '' && $value !== $old_value) {
321 update_comment_meta($id, $key, $value);
322 } elseif ('' === $value && $old_value) {
323 delete_comment_meta($id, $key, $value);
324 }
325 }
326
327 /**
328 * Clear all meta before adding custom field
329 */
330 public function clear_custom_field($id, $key)
331 {
332 delete_comment_meta($id, $key);
333 }
334
335 public function add_version_tag()
336 {
337 if ($this->is_session_tag_enabled()) {
338 $this->add_session_tag('comment');
339 } else {
340 update_comment_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
341 }
342 }
343 }
344