-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-assets.php
More file actions
1746 lines (1557 loc) · 45.3 KB
/
class-assets.php
File metadata and controls
1746 lines (1557 loc) · 45.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Cloudinary non media library assets.
*
* @package Cloudinary
*/
namespace Cloudinary;
use Cloudinary\Assets\Rest_Assets;
use Cloudinary\Connect\Api;
use Cloudinary\Sync;
use Cloudinary\Cron;
use Cloudinary\Traits\Params_Trait;
use Cloudinary\Utils;
use WP_Error;
/**
* Class Assets
*
* @package Cloudinary
*/
class Assets extends Settings_Component {
use Params_Trait;
/**
* Holds the plugin instance.
*
* @var Plugin Instance of the global plugin.
*/
public $plugin;
/**
* Holds the Media instance.
*
* @var Media
*/
public $media;
/**
* Holds the Delivery instance.
*
* @var Delivery
*/
public $delivery;
/**
* Post type.
*
* @var \WP_Post_Type
*/
protected $post_type;
/**
* Holds registered asset parents.
*
* @var \WP_Post[]
*/
protected $asset_parents;
/**
* Holds active asset parents.
*
* @var \WP_Post[]
*/
protected $active_parents = array();
/**
* Holds a list of found urls that need to be created.
*
* @var array
*/
protected $to_create;
/**
* Holds the ID's of assets.
*
* @var array
*/
protected $asset_ids;
/**
* Holds the Assets REST instance.
*
* @var Rest_Assets
*/
protected $rest;
/**
* Holds the post type.
*/
const POST_TYPE_SLUG = 'cloudinary_asset';
/**
* Holds the meta keys.
*
* @var array
*/
const META_KEYS = array(
'excludes' => '_excluded_urls',
'lock' => '_asset_lock',
'edits' => '_edited_assets',
);
/**
* Static instance of this class.
*
* @var self
*/
public static $instance;
/**
* Assets constructor.
*
* @param Plugin $plugin Instance of the plugin.
*/
public function __construct( Plugin $plugin ) {
parent::__construct( $plugin );
$this->media = $plugin->get_component( 'media' );
$this->delivery = $plugin->get_component( 'delivery' );
// Add activation hooks.
add_action( 'cloudinary_connected', array( $this, 'init' ) );
add_filter( 'cloudinary_admin_pages', array( $this, 'register_settings' ) );
self::$instance = $this;
// Set separator.
$this->separator = '/';
}
/**
* Init the class.
*/
public function init() {
$this->register_post_type();
$this->init_asset_parents();
$this->register_hooks();
$this->rest = new Rest_Assets( $this );
}
/**
* Register the hooks.
*/
protected function register_hooks() {
// Filters.
add_filter( 'cloudinary_is_content_dir', array( $this, 'check_asset' ), 10, 2 );
add_filter( 'cloudinary_is_media', array( $this, 'is_media' ), 10, 2 );
add_filter( 'get_attached_file', array( $this, 'get_attached_file' ), 10, 2 );
add_filter( 'cloudinary_sync_base_struct', array( $this, 'add_sync_type' ) );
add_filter( 'intermediate_image_sizes_advanced', array( $this, 'no_sizes' ), PHP_INT_MAX, 3 );
add_filter( 'cloudinary_can_sync_asset', array( $this, 'can_sync' ), 10, 2 );
add_filter( 'cloudinary_local_url', array( $this, 'local_url' ), 10, 2 );
add_filter( 'cloudinary_is_folder_synced', array( $this, 'filter_folder_sync' ), 10, 2 );
add_filter( 'cloudinary_asset_state', array( $this, 'filter_asset_state' ), 10, 2 );
add_filter( 'cloudinary_set_usable_asset', array( $this, 'check_usable_asset' ) );
// Actions.
add_action( 'cloudinary_ready', array( $this, 'setup' ) );
add_action( 'cloudinary_thread_queue_details_query', array( $this, 'connect_post_type' ) );
add_action( 'cloudinary_build_queue_query', array( $this, 'connect_post_type' ) );
add_action( 'cloudinary_string_replace', array( $this, 'add_url_replacements' ), 20 );
add_action( 'shutdown', array( $this, 'meta_updates' ) );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_cache' ), 100 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'cloudinary_delete_asset', array( $this, 'purge_parent' ) );
}
/**
* Filter the asset state to allow syncing in manual.
*
* @param int $state The current state.
* @param int $attachment_id The attachment ID.
*
* @return int
*/
public function filter_asset_state( $state, $attachment_id ) {
if ( self::is_asset_type( $attachment_id ) || ! $this->media->sync->been_synced( $attachment_id ) ) {
$state = 0;
}
return $state;
}
/**
* Filter to ensure an asset type is never identified as folder synced.
*
* @param bool $is Flag to indicate is folder synced.
* @param int $attachment_id The attachment ID.
*
* @return bool
*/
public function filter_folder_sync( $is, $attachment_id ) {
if ( self::is_asset_type( $attachment_id ) ) {
$is = false;
}
return $is;
}
/**
* Enqueue assets.
*/
public function enqueue_assets() {
if ( Utils::user_can( 'status' ) && 'on' === $this->plugin->settings->image_settings->_overlay ) {
wp_enqueue_script( 'front-overlay', $this->plugin->dir_url . 'js/front-overlay.js', array(), $this->plugin->version, true );
wp_enqueue_style( 'front-overlay', $this->plugin->dir_url . 'css/front-overlay.css', array(), $this->plugin->version );
}
}
/**
* Get the local url for an asset.
*
* @hook cloudinary_local_url
*
* @param string|false $url The url to filter.
* @param int $asset_id The asset ID.
*
* @return string|false
*/
public function local_url( $url, $asset_id ) {
if ( self::is_asset_type( $asset_id ) ) {
$url = get_the_title( $asset_id );
}
return $url;
}
/**
* Add Cloudinary menu to admin bar.
*
* @param \WP_Admin_Bar $admin_bar The admin bar object.
*/
public function admin_bar_cache( $admin_bar ) {
if ( ! Utils::user_can( 'status' ) || is_admin() ) {
return;
}
if ( 'on' === $this->plugin->settings->image_settings->_overlay ) {
$title = __( 'Disable Cloudinary status', 'cloudinary' );
} else {
$title = __( 'Enable Cloudinary status', 'cloudinary' );
}
$nonce = wp_create_nonce( 'cloudinary-cache-overlay' );
$overlay = array(
'id' => 'cloudinary-overlay',
'title' => $title,
'href' => '?cloudinary-cache-overlay=' . $nonce,
'meta' => array(
'title' => $title,
),
);
$admin_bar->add_menu( $overlay );
}
/**
* Sets the autosync to work on cloudinary_assets even when the autosync is disabled.
*
* @hook cloudinary_can_sync_asset
*
* @param bool $can The can sync check value.
* @param int $asset_id The asset ID.
*
* @return bool
*/
public function can_sync( $can, $asset_id ) {
if ( self::is_asset_type( $asset_id ) && 'off' === $this->settings->get_value( 'auto_sync' ) && 'on' === $this->settings->get_value( 'content.enabled' ) ) {
$can = true;
}
if ( $can && ! $this->plugin->get_component( 'delivery' )->is_deliverable( $asset_id ) ) {
$can = false;
}
// If the asset is set to a 'disable' state, we do not allow syncing.
if ( $can && self::is_asset_type( $asset_id ) && $this->is_disable_state( $asset_id ) ) {
$can = false;
}
return $can;
}
/**
* Verify if the asset is set to a 'disable' state.
*
* @param int $asset_id The asset ID to check.
*
* @return bool
*/
protected function is_disable_state( $asset_id ) {
global $wpdb;
$wpdb->cld_table = Utils::get_relationship_table();
$media_context = Utils::get_media_context( $asset_id );
$prepare = $wpdb->prepare( "SELECT `post_state` FROM $wpdb->cld_table WHERE post_id = %d AND media_context = %s;", (int) $asset_id, $media_context ); // phpcs:ignore WordPress.DB.PreparedSQL
$state = $wpdb->get_var( $prepare ); // phpcs:ignore WordPress.DB
return 'disable' === $state;
}
/**
* Check if the post is a asset post type.
*
* @param int $post_id The ID to check.
*
* @return bool
*/
public static function is_asset_type( $post_id ) {
$post = get_post( $post_id );
return ! in_array( $post, self::$instance->get_asset_parents(), true ) && self::POST_TYPE_SLUG === get_post_type( $post_id );
}
/**
* Filter out sizes for assets.
*
* @hook intermediate_image_sizes_advanced
*
* @param array $new_sizes The sizes to remove.
* @param array $image_meta The image meta.
* @param int|null $attachment_id The asset ID.
*
* @return array
*/
public function no_sizes( $new_sizes, $image_meta, $attachment_id = null ) {
if ( is_null( $attachment_id ) ) {
$attachment_id = $this->plugin->settings->get_param( '_currrent_attachment', 0 );
}
if ( self::is_asset_type( $attachment_id ) ) {
$new_sizes = array();
}
return $new_sizes;
}
/**
* Compiles all metadata and preps upload at shutdown.
*/
public function meta_updates() {
if ( $this->is_locked() ) {
return;
}
if ( ! empty( $this->delivery->unusable ) ) {
$assets = array();
foreach ( $this->delivery->unusable as $unusable ) {
if ( 'asset' === $unusable['sync_type'] && isset( $this->active_parents[ $unusable['parent_path'] ] ) && ! in_array( $unusable['post_id'], $assets, true ) ) {
$asset_id = (int) $unusable['post_id'];
if ( $this->media->sync->can_sync( $asset_id ) ) {
$this->media->sync->set_signature_item( $asset_id, 'cld_asset', 'reset' );
$this->media->sync->add_to_sync( $asset_id );
$assets[] = $unusable['post_id'];
}
}
}
}
// Create found asset that's not media library.
if ( ! empty( $this->to_create ) && ! empty( $this->delivery->unknown ) ) {
// Do not create assets if the image delivery is disabled.
if ( 'on' === $this->plugin->settings->get_value( 'image_delivery' ) ) {
foreach ( $this->delivery->unknown as $url ) {
if ( isset( $this->to_create[ $url ] ) ) {
$this->create_asset( $url, $this->to_create[ $url ] );
}
}
}
}
}
/**
* Set urls to be replaced.
*
* @hook cloudinary_string_replace
*/
public function add_url_replacements() {
// Due to the output buffers, this can be called multiple times.
if ( 1 < did_action( 'cloudinary_string_replace' ) ) {
return;
}
$overlay = Utils::get_sanitized_text( 'cloudinary-cache-overlay' );
$setting = $this->plugin->settings->image_settings->overlay;
if ( $overlay && wp_verify_nonce( $overlay, 'cloudinary-cache-overlay' ) ) {
$referrer = filter_input( INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_URL );
if ( $setting->get_value() === 'on' ) {
$setting->save_value( 'off' );
} else {
$setting->save_value( 'on' );
}
wp_safe_redirect( $referrer );
exit;
}
}
/**
* Connect our post type to the sync query, to allow it to be queued.
*
* @hook cloudinary_thread_queue_details_query, cloudinary_build_queue_query
*
* @param array $query The Query.
*
* @return array
*/
public function connect_post_type( $query ) {
$query['post_type'] = array_merge( (array) $query['post_type'], (array) self::POST_TYPE_SLUG );
return $query;
}
/**
* Retrieve the assets settings.
*
* This method fetches the assets settings from the configuration.
* If the assets settings are empty or the settings are locked, it returns an empty array.
*
* @return array The assets settings array.
*/
public function get_assets_settings() {
$assets = $this->settings->get_setting( 'assets' )->get_settings();
if ( empty( $assets ) || $this->is_locked() ) {
return array();
}
return $assets;
}
/**
* Update asset paths.
*
* @return void
*/
public function update_asset_paths() {
$assets = $this->get_assets_settings();
if ( empty( $assets ) ) {
return;
}
foreach ( $assets as $asset ) {
$paths = $asset->get_setting( 'paths' );
foreach ( $paths->get_settings() as $path ) {
if ( 'on' === $path->get_value() ) {
$conf = $path->get_params();
$path = urldecode( trailingslashit( $conf['url'] ) );
$version = $conf['version'];
$asset_path = $this->get_asset_parent( $path );
if ( null === $asset_path ) {
$asset_parent_id = $this->create_asset_parent( $path, $version );
if ( is_wp_error( $asset_parent_id ) ) {
return; // Bail.
}
$asset_path = get_post( $asset_parent_id );
}
// Check and update version if needed.
if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) {
$this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version );
$this->sync_parent( $asset_path->ID );
}
}
}
}
}
/**
* Activate parent assets based on the current settings and purges unused parent assets.
*
* @return void
*/
protected function activate_parents() {
$assets = $this->get_assets_settings();
if ( empty( $assets ) ) {
return;
}
foreach ( $assets as $asset ) {
$paths = $asset->get_setting( 'paths' );
foreach ( $paths->get_settings() as $path ) {
if ( 'on' === $path->get_value() ) {
$conf = $path->get_params();
self::activate_parent( urldecode( trailingslashit( $conf['url'] ) ) );
}
}
}
// Get the disabled items.
foreach ( $this->asset_parents as $url => $parent ) {
if ( isset( $this->active_parents[ $url ] ) ) {
continue;
}
$this->purge_parent( $parent->ID );
// Remove parent.
wp_delete_post( $parent->ID );
}
}
/**
* Activate a parent asset path.
*
* @param string $url The path to activate.
*/
public function activate_parent( $url ) {
$url = $this->clean_path( $url );
if ( isset( $this->asset_parents[ $url ] ) ) {
$this->active_parents[ $url ] = $this->asset_parents[ $url ];
$this->set_param( trim( $url, $this->separator ), $this->asset_parents[ $url ] );
}
}
/**
* Clean a path for saving as a title.
*
* @param string $path The path to clean.
*
* @return string
*/
public function clean_path( $path ) {
$home = Utils::clean_url( trailingslashit( Utils::home_url() ) );
$path = str_replace( $home, '', Utils::clean_url( $path ) );
if ( empty( Utils::pathinfo( $path, PATHINFO_EXTENSION ) ) ) {
$path = urldecode( trailingslashit( $path ) );
}
return $path;
}
/**
* Create an asset parent.
*
* @param string $path The path to create.
* @param string $version The version.
*
* @return int|\WP_Error
*/
public function create_asset_parent( $path, $version ) {
$path = $this->clean_path( $path );
$args = array(
'post_title' => $path,
'post_name' => md5( $path ),
'post_type' => self::POST_TYPE_SLUG,
'post_status' => 'publish',
);
$parent_id = wp_insert_post( $args );
if ( $parent_id ) {
$this->media->update_post_meta( $parent_id, Sync::META_KEYS['version'], $version );
$this->media->update_post_meta( $parent_id, self::META_KEYS['excludes'], array() );
$this->asset_parents[ $path ] = get_post( $parent_id );
}
return $parent_id;
}
/**
* Process all child assets of a parent with a given callback.
*
* @param int $parent_id The Asset parent to process.
* @param callable $callback The callback function to execute on each post.
*/
private function process_parent_assets( $parent_id, $callback ) {
$query_args = array(
'post_type' => self::POST_TYPE_SLUG,
'posts_per_page' => 100,
'post_parent' => $parent_id,
'post_status' => array( 'inherit', 'draft' ),
'fields' => 'ids',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$query = new \WP_Query( $query_args );
$previous_total = $query->found_posts;
do {
$this->lock_assets();
$posts = $query->get_posts();
foreach ( $posts as $post_id ) {
call_user_func( $callback, $post_id );
}
$query_args = $query->query_vars;
$query = new \WP_Query( $query_args );
if ( $previous_total === $query->found_posts ) {
break;
}
} while ( $query->have_posts() );
}
/**
* Sync the assets of a parent.
*
* @param int $parent_id The Asset parent to sync.
*/
public function sync_parent( $parent_id ) {
$this->process_parent_assets(
$parent_id,
function ( $post_id ) {
if ( empty( $this->media->sync ) || ! $this->media->sync->can_sync( $post_id ) ) {
return;
}
$this->media->sync->set_signature_item( $post_id, 'file', '' );
$this->media->sync->set_signature_item( $post_id, 'cld_asset' );
$this->media->sync->add_to_sync( $post_id );
}
);
}
/**
* Purge a single asset parent.
*
* @param int $parent_id The Asset parent to purge.
*/
public function purge_parent( $parent_id ) {
$this->process_parent_assets(
$parent_id,
function ( $post_id ) {
wp_delete_post( $post_id );
}
);
}
/**
* Lock asset creation for performing things like purging that require no changes.
*/
public function lock_assets() {
set_transient( self::META_KEYS['lock'], true, 10 );
}
/**
* Unlock asset creation.
*/
public function unlock_assets() {
delete_transient( self::META_KEYS['lock'] );
}
/**
* Check if assets are locked.
*
* @return bool
*/
public function is_locked() {
return get_transient( self::META_KEYS['lock'] );
}
/**
* Generate the signature for sync.
*
* @param int $asset_id The attachment/asset ID.
*
* @return string
*/
public function generate_file_signature( $asset_id ) {
$path = $this->clean_path( $this->media->local_url( $asset_id ) );
$parent = $this->get_param( $path );
$str = $asset_id;
if ( $parent ) {
$str .= $parent->post_date;
}
return $str;
}
/**
* Generate the signature for sync storage.
*
* @param int $asset_id The attachment/asset ID.
*
* @return string
*/
public function generate_storage_signature( $asset_id ) {
return $this->get_asset_storage_folder( $asset_id ) === $this->media->get_public_id( $asset_id );
}
/**
* Generate the signature for sync.
*
* @param int $attachment_id The attachment/asset ID.
*
* @return string
*/
public function generate_edit_signature( $attachment_id ) {
$sig = wp_json_encode( (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ) );
$file = Utils::get_path_from_url( $this->media->local_url( $attachment_id ), true );
return $sig . $file;
}
/**
* Upload an asset.
*
* @param int $asset_id The asset ID to upload.
*
* @return array|\WP_Error
*/
public function upload( $asset_id ) {
$connect = $this->plugin->get_component( 'connect' );
$options = array(
'use_filename' => true,
'overwrite' => false,
'resource_type' => $this->media->get_resource_type( $asset_id ),
);
$folder = untrailingslashit( $this->media->get_cloudinary_folder() );
$asset_parent = self::POST_TYPE_SLUG === Utils::get_post_parent( $asset_id )->post_type;
if ( ! empty( $asset_parent ) ) {
$folder = $this->get_asset_storage_folder( get_the_title( $asset_id ) );
$options['overwrite'] = true; // Ensure we maintain this path and filename.
$options['unique_filename'] = false; // Ensure we don't append a suffix.
}
// Add folder.
$options['folder'] = $folder;
$result = $connect->api->upload( $asset_id, $options, array() );
if ( ! is_wp_error( $result ) && isset( $result['public_id'] ) ) {
$this->media->update_post_meta( $asset_id, Sync::META_KEYS['public_id'], $result['public_id'] );
Delivery::update_size_relations_public_id( $asset_id, $result['public_id'] );
Delivery::update_size_relations_state( $asset_id, 'enable' );
$this->media->sync->set_signature_item( $asset_id, 'file' );
$this->media->sync->set_signature_item( $asset_id, 'cld_asset' );
$this->media->sync->set_signature_item( $asset_id, 'asset_storage' );
$this->plugin->get_component( 'storage' )->size_sync( $asset_id, $result['public_id'] );
}
return $result;
}
/**
* Get the storage location on Cloudinary, for an asset.
*
* @param int|string $url_id The url or asset ID.
*
* @return string
*/
public function get_asset_storage_folder( $url_id ) {
if ( is_numeric( $url_id ) ) {
$url_id = $this->media->local_url( $url_id );
}
$url = $this->clean_path( $url_id );
$domain = wp_parse_url( Utils::home_url(), PHP_URL_HOST );
$folder = wp_normalize_path( dirname( trim( $url, './' ) ) );
if ( ! empty( $domain ) ) {
$folder = path_join( $domain, $folder );
}
return $folder;
}
/**
* Validate if sync type is valid.
*
* @param int $attachment_id The attachment id to validate.
*
* @return bool
*/
public function validate_asset_sync( $attachment_id ) {
// Default is either a asset type or auto sync off, if it's a media library item.
$valid = self::is_asset_type( $attachment_id );
// Check to see if there is a parent. If there is, then the asset is enabled to be synced.
if ( true === $valid ) {
$parent = $this->find_parent( $attachment_id );
if ( ! $parent ) {
$valid = false;
}
}
return $valid;
}
/**
* Validate if sync type is valid as an edited asset.
*
* @param int $attachment_id The attachment id to validate.
*
* @return bool
*/
public function validate_edited_asset_sync( $attachment_id ) {
$valid = false;
if ( ! self::is_asset_type( $attachment_id ) ) {
$sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
if ( ! empty( $sizes ) ) {
$valid = true;
}
}
return $valid;
}
/**
* Register our sync type.
*
* @hook cloudinary_sync_base_struct
*
* @param array $structs The structure of all sync types.
*
* @return array
*/
public function add_sync_type( $structs ) {
$structs['cld_asset'] = array(
'generate' => array( $this, 'generate_file_signature' ),
'priority' => 2,
'sync' => array( $this, 'upload' ),
'validate' => array( $this, 'validate_asset_sync' ),
'state' => 'disabled',
'note' => __( 'Caching', 'cloudinary' ),
'required' => true,
'asset_state' => 0,
);
$structs['edited_asset'] = array(
'generate' => array( $this, 'generate_edit_signature' ),
'priority' => 5.3,
'sync' => array( $this, 'create_edited_asset' ),
'validate' => array( $this, 'validate_edited_asset_sync' ),
'state' => 'disabled',
'note' => __( 'Creating shadow assets', 'cloudinary' ),
'required' => false,
'realtime' => true,
);
$structs['asset_storage'] = array(
'generate' => array( $this, 'generate_storage_signature' ),
'priority' => 19,
'sync' => array( $this, 'relocate_asset' ),
'validate' => array( $this, 'validate_asset_storage' ),
'state' => 'disabled',
'note' => __( 'Updating asset storage', 'cloudinary' ),
'required' => false,
'asset_state' => 0,
);
return $structs;
}
/**
* Validate that the asset Storage matches the Public_id.
*
* @param int $asset_id The asset ID.
*
* @return bool
*/
public function validate_asset_storage( $asset_id ) {
$valid = false;
if ( self::is_asset_type( $asset_id ) && $this->media->has_public_id( $asset_id ) ) {
$location = $this->get_asset_storage_folder( $asset_id );
$public_id = $this->media->get_public_id( $asset_id );
$valid = dirname( $public_id ) !== $location;
}
return $valid;
}
/**
* Relocate an non-media library asset.
*
* @param int $asset_id The asset to relocate.
*
* @return array|WP_Error
*/
public function relocate_asset( $asset_id ) {
$connect = $this->plugin->get_component( 'connect' );
$local_url = $this->media->local_url( $asset_id );
$filename = Utils::pathinfo( $local_url, PATHINFO_FILENAME );
$new_public_id = $this->get_asset_storage_folder( $local_url );
$type = $this->media->get_resource_type( $asset_id );
$params = array(
'from_public_id' => $this->media->get_public_id( $asset_id ),
'to_public_id' => path_join( $new_public_id, $filename ),
'overwrite' => true,
);
$result = $connect->api->rename( $type, $params );
if ( ! is_wp_error( $result ) && isset( $result['public_id'] ) ) {
$this->media->update_post_meta( $asset_id, Sync::META_KEYS['public_id'], $result['public_id'] );
Delivery::update_size_relations_public_id( $asset_id, $result['public_id'] );
$this->media->sync->set_signature_item( $asset_id, 'asset_storage' );
}
return $result;
}
/**
* Create an edited asset which acts as a shadow media item for edits.
*
* @param int $attachment_id The attachment to create from.
*
* @return array
*/
public function create_edited_asset( $attachment_id ) {
$sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
$assets = $this->media->get_post_meta( $attachment_id, self::META_KEYS['edits'], true );
$current = wp_basename( get_attached_file( $attachment_id, true ) );
if ( empty( $assets ) ) {
$assets = array();
}
if ( ! empty( $sizes ) ) {
$base = dirname( $this->media->local_url( $attachment_id ) );
foreach ( $sizes as $size => $data ) {
if ( 'full-' !== substr( $size, 0, 5 ) ) {
continue;
}
$url = Utils::clean_url( path_join( $base, $data['file'] ) );
if ( wp_basename( $url ) === $current ) {
// Currently the original.
if ( isset( $assets[ $url ] ) ) {
// Restored from a crop.
wp_delete_post( $assets[ $url ] );
unset( $assets[ $url ] );
$this->media->delete_post_meta( $attachment_id, Sync::META_KEYS['relationship'] );
}
continue;
}
if ( ! isset( $assets[ $url ] ) ) {
$asset = $this->create_asset( $url, $attachment_id );
$assets[ $url ] = $asset;
}
}
$this->media->update_post_meta( $attachment_id, self::META_KEYS['edits'], $assets );
}
$this->media->sync->set_signature_item( $attachment_id, 'edited_asset' );
return $assets;
}
/**
* Init asset parents.
*/
protected function init_asset_parents() {
$args = array(
'post_type' => self::POST_TYPE_SLUG,
'post_parent' => 0,
'posts_per_page' => 100,
'paged' => 1,
'post_status' => 'publish',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$query = new \WP_Query( $args );
$this->asset_parents = array();
do {
foreach ( $query->get_posts() as $post ) {
$this->asset_parents[ $post->post_title ] = $post;
}
$args = $query->query_vars;
++$args['paged'];
$query = new \WP_Query( $args );
} while ( $query->have_posts() );
}
/**
* Check if the non-local URL should be added as an asset.
*
* @hook cloudinary_is_content_dir
*
* @param bool $is_local The is_local flag.
* @param string $url The URL to check.
*
* @return bool
*/
public function check_asset( $is_local, $url ) {
if ( $is_local || ! $this->syncable_asset( $url ) ) {
return $is_local;
}
$found = null;
$try = $this->clean_path( $url );
while ( false !== strpos( $try, $this->separator ) ) {
$try = substr( $try, 0, strripos( $try, $this->separator ) );
if ( ! empty( $try ) && $this->has_param( $try ) ) {
$found = $this->get_param( $try );
break;
}
}
if ( $found instanceof \WP_Post ) {
$is_local = true;
if ( $this->delivery->is_deliverable( $found->ID ) ) {
$this->to_create[ $url ] = $found->ID;
}
}
return $is_local;
}