PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.3
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.3, at class/Common/Importer/Mapper/AttachmentMapper.php

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