| @@ -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 PostMapper extends AbstractMapper implements MapperInterface |
| 10 | 11 | { |
| 11 | 12 | /** |
| @@ -53,25 +54,14 @@ | ||
| 53 | 54 | } |
| 54 | 55 | |
| 55 | 56 | public function exists(ParsedData $data) |
| 56 | 57 | { |
| 57 | - $unique_fields = ['ID', 'post_name']; | |
| 58 | - | |
| 59 | - // allow user to set unique field name, get from importer setting | |
| 60 | - $unique_field = $this->importer->getSetting('unique_field'); | |
| 61 | - if ($unique_field !== null) { | |
| 62 | - $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field; | |
| 63 | - } | |
| 64 | - | |
| 65 | - $unique_fields = $this->getUniqueIdentifiers($unique_fields); | |
| 66 | - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer); | |
| 67 | - | |
| 58 | + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data); | |
| 68 | 59 | $unique_field_found = false; |
| 69 | 60 | |
| 70 | 61 | $post_type = $this->importer->getSetting('post_type'); |
| 71 | 62 | $post_status = 'any, trash, future'; |
| 72 | 63 | |
| 73 | - $meta_args = array(); | |
| 74 | 64 | $query_args = array( |
| 75 | 65 | 'post_type' => $post_type, |
| 76 | 66 | 'post_status' => $post_status, |
| 77 | 67 | 'fields' => 'ids', |
| @@ -80,69 +70,57 @@ | ||
| 80 | 70 | 'update_post_term_cache' => false, |
| 81 | 71 | 'no_found_rows' => true, |
| 82 | 72 | ); |
| 83 | 73 | |
| 84 | - $has_unique_field = false; | |
| 74 | + if (!$has_unique_field) { | |
| 75 | + foreach ($unique_fields as $field) { | |
| 85 | 76 | |
| 86 | - foreach ($unique_fields as $field) { | |
| 77 | + // check all groups for a unique value | |
| 78 | + $unique_value = $this->find_unique_field_in_data($data, $field); | |
| 87 | 79 | |
| 88 | - // check all groups for a unique value | |
| 89 | - $unique_value = $data->getValue($field, '*'); | |
| 90 | - if (empty($unique_value)) { | |
| 91 | - $cf = $data->getData('custom_fields'); | |
| 92 | - if (!empty($cf)) { | |
| 93 | - $cf_index = intval($cf['custom_fields._index']); | |
| 94 | - if ($cf_index > 0) { | |
| 95 | - for ($i = 0; $i < $cf_index; $i++) { | |
| 96 | - $row = 'custom_fields.' . $i . '.'; | |
| 97 | - $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']); | |
| 98 | - if ($custom_field_key !== $field) { | |
| 99 | - 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; | |
| 100 | 95 | } |
| 101 | - $unique_value = $cf[$row . 'value']; | |
| 102 | - break; | |
| 103 | 96 | } |
| 97 | + } else { | |
| 98 | + $meta_args[] = array( | |
| 99 | + 'key' => $field, | |
| 100 | + 'value' => $unique_value | |
| 101 | + ); | |
| 104 | 102 | } |
| 103 | + $unique_field_found = $field; | |
| 104 | + $this->set_unique_identifier_settings($unique_field_found, $unique_value); | |
| 105 | + break; | |
| 105 | 106 | } |
| 106 | 107 | } |
| 108 | + } | |
| 107 | 109 | |
| 108 | - if (!empty($unique_value)) { | |
| 109 | - $has_unique_field = true; | |
| 110 | 110 | |
| 111 | - if (in_array($field, $this->_post_fields, true)) { | |
| 112 | 111 | |
| 113 | - if (array_key_exists($field, $this->_query_vars)) { | |
| 114 | - $query_args[$this->_query_vars[$field]] = $unique_value; | |
| 115 | - } else { | |
| 116 | - switch ($field) { | |
| 117 | - case 'post_title': | |
| 118 | - $query_args['title'] = $unique_value; | |
| 119 | - break; | |
| 120 | - default: | |
| 121 | - $query_args[$field] = $unique_value; | |
| 122 | - break; | |
| 123 | - } | |
| 124 | - } | |
| 125 | - } else { | |
| 126 | - $meta_args[] = array( | |
| 127 | - 'key' => $field, | |
| 128 | - 'value' => $unique_value | |
| 129 | - ); | |
| 130 | - } | |
| 131 | - $unique_field_found = $field; | |
| 132 | - break; | |
| 133 | - } | |
| 134 | - } | |
| 135 | 112 | |
| 136 | 113 | if (!$has_unique_field) { |
| 137 | 114 | |
| 138 | 115 | // fallback to post_title |
| 139 | 116 | $unique_value = $data->getValue('post_title'); |
| 140 | - if (empty($unique_value)) { | |
| 141 | - 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')); | |
| 142 | 119 | } |
| 143 | 120 | |
| 144 | 121 | $query_args['title'] = $unique_value; |
| 122 | + $this->set_unique_identifier_settings('title', $unique_value); | |
| 145 | 123 | } |
| 146 | 124 | |
| 147 | 125 | if (!empty($meta_args)) { |
| 148 | 126 | $query_args['meta_query'] = $meta_args; |
| @@ -147,11 +125,14 @@ | ||
| 147 | 125 | if (!empty($meta_args)) { |
| 148 | 126 | $query_args['meta_query'] = $meta_args; |
| 149 | 127 | } |
| 150 | 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 | + | |
| 151 | 132 | $query = new \WP_Query($query_args); |
| 152 | 133 | if ($query->post_count > 1) { |
| 153 | - 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))); | |
| 154 | 135 | } |
| 155 | 136 | |
| 156 | 137 | if ($query->post_count == 1) { |
| 157 | 138 | $this->ID = $query->posts[0]; |
| @@ -183,8 +164,10 @@ | ||
| 183 | 164 | |
| 184 | 165 | // set post_type |
| 185 | 166 | $post['post_type'] = $this->importer->getSetting('post_type'); |
| 186 | 167 | |
| 168 | + Logger::debug('PostMapper::insert -wp_insert_post=' . wp_json_encode($post)); | |
| 169 | + | |
| 187 | 170 | $this->ID = $this->create_post($post, $data); |
| 188 | 171 | |
| 189 | 172 | if (is_wp_error($this->ID)) { |
| 190 | 173 | throw new MapperException($this->ID->get_error_message()); |
| @@ -193,14 +176,14 @@ | ||
| 193 | 176 | // TODO: Do we merge in the custom fields, or do we process that in post_process |
| 194 | 177 | $this->template->process($this->ID, $data, $this->importer); |
| 195 | 178 | |
| 196 | 179 | $meta = array_merge($meta, $data->getData('custom_fields')); |
| 180 | + Logger::debug('PostMapper::update -meta=' . wp_json_encode($meta)); | |
| 197 | 181 | |
| 198 | 182 | // create post meta |
| 199 | 183 | if ($this->ID && !empty($meta)) { |
| 200 | 184 | foreach ($meta as $key => $value) { |
| 201 | 185 | if (is_array($value)) { |
| 202 | - $this->clear_custom_field($this->ID, $key); | |
| 203 | 186 | foreach ($value as $v) { |
| 204 | 187 | $this->add_custom_field($this->ID, $key, $v); |
| 205 | 188 | } |
| 206 | 189 | } else { |
| @@ -209,8 +192,9 @@ | ||
| 209 | 192 | } |
| 210 | 193 | } |
| 211 | 194 | |
| 212 | 195 | $this->add_version_tag(); |
| 196 | + $this->add_reference_tag($data); | |
| 213 | 197 | $this->template->post_process($this->ID, $data); |
| 214 | 198 | |
| 215 | 199 | clean_post_cache($this->ID); |
| 216 | 200 | |
| @@ -276,8 +260,10 @@ | ||
| 276 | 260 | } |
| 277 | 261 | } |
| 278 | 262 | } |
| 279 | 263 | |
| 264 | + Logger::debug('PostMapper::update -wp_update_post=' . wp_json_encode($post)); | |
| 265 | + | |
| 280 | 266 | if (!empty($post)) { |
| 281 | 267 | // update remaining |
| 282 | 268 | $post['ID'] = $this->ID; |
| 283 | 269 | $res = wp_update_post($post, true); |
| @@ -291,12 +277,14 @@ | ||
| 291 | 277 | $this->template->process($this->ID, $data, $this->importer); |
| 292 | 278 | |
| 293 | 279 | // update post meta |
| 294 | 280 | $meta = array_merge($meta, $data->getData('custom_fields')); |
| 281 | + Logger::debug('PostMapper::update -meta=' . wp_json_encode($meta)); | |
| 282 | + | |
| 295 | 283 | if (!empty($meta)) { |
| 296 | 284 | foreach ($meta as $key => $value) { |
| 285 | + $this->clear_custom_field($this->ID, $key); | |
| 297 | 286 | if (is_array($value)) { |
| 298 | - $this->clear_custom_field($this->ID, $key); | |
| 299 | 287 | foreach ($value as $v) { |
| 300 | 288 | $this->add_custom_field($this->ID, $key, $v); |
| 301 | 289 | } |
| 302 | 290 | } else { |
| @@ -305,8 +293,10 @@ | ||
| 305 | 293 | } |
| 306 | 294 | } |
| 307 | 295 | |
| 308 | 296 | $this->add_version_tag(); |
| 297 | + $this->add_reference_tag($data); | |
| 298 | + | |
| 309 | 299 | $this->template->post_process($this->ID, $data); |
| 310 | 300 | |
| 311 | 301 | // Delete trash flag once post has been updated. |
| 312 | 302 | if (!empty($trash_status)) { |
| @@ -321,19 +311,32 @@ | ||
| 321 | 311 | |
| 322 | 312 | public function get_objects_for_removal() |
| 323 | 313 | { |
| 324 | 314 | if ($this->is_session_tag_enabled()) { |
| 325 | - return $this->get_ids_without_session_tag('pt-' . $this->importer->getSetting('post_type')); | |
| 315 | + return $this->get_ids_without_session_tag('pt-' . implode('|', (array)$this->importer->getSetting('post_type'))); | |
| 326 | 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 | + | |
| 327 | 336 | $q = new \WP_Query(array( |
| 328 | 337 | 'post_type' => $this->importer->getSetting('post_type'), |
| 329 | - 'meta_query' => array( | |
| 330 | - array( | |
| 331 | - 'key' => '_iwp_session_' . $this->importer->getId(), | |
| 332 | - 'value' => $this->importer->getStatusId(), | |
| 333 | - 'compare' => '!=' | |
| 334 | - ) | |
| 335 | - ), | |
| 338 | + 'meta_query' => $meta_query, | |
| 336 | 339 | 'fields' => 'ids', |
| 337 | 340 | 'posts_per_page' => -1, |
| 338 | 341 | 'cache_results' => false, |
| 339 | 342 | 'update_post_meta_cache' => false, |
| @@ -358,12 +361,22 @@ | ||
| 358 | 361 | update_post_meta($id, '_iwp_trash_status', get_post_status($id)); |
| 359 | 362 | update_post_meta($id, '_iwp_trash_importer', $this->importer->getId()); |
| 360 | 363 | wp_trash_post($id); |
| 361 | 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 | + | |
| 362 | 375 | wp_delete_post($id, $force); |
| 363 | 376 | } |
| 364 | 377 | |
| 365 | - $this->remove_session_tag($id, 'pt-' . $this->importer->getSetting('post_type')); | |
| 378 | + $this->remove_session_tag($id, 'pt-' . implode('|', (array)$this->importer->getSetting('post_type'))); | |
| 366 | 379 | } |
| 367 | 380 | |
| 368 | 381 | /** |
| 369 | 382 | * Sort fields into post and meta array |
| @@ -416,8 +429,13 @@ | ||
| 416 | 429 | |
| 417 | 430 | add_post_meta($post_id, $key, $value); |
| 418 | 431 | } |
| 419 | 432 | |
| 433 | + public function get_custom_field($id, $key = '', $single = false) | |
| 434 | + { | |
| 435 | + return get_post_meta($id, $key, $single); | |
| 436 | + } | |
| 437 | + | |
| 420 | 438 | public function update_custom_field($post_id, $key, $value, $unique = false, $skip_permissions = false) |
| 421 | 439 | { |
| 422 | 440 | |
| 423 | 441 | $old_value = get_post_meta($post_id, $key, true); |
| @@ -443,9 +461,9 @@ | ||
| 443 | 461 | |
| 444 | 462 | public function add_version_tag() |
| 445 | 463 | { |
| 446 | 464 | if ($this->is_session_tag_enabled()) { |
| 447 | - $this->add_session_tag('pt-' . $this->importer->getSetting('post_type')); | |
| 465 | + $this->add_session_tag('pt-' . implode('|', (array)$this->importer->getSetting('post_type'))); | |
| 448 | 466 | } else { |
| 449 | 467 | update_post_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId()); |
| 450 | 468 | } |
| 451 | 469 | } |