PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / item.php

item.php in Media Cloud Sync 1.3.0, at includes/base/item.php

788 lines 29.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Item {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11
12 protected $config;
13 protected $bucketConfig;
14 protected $settings;
15 protected $credentials;
16 protected $service;
17
18 protected $bucket_name;
19 protected $region = '';
20
21 /**
22 * Admin constructor.
23 * @since 1.0.0
24 */
25 public function __construct() {
26 $this->assets_url = WPMCS_ASSETS_URL;
27 $this->version = WPMCS_VERSION;
28 $this->token = WPMCS_TOKEN;
29
30 // Initialize setup
31 $this->init();
32 }
33
34 /**
35 * Initialize datas
36 */
37 public function init() {
38 $this->settings = Utils::get_settings();
39 $this->credentials = Utils::get_credentials();
40 $this->config = isset($this->credentials['config']) && !empty($this->credentials['config'])
41 ? $this->credentials['config']
42 : [];
43 $this->bucketConfig = isset($this->credentials['bucketConfig']) && !empty($this->credentials['bucketConfig'])
44 ? $this->credentials['bucketConfig']
45 : [];
46 $this->service = isset($this->credentials['service']) && !empty($this->credentials['service'])
47 ? $this->credentials['service']
48 : [];
49
50 if (isset($this->bucketConfig['bucket_name'])) {
51 $this->bucket_name = $this->bucketConfig['bucket_name'];
52 }
53 if (isset($this->config['region'])) {
54 $this->region = $this->config['region'];
55 }
56 }
57
58 /**
59 * Add Item In database
60 * @since 1.0.0
61 * @param
62 */
63 public function add(
64 $source_id,
65 $url,
66 $key,
67 $source_path,
68 $meta,
69 $source_type = 'media_library',
70 $is_private = 0
71 ) {
72 global $wpdb;
73 $item_id = false;
74 $data = array(
75 'provider' => $this->service,
76 'region' => $this->region,
77 'storage' => $this->bucket_name,
78 'source_id' => $source_id,
79 'source_path' => $source_path,
80 'source_type' => $source_type,
81 'url' => $url,
82 'key' => $key,
83 'is_private' => $is_private,
84 'extra' => maybe_serialize($meta),
85 );
86
87 // Do some pre-update actions
88 $this->pre_update_item($source_id, $data, [], $source_type);
89
90 if ($wpdb->insert(Db::get_table_name(), $data)) {
91 $item_id = $wpdb->insert_id;
92 $data['id'] = $item_id;
93 Integration::update_meta($source_id, 'item', $data, false, false, $source_type);
94 Cache::update_item_cache($source_id.'_item_'.$source_type, $data);
95 Counter::add( 'uploaded', $source_type );
96 }
97
98 // Do some post-update actions
99 $this->post_update_item($source_id, $data, $source_type);
100
101 return $item_id;
102 }
103
104
105 /**
106 * Get Item From Db
107 */
108 public function get($source_id, $source_type = 'media_library') {
109 global $wpdb;
110 $item = Cache::get_item_cache($source_id.'_item_'.$source_type, false);
111 if($item === false) {
112 $item = Integration::get_meta($source_id, 'item', false, false, false, $source_type);
113 if($item == false){
114 $item_table = Db::get_table_name();
115 $item = $wpdb->get_row("SELECT * FROM ".$item_table." WHERE source_id = $source_id AND source_type='$source_type'", ARRAY_A);
116
117 if($wpdb->last_error || null === $item || !(isset($item) && !empty($item))) {
118 Cache::update_item_cache($source_id.'_item_'.$source_type, '');
119 return false;
120 }
121
122 Integration::update_meta($source_id, 'item', $item, false, false, $source_type);
123 }
124 // Update cache
125 Cache::update_item_cache($source_id.'_item_'.$source_type, $item == false ? '' : $item);
126 }
127
128 return !empty($item) ? $item : false;
129 }
130
131 /**
132 * Function to delete item from data base
133 * @since 1.0.0
134 */
135 public function delete($source_id, $source_type = 'media_library'){
136 global $wpdb;
137 if (isset($source_id) && !Utils::is_empty($source_id)) {
138 // Delete attachments by item from cloud
139 $item = $this->get($source_id, $source_type);
140 $this->delete_attachments_by_item($item);
141
142 $rows = $wpdb->delete(Db::get_table_name(), array('source_id' => $source_id, 'source_type' => $source_type));
143 if ($wpdb->last_error || false === $rows) {
144 return false;
145 }
146 Integration::delete_meta($source_id, 'item', false, $source_type);
147 Cache::delete_item_cache($source_id.'_item_'.$source_type);
148 Counter::remove( 'uploaded', $source_type );
149 return true;
150 }
151 return false;
152 }
153
154
155 /**
156 * Function to update item in data base
157 * @since 1.0.0
158 */
159 public function update($source_id, $data, $source_type = 'media_library') {
160 global $wpdb;
161 if (isset($source_id) && !Utils::is_empty($source_id)) {
162
163 $data['provider'] = $this->service;
164 $data['region'] = $this->region;
165 $data['storage'] = $this->bucket_name;
166
167 $old_item = $this->get($source_id, $source_type);
168
169 // Do some pre-update actions
170 $this->pre_update_item($source_id, $data, $old_item, $source_type);
171
172 $rows = $wpdb->update(Db::get_table_name(), $data, array('source_id' => $source_id, 'source_type' => $source_type));
173 if ($wpdb->last_error || false === $rows) {
174 return false;
175 }
176
177 Integration::delete_meta($source_id, 'item', false, $source_type);
178 Cache::delete_item_cache($source_id.'_item_'.$source_type);
179
180 // Reset cache
181 $this->get($source_id, $source_type);
182 // Do some post-update actions
183 $this->post_update_item($source_id, $data, $source_type);
184
185 return true;
186 }
187 return false;
188 }
189
190 /**
191 * Get extra values of item from database
192 * @since 1.0.0
193 * @param
194 */
195 public function get_extras($source_id, $field = false, $source_type = 'media_library'){
196 $data = $this->get($source_id, $source_type);
197 if ($data) {
198 if (isset($data['extra']) && !empty($data['extra'])) {
199
200 if(!Utils::is_empty($field)) {
201 $extras = Utils::maybe_unserialize($data['extra']);
202 return isset($extras[$field]) && !empty($extras[$field]) ? $extras[$field] : false;
203 }
204
205 return Utils::maybe_unserialize($data['extra']);
206 }
207 }
208 return false;
209 }
210
211
212 /**
213 * Get column of item from database
214 * @since 1.0.0
215 * @param
216 */
217 public function get_field($source_id, $field, $source_type = 'media_library'){
218 $data = $this->get($source_id, $source_type);
219 if ($data) {
220 if (isset($data[$field]) && !empty($data[$field])) {
221 return $data[$field];
222 }
223 }
224 return false;
225 }
226
227
228 /**
229 * Get backup values of item from database
230 * @since 1.0.0
231 * @param
232 */
233 public function get_backup($source_id, $source_type = 'media_library'){
234 $data = $this->get_extras($source_id, false, $source_type);
235 if ($data) {
236 if (isset($data['backup']) && !empty($data['backup'])) {
237 return Utils::maybe_unserialize($data['backup']);
238 }
239 }
240 return false;
241 }
242
243
244 /**
245 * Checking Item that is served by provider And Is rewrite URL is enabled
246 * @since 1.0.0
247 * @param
248 */
249 public function is_available_from_provider($attachment_id, $check_rewrite = true, $source_type = 'media_library') {
250 $item = $this->get($attachment_id, $source_type);
251 if(Utils::is_empty($item)) {
252 return false;
253 }
254
255 if ($item['provider'] == $this->service) {
256 if(
257 ($check_rewrite && (isset($this->settings['rewrite_url']) && $this->settings['rewrite_url'])) ||
258 !$check_rewrite
259 ) {
260 return true;
261 }
262 }
263 return false;
264 }
265
266 /**
267 * Get items by paths
268 */
269 public function get_items_by_source_paths( $paths = [] ) {
270 global $wpdb;
271
272 $items = [];
273
274 if (!Utils::is_empty($paths)) {
275 // Ensure paths are properly sanitized and ready for the query
276 $like_conditions = array();
277 foreach ($paths as $path) {
278 // Prepare the SQL conditions for exact match in source_path and partial match in extras column
279 $like_conditions[] = $wpdb->prepare("(source_path = %s OR extra LIKE %s)", $path, '%' . $wpdb->esc_like($path) . '%');
280 }
281
282 // Combine the conditions with OR
283 $where_clause = implode(' OR ', $like_conditions);
284
285 // Execute the SQL query to fetch source_id instead of source_path
286 $item_table = Db::get_table_name();
287 $sql = "SELECT DISTINCT source_id, source_type FROM " . $item_table . " WHERE " . $where_clause;
288 $results = $wpdb->get_results($sql, ARRAY_A);
289
290 if ($wpdb->last_error || Utils::is_empty($results)) {
291 return [];
292 }
293
294 $source_ids = array();
295 foreach ($results as $row) {
296 $item = $this->get((int)$row['source_id'], $row['source_type']);
297 if(!Utils::is_empty($item)) {
298 $items[] = $item;
299 }
300 }
301 return $items;
302 }
303 return $items;
304 }
305
306
307 /**
308 * Get similar existing files like origin path
309 * @since 1.0.0
310 */
311 public function get_similar_files_by_path($path){
312 global $wpdb;
313 if (isset($path) && !empty($path)) {
314 $item_table = Db::get_table_name();
315 $results = $wpdb->get_results("SELECT source_path FROM " . $item_table . " WHERE source_path LIKE '$path%'", ARRAY_A);
316 if ($wpdb->last_error || null === $results || !(isset($results) && !empty($results))) {
317 return false;
318 }
319 $source_paths = array();
320 foreach ($results as $row) {
321 $source_paths[] = $row['source_path'];
322 }
323 return $source_paths;
324 }
325 return false;
326 }
327
328
329 /**
330 * Get service url of item from database
331 * @since 1.0.0
332 * @param
333 */
334 public function get_url($source_id, $size = 'full', $source_type = 'media_library'){
335 if ($data = $this->get($source_id, $source_type)) {
336 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
337 $key = '';
338 $url = '';
339 switch($size) {
340 case 'full':
341 $key = $data['key'];
342 $url = $data['url'];
343 break;
344 case 'original':
345 if(
346 isset($extras) && !empty($extras) &&
347 isset($extras['original']) && !empty($extras['original'])
348 ) {
349 $key = $extras['original']['key'];
350 $url = $extras['original']['url'];
351 }
352 default:
353 if(
354 isset($extras) && !empty($extras) &&
355 isset($extras['sizes']) && !empty($extras['sizes']) &&
356 isset($extras['sizes'][$size]) && !empty($extras['sizes'][$size])
357 ) {
358 $key = $extras['sizes'][$size]['key'];
359 $url = $extras['sizes'][$size]['url'];
360 }
361 }
362
363 if(!empty($key)){
364 if (
365 isset($this->settings['enable_presigned']) && $this->settings['enable_presigned'] &&
366 isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire'])
367 ) {
368 $preSignedUrl = Integration::get_meta( $source_id, 'presigned_url_'.$size, false, false, false, $source_type );
369 if ($preSignedUrl === false) {
370 $new_url = Service::instance()->get_presigned_url($key);
371
372 if (!Utils::is_empty($new_url)) {
373 $preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key);
374 $expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']))
375 ? $this->settings['presigned_expire']
376 : 20;
377 $expireSeconds = $expireMinutes * 60;
378
379 Integration::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds, $source_type);
380 }
381 }
382 return $preSignedUrl;
383 } else {
384 return Cdn::may_generate_cdn_url($url, $key);
385 }
386 }
387 }
388 return false;
389 }
390
391
392 /**
393 * Get service path of item from database
394 * @since 1.0.0
395 * @param
396 */
397 public function moveToServer($source_id, $size = 'full', $all = false, $source_type = 'media_library') {
398 $server_files = array();
399 $server_file = false;
400 $upload_dir = wp_get_upload_dir();
401 $source_id = (int)$source_id;
402
403 if ($data = $this->get($source_id, $source_type)) {
404 $wpmcsService = Service::instance();
405 if($all) {
406 if (
407 isset($data['source_path']) && !empty($data['source_path']) &&
408 isset($data['key']) && !empty($data['key'])
409 ) {
410 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
411 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
412 $server_files['full'] = $file_path;
413 }
414 }
415 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
416 if (isset($extras['original']) && !empty($extras['original'])) {
417 $original = $extras['original'];
418 if(!empty($original)) {
419 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
420 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
421 $server_files['original'] = $original_file_path;
422 }
423 }
424 }
425 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
426 $sizes = $extras['sizes'];
427 foreach($sizes as $sub_size => $sub_file) {
428 if(!empty($sub_file)) {
429 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
430 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sub_file['key'], $sub_file_path)) {
431 $server_files[$sub_size] = $sub_file_path;
432 }
433 }
434 }
435 }
436 return !empty($server_files) ? $server_files : false;
437 } else {
438 if($size === 'full') {
439 if (
440 isset($data['source_path']) && !empty($data['source_path']) &&
441 isset($data['key']) && !empty($data['key'])
442 ) {
443 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
444 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
445 $server_file = $file_path;
446 }
447 }
448 } else if($size === 'original') {
449 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
450 if (isset($extras['original']) && !empty($extras['original'])) {
451 $original = $extras['original'];
452 if(!empty($original)) {
453 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
454 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
455 $server_file = $original_file_path;
456 }
457 }
458 }
459 } else {
460 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
461 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
462 $sizes = $extras['sizes'];
463 if(isset($sizes[$size]) && !empty($sizes[$size])) {
464 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
465 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
466 $server_file = $sub_file_path;
467 }
468 }
469 }
470 }
471 return $server_file;
472 }
473 }
474 return false;
475 }
476
477
478 /**
479 * Get service path of item from database by source url
480 * @since 1.0.0
481 * @param
482 */
483 public function moveToServerBySourcePath($source_id, $file, $source_type = 'media_library'){
484 $server_files = array();
485 $server_file = false;
486 $upload_dir = wp_get_upload_dir();
487 $source_id = (int)$source_id;
488
489 if ($data = $this->get($source_id, $source_type)) {
490 $source_path = Utils::get_attachment_source_path($file);
491
492 if( isset($data['source_path']) && !empty($data['source_path']) && $data['source_path'] == $source_path ) {
493 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
494 if(file_exists($file_path) || Service::instance()->object_to_server($data['key'], $file_path)) {
495 return true;
496 }
497 } else {
498 // Check in extras
499 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
500 if (isset($extras['original']) && !empty($extras['original']) && $extras['original']['source_path'] == $source_path) {
501 $original = $extras['original'];
502 if(!empty($original)) {
503 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
504 if(file_exists($original_file_path) || Service::instance()->object_to_server($original['key'], $original_file_path)) {
505 return true;
506 }
507 }
508 }
509
510 // Check in sizes
511 $sizes = isset($extras['sizes']) && !empty($extras['sizes']) ? $extras['sizes'] : [];
512 if(isset($sizes) && !empty($sizes)) {
513 foreach($sizes as $sub_size => $sub_file) {
514 if(!empty($sub_file) && $sub_file['source_path'] == $source_path) {
515 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
516 if(file_exists($sub_file_path) || Service::instance()->object_to_server($sub_file['key'], $sub_file_path)) {
517 return true;
518 }
519 }
520 }
521 }
522 }
523 }
524 return false;
525 }
526
527
528 /**
529 * Move original file to server
530 *
531 * @param int $source_id
532 * @param string $source_type
533 * @return bool
534 */
535 public function moveOriginalToServer($source_id, $source_type = 'media_library') {
536 $data = $this->get($source_id, $source_type);
537 if ($data) {
538 $size = 'full';
539 $original = $this->get_extras($source_id, 'original', $source_type);
540 if ($original && isset($original['key']) && !empty($original['key'])) {
541 $size = 'original';
542 }
543 return $this->moveToServer($source_id, $size, false, $source_type);
544 }
545 return false;
546 }
547
548
549 /**
550 * Delete media item
551 */
552 public function delete_attachments_by_item($item, $delete_backup = true) {
553 $upload_dir = wp_get_upload_dir();
554
555 if (isset($item['extra']) && !empty($item['extra'])) {
556 $extras = Utils::maybe_unserialize($item['extra']);
557 if (
558 isset($extras) && !empty($extras) &&
559 isset($extras['sizes']) && !empty($extras['sizes'])
560 ) {
561 foreach ($extras['sizes'] as $sub_image) {
562 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
563 Service::instance()->deleteSingle($sub_image['key']);
564 }
565 }
566 }
567
568 if (
569 isset($extras) && !empty($extras) &&
570 isset($extras['original']) && !empty($extras['original'])
571 ) {
572 Service::instance()->deleteSingle($extras['original']['key']);
573 }
574
575 if (
576 isset($extras) && !empty($extras) &&
577 isset($extras['backup']) && !empty($extras['backup']) &&
578 $delete_backup
579 ) {
580 $backup = Utils::maybe_unserialize($extras['backup']);
581 if (isset($backup) && !empty($backup)) {
582 $this->delete_attachments_by_item($backup, false);
583 }
584 }
585 }
586 if (isset($item['key']) && !empty($item['key'])) {
587 Service::instance()->deleteSingle($item['key']);
588 }
589 }
590
591
592 /**
593 * Pre-update item actions
594 * @since 1.2.13
595 * @param int $source_id
596 * @param array $data
597 * @param string $source_type
598 */
599 public function pre_update_item($source_id, $new_item, $old_item = [], $source_type = 'media_library') {
600 // Hook for pre-update actions
601 do_action('wpmcs_pre_update_item', $source_id, $new_item, $old_item, $source_type);
602
603 // Additional filter to modify files to be removed from server if needed
604 $files_to_remove = apply_filters('wpmcs_pre_update_item_additional_files_to_remove_from_server', [], $source_id, $new_item, $old_item, $source_type);
605
606 // Delete files if any
607 if (!Utils::is_empty($files_to_remove)) {
608 $this->may_be_delete_server_files_by_source_paths($files_to_remove);
609 }
610 }
611
612
613 /**
614 * Post-update item actions
615 * @since 1.2.13
616 * @param int $source_id
617 * @param array $data
618 * @param string $source_type
619 * This function is called after an item has been updated in the database.
620 * It triggers a WordPress action hook 'wpmcs_post_update_item' to allow other functions to hook into this event.
621 * After that, it calls may_be_delete_server_files_by_id to potentially delete server files associated with the item.
622 *
623 * @example
624 * $item = Item::instance();
625 * $item->post_update_item(123, $data, 'media_library');
626 *
627 * This example will trigger the post-update actions for the item with ID 123.
628 * It will execute any functions hooked to 'wpmcs_post_update_item' and may delete server files if the settings allow it.
629 */
630 public function post_update_item($source_id, $data, $source_type = 'media_library') {
631 // Hook for post-update actions
632 do_action('wpmcs_post_update_item', $source_id, $data, $source_type);
633
634 // May be delete server files
635 $this->may_be_delete_server_files_by_id($source_id, $source_type, true, true);
636 }
637
638
639 public function may_be_delete_server_files_by_source_paths($source_paths) {
640 if (Utils::is_empty($source_paths) || !is_array($source_paths)) {
641 return false;
642 }
643
644 if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) {
645 return false;
646 }
647
648 foreach ($source_paths as $path) {
649 if(file_exists($path)) {
650 wp_delete_file($path, true);
651 }
652 }
653 return true;
654 }
655
656 /**
657 * Function to remove media from server by id
658 * @param int $attachment_id
659 * @param string $source_type
660 * @param bool $delete_main_file
661 * @param bool $delete_backup
662 *
663 * This function checks if the item exists and if the setting to remove from server is enabled.
664 * If so, it deletes the main file and any backup files associated with the item.
665 * It also checks if the item has any extra data, and if so, it attempts to delete the backup files if specified.
666 * Finally, it deletes the main file associated with the item.
667 *
668 * @since 1.2.13
669 * @return bool Returns true if the deletion process was initiated, false otherwise.
670 *
671 * @throws \Exception If the item does not exist or if the removal from server setting is not enabled.
672 *
673 * @example
674 * $item = Item::instance();
675 * $item->may_be_delete_server_files_by_id(123, 'media_library', true, true);
676 *
677 * This example will attempt to delete the server files for the attachment with ID 123,
678 * including the main file and any backup files, if the settings allow it.
679 *
680 * @see Item::may_be_delete_server_files_by_item() for the function that actually performs the deletion.
681 */
682 public function may_be_delete_server_files_by_id($attachment_id, $source_type = 'media_library', $delete_main_file=false, $delete_backup = false) {
683 $item = $this->get($attachment_id, $source_type);
684 if(Utils::is_empty($item)) {
685 return false;
686 }
687
688 if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) {
689 return false;
690 }
691
692 if (isset($item['extra']) && !empty($item['extra'])) {
693 $extras = Utils::maybe_unserialize($item['extra']);
694 if (
695 isset($extras) && !empty($extras) &&
696 isset($extras['backup']) && !empty($extras['backup']) &&
697 $delete_backup
698 ) {
699 $backup = Utils::maybe_unserialize($extras['backup']);
700 if (isset($backup) && !empty($backup)) {
701 $this->may_be_delete_server_files_by_item($backup, $delete_main_file);
702 }
703 }
704 }
705
706 $this->may_be_delete_server_files_by_item($item, $delete_main_file, $delete_backup);
707
708 return true;
709 }
710
711 /**
712 * Function to remove media from server by item
713 */
714 public function may_be_delete_server_files_by_item( $item, $delete_main_file=false ) {
715 if(Utils::is_empty($item)) {
716 return false;
717 }
718
719 if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) {
720 return false;
721 }
722
723 $upload_dir = wp_get_upload_dir();
724 $has_original = false;
725 $files_to_remove = array();
726
727 $file_path = trailingslashit($upload_dir['basedir']) . $item['source_path'];
728
729 if (isset($item['extra']) && !empty($item['extra'])) {
730 $extras = Utils::maybe_unserialize($item['extra']);
731 if (
732 isset($extras) && !empty($extras) &&
733 isset($extras['sizes']) && !empty($extras['sizes'])
734 ) {
735 foreach ($extras['sizes'] as $sub_image) {
736 if (isset($sub_image['source_path']) && !empty($sub_image['source_path'])) {
737 $file = trailingslashit($upload_dir['basedir']) . $sub_image['source_path'];
738 if(file_exists($file)) {
739 $files_to_remove[] = $file;
740 }
741 }
742 }
743 }
744
745 if (
746 isset($extras) && !empty($extras) &&
747 isset($extras['original']) && !empty($extras['original'])
748 ) {
749 $has_original = true;
750 $file = trailingslashit($upload_dir['basedir']).$extras['original']['source_path'];
751 if(file_exists($file) && $delete_main_file) {
752 $files_to_remove[] = $file;
753 }
754 }
755 }
756 if(file_exists($file_path)) {
757 if ($has_original || (!$has_original && $delete_main_file)) {
758 $files_to_remove[] = $file_path;
759 }
760 }
761
762
763 $files_to_remove = apply_filters('wpmcs_files_to_remove_from_server', array_unique($files_to_remove), $item['source_id'], $item);
764
765 if (!Utils::is_empty($files_to_remove)) {
766 foreach ($files_to_remove as $file) {
767 wp_delete_file($file, true);
768 }
769 }
770
771 return true;
772 }
773
774
775 /**
776 * Ensures only one instance of Class is loaded or can be loaded.
777 *
778 * @return Item Class instance
779 * @since 1.0.0
780 * @static
781 */
782 public static function instance(){
783 if (is_null(self::$instance)) {
784 self::$instance = new self();
785 }
786 return self::$instance;
787 }
788 }