PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
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.2.12, at includes/base/item.php

636 lines 23.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
75 $data = array(
76 'provider' => $this->service,
77 'region' => $this->region,
78 'storage' => $this->bucket_name,
79 'source_id' => $source_id,
80 'source_path' => $source_path,
81 'source_type' => $source_type,
82 'url' => $url,
83 'key' => $key,
84 'is_private' => $is_private,
85 'extra' => maybe_serialize($meta),
86 );
87 if ($wpdb->insert(Db::get_table_name(), $data)) {
88 $item_id = $wpdb->insert_id;
89 $data['id'] = $item_id;
90 Integration::update_meta($source_id, 'item', $data, false, false, $source_type);
91 Cache::update_item_cache($source_id.'_item_'.$source_type, $data);
92 Counter::add( 'uploaded', $source_type );
93 }
94
95 return $item_id;
96 }
97
98
99 /**
100 * Get Item From Db
101 */
102 public function get($source_id, $source_type = 'media_library') {
103 global $wpdb;
104 $item = Cache::get_item_cache($source_id.'_item_'.$source_type, false);
105 if($item === false) {
106 $item = Integration::get_meta($source_id, 'item', false, false, false, $source_type);
107 if($item == false){
108 $item_table = Db::get_table_name();
109 $item = $wpdb->get_row("SELECT * FROM ".$item_table." WHERE source_id = $source_id AND source_type='$source_type'", ARRAY_A);
110
111 if($wpdb->last_error || null === $item || !(isset($item) && !empty($item))) {
112 Cache::update_item_cache($source_id.'_item_'.$source_type, '');
113 return false;
114 }
115
116 Integration::update_meta($source_id, 'item', $item, false, false, $source_type);
117 }
118 // Update cache
119 Cache::update_item_cache($source_id.'_item_'.$source_type, $item == false ? '' : $item);
120 }
121
122 return !empty($item) ? $item : false;
123 }
124
125 /**
126 * Function to delete item from data base
127 * @since 1.0.0
128 */
129 public function delete($source_id, $source_type = 'media_library'){
130 global $wpdb;
131 if (isset($source_id) && !Utils::is_empty($source_id)) {
132 $rows = $wpdb->delete(Db::get_table_name(), array('source_id' => $source_id, 'source_type' => $source_type));
133 if ($wpdb->last_error || false === $rows) {
134 return false;
135 }
136 Integration::delete_meta($source_id, 'item', false, $source_type);
137 Cache::delete_item_cache($source_id.'_item_'.$source_type);
138 Counter::remove( 'uploaded', $source_type );
139 return true;
140 }
141 return false;
142 }
143
144
145 /**
146 * Function to update item in data base
147 * @since 1.0.0
148 */
149 public function update($source_id, $data, $source_type = 'media_library') {
150 global $wpdb;
151 if (isset($source_id) && !Utils::is_empty($source_id)) {
152
153 $data['provider'] = $this->service;
154 $data['region'] = $this->region;
155 $data['storage'] = $this->bucket_name;
156
157 $rows = $wpdb->update(Db::get_table_name(), $data, array('source_id' => $source_id, 'source_type' => $source_type));
158 if ($wpdb->last_error || false === $rows) {
159 return false;
160 }
161
162 Integration::delete_meta($source_id, 'item', false, $source_type);
163 Cache::delete_item_cache($source_id.'_item_'.$source_type);
164
165 // Reset cache
166 $current_data = $this->get($source_id, $source_type);
167 return true;
168 }
169 return false;
170 }
171
172 /**
173 * Get extra values of item from database
174 * @since 1.0.0
175 * @param
176 */
177 public function get_extras($source_id, $field = false, $source_type = 'media_library'){
178 $data = $this->get($source_id, $source_type);
179 if ($data) {
180 if (isset($data['extra']) && !empty($data['extra'])) {
181
182 if(!Utils::is_empty($field)) {
183 $extras = Utils::maybe_unserialize($data['extra']);
184 return isset($extras[$field]) && !empty($extras[$field]) ? $extras[$field] : false;
185 }
186
187 return Utils::maybe_unserialize($data['extra']);
188 }
189 }
190 return false;
191 }
192
193
194 /**
195 * Get column of item from database
196 * @since 1.0.0
197 * @param
198 */
199 public function get_field($source_id, $field, $source_type = 'media_library'){
200 $data = $this->get($source_id, $source_type);
201 if ($data) {
202 if (isset($data[$field]) && !empty($data[$field])) {
203 return $data[$field];
204 }
205 }
206 return false;
207 }
208
209
210 /**
211 * Get backup values of item from database
212 * @since 1.0.0
213 * @param
214 */
215 public function get_backup($source_id, $source_type = 'media_library'){
216 $data = $this->get_extras($source_id, false, $source_type);
217 if ($data) {
218 if (isset($data['backup']) && !empty($data['backup'])) {
219 return Utils::maybe_unserialize($data['backup']);
220 }
221 }
222 return false;
223 }
224
225
226 /**
227 * Checking Item that is served by provider And Is rewrite URL is enabled
228 * @since 1.0.0
229 * @param
230 */
231 public function is_available_from_provider($attachment_id, $check_rewrite = true, $source_type = 'media_library') {
232 $item = $this->get($attachment_id, $source_type);
233 if(Utils::is_empty($item)) {
234 return false;
235 }
236
237 if ($item['provider'] == $this->service) {
238 if(
239 ($check_rewrite && (isset($this->settings['rewrite_url']) && $this->settings['rewrite_url'])) ||
240 !$check_rewrite
241 ) {
242 return true;
243 }
244 }
245 return false;
246 }
247
248 /**
249 * Get items by paths
250 */
251 public function get_items_by_source_paths( $paths = [] ) {
252 global $wpdb;
253
254 $items = [];
255
256 if (!Utils::is_empty($paths)) {
257 // Ensure paths are properly sanitized and ready for the query
258 $like_conditions = array();
259 foreach ($paths as $path) {
260 // Prepare the SQL conditions for exact match in source_path and partial match in extras column
261 $like_conditions[] = $wpdb->prepare("(source_path = %s OR extra LIKE %s)", $path, '%' . $wpdb->esc_like($path) . '%');
262 }
263
264 // Combine the conditions with OR
265 $where_clause = implode(' OR ', $like_conditions);
266
267 // Execute the SQL query to fetch source_id instead of source_path
268 $item_table = Db::get_table_name();
269 $sql = "SELECT DISTINCT source_id, source_type FROM " . $item_table . " WHERE " . $where_clause;
270 $results = $wpdb->get_results($sql, ARRAY_A);
271
272 if ($wpdb->last_error || Utils::is_empty($results)) {
273 return [];
274 }
275
276 $source_ids = array();
277 foreach ($results as $row) {
278 $item = $this->get((int)$row['source_id'], $row['source_type']);
279 if(!Utils::is_empty($item)) {
280 $items[] = $item;
281 }
282 }
283 return $items;
284 }
285 return $items;
286 }
287
288
289 /**
290 * Get similar existing files like origin path
291 * @since 1.0.0
292 */
293 public function get_similar_files_by_path($path){
294 global $wpdb;
295 if (isset($path) && !empty($path)) {
296 $item_table = Db::get_table_name();
297 $results = $wpdb->get_results("SELECT source_path FROM " . $item_table . " WHERE source_path LIKE '$path%'", ARRAY_A);
298 if ($wpdb->last_error || null === $results || !(isset($results) && !empty($results))) {
299 return false;
300 }
301 $source_paths = array();
302 foreach ($results as $row) {
303 $source_paths[] = $row['source_path'];
304 }
305 return $source_paths;
306 }
307 return false;
308 }
309
310
311 /**
312 * Get service url of item from database
313 * @since 1.0.0
314 * @param
315 */
316 public function get_url($source_id, $size = 'full', $source_type = 'media_library'){
317 if ($data = $this->get($source_id, $source_type)) {
318 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
319 $key = '';
320 $url = '';
321 switch($size) {
322 case 'full':
323 $key = $data['key'];
324 $url = $data['url'];
325 break;
326 case 'original':
327 if(
328 isset($extras) && !empty($extras) &&
329 isset($extras['original']) && !empty($extras['original'])
330 ) {
331 $key = $extras['original']['key'];
332 $url = $extras['original']['url'];
333 }
334 default:
335 if(
336 isset($extras) && !empty($extras) &&
337 isset($extras['sizes']) && !empty($extras['sizes']) &&
338 isset($extras['sizes'][$size]) && !empty($extras['sizes'][$size])
339 ) {
340 $key = $extras['sizes'][$size]['key'];
341 $url = $extras['sizes'][$size]['url'];
342 }
343 }
344
345 if(!empty($key)){
346 if (
347 isset($this->settings['enable_presigned']) && $this->settings['enable_presigned'] &&
348 isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire'])
349 ) {
350 $preSignedUrl = Integration::get_meta( $source_id, 'presigned_url_'.$size, false, false, false, $source_type );
351 if ($preSignedUrl === false) {
352 $new_url = Service::instance()->get_presigned_url($key);
353
354 if (!Utils::is_empty($new_url)) {
355 $preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key);
356 $expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']))
357 ? $this->settings['presigned_expire']
358 : 20;
359 $expireSeconds = $expireMinutes * 60;
360
361 Integration::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds, $source_type);
362 }
363 }
364 return $preSignedUrl;
365 } else {
366 return Cdn::may_generate_cdn_url($url, $key);
367 }
368 }
369 }
370 return false;
371 }
372
373
374 /**
375 * Get service path of item from database
376 * @since 1.0.0
377 * @param
378 */
379 public function moveToServer($source_id, $size = 'full', $all = false, $source_type = 'media_library') {
380 $server_files = array();
381 $server_file = false;
382 $upload_dir = wp_get_upload_dir();
383 $source_id = (int)$source_id;
384
385 if ($data = $this->get($source_id, $source_type)) {
386 $wpmcsService = Service::instance();
387 if($all) {
388 if (
389 isset($data['source_path']) && !empty($data['source_path']) &&
390 isset($data['key']) && !empty($data['key'])
391 ) {
392 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
393 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
394 $server_files['full'] = $file_path;
395 }
396 }
397 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
398 if (isset($extras['original']) && !empty($extras['original'])) {
399 $original = $extras['original'];
400 if(!empty($original)) {
401 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
402 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
403 $server_files['original'] = $original_file_path;
404 }
405 }
406 }
407 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
408 $sizes = $extras['sizes'];
409 foreach($sizes as $sub_size => $sub_file) {
410 if(!empty($sub_file)) {
411 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
412 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sub_file['key'], $sub_file_path)) {
413 $server_files[$sub_size] = $sub_file_path;
414 }
415 }
416 }
417 }
418 return !empty($server_files) ? $server_files : false;
419 } else {
420 if($size === 'full') {
421 if (
422 isset($data['source_path']) && !empty($data['source_path']) &&
423 isset($data['key']) && !empty($data['key'])
424 ) {
425 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
426 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
427 $server_file = $file_path;
428 }
429 }
430 } else if($size === 'original') {
431 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
432 if (isset($extras['original']) && !empty($extras['original'])) {
433 $original = $extras['original'];
434 if(!empty($original)) {
435 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
436 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
437 $server_file = $original_file_path;
438 }
439 }
440 }
441 } else {
442 $extras = $this->get_extras($source_id, false, $source_type) ?: [];
443 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
444 $sizes = $extras['sizes'];
445 if(isset($sizes[$size]) && !empty($sizes[$size])) {
446 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
447 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
448 $server_file = $sub_file_path;
449 }
450 }
451 }
452 }
453 return $server_file;
454 }
455 }
456 return false;
457 }
458
459
460 /**
461 * Get service path of item from database by source url
462 * @since 1.0.0
463 * @param
464 */
465 public function moveToServerBySourcePath($source_id, $file, $source_type = 'media_library'){
466 $server_files = array();
467 $server_file = false;
468 $upload_dir = wp_get_upload_dir();
469 $source_id = (int)$source_id;
470
471 if ($data = $this->get($source_id, $source_type)) {
472 $source_path = Utils::get_attachment_source_path($file);
473
474 if( isset($data['source_path']) && !empty($data['source_path']) && $data['source_path'] == $source_path ) {
475 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
476 if(file_exists($file_path) || Service::instance()->object_to_server($data['key'], $file_path)) {
477 return true;
478 }
479 } else {
480 $sizes = $this->get_extras($source_id, 'sizes', $source_type) ?: [];
481 foreach($sizes as $sub_size => $sub_file) {
482 if(!empty($sub_file) && $sub_file['source_path'] == $source_path) {
483 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
484 if(file_exists($sub_file_path) || Service::instance()->object_to_server($sub_file['key'], $sub_file_path)) {
485 return true;
486 }
487 }
488 }
489 }
490 }
491 return false;
492 }
493
494 /**
495 * Delete media item
496 */
497 public function delete_attachments_by_item($item, $delete_backup = true) {
498 $upload_dir = wp_get_upload_dir();
499
500 if (isset($item['extra']) && !empty($item['extra'])) {
501 $extras = Utils::maybe_unserialize($item['extra']);
502 if (
503 isset($extras) && !empty($extras) &&
504 isset($extras['sizes']) && !empty($extras['sizes'])
505 ) {
506 foreach ($extras['sizes'] as $sub_image) {
507 if (isset($sub_image['key']) && !empty($sub_image['key'])) {
508 Service::instance()->deleteSingle($sub_image['key']);
509 }
510 }
511 }
512
513 if (
514 isset($extras) && !empty($extras) &&
515 isset($extras['original']) && !empty($extras['original'])
516 ) {
517 Service::instance()->deleteSingle($extras['original']['key']);
518 }
519
520 if (
521 isset($extras) && !empty($extras) &&
522 isset($extras['backup']) && !empty($extras['backup']) &&
523 $delete_backup
524 ) {
525 $backup = Utils::maybe_unserialize($extras['backup']);
526 if (isset($backup) && !empty($backup)) {
527 $this->delete_attachments_by_item($backup, false);
528 }
529 }
530 }
531 if (isset($item['key']) && !empty($item['key'])) {
532 Service::instance()->deleteSingle($item['key']);
533 }
534 }
535
536 /**
537 * Function to remove media from server by id
538 * @since 1.0.0
539 *
540 */
541 public function may_be_delete_server_files_by_id($attachment_id, $source_type = 'media_library', $delete_main_file=false, $delete_backup = false) {
542 $item = $this->get($attachment_id, $source_type);
543 if(Utils::is_empty($item)) {
544 return false;
545 }
546
547 if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) {
548 return false;
549 }
550
551 if (isset($item['extra']) && !empty($item['extra'])) {
552 $extras = Utils::maybe_unserialize($item['extra']);
553 if (
554 isset($extras) && !empty($extras) &&
555 isset($extras['backup']) && !empty($extras['backup']) &&
556 $delete_backup
557 ) {
558 $backup = Utils::maybe_unserialize($extras['backup']);
559 if (isset($backup) && !empty($backup)) {
560 $this->may_be_delete_server_files_by_item($backup, $delete_main_file);
561 }
562 }
563 }
564
565 $this->may_be_delete_server_files_by_item($item, $delete_main_file, $delete_backup);
566
567 return true;
568 }
569
570 /**
571 * Function to remove media from server by item
572 */
573 public function may_be_delete_server_files_by_item( $item, $delete_main_file=false ) {
574 if(Utils::is_empty($item)) {
575 return false;
576 }
577
578 if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) {
579 return false;
580 }
581
582 $upload_dir = wp_get_upload_dir();
583 $has_original = false;
584
585 $file_path = trailingslashit($upload_dir['basedir']) . $item['source_path'];
586
587 if (isset($item['extra']) && !empty($item['extra'])) {
588 $extras = Utils::maybe_unserialize($item['extra']);
589 if (
590 isset($extras) && !empty($extras) &&
591 isset($extras['sizes']) && !empty($extras['sizes'])
592 ) {
593 foreach ($extras['sizes'] as $sub_image) {
594 if (isset($sub_image['source_path']) && !empty($sub_image['source_path'])) {
595 $file = trailingslashit($upload_dir['basedir']) . $sub_image['source_path'];
596 if(file_exists($file)) {
597 wp_delete_file($file);
598 }
599 }
600 }
601 }
602
603 if (
604 isset($extras) && !empty($extras) &&
605 isset($extras['original']) && !empty($extras['original'])
606 ) {
607 $has_original = true;
608 $file = trailingslashit($upload_dir['basedir']).$extras['original']['source_path'];
609 if(file_exists($file) && $delete_main_file) {
610 wp_delete_file($file);
611 }
612 }
613 }
614 if(file_exists($file_path)) {
615 if ($has_original || (!$has_original && $delete_main_file)) {
616 wp_delete_file($file_path);
617 }
618 }
619 return true;
620 }
621
622
623 /**
624 * Ensures only one instance of Class is loaded or can be loaded.
625 *
626 * @return Item Class instance
627 * @since 1.0.0
628 * @static
629 */
630 public static function instance(){
631 if (is_null(self::$instance)) {
632 self::$instance = new self();
633 }
634 return self::$instance;
635 }
636 }