PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.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
← All changes | class/Common/Importer/Mapper/CommentMapper.php +60 -55 2.8.02.15.0 View file →
@@ -4,8 +4,9 @@
4 4
5 5 use ImportWP\Common\Importer\Exception\MapperException;
6 6 use ImportWP\Common\Importer\MapperInterface;
7 7 use ImportWP\Common\Importer\ParsedData;
8 +use ImportWP\Common\Util\Logger;
8 9
9 10 class CommentMapper extends AbstractMapper implements MapperInterface
10 11 {
11 12 protected $_core_fields = [
@@ -35,19 +36,10 @@
35 36 }
36 37
37 38 public function exists(ParsedData $data)
38 39 {
39 - $unique_fields = ['comment_ID', '_iwp_ref_id'];
40 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
40 41
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 42 $query_args = [
51 43 'fields' => 'ids',
52 44 'update_comment_meta_cache' => false,
53 45 'update_comment_post_cache' => false,
@@ -54,55 +46,40 @@
54 46 'no_found_rows' => true,
55 47 ];
56 48
57 49 $unique_field_found = false;
58 - $has_unique_field = false;
59 50
60 - foreach ($unique_fields as $field) {
51 + if (!$has_unique_field) {
52 + foreach ($unique_fields as $field) {
61 53
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 - }
54 + // check all groups for a unique value
55 + $unique_value = $this->find_unique_field_in_data($data, $field);
81 56
82 - if (!empty($unique_value)) {
83 - $has_unique_field = true;
57 + if ($this->has_identifier_value($unique_value)) {
58 + $has_unique_field = true;
84 59
85 - if (in_array($field, $this->_core_fields, true)) {
60 + if (in_array($field, $this->_core_fields, true)) {
86 61
87 - switch ($field) {
88 - case 'comment_ID':
89 - $query_args['comment__in'] = [intval($unique_value)];
90 - break;
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 + );
91 72 }
92 - } else {
93 - $meta_args[] = array(
94 - 'key' => $field,
95 - 'value' => $unique_value
96 - );
73 + $unique_field_found = $field;
74 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
75 + break;
97 76 }
98 - $unique_field_found = $field;
99 - break;
100 77 }
101 78 }
102 79
103 80 if (!$has_unique_field) {
104 - throw new MapperException("No Unique fields present.");
81 + throw new MapperException(__('No unique identifier value present. Check that Permissions has a unique identifier set and this row has a non-empty value. File column references are stored as _iwp_ref_uid.', 'jc-importer'));
105 82 }
106 83
107 84 if (!empty($meta_args)) {
108 85 $query_args['meta_query'] = $meta_args;
@@ -107,13 +84,15 @@
107 84 if (!empty($meta_args)) {
108 85 $query_args['meta_query'] = $meta_args;
109 86 }
110 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));
111 90 $query = new \WP_Comment_Query($query_args);
112 91
113 92 // $query->found_comments doesnt work when using field ids
114 93 if (count($query->comments) > 1) {
115 - throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->comments) . ").");
94 + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->comments)));
116 95 }
117 96
118 97 if (count($query->comments) == 1) {
119 98 $this->ID = $query->comments[0];
@@ -162,16 +141,18 @@
162 141 }
163 142
164 143 $this->sortFields($fields, $comment, $meta);
165 144
145 + Logger::debug('CommentMapper::insert -wp_insert_comment=' . wp_json_encode($comment));
166 146 $this->ID = wp_insert_comment($comment);
167 147 if (!$this->ID) {
168 - throw new MapperException("Unable to insert comment");
148 + throw new MapperException(__("Unable to insert comment", 'jc-importer'));
169 149 }
170 150
171 151 $this->template->process($this->ID, $data, $this->importer);
172 152
173 153 $meta = array_merge($meta, $data->getData('custom_fields'));
154 + Logger::debug('CommentMapper::insert -meta=' . wp_json_encode($meta));
174 155
175 156 // create meta
176 157 if ($this->ID && !empty($meta)) {
177 158 foreach ($meta as $key => $value) {
@@ -186,8 +167,9 @@
186 167 }
187 168 }
188 169
189 170 $this->add_version_tag();
171 + $this->add_reference_tag($data);
190 172 $this->template->post_process($this->ID, $data);
191 173
192 174 clean_comment_cache($this->ID);
193 175
@@ -210,8 +192,10 @@
210 192 if (!$this->ID) {
211 193 return false;
212 194 }
213 195
196 + Logger::debug('CommentMapper::update -wp_update_comment=' . wp_json_encode($comment));
197 +
214 198 if (!empty($comment)) {
215 199 $comment['comment_ID'] = $this->ID;
216 200 $result = wp_update_comment($comment, true);
217 201 if (is_wp_error($result)) {
@@ -222,8 +206,10 @@
222 206 $this->template->process($this->ID, $data, $this->importer);
223 207
224 208 // update post meta
225 209 $meta = array_merge($meta, $data->getData('custom_fields'));
210 + Logger::debug('CommentMapper::update -meta=' . wp_json_encode($meta));
211 +
226 212 if (!empty($meta)) {
227 213 foreach ($meta as $key => $value) {
228 214 if (is_array($value)) {
229 215 $this->clear_custom_field($this->ID, $key);
@@ -236,8 +222,9 @@
236 222 }
237 223 }
238 224
239 225 $this->add_version_tag();
226 + $this->add_reference_tag($data);
240 227 $this->template->post_process($this->ID, $data);
241 228
242 229 clean_comment_cache($this->ID);
243 230
@@ -248,16 +235,29 @@
248 235 {
249 236 if ($this->is_session_tag_enabled()) {
250 237 return $this->get_ids_without_session_tag('comment');
251 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 +
252 258 $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 - ),
259 + 'meta_query' => $meta_query,
260 260 'fields' => 'ids',
261 261 'update_comment_meta_cache' => false,
262 262 'update_comment_post_cache' => false,
263 263 'no_found_rows' => true,
@@ -292,8 +292,13 @@
292 292 $value = unserialize($value);
293 293 }
294 294
295 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);
296 301 }
297 302
298 303 public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
299 304 {