PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.1
Import WP – CSV & XML Import Export for WordPress v2.8.1
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
jc-importer / class / Common / Importer / Mapper / AttachmentMapper.php

AttachmentMapper.php in Import WP – CSV & XML Import Export for WordPress 2.8.1, at class/Common/Importer/Mapper/AttachmentMapper.php

411 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\Mapper;
4
5 use ImportWP\Common\Attachment\Attachment;
6 use ImportWP\Common\Filesystem\Filesystem;
7 use ImportWP\Common\Ftp\Ftp;
8 use ImportWP\Common\Importer\Exception\MapperException;
9 use ImportWP\Common\Importer\ParsedData;
10 use ImportWP\Container;
11
12 class AttachmentMapper extends PostMapper
13 {
14 public function exists(ParsedData $data)
15 {
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
27 $unique_field_found = false;
28
29 $post_type = 'attachment';
30 $post_status = 'any, trash, future';
31
32 $meta_args = array();
33 $query_args = array(
34 'post_type' => $post_type,
35 'post_status' => $post_status,
36 'fields' => 'ids',
37 'cache_results' => false,
38 'update_post_meta_cache' => false,
39 'update_post_term_cache' => false,
40 'no_found_rows' => true,
41 );
42
43 $has_unique_field = false;
44
45 foreach ($unique_fields as $field) {
46
47 if ($field === 'src') {
48 /**
49 * @var Attachment $attachment
50 */
51 $attachment = Container::getInstance()->get('attachment');
52
53 $location = $data->getValue('post.file.location', 'post');
54 $download = $data->getValue('post.file.settings._download', 'post');
55 $ftp_path = $data->getValue('post.file.settings._ftp_path', 'post');
56 $remote_url = $data->getValue('post.file.settings._remote_url', 'post');
57 $local_url = $data->getValue('post.file.settings._local_url', 'post');
58
59 if (empty($location)) {
60 continue;
61 }
62
63 $attachment_id = null;
64
65 switch ($download) {
66 case 'remote':
67 $source = $remote_url . $location;
68 $attachment_id = $attachment->get_attachment_by_hash($source);
69 break;
70 case 'ftp':
71 $source = $ftp_path . $location;
72 $attachment_id = $attachment->get_attachment_by_hash($source);
73 break;
74 case 'local':
75 $source = $local_url . $location;
76 $attachment_salt = file_exists($source) ? md5_file($source) : '';
77 $attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt);
78 break;
79 case 'media':
80 $source = $location;
81 $attachment_id = $attachment->attachment_partial_url_to_postid($source);
82 break;
83 }
84
85 if ($attachment_id > 0) {
86 $has_unique_field = true;
87 $query_args['p'] = $attachment_id;
88 break;
89 } else {
90 return false;
91 }
92 } else {
93
94 // 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 }
113
114 if (!empty($unique_value)) {
115 $has_unique_field = true;
116
117 if (in_array($field, $this->_post_fields, true)) {
118
119 if (array_key_exists($field, $this->_query_vars)) {
120 $query_args[$this->_query_vars[$field]] = $unique_value;
121 } else {
122 switch ($field) {
123 case 'post_title':
124 $query_args['title'] = $unique_value;
125 break;
126 default:
127 $query_args[$field] = $unique_value;
128 break;
129 }
130 }
131 } else {
132 $meta_args[] = array(
133 'key' => $field,
134 'value' => $unique_value
135 );
136 }
137 $unique_field_found = $field;
138 break;
139 }
140 }
141 }
142
143 if (!$has_unique_field) {
144 throw new MapperException("No Unique fields present.");
145 }
146
147 if (!empty($meta_args)) {
148 $query_args['meta_query'] = $meta_args;
149 }
150
151 $query = new \WP_Query($query_args);
152 if ($query->post_count > 1) {
153 throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->posts) . ").");
154 }
155
156 if ($query->post_count == 1) {
157 $this->ID = $query->posts[0];
158 return $this->ID;
159 }
160
161
162 return false;
163 }
164
165 public function update_post_object($fields, $data)
166 {
167 $post = array();
168 $meta = array();
169
170 // we dont import the ID
171 if (isset($fields['ID'])) {
172 unset($fields['ID']);
173 }
174
175 $this->sortFields($fields, $post, $meta);
176
177 if (!empty($post)) {
178
179 $post['ID'] = $this->ID;
180
181 $res = wp_update_post($post);
182 if (is_wp_error($res)) {
183 throw new MapperException($res->get_error_message());
184 }
185 }
186
187 $this->template->process($this->ID, $data, $this->importer);
188
189 $meta = array_merge($meta, $data->getData('custom_fields'));
190
191 // create post meta
192 if ($this->ID && !empty($meta)) {
193 foreach ($meta as $key => $value) {
194 if (is_array($value)) {
195 $this->clear_custom_field($this->ID, $key);
196 foreach ($value as $v) {
197 $this->add_custom_field($this->ID, $key, $v);
198 }
199 } else {
200 $this->update_custom_field($this->ID, $key, $value);
201 }
202 }
203 }
204 }
205
206 public function insert(ParsedData $data)
207 {
208 $fields = $data->getData('default');
209 if (isset($fields['ID'])) {
210 unset($fields['ID']);
211 }
212
213 $this->ID = $attachment_id = $this->download_attachment($data);
214 if (!$this->ID) {
215 throw new MapperException("Unable to download and insert attachment");
216 }
217
218 $this->update_post_object($fields, $data);
219
220 $this->add_version_tag();
221 $this->template->post_process($this->ID, $data);
222
223 return $attachment_id;
224 }
225
226 public function update(ParsedData $data)
227 {
228 $attachment_id = $data->getId();
229
230 $fields = $data->getData('default');
231 if (isset($fields['ID'])) {
232 unset($fields['ID']);
233 }
234
235 $this->download_attachment($data);
236
237 $this->update_post_object($fields, $data);
238
239 $this->add_version_tag();
240 $this->template->post_process($this->ID, $data);
241
242 return $attachment_id;
243 }
244
245 /**
246 * Download Attachment
247 *
248 * @param ParsedData $data
249 */
250 public function download_attachment($data)
251 {
252 if (!$this->importer->isEnabledField('post.file')) {
253 return false;
254 }
255
256 $attachment_id = intval($data->getId());
257
258 /**
259 * @var Attachment $attachment
260 */
261 $attachment = Container::getInstance()->get('attachment');
262
263 $location = $data->getValue('post.file.location', 'post');
264 $download = $data->getValue('post.file.settings._download', 'post');
265 $ftp_path = $data->getValue('post.file.settings._ftp_path', 'post');
266 $remote_url = $data->getValue('post.file.settings._remote_url', 'post');
267 $local_url = $data->getValue('post.file.settings._local_url', 'post');
268 $ftp_host = $data->getValue('post.file.settings._ftp_host', 'post');
269 $ftp_user = $data->getValue('post.file.settings._ftp_user', 'post');
270 $ftp_pass = $data->getValue('post.file.settings._ftp_pass', 'post');
271
272 if (empty($location)) {
273 return $attachment_id;
274 }
275
276 $attachment_salt = '';
277 $result = false;
278
279 switch ($download) {
280 case 'remote':
281
282 $source = $remote_url . $location;
283 $attachment_salt = file_exists($source) ? md5_file($source) : '';
284
285 /**
286 * @var Filesystem $filesystem
287 */
288 $filesystem = Container::getInstance()->get('filesystem');
289
290 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
291 $result = $filesystem->download_file($source, null, null, $custom_filename);
292
293 break;
294 case 'ftp':
295
296 $source = $ftp_path . $location;
297 $attachment_salt = file_exists($source) ? md5_file($source) : '';
298
299 /**
300 * @var Ftp $ftp
301 */
302 $ftp = Container::getInstance()->get('ftp');
303
304 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
305 $result = $ftp->download_file($source, $ftp_host, $ftp_user, $ftp_pass, $custom_filename);
306 break;
307 case 'local':
308
309 $source = $local_url . $location;
310 $attachment_salt = file_exists($source) ? md5_file($source) : '';
311
312 /**
313 * @var Filesystem $filesystem
314 */
315 $filesystem = Container::getInstance()->get('filesystem');
316
317 $custom_filename = apply_filters('iwp/attachment/filename', null, $source);
318 $result = $filesystem->copy_file($source, null, $custom_filename);
319 break;
320 }
321
322 if (is_wp_error($result)) {
323 throw new MapperException($result->get_error_message());
324 }
325
326 if ($result) {
327
328 // We have downloaded a file
329 if ($attachment_id) {
330
331 // updated existing
332 $attachment_id = wp_update_post([
333 'ID' => $attachment_id,
334 'post_mime_type' => $result['mime'],
335 'file' => $result['dest']
336 ]);
337 } else {
338
339 // insert new
340 $attachment_id = $attachment->insert_attachment(0, $result['dest'], $result['mime']);
341 }
342
343 if ($attachment_id) {
344
345 // Attachment has been created or updated
346 $attachment->generate_image_sizes($attachment_id, $result['dest']);
347 $attachment->store_attachment_hash($attachment_id, $source, $attachment_salt);
348 }
349
350 return $attachment_id;
351 }
352
353 return false;
354 }
355
356 public function get_objects_for_removal()
357 {
358 if ($this->is_session_tag_enabled()) {
359 return $this->get_ids_without_session_tag('pt-' . 'attachment');
360 } else {
361 $q = new \WP_Query(array(
362 '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 ),
370 'fields' => 'ids',
371 'posts_per_page' => -1,
372 'cache_results' => false,
373 'update_post_meta_cache' => false,
374 'post_status' => 'any'
375 ));
376
377 if ($q->have_posts()) {
378 return $q->posts;
379 }
380 }
381
382 return false;
383 }
384
385 public function delete($id)
386 {
387 $permissions = $this->importer->getPermission('remove');
388 $force = isset($permissions['trash']) ? !$permissions['trash'] : true; // trash = true
389
390 if (!$force) {
391
392 // set trash flag
393 update_post_meta($id, '_iwp_trash_status', get_post_status($id));
394 update_post_meta($id, '_iwp_trash_importer', $this->importer->getId());
395 }
396
397 wp_delete_attachment($id, $force);
398
399 $this->remove_session_tag($id, 'pt-attachment');
400 }
401
402 public function add_version_tag()
403 {
404 if ($this->is_session_tag_enabled()) {
405 $this->add_session_tag('pt-attachment');
406 } else {
407 update_post_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
408 }
409 }
410 }
411