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/AttachmentMapper.php +33 -44 2.9.0 → 2.15.0 View file →
@@ -6,9 +6,8 @@
6 6 use ImportWP\Common\Filesystem\Filesystem;
7 7 use ImportWP\Common\Ftp\Ftp;
8 8 use ImportWP\Common\Importer\Exception\MapperException;
9 9 use ImportWP\Common\Importer\ParsedData;
10 -use ImportWP\Common\Importer\Template\TemplateManager;
11 10 use ImportWP\Common\Util\Logger;
12 11 use ImportWP\Container;
13 12
14 13 class AttachmentMapper extends PostMapper
@@ -14,25 +13,14 @@
14 13 class AttachmentMapper extends PostMapper
15 14 {
16 15 public function exists(ParsedData $data)
17 16 {
18 - $unique_fields = TemplateManager::get_template_unique_fields($this->template);
19 -
20 - // allow user to set unique field name, get from importer setting
21 - $unique_field = $this->importer->getSetting('unique_field');
22 - if ($unique_field !== null) {
23 - $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
24 - }
25 -
26 - $unique_fields = $this->getUniqueIdentifiers($unique_fields);
27 - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
28 -
17 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
29 18 $unique_field_found = false;
30 19
31 20 $post_type = 'attachment';
32 21 $post_status = 'any, trash, future';
33 22
34 - $meta_args = array();
35 23 $query_args = array(
36 24 'post_type' => $post_type,
37 25 'post_status' => $post_status,
38 26 'fields' => 'ids',
@@ -41,10 +29,8 @@
41 29 'update_post_term_cache' => false,
42 30 'no_found_rows' => true,
43 31 );
44 32
45 - $has_unique_field = false;
46 -
47 33 foreach ($unique_fields as $field) {
48 34
49 35 if ($field === 'src') {
50 36 /**
@@ -83,8 +69,10 @@
83 69 $attachment_id = $attachment->attachment_partial_url_to_postid($source);
84 70 break;
85 71 }
86 72
73 + $this->set_unique_identifier_settings('src', $source);
74 +
87 75 if ($attachment_id > 0) {
88 76 $has_unique_field = true;
89 77 $query_args['p'] = $attachment_id;
90 78 break;
@@ -93,28 +81,11 @@
93 81 }
94 82 } else {
95 83
96 84 // check all groups for a unique value
97 - $unique_value = $data->getValue($field, '*');
98 - if (empty($unique_value)) {
99 - $cf = $data->getData('custom_fields');
100 - if (!empty($cf)) {
101 - $cf_index = intval($cf['custom_fields._index']);
102 - if ($cf_index > 0) {
103 - for ($i = 0; $i < $cf_index; $i++) {
104 - $row = 'custom_fields.' . $i . '.';
105 - $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
106 - if ($custom_field_key !== $field) {
107 - continue;
108 - }
109 - $unique_value = $cf[$row . 'value'];
110 - break;
111 - }
112 - }
113 - }
114 - }
85 + $unique_value = $this->find_unique_field_in_data($data, $field);
115 86
116 - if (!empty($unique_value)) {
87 + if ($this->has_identifier_value($unique_value)) {
117 88 $has_unique_field = true;
118 89
119 90 if (in_array($field, $this->_post_fields, true)) {
120 91
@@ -136,8 +107,9 @@
136 107 'value' => $unique_value
137 108 );
138 109 }
139 110 $unique_field_found = $field;
111 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
140 112 break;
141 113 }
142 114 }
143 115 }
@@ -142,9 +114,9 @@
142 114 }
143 115 }
144 116
145 117 if (!$has_unique_field) {
146 - throw new MapperException("No Unique fields present.");
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'));
147 119 }
148 120
149 121 if (!empty($meta_args)) {
150 122 $query_args['meta_query'] = $meta_args;
@@ -149,11 +121,13 @@
149 121 if (!empty($meta_args)) {
150 122 $query_args['meta_query'] = $meta_args;
151 123 }
152 124
125 + $query_args = apply_filters('iwp/importer/mapper/attachment_exists_query', $query_args);
126 + Logger::debug("AttachmentMapper::exists -query=" . wp_json_encode($query_args));
153 127 $query = new \WP_Query($query_args);
154 128 if ($query->post_count > 1) {
155 - throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->posts) . ").");
129 + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->posts)));
156 130 }
157 131
158 132 if ($query->post_count == 1) {
159 133 $this->ID = $query->posts[0];
@@ -216,14 +190,15 @@
216 190 }
217 191
218 192 $this->ID = $attachment_id = $this->download_attachment($data);
219 193 if (!$this->ID) {
220 - throw new MapperException("Unable to download and insert attachment");
194 + throw new MapperException(__("Unable to download and insert attachment", 'jc-importer'));
221 195 }
222 196
223 197 $this->update_post_object($fields, $data);
224 198
225 199 $this->add_version_tag();
200 + $this->add_reference_tag($data);
226 201 $this->template->post_process($this->ID, $data);
227 202
228 203 return $attachment_id;
229 204 }
@@ -241,8 +216,9 @@
241 216
242 217 $this->update_post_object($fields, $data);
243 218
244 219 $this->add_version_tag();
220 + $this->add_reference_tag($data);
245 221 $this->template->post_process($this->ID, $data);
246 222
247 223 return $attachment_id;
248 224 }
@@ -367,17 +343,30 @@
367 343 {
368 344 if ($this->is_session_tag_enabled()) {
369 345 return $this->get_ids_without_session_tag('pt-' . 'attachment');
370 346 } else {
347 +
348 + $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
349 + if (empty($import_ids)) {
350 + return false;
351 + }
352 +
353 + $meta_query = [];
354 + foreach ($import_ids as $import_id) {
355 + $meta_query[] = array(
356 + 'key' => '_iwp_session_' . $import_id,
357 + 'value' => $this->importer->getStatusId(),
358 + 'compare' => '!='
359 + );
360 + }
361 +
362 + if (count($meta_query) > 1) {
363 + $meta_query['relation'] = 'AND';
364 + }
365 +
371 366 $q = new \WP_Query(array(
372 367 'post_type' => 'attachment',
373 - 'meta_query' => array(
374 - array(
375 - 'key' => '_iwp_session_' . $this->importer->getId(),
376 - 'value' => $this->importer->getStatusId(),
377 - 'compare' => '!='
378 - )
379 - ),
368 + 'meta_query' => $meta_query,
380 369 'fields' => 'ids',
381 370 'posts_per_page' => -1,
382 371 'cache_results' => false,
383 372 'update_post_meta_cache' => false,