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

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

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