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 +42 -44 2.8.3 → 2.15.0 View file →
@@ -6,9 +6,9 @@
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;
10 +use ImportWP\Common\Util\Logger;
11 11 use ImportWP\Container;
12 12
13 13 class AttachmentMapper extends PostMapper
14 14 {
@@ -13,25 +13,14 @@
13 13 class AttachmentMapper extends PostMapper
14 14 {
15 15 public function exists(ParsedData $data)
16 16 {
17 - $unique_fields = TemplateManager::get_template_unique_fields($this->template);
18 -
19 - // allow user to set unique field name, get from importer setting
20 - $unique_field = $this->importer->getSetting('unique_field');
21 - if ($unique_field !== null) {
22 - $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
23 - }
24 -
25 - $unique_fields = $this->getUniqueIdentifiers($unique_fields);
26 - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
27 -
17 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
28 18 $unique_field_found = false;
29 19
30 20 $post_type = 'attachment';
31 21 $post_status = 'any, trash, future';
32 22
33 - $meta_args = array();
34 23 $query_args = array(
35 24 'post_type' => $post_type,
36 25 'post_status' => $post_status,
37 26 'fields' => 'ids',
@@ -40,10 +29,8 @@
40 29 'update_post_term_cache' => false,
41 30 'no_found_rows' => true,
42 31 );
43 32
44 - $has_unique_field = false;
45 -
46 33 foreach ($unique_fields as $field) {
47 34
48 35 if ($field === 'src') {
49 36 /**
@@ -82,8 +69,10 @@
82 69 $attachment_id = $attachment->attachment_partial_url_to_postid($source);
83 70 break;
84 71 }
85 72
73 + $this->set_unique_identifier_settings('src', $source);
74 +
86 75 if ($attachment_id > 0) {
87 76 $has_unique_field = true;
88 77 $query_args['p'] = $attachment_id;
89 78 break;
@@ -92,28 +81,11 @@
92 81 }
93 82 } else {
94 83
95 84 // check all groups for a unique value
96 - $unique_value = $data->getValue($field, '*');
97 - if (empty($unique_value)) {
98 - $cf = $data->getData('custom_fields');
99 - if (!empty($cf)) {
100 - $cf_index = intval($cf['custom_fields._index']);
101 - if ($cf_index > 0) {
102 - for ($i = 0; $i < $cf_index; $i++) {
103 - $row = 'custom_fields.' . $i . '.';
104 - $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
105 - if ($custom_field_key !== $field) {
106 - continue;
107 - }
108 - $unique_value = $cf[$row . 'value'];
109 - break;
110 - }
111 - }
112 - }
113 - }
85 + $unique_value = $this->find_unique_field_in_data($data, $field);
114 86
115 - if (!empty($unique_value)) {
87 + if ($this->has_identifier_value($unique_value)) {
116 88 $has_unique_field = true;
117 89
118 90 if (in_array($field, $this->_post_fields, true)) {
119 91
@@ -135,8 +107,9 @@
135 107 'value' => $unique_value
136 108 );
137 109 }
138 110 $unique_field_found = $field;
111 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
139 112 break;
140 113 }
141 114 }
142 115 }
@@ -141,9 +114,9 @@
141 114 }
142 115 }
143 116
144 117 if (!$has_unique_field) {
145 - 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'));
146 119 }
147 120
148 121 if (!empty($meta_args)) {
149 122 $query_args['meta_query'] = $meta_args;
@@ -148,11 +121,13 @@
148 121 if (!empty($meta_args)) {
149 122 $query_args['meta_query'] = $meta_args;
150 123 }
151 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));
152 127 $query = new \WP_Query($query_args);
153 128 if ($query->post_count > 1) {
154 - 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)));
155 130 }
156 131
157 132 if ($query->post_count == 1) {
158 133 $this->ID = $query->posts[0];
@@ -174,8 +149,10 @@
174 149 }
175 150
176 151 $this->sortFields($fields, $post, $meta);
177 152
153 + Logger::debug('AttachmentMapper::update_post_object -wp_update_post=' . wp_json_encode($post));
154 +
178 155 if (!empty($post)) {
179 156
180 157 $post['ID'] = $this->ID;
181 158
@@ -187,8 +164,9 @@
187 164
188 165 $this->template->process($this->ID, $data, $this->importer);
189 166
190 167 $meta = array_merge($meta, $data->getData('custom_fields'));
168 + Logger::debug('AttachmentMapper::update_post_object -meta=' . wp_json_encode($meta));
191 169
192 170 // create post meta
193 171 if ($this->ID && !empty($meta)) {
194 172 foreach ($meta as $key => $value) {
@@ -212,14 +190,15 @@
212 190 }
213 191
214 192 $this->ID = $attachment_id = $this->download_attachment($data);
215 193 if (!$this->ID) {
216 - throw new MapperException("Unable to download and insert attachment");
194 + throw new MapperException(__("Unable to download and insert attachment", 'jc-importer'));
217 195 }
218 196
219 197 $this->update_post_object($fields, $data);
220 198
221 199 $this->add_version_tag();
200 + $this->add_reference_tag($data);
222 201 $this->template->post_process($this->ID, $data);
223 202
224 203 return $attachment_id;
225 204 }
@@ -237,8 +216,9 @@
237 216
238 217 $this->update_post_object($fields, $data);
239 218
240 219 $this->add_version_tag();
220 + $this->add_reference_tag($data);
241 221 $this->template->post_process($this->ID, $data);
242 222
243 223 return $attachment_id;
244 224 }
@@ -253,8 +233,13 @@
253 233 if (!$this->importer->isEnabledField('post.file')) {
254 234 return false;
255 235 }
256 236
237 + $is_allowed = $data->permission()->validate(['file' => ''], $data->getMethod(), 'post');
238 + if (!isset($is_allowed['file'])) {
239 + return false;
240 + }
241 +
257 242 $attachment_id = intval($data->getId());
258 243
259 244 /**
260 245 * @var Attachment $attachment
@@ -358,17 +343,30 @@
358 343 {
359 344 if ($this->is_session_tag_enabled()) {
360 345 return $this->get_ids_without_session_tag('pt-' . 'attachment');
361 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 +
362 366 $q = new \WP_Query(array(
363 367 'post_type' => 'attachment',
364 - 'meta_query' => array(
365 - array(
366 - 'key' => '_iwp_session_' . $this->importer->getId(),
367 - 'value' => $this->importer->getStatusId(),
368 - 'compare' => '!='
369 - )
370 - ),
368 + 'meta_query' => $meta_query,
371 369 'fields' => 'ids',
372 370 'posts_per_page' => -1,
373 371 'cache_results' => false,
374 372 'update_post_meta_cache' => false,