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