| @@ -4,9 +4,8 @@ | ||
| 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\Importer\Template\TemplateManager; | |
| 9 | 8 | use ImportWP\Common\Util\Logger; |
| 10 | 9 | |
| 11 | 10 | class CommentMapper extends AbstractMapper implements MapperInterface |
| 12 | 11 | { |
| @@ -37,19 +36,10 @@ | ||
| 37 | 36 | } |
| 38 | 37 | |
| 39 | 38 | public function exists(ParsedData $data) |
| 40 | 39 | { |
| 41 | - $unique_fields = TemplateManager::get_template_unique_fields($this->template); | |
| 40 | + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data); | |
| 42 | 41 | |
| 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 | 42 | $query_args = [ |
| 53 | 43 | 'fields' => 'ids', |
| 54 | 44 | 'update_comment_meta_cache' => false, |
| 55 | 45 | 'update_comment_post_cache' => false, |
| @@ -56,55 +46,40 @@ | ||
| 56 | 46 | 'no_found_rows' => true, |
| 57 | 47 | ]; |
| 58 | 48 | |
| 59 | 49 | $unique_field_found = false; |
| 60 | - $has_unique_field = false; | |
| 61 | 50 | |
| 62 | - foreach ($unique_fields as $field) { | |
| 51 | + if (!$has_unique_field) { | |
| 52 | + foreach ($unique_fields as $field) { | |
| 63 | 53 | |
| 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 | - } | |
| 54 | + // check all groups for a unique value | |
| 55 | + $unique_value = $this->find_unique_field_in_data($data, $field); | |
| 83 | 56 | |
| 84 | - if (!empty($unique_value)) { | |
| 85 | - $has_unique_field = true; | |
| 57 | + if ($this->has_identifier_value($unique_value)) { | |
| 58 | + $has_unique_field = true; | |
| 86 | 59 | |
| 87 | - if (in_array($field, $this->_core_fields, true)) { | |
| 60 | + if (in_array($field, $this->_core_fields, true)) { | |
| 88 | 61 | |
| 89 | - switch ($field) { | |
| 90 | - case 'comment_ID': | |
| 91 | - $query_args['comment__in'] = [intval($unique_value)]; | |
| 92 | - 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 | + ); | |
| 93 | 72 | } |
| 94 | - } else { | |
| 95 | - $meta_args[] = array( | |
| 96 | - 'key' => $field, | |
| 97 | - 'value' => $unique_value | |
| 98 | - ); | |
| 73 | + $unique_field_found = $field; | |
| 74 | + $this->set_unique_identifier_settings($unique_field_found, $unique_value); | |
| 75 | + break; | |
| 99 | 76 | } |
| 100 | - $unique_field_found = $field; | |
| 101 | - break; | |
| 102 | 77 | } |
| 103 | 78 | } |
| 104 | 79 | |
| 105 | 80 | if (!$has_unique_field) { |
| 106 | - 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')); | |
| 107 | 82 | } |
| 108 | 83 | |
| 109 | 84 | if (!empty($meta_args)) { |
| 110 | 85 | $query_args['meta_query'] = $meta_args; |
| @@ -109,13 +84,15 @@ | ||
| 109 | 84 | if (!empty($meta_args)) { |
| 110 | 85 | $query_args['meta_query'] = $meta_args; |
| 111 | 86 | } |
| 112 | 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)); | |
| 113 | 90 | $query = new \WP_Comment_Query($query_args); |
| 114 | 91 | |
| 115 | 92 | // $query->found_comments doesnt work when using field ids |
| 116 | 93 | if (count($query->comments) > 1) { |
| 117 | - 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))); | |
| 118 | 95 | } |
| 119 | 96 | |
| 120 | 97 | if (count($query->comments) == 1) { |
| 121 | 98 | $this->ID = $query->comments[0]; |
| @@ -167,9 +144,9 @@ | ||
| 167 | 144 | |
| 168 | 145 | Logger::debug('CommentMapper::insert -wp_insert_comment=' . wp_json_encode($comment)); |
| 169 | 146 | $this->ID = wp_insert_comment($comment); |
| 170 | 147 | if (!$this->ID) { |
| 171 | - throw new MapperException("Unable to insert comment"); | |
| 148 | + throw new MapperException(__("Unable to insert comment", 'jc-importer')); | |
| 172 | 149 | } |
| 173 | 150 | |
| 174 | 151 | $this->template->process($this->ID, $data, $this->importer); |
| 175 | 152 | |
| @@ -190,8 +167,9 @@ | ||
| 190 | 167 | } |
| 191 | 168 | } |
| 192 | 169 | |
| 193 | 170 | $this->add_version_tag(); |
| 171 | + $this->add_reference_tag($data); | |
| 194 | 172 | $this->template->post_process($this->ID, $data); |
| 195 | 173 | |
| 196 | 174 | clean_comment_cache($this->ID); |
| 197 | 175 | |
| @@ -244,8 +222,9 @@ | ||
| 244 | 222 | } |
| 245 | 223 | } |
| 246 | 224 | |
| 247 | 225 | $this->add_version_tag(); |
| 226 | + $this->add_reference_tag($data); | |
| 248 | 227 | $this->template->post_process($this->ID, $data); |
| 249 | 228 | |
| 250 | 229 | clean_comment_cache($this->ID); |
| 251 | 230 | |
| @@ -256,16 +235,29 @@ | ||
| 256 | 235 | { |
| 257 | 236 | if ($this->is_session_tag_enabled()) { |
| 258 | 237 | return $this->get_ids_without_session_tag('comment'); |
| 259 | 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 | + | |
| 260 | 258 | $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 | - ), | |
| 259 | + 'meta_query' => $meta_query, | |
| 268 | 260 | 'fields' => 'ids', |
| 269 | 261 | 'update_comment_meta_cache' => false, |
| 270 | 262 | 'update_comment_post_cache' => false, |
| 271 | 263 | 'no_found_rows' => true, |
| @@ -300,8 +292,13 @@ | ||
| 300 | 292 | $value = unserialize($value); |
| 301 | 293 | } |
| 302 | 294 | |
| 303 | 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); | |
| 304 | 301 | } |
| 305 | 302 | |
| 306 | 303 | public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false) |
| 307 | 304 | { |