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

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