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

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

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