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