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 / integrations / media-library.php

media-library.php in Media Cloud Sync 1.2.12, at includes/integrations/media-library.php

946 lines 34.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 MediaLibrary {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11
12 protected $settings;
13 private $deleting_attachment = false;
14
15
16 public static $source_type_prefix = "media";
17 public static $label = 'Media Library';
18
19 // Map which meta to be updated
20 public static $source_types = [
21 "media_library" => [
22 'table' => 'posts',
23 'class' => 'MediaLibrary'
24 ]
25 ];
26
27 /**
28 * Admin constructor.
29 * @since 1.0.0
30 */
31 public function __construct() {
32 $this->assets_url = WPMCS_ASSETS_URL;
33 $this->version = WPMCS_VERSION;
34 $this->token = WPMCS_TOKEN;
35 $this->settings = Utils::get_settings();
36
37 // Initialize setup
38 $this->registerActions();
39 }
40
41 /**
42 * Register integration access
43 */
44 public function registerActions() {
45 /** IMAGE EDITOR COMPATIBILITY */
46 add_action( 'attachment_submitbox_misc_actions', array( $this, 'attachment_submitbox_metadata'),99 );
47 //Media Modal Ajax
48 add_action( 'wp_ajax_wpmcs_get_attachment_details', array( $this, 'ajax_get_attachment_details' ) );
49
50 /** URL RE-WRITING HOOKS */
51 add_filter( 'wp_get_attachment_url', [ $this, 'wp_get_attachment_url' ], 99, 2 );
52 add_filter( 'wp_get_attachment_image_attributes', [ $this, 'wp_get_attachment_image_attributes' ], 99, 3 );
53 add_filter( 'wp_calculate_image_srcset', [ $this, 'wp_calculate_image_srcset' ], 99, 5 );
54 add_filter( 'get_attached_file', [ $this, 'get_attached_file' ], 10, 2 );
55 add_filter( 'wp_get_original_image_path', [ $this, 'get_attached_file' ], 10, 2 );
56 add_filter( 'wp_video_shortcode', [ $this, 'wp_media_shortcode' ], 100, 5 );
57 add_filter( 'wp_audio_shortcode', [ $this, 'wp_media_shortcode' ], 100, 5 );
58
59 /** FILE MANAGEMENT HOOKS */
60 add_filter( 'wp_unique_filename', [ $this, 'wp_unique_filename' ], 10, 3 );
61 add_filter( 'wp_update_attachment_metadata', [ $this, 'update_attachment_metadata' ], 110, 2 );
62 add_filter( 'wp_generate_attachment_metadata', [ $this, 'wp_generate_attachment_metadata' ], 110, 3 );
63 add_filter( 'pre_delete_attachment', [ $this, 'pre_delete_attachment' ], 20 );
64 add_filter( 'delete_attachment', [ $this, 'delete_attachment' ], 20 );
65 add_action( 'delete_post', [$this, 'delete_post'] );
66 add_filter( 'update_attached_file', [ $this, 'update_attached_file' ], 100, 2 );
67 add_filter( 'load_image_to_edit_path', [ $this, 'load_image_to_edit_path' ], 10, 3 );
68 }
69
70
71 /**
72 * Function to execute on load_image_to_edit_path
73 * @since 1.0.0
74 * @return string
75 *
76 */
77 public function load_image_to_edit_path($path, $attachment_id, $size) {
78 $wpmcsItem = Item::instance();
79 if (!Utils::is_ok_to_serve($attachment_id) && !$wpmcsItem->is_available_from_provider($attachment, true, 'media_library') ) {
80 return $path;
81 }
82
83
84 $server_file = false;
85 //If operation is Image editor Image Save
86 if(
87 isset($_REQUEST['do']) &&
88 isset($_REQUEST['action']) &&
89 $_REQUEST['action'] === 'image-editor' &&
90 $_REQUEST['do']==='save'
91 ) {
92 $server_file = $wpmcsItem->moveToServer($attachment_id, $size, false, 'media_library');
93 }
94
95 return $server_file ? $server_file : $path;
96 }
97
98
99 /**
100 * Allow processes to update the file on provider via update_attached_file()
101 * @since 1.0.0
102 * @param string $file
103 * @param int $attachment_id
104 *
105 * @return string
106 */
107 public function update_attached_file($file, $attachment_id) {
108 if( !Utils::is_ok_to_upload($attachment_id) ) {
109 return $file;
110 }
111
112 $item = Item::instance()->get($attachment_id, 'media_library');
113
114 if (Utils::is_empty($item)) {
115 return $file;
116 }
117
118 $file = apply_filters('wpmcs_update_attached_file', $file, $attachment_id, $item);
119
120 return $file;
121 }
122
123
124 /**
125 * Function to remove data from provider by attachment ID
126 * @since 1.0.0
127 *
128 */
129 public function delete_attachment($attachment_id){
130 if (!Utils::is_service_enabled()) {
131 return $attachment_id;
132 }
133
134 $wpmcsItem = Item::instance();
135 $item = $wpmcsItem->get($attachment_id, 'media_library');
136
137 if (Utils::is_empty($item)) {
138 return $attachment_id;
139 }
140
141 $wpmcsItem->delete_attachments_by_item($item);
142
143 $wpmcsItem->delete($attachment_id, 'media_library');
144
145 return $attachment_id;
146 }
147
148 /**
149 * Function to execute on wp_update_attachment_metadata
150 * @since 1.0.0
151 *
152 */
153 public function update_attachment_metadata($attachment_meta, $attachment_id) {
154 $wpmcsItem = Item::instance();
155
156 $attachment_id = (int)$attachment_id;
157 $type = get_post_mime_type($attachment_id);
158 $is_image = (0 === strpos($type, 'image/'));
159 $is_image_edit = false;
160
161 if (!Utils::is_ok_to_upload($attachment_id)) return $attachment_meta;
162
163 //Generate attachment meta if empty
164 if (empty($attachment_meta)) {
165 $attachment_meta = wp_get_attachment_metadata($attachment_id);
166 }
167
168 // Get File Name/Path from Image meta
169 $file = isset($attachment_meta) && !empty($attachment_meta) && isset($attachment_meta['file']) && !empty($attachment_meta['file'])
170 ? $attachment_meta['file']
171 : Utils::get_post_meta((int) $attachment_id, '_wp_attached_file', true);
172
173 if($file === basename($file)) {
174 $file = Utils::get_post_meta((int) $attachment_id, '_wp_attached_file', true);
175 }
176
177 // Generate relative path of the file
178 $source_path = Utils::get_attachment_source_path($file);
179
180 if($source_path){
181 $existing = $wpmcsItem->get($attachment_id, 'media_library');
182 $backup = $wpmcsItem->get_backup($attachment_id, 'media_library');
183
184 if(Utils::is_empty($existing)) { // First Upload attempt
185 $uploaded_data = $this->uploadMedia($attachment_meta, $attachment_id, $source_path);
186 if(isset($uploaded_data['file'])) {
187 $wpmcsItem->add(
188 $attachment_id,
189 $uploaded_data['file']['url'],
190 $uploaded_data['file']['key'],
191 $uploaded_data['file']['source_path'],
192 $uploaded_data['extra'],
193 'media_library'
194 );
195 }
196 } else {
197 if(Utils::is_empty($backup)) { // if no backup available
198 $uploaded_data = $this->uploadMedia($attachment_meta, $attachment_id, $source_path);
199
200 if( $existing['source_path'] != $source_path ) { // if existing item is different from new
201 $uploaded_data['extra']['backup'] = serialize($existing);
202 $is_image_edit = true; // if existing item is different from new it is image edit
203 }
204
205 if(isset($uploaded_data['file'])) {
206 $wpmcsItem->update(
207 $attachment_id,
208 [
209 'source_path' => $uploaded_data['file']['source_path'],
210 'url' => $uploaded_data['file']['url'],
211 'key' => $uploaded_data['file']['key'],
212 'extra' => maybe_serialize($uploaded_data['extra']),
213 ],
214 'media_library'
215 );
216 }
217 } else { // if backup available
218 if($backup['source_path'] != $source_path) {
219 $uploaded_data = $this->uploadMedia($attachment_meta, $attachment_id, $source_path);
220 $uploaded_data['extra']['backup'] = maybe_serialize($backup);
221
222 if( $existing['source_path'] != $source_path ) { // if existing item is different from new
223 /**
224 * Since the backup is present and it is not the first edit
225 * It is a re-edit, So we don't need intermediate edit state.
226 * So removing cloud and server files immediately
227 */
228 Item::instance()->delete_attachments_by_item($existing, false);
229 Item::instance()->may_be_delete_server_files_by_item($existing, true);
230
231 $is_image_edit = true; // if existing item is different from new it is image edit
232 }
233
234 if(isset($uploaded_data['file'])) {
235 $wpmcsItem->update(
236 $attachment_id,
237 [
238 'source_path' => $uploaded_data['file']['source_path'],
239 'url' => $uploaded_data['file']['url'],
240 'key' => $uploaded_data['file']['key'],
241 'extra' => maybe_serialize($uploaded_data['extra']),
242 ],
243 'media_library'
244 );
245 }
246 } else { // Restore backup
247 Item::instance()->delete_attachments_by_item($existing, false);
248 $wpmcsItem->update(
249 $attachment_id,
250 [
251 'source_path' => $backup['source_path'],
252 'url' => $backup['url'],
253 'key' => $backup['key'],
254 'extra' => $backup['extra'],
255 ],
256 'media_library'
257 );
258 }
259 }
260 }
261 }
262
263 // Remove files from server if enabled ---- Remove main file if it is not image or a new image source url (image edit)
264 $delete_main_file = !$is_image || $is_image_edit;
265 // Remove files from server for backup ---- Remove backup from server if it is image edit (To remove restored file)
266 $delete_backup = $is_image_edit;
267 Item::instance()->may_be_delete_server_files_by_id($attachment_id, 'media_library', $delete_main_file, $delete_backup);
268
269 return $attachment_meta;
270 }
271
272 /**
273 * Create unique names for files effects mainly on delete files from server settings
274 * @since 1.0.0
275 * @return string
276 */
277 public function wp_unique_filename($filename, $ext, $dir) {
278 // Get Post ID if uploaded in post screen.
279 $post_id = Utils::filter_input( 'post_id', INPUT_POST, FILTER_VALIDATE_INT );
280
281 $filename = $this->filter_unique_filename($filename, $ext, $dir, $post_id);
282
283 return $filename;
284 }
285
286 /**
287 * filter unique file names
288 * @since 1.0.0
289 * @return string
290 */
291 private function filter_unique_filename($filename, $ext, $dir, $post_id = null) {
292 if (!Utils::is_service_enabled()) {
293 return $filename;
294 }
295
296 // sanitize the file name before we begin processing
297 $filename = sanitize_file_name($filename);
298 $ext = strtolower($ext);
299 $name = wp_basename($filename, $ext);
300
301 // Edge case: if file is named '.ext', treat as an empty name.
302 if ($name === $ext) {
303 $name = '';
304 }
305
306 // Rebuild filename with lowercase extension as provider will have converted extension on upload.
307 $filename = $name . $ext;
308
309 return $this->generate_unique_filename($name, $ext, $dir);
310 }
311
312
313 /**
314 * Generate unique filename
315 * @since 1.0.0
316 * @param string $name
317 * @param string $ext
318 * @param string $time
319 *
320 * @return string
321 */
322 private function generate_unique_filename($name, $ext, $dir) {
323 $upload_dir = wp_get_upload_dir();
324 $filename = $name . $ext;
325 $no_ext_path = $dir . '/' . $name;
326 $rel_no_ext_path = substr($no_ext_path, strlen(trailingslashit($upload_dir['basedir'])),strlen($no_ext_path));
327 $path = $dir . '/' . $name . $ext;
328 $source_path = substr($path, strlen(trailingslashit($upload_dir['basedir'])),strlen($path));
329
330
331 $uploaded_files = Item::instance()->get_similar_files_by_path($rel_no_ext_path);
332
333 if ($uploaded_files !== false) {
334 if (Utils::check_existing_file_names($source_path, $uploaded_files) || file_exists($path)) {
335 $count = 1;
336 $new_file_name = '';
337 $found = true;
338 while ($found) {
339 $tmp_path = $dir . '/' . $name . '-' . $count . $ext;
340 $rel_temp_path = substr($tmp_path, strlen(trailingslashit($upload_dir['basedir'])),strlen($tmp_path));
341
342 if (Utils::check_existing_file_names($rel_temp_path, $uploaded_files) || file_exists($tmp_path)) {
343 $count++;
344 } else {
345 $found = false;
346 $new_file_name = $name . '-' . $count . $ext;
347 }
348 }
349 return $new_file_name;
350 }
351 } else {
352 if (file_exists($path)) {
353 $count = 1;
354 $new_file_name = '';
355 $found = true;
356
357 while ($found) {
358 $tmp_path = $dir . '/' . $name . '-' . $count . $ext;
359 if (file_exists($tmp_path)) {
360 $count++;
361 } else {
362 $found = false;
363 $new_file_name = $name . '-' . $count . $ext;
364 }
365 }
366 return $new_file_name;
367 }
368 }
369
370 return $filename;
371 }
372
373
374 /**
375 * Function to execute on wp_generate_attachment_metadata
376 * To delete files from server only after updating all meta
377 * @since 1.0.0
378 *
379 */
380
381 public function wp_generate_attachment_metadata( $attachment_meta, $attachment_id, $action) {
382 if($action == 'create') {
383 $attachment_id = (int)$attachment_id;
384
385 if (!Utils::is_ok_to_upload($attachment_id)) return $attachment_meta;
386
387 $item = Item::instance()->get($attachment_id, 'media_library');
388 if(!Utils::is_empty($item)) {
389 Item::instance()->may_be_delete_server_files_by_id($attachment_id, 'media_library', true, true);
390 }
391 }
392
393 return $attachment_meta;
394 }
395
396 /**
397 * Filters the audio & video shortcodes output to remove "&_=NN" params from source.src as it breaks signed URLs.
398 *
399 * @param string $html Shortcode HTML output.
400 * @param array $atts Array of shortcode attributes.
401 * @param string $media Media file.
402 * @param int $post_id Post ID.
403 * @param string $library Media library used for the shortcode.
404 *
405 * @return string
406 *
407 * Note: Depends on 30377.4.diff from https://core.trac.wordpress.org/ticket/30377
408 */
409 public function wp_media_shortcode( $html, $atts, $media, $post_id, $library ) {
410 return preg_replace( '/&#038;_=[0-9]+/', '', $html );
411 }
412
413 /**
414 * Return the provider URL when the local file is missing
415 * unless we know who the calling process is and we are happy
416 * to copy the file back to the server to be used.
417 *
418 * @handles get_attached_file
419 * @handles wp_get_original_image_path
420 * @since 1.0.0
421 * @param string $file
422 * @param int $attachment_id
423 *
424 * @return string
425 */
426 public function get_attached_file($file, $attachment_id) {
427 $attachment_id = (int)$attachment_id;
428
429 // During the deletion of an attachment, stream wrapper URLs should not be returned.
430 if ( $this->deleting_attachment ) {
431 return $file;
432 }
433
434 if (!Utils::is_ok_to_serve($attachment_id)) {
435 return $file;
436 }
437
438 if( isset($_REQUEST['action']) && $_REQUEST['action'] === 'image-editor' ) {
439 // Avoid rewriting URL when image restore in image editor
440 if(isset($_REQUEST['do']) && $_REQUEST['do']==='restore') {
441 return $file;
442 }
443 // Avoid rewriting URL when image save in image editor
444 if(isset($_REQUEST['do']) && $_REQUEST['do'] === 'save') {
445 return $file;
446 }
447 }
448
449 $wpmcsItem = Item::instance();
450
451 $item = $wpmcsItem->get($attachment_id, 'media_library');
452
453 if ( file_exists( $file ) || Utils::is_empty($item)) {
454 if(!Utils::is_empty($item)) {
455 /**
456 * Added filter to allow plugins to copy the pending size to the server
457 * before the file is served.
458 *
459 * @param string $file
460 * @param string $file
461 * @param int $attachment_id
462 */
463 return apply_filters( 'wpmcs_get_attached_file_noop', $file, $file, $attachment_id );
464 } else {
465 return $file;
466 }
467 }
468
469 $url = $wpmcsItem->get_url($attachment_id, 'full', 'media_library');
470 if(Utils::is_empty($url)) {
471 return $file;
472 }
473 /**
474 * This filter gives filter implementors a chance to copy back missing item files
475 * from the provider before WordPress returns the file name/path for it. Defaults to
476 * returning the remote URL.
477 *
478 * @param string $url Item URL
479 * @param string $file Local file path
480 * @param int $attachment_id Attachment ID
481 * @param array $item Item data
482 */
483 return apply_filters('wpmcs_get_attached_file', $url, $file, $attachment_id, $item);
484 }
485
486
487 /**
488 * Change src attributes
489 * @since 1.0.0
490 */
491
492 public function wp_calculate_image_srcset($sources, $size_array, $image_src, $attachment_meta, $attachment_id = 0) {
493 $attachment_id = (int)$attachment_id;
494
495 // Must need $attachment_id other wise not possible to get data from the table
496 if (!Utils::is_ok_to_serve($attachment_id)) {
497 return $sources;
498 }
499
500 $wpmcsItem = Item::instance();
501
502 $item = $wpmcsItem->get($attachment_id, 'media_library');
503
504 if (Utils::is_empty($item)) {
505 return $sources;
506 }
507
508 $item_extra = $wpmcsItem->get_extras($attachment_id, false, 'media_library');
509
510 if (isset($item_extra['width']) && !empty($item_extra['width'])) {
511 $sources[$item_extra['width']]=[
512 'url' => $wpmcsItem->get_url($attachment_id, 'full', 'media_library'),
513 'descriptor' => 'w',
514 'value' => $item_extra['width']
515 ];
516 }
517
518 if ($item_extra) {
519 if (isset($item_extra['sizes']) && !empty($item_extra['sizes'])) {
520 foreach ($item_extra['sizes'] as $size => $size_array) {
521 if (isset($size_array['width']) && !empty($size_array['width'])) {
522 $w = $size_array['width'];
523 if (isset($sources[$w]) && !empty($sources[$w])) {
524 $sources[$w]['url'] = $wpmcsItem->get_url($attachment_id, $size, 'media_library');
525 }
526 }
527 }
528 }
529 }
530
531 return $sources;
532 }
533
534 /**
535 * Filters the list of attachment image attributes.
536 *
537 * @since 1.0.0
538 * @param array $attr Attributes for the image markup.
539 * @param WP_Post $attachment Image attachment post.
540 * @param string|array $size Requested size. Image size or array of width and height values (in that order).
541 *
542 * @return array
543 */
544 public function wp_get_attachment_image_attributes($attr, $attachment, $size='thumbnail') {
545 if (
546 !$attachment ||
547 !Utils::is_ok_to_serve($attachment->ID)
548 ) {
549 return $attr;
550 }
551
552 $wpmcsItem = Item::instance();
553
554 $item = $wpmcsItem->get($attachment->ID, 'media_library');
555
556 if (Utils::is_empty($item)) {
557 return $attr;
558 }
559
560 $size = Utils::maybe_convert_size_to_string($attachment->ID, $size);
561 if ($size === false) {
562 return $attr;
563 }
564
565
566
567 if (
568 isset($size) && !empty($size) &&
569 isset($attr['src']) && !empty($attr['src'])
570 ) {
571 $source = $wpmcsItem->get_url($attachment->ID, $size, 'media_library');
572 if (isset($source) && !empty($source)) {
573 $attr['src'] = $source;
574 }
575 }
576
577 /**
578 * Filtered list of attachment image attributes.
579 *
580 * @param array $attr Attributes for the image markup.
581 * @param WP_Post $attachment Image attachment post.
582 * @param string $size Requested size.
583 */
584 return apply_filters('wpmcs_wp_get_attachment_image_attributes', $attr, $attachment, $size, $item);
585 }
586
587 /**
588 * Get attachment url
589 * @since 1.0.0
590 * @param string $url
591 * @param int $attachment_id
592 *
593 * @return bool|mixed|WP_Error
594 */
595 public function wp_get_attachment_url($url, $attachment_id) {
596 if (!Utils::is_ok_to_serve($attachment_id)) {
597 return $url;
598 }
599
600 $new_url = Item::instance()->get_url($attachment_id, 'full', 'media_library');
601
602 if (Utils::is_empty($new_url)) {
603 return $url;
604 }
605
606 $new_url = apply_filters('wpmcs_wp_get_attachment_url', $new_url, $url, $attachment_id);
607
608 return $new_url;
609 }
610
611 /**
612 * Upload media item
613 */
614 public function uploadMedia($attachment_meta, $attachment_id, $source_path) {
615 $wpmcsItem = Item::instance();
616 $upload_dir = wp_get_upload_dir();
617 $type = get_post_mime_type($attachment_id);
618 $is_image = (0 === strpos($type, 'image/'));
619 $existing = $wpmcsItem->get($attachment_id, 'media_library');
620 $existing_extras = $wpmcsItem->get_extras($attachment_id, false, 'media_library');
621 $has_existing = !Utils::is_empty($existing);
622 $sizes = [];
623 $uploaded = [];
624 $extras = [];
625 $prefix = '';
626 $file_path = '';
627 $file_dir = '';
628 $original_file_path = '';
629 $original_file_source_path = '';
630
631
632 // Add prefix if object versioning is ON
633 if (isset($this->settings['object_versioning']) && $this->settings['object_versioning']) {
634 $prefix = ($existing_extras && isset($existing_extras['prefix']) && !empty($existing_extras['prefix']))
635 ? $existing_extras['prefix']
636 : Utils::generate_object_versioning_prefix();
637 $extras['prefix'] = $prefix;
638 }
639
640 // Get width and height from image meta
641 if (isset($attachment_meta) && !empty($attachment_meta)) {
642 $extras['width'] = (isset($attachment_meta['width']) && !empty($attachment_meta['width'])) ? $attachment_meta['width'] : 0;
643 $extras['height'] = (isset($attachment_meta['height']) && !empty($attachment_meta['height'])) ? $attachment_meta['height'] : 0;
644 }
645
646
647 // Get Original File Name/Path from Image meta
648 $original_file = isset($attachment_meta) && !empty($attachment_meta) &&
649 isset($attachment_meta['original_image']) && !empty($attachment_meta['original_image'])
650 ? $attachment_meta['original_image'] : '';
651
652 $file_path = trailingslashit($upload_dir['basedir']) . $source_path;
653 $file_dir = isset(pathinfo($file_path)['dirname']) ? pathinfo($file_path)['dirname'] : '';
654
655 // Check whether the extension is enabled for uploading
656 if (!Utils::is_extension_available($file_path)) {
657 return $attachment_meta;
658 }
659
660 // Upload the Full Size file
661 if( $has_existing && ( $existing['source_path'] == $source_path ) ) {
662 $uploaded = [
663 'success' => true,
664 'file_url' => $existing['url'],
665 'key' => $existing['key']
666 ];
667 } else if(file_exists($file_path)){
668 $uploaded = Service::instance()->uploadSingle($file_path, $source_path, $prefix);
669 }
670
671 if(
672 isset($uploaded) && !empty($uploaded) &&
673 isset($uploaded['success']) && $uploaded['success']
674 ) {
675 if(isset($original_file) && !empty($original_file)){
676 // Find original image relative and absolute path
677 $original_file_path = trailingslashit($file_dir) . $original_file;
678 $original_file_source_path = Utils::get_attachment_source_path($original_file_path);
679 $uploaded_original = [];
680
681 // Upload Original File
682 if(
683 !Utils::is_empty($existing_extras) &&
684 isset($existing_extras['original']) && !Utils::is_empty($existing_extras['original']) &&
685 $existing_extras['original']['source_path'] == $original_file_source_path
686 ) {
687 $uploaded_original = [
688 'success' => true,
689 'file_url' => $existing_extras['original']['url'],
690 'key' => $existing_extras['original']['key']
691 ];
692 } else if(file_exists($original_file_path)) {
693 $uploaded_original = Service::instance()->uploadSingle($original_file_path, $original_file_source_path, $prefix);
694 }
695
696 if(
697 isset($uploaded_original) && !empty($uploaded_original) &&
698 isset($uploaded_original['success']) && $uploaded_original['success']
699 ) {
700 $extras['original'] = array(
701 'source_path' => $original_file_source_path,
702 'url' => $uploaded_original['file_url'],
703 'key' => $uploaded_original['key']
704 );
705 }
706 }
707
708 if (
709 $is_image &&
710 isset($attachment_meta['sizes']) && !empty($attachment_meta['sizes']) &&
711 isset($file_dir) && !empty($file_dir)
712 ) {
713 foreach ($attachment_meta['sizes'] as $size => $sub_image) {
714 $sub_size = [];
715 $sub_file = isset($sub_image['file']) ? $sub_image['file'] : false;
716 $sub_file_path = '';
717 $sub_file_source_path = '';
718 $uploaded_sub_image = [];
719
720 if ($sub_file) {
721 $sub_file_path = $file_dir . '/' . $sub_file;
722 $sub_file_source_path = Utils::get_attachment_source_path($sub_file_path);
723 }
724
725 // Upload Image size
726 if(
727 !Utils::is_empty($existing_extras) &&
728 isset($existing_extras['sizes']) && !Utils::is_empty($existing_extras['sizes']) &&
729 isset($existing_extras['sizes'][$size]) && !Utils::is_empty($existing_extras['sizes'][$size]) &&
730 $existing_extras['sizes'][$size]['source_path'] == $sub_file_source_path
731 ) {
732 $uploaded_sub_image = [
733 'success' => true,
734 'file_url' => $existing_extras['sizes'][$size]['url'],
735 'key' => $existing_extras['sizes'][$size]['key']
736 ];
737 } else if(file_exists($sub_file_path)) {
738 $uploaded_sub_image = Service::instance()->uploadSingle($sub_file_path, $sub_file_source_path, $prefix);
739 }
740
741 if (
742 isset($uploaded_sub_image) && !empty($uploaded_sub_image) &&
743 isset($uploaded_sub_image['success']) && $uploaded_sub_image['success']
744 ) {
745 $sub_size['source_path'] = $sub_file_source_path;
746 $sub_size['url'] = $uploaded_sub_image['file_url'];
747 $sub_size['key'] = $uploaded_sub_image['key'];
748 $sub_size['width'] = isset($sub_image['width']) ? $sub_image['width']: 0;
749 $sub_size['height'] = isset($sub_image['height']) ? $sub_image['height']: 0;
750 }
751
752 $sizes[$size] = $sub_size;
753 }
754 }
755
756
757 if (isset($sizes) && !empty($sizes)) {
758 $extras['sizes'] = $sizes;
759 }
760
761 return [
762 'file' => [
763 'source_path' => $source_path,
764 'url' => $uploaded['file_url'],
765 'key' => $uploaded['key'],
766 ],
767 'extra' => $extras
768 ];
769
770 }
771
772 return false;
773 }
774
775
776 /**
777 * Get Total Media Count (queried)
778 *
779 * Param added because need a generic function name,
780 * if the function calls by source_type
781 */
782 public static function get_total_media($source_type) {
783 global $wpdb;
784
785 // Fetch the count of attachments with the 'inherit' status
786 $count = $wpdb->get_var(
787 $wpdb->prepare(
788 "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s",
789 'attachment',
790 'inherit'
791 )
792 );
793
794 return (int)$count;
795 }
796
797
798 /**
799 * Edit Image Meta In View Image
800 * @since 1.0.0
801 */
802 function attachment_submitbox_metadata( ) {
803 $post = get_post();
804 $attachment_id = $post->ID;
805
806 if (!Utils::is_ok_to_serve($attachment_id)) {
807 return;
808 }
809
810 $wpmcsItem = Item::instance();
811
812 $item = $wpmcsItem->get($attachment_id, 'media_library');
813
814 if (Utils::is_empty($item)) {
815 return;
816 }
817
818 $provider = $wpmcsItem->get_field($attachment_id, 'provider', 'media_library');
819 if($provider) {
820 $label = Schema::getServiceLabels($provider); ?>
821 <div class="misc-pub-section misc-pub-provider">
822 <?php esc_html_e( 'Provider :', 'media-cloud-sync' ); ?> <strong><?php echo esc_textarea(!empty($label) ? $label : $provider); ?></strong></a>
823 </div>
824 <?php
825 }
826
827 $region = $wpmcsItem->get_field($attachment_id, 'region', 'media_library');
828 if($region) { ?>
829 <div class="misc-pub-section misc-pub-provider">
830 <?php esc_html_e( 'Region :', 'media-cloud-sync' ); ?> <strong><?php echo esc_textarea($region); ?></strong></a>
831 </div>
832 <?php
833 }
834 $private = (int)$wpmcsItem->get_field($attachment_id, 'is_private', 'media_library');
835 ?>
836 <div class="misc-pub-section misc-pub-provider">
837 <?php esc_html_e( 'Access :', 'media-cloud-sync' ); ?> <strong><?php $private ? esc_html_e( 'Private', 'media-cloud-sync' ) : esc_html_e( 'Public', 'media-cloud-sync' ); ?></strong></a>
838 </div>
839 <?php
840 }
841
842
843 /**
844 * Function to get attachment details by ID
845 */
846 public function ajax_get_attachment_details() {
847 $result = array(
848 'status' => false,
849 'data' => array(),
850 'exclude' => false
851 );
852
853 if ( ! isset( $_POST['id'] ) ) {
854 wp_send_json_success( $result );
855 }
856
857 check_ajax_referer( 'get_media_provider_details', '_nonce' );
858
859 $id= intval( sanitize_text_field( $_POST['id'] ) );
860
861 // Return if extension not allowed
862 $path = get_attached_file( $id );
863 if(!Utils::is_extension_available($path)) {
864 $result['exclude'] = true;
865 wp_send_json_success( $result );
866 }
867
868 if (!Utils::is_ok_to_serve($id)) {
869 wp_send_json_success( $result );
870 }
871
872 $wpmcsItem = Item::instance();
873
874 $item = $wpmcsItem->get($id, 'media_library');
875
876 if (Utils::is_empty($item)) {
877 wp_send_json_success( $result );
878 }
879
880 $provider = $wpmcsItem->get_field($id, 'provider', 'media_library');
881 if($provider){
882 $label = Schema::getServiceLabels($provider);
883 $item['provider'] = !empty($label) ? $label : $provider;
884 }
885 $region = $wpmcsItem->get_field($id, 'region', 'media_library');
886 if($region){
887 $item['region'] = $region;
888 }
889 $item['private'] = $wpmcsItem->get_field($id, 'private', 'media_library');
890 if($item) {
891 $result= array(
892 'status' => true,
893 'data' => $item
894 );
895 }
896
897 wp_send_json_success( $result );
898 }
899
900 /**
901 * Takes notice that an attachment is about to be deleted and prepares for it.
902 *
903 * @handles pre_delete_attachment
904 *
905 * @param bool|null $delete Whether to go forward with deletion.
906 *
907 * @return bool|null
908 */
909 public function pre_delete_attachment( $delete ) {
910 if ( is_null( $delete ) ) {
911 $this->deleting_attachment = true;
912 }
913
914 return $delete;
915 }
916
917 /**
918 * Takes notice that an attachment has been deleted and undoes previous preparations for the event.
919 *
920 * @handles delete_post
921 *
922 * Note: delete_post is used as there is a potential that deleted_post is not reached.
923 */
924 public function delete_post() {
925 $this->deleting_attachment = false;
926 }
927
928 /**
929 * Is installed?
930 *
931 * @return bool
932 */
933 public static function is_installed(): bool {
934 // It is inbuilt in worpress
935 return true;
936 }
937
938
939
940 public static function instance(){
941 if (is_null(self::$instance)) {
942 self::$instance = new self();
943 }
944 return self::$instance;
945 }
946 }