PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.0
Import WP – CSV & XML Import Export for WordPress v2.8.0
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.8.0, at class/Common/Importer/Mapper/CommentMapper.php

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