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