| @@ -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 PostMapper extends AbstractMapper implements MapperInterface |
| 12 | 11 | { |
| @@ -55,25 +54,14 @@ | ||
| 55 | 54 | } |
| 56 | 55 | |
| 57 | 56 | public function exists(ParsedData $data) |
| 58 | 57 | { |
| 59 | - $unique_fields = TemplateManager::get_template_unique_fields($this->template); | |
| 60 | - | |
| 61 | - // allow user to set unique field name, get from importer setting | |
| 62 | - $unique_field = $this->importer->getSetting('unique_field'); | |
| 63 | - if ($unique_field !== null) { | |
| 64 | - $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field; | |
| 65 | - } | |
| 66 | - | |
| 67 | - $unique_fields = $this->getUniqueIdentifiers($unique_fields); | |
| 68 | - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer); | |
| 69 | - | |
| 58 | + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data); | |
| 70 | 59 | $unique_field_found = false; |
| 71 | 60 | |
| 72 | 61 | $post_type = $this->importer->getSetting('post_type'); |
| 73 | 62 | $post_status = 'any, trash, future'; |
| 74 | 63 | |
| 75 | - $meta_args = array(); | |
| 76 | 64 | $query_args = array( |
| 77 | 65 | 'post_type' => $post_type, |
| 78 | 66 | 'post_status' => $post_status, |
| 79 | 67 | 'fields' => 'ids', |
| @@ -82,69 +70,57 @@ | ||
| 82 | 70 | 'update_post_term_cache' => false, |
| 83 | 71 | 'no_found_rows' => true, |
| 84 | 72 | ); |
| 85 | 73 | |
| 86 | - $has_unique_field = false; | |
| 74 | + if (!$has_unique_field) { | |
| 75 | + foreach ($unique_fields as $field) { | |
| 87 | 76 | |
| 88 | - foreach ($unique_fields as $field) { | |
| 77 | + // check all groups for a unique value | |
| 78 | + $unique_value = $this->find_unique_field_in_data($data, $field); | |
| 89 | 79 | |
| 90 | - // check all groups for a unique value | |
| 91 | - $unique_value = $data->getValue($field, '*'); | |
| 92 | - if (empty($unique_value)) { | |
| 93 | - $cf = $data->getData('custom_fields'); | |
| 94 | - if (!empty($cf)) { | |
| 95 | - $cf_index = intval($cf['custom_fields._index']); | |
| 96 | - if ($cf_index > 0) { | |
| 97 | - for ($i = 0; $i < $cf_index; $i++) { | |
| 98 | - $row = 'custom_fields.' . $i . '.'; | |
| 99 | - $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']); | |
| 100 | - if ($custom_field_key !== $field) { | |
| 101 | - continue; | |
| 80 | + if ($this->has_identifier_value($unique_value)) { | |
| 81 | + $has_unique_field = true; | |
| 82 | + | |
| 83 | + if (in_array($field, $this->_post_fields, true)) { | |
| 84 | + | |
| 85 | + if (array_key_exists($field, $this->_query_vars)) { | |
| 86 | + $query_args[$this->_query_vars[$field]] = $unique_value; | |
| 87 | + } else { | |
| 88 | + switch ($field) { | |
| 89 | + case 'post_title': | |
| 90 | + $query_args['title'] = $unique_value; | |
| 91 | + break; | |
| 92 | + default: | |
| 93 | + $query_args[$field] = $unique_value; | |
| 94 | + break; | |
| 102 | 95 | } |
| 103 | - $unique_value = $cf[$row . 'value']; | |
| 104 | - break; | |
| 105 | 96 | } |
| 97 | + } else { | |
| 98 | + $meta_args[] = array( | |
| 99 | + 'key' => $field, | |
| 100 | + 'value' => $unique_value | |
| 101 | + ); | |
| 106 | 102 | } |
| 103 | + $unique_field_found = $field; | |
| 104 | + $this->set_unique_identifier_settings($unique_field_found, $unique_value); | |
| 105 | + break; | |
| 107 | 106 | } |
| 108 | 107 | } |
| 108 | + } | |
| 109 | 109 | |
| 110 | - if (!empty($unique_value)) { | |
| 111 | - $has_unique_field = true; | |
| 112 | 110 | |
| 113 | - if (in_array($field, $this->_post_fields, true)) { | |
| 114 | 111 | |
| 115 | - if (array_key_exists($field, $this->_query_vars)) { | |
| 116 | - $query_args[$this->_query_vars[$field]] = $unique_value; | |
| 117 | - } else { | |
| 118 | - switch ($field) { | |
| 119 | - case 'post_title': | |
| 120 | - $query_args['title'] = $unique_value; | |
| 121 | - break; | |
| 122 | - default: | |
| 123 | - $query_args[$field] = $unique_value; | |
| 124 | - break; | |
| 125 | - } | |
| 126 | - } | |
| 127 | - } else { | |
| 128 | - $meta_args[] = array( | |
| 129 | - 'key' => $field, | |
| 130 | - 'value' => $unique_value | |
| 131 | - ); | |
| 132 | - } | |
| 133 | - $unique_field_found = $field; | |
| 134 | - break; | |
| 135 | - } | |
| 136 | - } | |
| 137 | 112 | |
| 138 | 113 | if (!$has_unique_field) { |
| 139 | 114 | |
| 140 | 115 | // fallback to post_title |
| 141 | 116 | $unique_value = $data->getValue('post_title'); |
| 142 | - if (empty($unique_value)) { | |
| 143 | - throw new MapperException("No Unique fields present."); | |
| 117 | + if (!$this->has_identifier_value($unique_value) || !$this->importer->has_legacy_unique_identifier()) { | |
| 118 | + 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')); | |
| 144 | 119 | } |
| 145 | 120 | |
| 146 | 121 | $query_args['title'] = $unique_value; |
| 122 | + $this->set_unique_identifier_settings('title', $unique_value); | |
| 147 | 123 | } |
| 148 | 124 | |
| 149 | 125 | if (!empty($meta_args)) { |
| 150 | 126 | $query_args['meta_query'] = $meta_args; |
| @@ -149,11 +125,14 @@ | ||
| 149 | 125 | if (!empty($meta_args)) { |
| 150 | 126 | $query_args['meta_query'] = $meta_args; |
| 151 | 127 | } |
| 152 | 128 | |
| 129 | + $query_args = apply_filters('iwp/importer/mapper/post_exists_query', $query_args); | |
| 130 | + Logger::debug("PostMapper::exists -query=" . wp_json_encode($query_args)); | |
| 131 | + | |
| 153 | 132 | $query = new \WP_Query($query_args); |
| 154 | 133 | if ($query->post_count > 1) { |
| 155 | - throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->posts) . ")."); | |
| 134 | + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->posts))); | |
| 156 | 135 | } |
| 157 | 136 | |
| 158 | 137 | if ($query->post_count == 1) { |
| 159 | 138 | $this->ID = $query->posts[0]; |
| @@ -203,9 +182,8 @@ | ||
| 203 | 182 | // create post meta |
| 204 | 183 | if ($this->ID && !empty($meta)) { |
| 205 | 184 | foreach ($meta as $key => $value) { |
| 206 | 185 | if (is_array($value)) { |
| 207 | - $this->clear_custom_field($this->ID, $key); | |
| 208 | 186 | foreach ($value as $v) { |
| 209 | 187 | $this->add_custom_field($this->ID, $key, $v); |
| 210 | 188 | } |
| 211 | 189 | } else { |
| @@ -214,8 +192,9 @@ | ||
| 214 | 192 | } |
| 215 | 193 | } |
| 216 | 194 | |
| 217 | 195 | $this->add_version_tag(); |
| 196 | + $this->add_reference_tag($data); | |
| 218 | 197 | $this->template->post_process($this->ID, $data); |
| 219 | 198 | |
| 220 | 199 | clean_post_cache($this->ID); |
| 221 | 200 | |
| @@ -302,10 +281,10 @@ | ||
| 302 | 281 | Logger::debug('PostMapper::update -meta=' . wp_json_encode($meta)); |
| 303 | 282 | |
| 304 | 283 | if (!empty($meta)) { |
| 305 | 284 | foreach ($meta as $key => $value) { |
| 285 | + $this->clear_custom_field($this->ID, $key); | |
| 306 | 286 | if (is_array($value)) { |
| 307 | - $this->clear_custom_field($this->ID, $key); | |
| 308 | 287 | foreach ($value as $v) { |
| 309 | 288 | $this->add_custom_field($this->ID, $key, $v); |
| 310 | 289 | } |
| 311 | 290 | } else { |
| @@ -314,8 +293,10 @@ | ||
| 314 | 293 | } |
| 315 | 294 | } |
| 316 | 295 | |
| 317 | 296 | $this->add_version_tag(); |
| 297 | + $this->add_reference_tag($data); | |
| 298 | + | |
| 318 | 299 | $this->template->post_process($this->ID, $data); |
| 319 | 300 | |
| 320 | 301 | // Delete trash flag once post has been updated. |
| 321 | 302 | if (!empty($trash_status)) { |
| @@ -332,17 +313,30 @@ | ||
| 332 | 313 | { |
| 333 | 314 | if ($this->is_session_tag_enabled()) { |
| 334 | 315 | return $this->get_ids_without_session_tag('pt-' . implode('|', (array)$this->importer->getSetting('post_type'))); |
| 335 | 316 | } else { |
| 317 | + | |
| 318 | + $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId()); | |
| 319 | + if (empty($import_ids)) { | |
| 320 | + return false; | |
| 321 | + } | |
| 322 | + | |
| 323 | + $meta_query = []; | |
| 324 | + foreach ($import_ids as $import_id) { | |
| 325 | + $meta_query[] = array( | |
| 326 | + 'key' => '_iwp_session_' . $import_id, | |
| 327 | + 'value' => $this->importer->getStatusId(), | |
| 328 | + 'compare' => '!=' | |
| 329 | + ); | |
| 330 | + } | |
| 331 | + | |
| 332 | + if (count($meta_query) > 1) { | |
| 333 | + $meta_query['relation'] = 'AND'; | |
| 334 | + } | |
| 335 | + | |
| 336 | 336 | $q = new \WP_Query(array( |
| 337 | 337 | 'post_type' => $this->importer->getSetting('post_type'), |
| 338 | - 'meta_query' => array( | |
| 339 | - array( | |
| 340 | - 'key' => '_iwp_session_' . $this->importer->getId(), | |
| 341 | - 'value' => $this->importer->getStatusId(), | |
| 342 | - 'compare' => '!=' | |
| 343 | - ) | |
| 344 | - ), | |
| 338 | + 'meta_query' => $meta_query, | |
| 345 | 339 | 'fields' => 'ids', |
| 346 | 340 | 'posts_per_page' => -1, |
| 347 | 341 | 'cache_results' => false, |
| 348 | 342 | 'update_post_meta_cache' => false, |
| @@ -367,8 +361,18 @@ | ||
| 367 | 361 | update_post_meta($id, '_iwp_trash_status', get_post_status($id)); |
| 368 | 362 | update_post_meta($id, '_iwp_trash_importer', $this->importer->getId()); |
| 369 | 363 | wp_trash_post($id); |
| 370 | 364 | } else { |
| 365 | + | |
| 366 | + // Remove connected attachments | |
| 367 | + $remove_media = isset($permissions['media']) ? $permissions['media'] : false; | |
| 368 | + if ($remove_media) { | |
| 369 | + $attachments = get_attached_media('', $id); | |
| 370 | + foreach ($attachments as $attachment) { | |
| 371 | + wp_delete_attachment($attachment->ID, 'true'); | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 371 | 375 | wp_delete_post($id, $force); |
| 372 | 376 | } |
| 373 | 377 | |
| 374 | 378 | $this->remove_session_tag($id, 'pt-' . implode('|', (array)$this->importer->getSetting('post_type'))); |
| @@ -423,8 +427,13 @@ | ||
| 423 | 427 | $value = unserialize($value); |
| 424 | 428 | } |
| 425 | 429 | |
| 426 | 430 | add_post_meta($post_id, $key, $value); |
| 431 | + } | |
| 432 | + | |
| 433 | + public function get_custom_field($id, $key = '', $single = false) | |
| 434 | + { | |
| 435 | + return get_post_meta($id, $key, $single); | |
| 427 | 436 | } |
| 428 | 437 | |
| 429 | 438 | public function update_custom_field($post_id, $key, $value, $unique = false, $skip_permissions = false) |
| 430 | 439 | { |