-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-cache-point.php
More file actions
980 lines (887 loc) · 26.3 KB
/
class-cache-point.php
File metadata and controls
980 lines (887 loc) · 26.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
<?php
/**
* Handles cache point management.
*
* @package Cloudinary
*/
namespace Cloudinary\Cache;
use Cloudinary\Cache;
use Cloudinary\Cache\Cache_Controller;
/**
* Class Cache Point.
*
* Handles managing cache points.
*/
class Cache_Point {
/**
* The plugin instance.
*
* @var Cache
*/
protected $cache;
/**
* Holds the list of active cache_points.
*
* @var \WP_Post[]
*/
protected $active_cache_points = array();
/**
* Holds a list of pre-found cached urls before querying to find cached items
*
* @var array.
*/
protected $pre_cached = array();
/**
* Holds the list of registered cache_points.
*
* @var \WP_Post[]
*/
protected $registered_cache_points = array();
/**
* Holds the list of cache points requiring meta updates.
*
* @var array
*/
public $meta_updates = array();
/**
* Post type.
*
* @var \WP_Post_Type
*/
protected $post_type;
/**
* Holds the post type.
*/
const POST_TYPE_SLUG = 'cloudinary_asset';
/**
* Holds the list of items to upload.
*
* @var array
*/
protected $to_upload = array();
/**
* Holds the limit of items to sync per visitor.
*
* @var int
*/
protected $sync_limit;
/**
* Holds the meta keys.
*
* @var array
*/
const META_KEYS = array(
'excluded_urls' => 'excluded_urls',
'cached_urls' => 'cached_urls',
'src_path' => 'src_path',
'url' => 'url',
'base_url' => 'base_url',
'src_file' => 'src_file',
'last_updated' => 'last_updated',
'upload_error' => 'upload_error',
'version' => 'version',
);
/**
* Cache Point constructor.
*
* @param Cache $cache The plugin ache object.
*/
public function __construct( Cache $cache ) {
$this->cache = $cache;
/**
* Filter the on demand synced items limit.
*
* @hook cloudinary_on_demand_sync_limit
* @default 100
*
* @param $value {int} The default number of static assets.
*
* @return {int}
*/
$this->sync_limit = apply_filters( 'cloudinary_on_demand_sync_limit', 100 );
$this->register_post_type();
add_filter( 'update_post_metadata', array( $this, 'update_meta' ), 10, 4 );
add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 3 );
add_filter( 'delete_post_metadata', array( $this, 'delete_meta' ), 10, 4 );
add_action( 'shutdown', array( $this, 'meta_updates' ) );
add_action( 'wp_resource_hints', array( $this, 'dns_prefetch' ), 10, 2 );
}
/**
* Add DNS prefetch link tag for assets.
*
* @param array $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
*
* @return array
*/
public function dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' === $relation_type && ! empty( $this->active_cache_points ) ) {
$urls[] = $this->cache->media->base_url;
}
return $urls;
}
/**
* Update our cache point meta data.
*
* @param null|bool $check The check to allow short circuit of get_metadata.
* @param int $object_id The object ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*
* @return bool|null
*/
public function update_meta( $check, $object_id, $meta_key, $meta_value ) {
if ( self::POST_TYPE_SLUG === get_post_type( $object_id ) ) {
$check = true;
$meta = $this->get_meta_cache( $object_id );
if ( ! isset( $meta[ $meta_key ] ) || $meta_value !== $meta[ $meta_key ] ) {
$meta[ $meta_key ] = $meta_value;
$check = $this->set_meta_cache( $object_id, $meta );
}
}
return $check;
}
/**
* Delete our cache point meta data.
*
* @param null|bool $check The check to allow short circuit of get_metadata.
* @param int $object_id The object ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*
* @return bool
*/
public function delete_meta( $check, $object_id, $meta_key, $meta_value ) {
if ( self::POST_TYPE_SLUG === get_post_type( $object_id ) ) {
$check = false;
$meta = $this->get_meta_cache( $object_id );
if ( isset( $meta[ $meta_key ] ) && $meta[ $meta_key ] === $meta_value || is_null( $meta_value ) ) {
unset( $meta[ $meta_key ] );
$check = $this->set_meta_cache( $object_id, $meta );
}
}
return $check;
}
/**
* Get our cache point meta data.
*
* @param null|bool $check The check to allow short circuit of get_metadata.
* @param int $object_id The object ID.
* @param string $meta_key The meta key.
*
* @return mixed
*/
public function get_meta( $check, $object_id, $meta_key ) {
if ( self::POST_TYPE_SLUG === get_post_type( $object_id ) ) {
$meta = $this->get_meta_cache( $object_id );
$value = '';
if ( empty( $meta_key ) ) {
$value = $meta;
} elseif ( isset( $meta[ $meta_key ] ) ) {
$value = array();
$value[] = $meta[ $meta_key ];
}
return $value;
}
return $check;
}
/**
* Get meta data for a cache point.
*
* @param int $object_id The post ID.
*
* @return mixed
*/
protected function get_meta_cache( $object_id ) {
$meta = wp_cache_get( $object_id, 'cloudinary_asset' );
if ( ! $meta ) {
$post = get_post( $object_id );
$meta = json_decode( $post->post_content, true );
wp_cache_add( $object_id, $meta, 'cloudinary_asset' );
}
return $meta;
}
/**
* Set meta data for a cache point.
*
* @param int $object_id The post ID.
* @param mixed $meta The meta to set.
*
* @return bool
*/
protected function set_meta_cache( $object_id, $meta ) {
if ( ! in_array( $object_id, $this->meta_updates, true ) ) {
$this->meta_updates[] = $object_id;
}
return wp_cache_replace( $object_id, $meta, 'cloudinary_asset' );
}
/**
* Compiles all metadata and preps upload at shutdown.
*/
public function meta_updates() {
foreach ( $this->meta_updates as $id ) {
$meta = $this->get_meta_cache( $id );
$params = array(
'ID' => $id,
'post_content' => wp_json_encode( $meta ),
);
wp_update_post( $params );
}
// Prep the upload for un-synced items.
if ( ! empty( $this->to_upload ) ) {
$api = $this->cache->plugin->get_component( 'api' );
if ( $api ) {
$api->background_request( 'upload_cache', array( 'ids' => $this->to_upload ), 'POST' );
}
}
}
/**
* Init the cache_points.
*/
public function init() {
$params = array(
'post_type' => self::POST_TYPE_SLUG,
'post_status' => array( 'enabled', 'disabled' ),
'post_parent' => 0,
'posts_per_page' => 100,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$query = new \WP_Query( $params );
foreach ( $query->get_posts() as $post ) {
$this->registered_cache_points[ $post->post_title ] = $post;
}
do_action( 'cloudinary_cache_init_cache_points' );
}
/**
* Checks if the cache point is registered.
*
* @param string $url the URL to check.
*
* @return bool
*/
protected function is_registered( $url ) {
$url = trailingslashit( $url );
return isset( $this->registered_cache_points[ $url ] );
}
/**
* Register a cache path.
*
* @param string $url The URL to register.
* @param string $src_path The source path to register.
* @param string $version The version of the cache point.
*/
public function register_cache_path( $url, $src_path, $version ) {
$this->create_cache_point( $url, $src_path, $version );
$this->activate_cache_point( $url );
}
/**
* Enable a cache path.
*
* @param string $url The path to enable.
*/
public function activate_cache_point( $url ) {
$url = trailingslashit( $url );
if ( $this->is_registered( $url ) ) {
$cache_point = $this->registered_cache_points[ $url ];
$this->active_cache_points[ $url ] = $cache_point;
// Init the metadata.
$this->get_meta_cache( $cache_point->ID );
}
}
/**
* Add the url to the cache point's exclude list.
*
* @param int $cache_point_id The cache point ID to add to.
* @param string $url The url to add.
*/
public function exclude_url( $cache_point_id, $url ) {
$excludes = get_post_meta( $cache_point_id, self::META_KEYS['excluded_urls'], true );
if ( empty( $excludes ) ) {
$excludes = array();
}
if ( ! in_array( $url, $excludes, true ) ) {
$excludes[] = $url;
update_post_meta( $cache_point_id, self::META_KEYS['excluded_urls'], $excludes );
}
}
/**
* Add the url to the cache point's exclude list.
*
* @param int $cache_point_id The cache point ID to add to.
* @param string $url The url to add.
*/
public function remove_excluded_url( $cache_point_id, $url ) {
$excludes = get_post_meta( $cache_point_id, self::META_KEYS['excluded_urls'], true );
if ( ! empty( $excludes ) ) {
$index = array_search( $url, (array) $excludes, true );
if ( false !== $index ) {
unset( $excludes[ $index ] );
update_post_meta( $cache_point_id, self::META_KEYS['excluded_urls'], $excludes );
}
}
}
/**
* Checks if the file url is valid (exists).
*
* @param string $url The url to test.
*
* @return bool
*/
protected function is_valid_url( $url ) {
static $validated_urls = array();
if ( isset( $validated_urls[ $url ] ) ) {
return $validated_urls[ $url ];
}
$validated_urls[ $url ] = ! is_null( $this->url_to_path( $url ) );
return $validated_urls[ $url ];
}
/**
* Get all active cache_points.
*
* @param bool $ids_only Flag to get only the ids.
*
* @return int[]|\WP_Post[]
*/
public function get_active_cache_points( $ids_only = false ) {
$return = $this->active_cache_points;
if ( $ids_only ) {
$return = array_map(
function ( $post ) {
return $post->ID;
},
$return
);
}
return $return;
}
/**
* Convert a URl to a path.
*
* @param string $url The URL to convert.
*
* @return string
*/
public function url_to_path( $url ) {
$url = $this->clean_url( $url );
$src_path = $this->cache->file_system->get_src_path( $url );
if ( $this->cache->file_system->is_dir( $src_path ) ) {
$src_path = trailingslashit( $src_path );
}
return $src_path;
}
/**
* Load a cache point from a url.
*
* @param string $url The cache point url to get.
*
* @return \WP_Post | null
*/
protected function load_cache_point( $url ) {
if ( ! isset( $this->registered_cache_points[ $url ] ) ) {
$key = $this->get_key_name( $url );
$url = trailingslashit( $url );
$cache_point = null;
$params = array(
'name' => $key,
'post_type' => self::POST_TYPE_SLUG,
'posts_per_page' => 1,
'suppress_filters' => false,
);
$found = get_posts( $params ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
if ( ! empty( $found ) ) {
$cache_point = array_shift( $found );
$this->registered_cache_points[ $url ] = $cache_point;
}
}
return isset( $this->registered_cache_points[ $url ] ) ? $this->registered_cache_points[ $url ] : null;
}
/**
* Get a cache point from a url.
*
* @param string $url The cache point url to get.
*
* @return \WP_Post
*/
public function get_cache_point( $url ) {
// Lets check if the cache_point is a file.
if ( pathinfo( $url, PATHINFO_EXTENSION ) ) {
return $this->get_parent_cache_point( $url );
}
$url = trailingslashit( $url );
$cache_point = null;
if ( isset( $this->active_cache_points[ $url ] ) ) {
$cache_point = $this->active_cache_points[ $url ];
} else {
$cache_point = $this->load_cache_point( $url );
}
return $cache_point;
}
/**
* Get the parent cache point for a file URL.
*
* @param string $url The url of the file.
*
* @return \WP_Post|null
*/
protected function get_parent_cache_point( $url ) {
$parent = null;
foreach ( $this->active_cache_points as $key => $cache_point ) {
if ( false !== strpos( $url, $key ) ) {
$excludes = (array) get_post_meta( $cache_point->ID, self::META_KEYS['excluded_urls'], true );
if ( ! in_array( $url, $excludes, true ) ) {
$parent = $cache_point;
}
break;
}
}
return $parent;
}
/**
* Get all cache items for a cache point.
*
* @param string|int $cache_point_id_url The cache point ID or URL.
* @param bool $id_only Flag to get ID's only.
*
* @return \WP_Post[]|int[]
*/
public function get_cache_items( $cache_point_id_url, $id_only = false ) {
$items = array();
if ( ! is_int( $cache_point_id_url ) ) {
$cache_point = $this->get_cache_point( $cache_point_id_url );
} else {
$cache_point = get_post( $cache_point_id_url );
}
if ( ! is_null( $cache_point ) ) {
$params = array(
'post_type' => self::POST_TYPE_SLUG,
'posts_per_page' => 100,
'post_status' => array( 'enabled', 'disabled' ),
'post_parent' => $cache_point->ID,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'paged' => 1,
);
if ( true === $id_only ) {
$params['fields'] = 'ids';
}
$posts = new \WP_Query( $params );
do {
$found = $posts->get_posts();
$items = array_merge( $items, $found );
++$params['paged'];
$posts = new \WP_Query( $params );
} while ( $posts->have_posts() );
}
return $items;
}
/**
* Get a cache point from a url.
*
* @param Int $id The cache point ID to get cache for.
* @param string|null $search Optional search.
* @param int $page The page or results to load.
*
* @return array
*/
public function get_cache_point_cache( $id, $search = null, $page = 1 ) {
$cache_point = get_post( $id );
if ( is_null( $cache_point ) ) {
return array();
}
$cached_items = (array) get_post_meta( $cache_point->ID, self::META_KEYS['cached_urls'], true );
$excluded = (array) get_post_meta( $cache_point->ID, self::META_KEYS['excluded_urls'], true );
$cached_items = array_filter( $cached_items );
$args = array(
'post_type' => self::POST_TYPE_SLUG,
'posts_per_page' => 20,
'paged' => $page,
'post_parent' => $id,
'post_status' => array( 'enabled', 'disabled' ),
);
if ( ! empty( $search ) ) {
$args['s'] = $search;
}
$posts = new \WP_Query( $args );
$items = array();
foreach ( $posts->get_posts() as $post ) {
$meta = get_post_meta( $post->ID );
$has = array_intersect_key( $meta[ self::META_KEYS['cached_urls'] ], $cached_items );
if ( empty( $has ) ) {
continue; // Not yet uploaded.
}
$items[] = array(
'ID' => $post->ID,
'key' => $post->post_name,
'local_url' => $meta[ self::META_KEYS['base_url'] ],
'short_url' => str_replace( $cache_point->post_title, '', $meta[ self::META_KEYS['base_url'] ] ),
'active' => ! in_array( $meta[ self::META_KEYS['base_url'] ], $excluded, true ),
);
}
$total_items = count( $items );
$pages = ceil( $total_items / 20 );
// translators: The current page and total pages.
$description = sprintf( __( 'Page %1$d of %2$d', 'cloudinary' ), $page, $pages );
// translators: The number of files.
$totals = sprintf( _n( '%d cached file', '%d cached files', $total_items, 'cloudinary' ), $total_items );
$return = array(
'items' => $items,
'total' => $total_items,
'total_pages' => $pages,
'current_page' => $page,
'nav_text' => $totals . ' | ' . $description,
);
if ( empty( $items ) ) {
if ( ! empty( $search ) ) {
$return['nav_text'] = __( 'No items found.', 'cloudinary' );
} else {
$return['nav_text'] = __( 'No items cached.', 'cloudinary' );
}
}
return $return;
}
/**
* Create a new cache point from a url.
*
* @param string $url The url to create the cache point for.
* @param string $src_path The path to be cached.
* @param string $version The version of the cache point.
*/
public function create_cache_point( $url, $src_path, $version ) {
if ( ! $this->is_registered( $url ) ) {
$key = $this->get_key_name( $url );
$url = trailingslashit( $url );
$src_path = str_replace( ABSPATH, '', trailingslashit( $src_path ) );
// Add meta data.
$meta = array(
self::META_KEYS['excluded_urls'] => array(),
self::META_KEYS['cached_urls'] => array(),
self::META_KEYS['src_path'] => $src_path,
self::META_KEYS['url'] => $url,
self::META_KEYS['version'] => $version,
);
// Create new Cache point.
$params = array(
'post_name' => $key,
'post_type' => self::POST_TYPE_SLUG,
'post_title' => $url,
'post_content' => wp_json_encode( $meta ),
'post_status' => 'enabled',
);
$post_id = wp_insert_post( $params );
$this->registered_cache_points[ $url ] = get_post( $post_id );
}
$this->check_version( $url, $version );
}
/**
* Check and update the version if needed.
*
* @param string $url The url of the cache point.
* @param string $version the version.
*/
protected function check_version( $url, $version ) {
$cache_point = $this->get_cache_point( $url );
if ( ! is_numeric( $cache_point ) ) {
$prev_version = get_post_meta( $cache_point->ID, self::META_KEYS['version'], true );
if ( $prev_version !== $version ) {
update_post_meta( $cache_point->ID, self::META_KEYS['version'], $version );
}
}
}
/**
* Get a key name for a cache point.
*
* @param string $url The url to get the key name for.
*
* @return string
*/
protected function get_key_name( $url ) {
return md5( trailingslashit( $this->clean_url( $url ) ) );
}
/**
* Checks to see if a url is cacheable.
*
* @param string $url The URL to check if it can sync.
*
* @return bool
*/
public function can_cache_url( $url ) {
return ! is_null( $this->get_parent_cache_point( $url ) );
}
/**
* Clean URLs te remove any query arguments and fragments.
*
* @param string $url The URL to clean.
*
* @return string
*/
public function clean_url( $url ) {
$default = array(
'scheme' => '',
'host' => '',
'path' => '',
);
$parts = wp_parse_args( wp_parse_url( $url ), $default );
return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}
/**
* Get cached urls from cachepoint cache.
*
* @param array $urls List of URLS to extract.
*
* @return array
*/
protected function pre_cache_urls( $urls ) {
foreach ( $urls as $index => $url ) {
$cache_point = $this->get_cache_point( $url );
if ( $cache_point ) {
$cached_urls = get_post_meta( $cache_point->ID, self::META_KEYS['cached_urls'], true );
if ( isset( $cached_urls[ $url ] ) ) {
$this->pre_cached[ $url ] = $cached_urls[ $url ];
unset( $urls[ $index ] );
}
}
}
return $urls;
}
/**
* Purge the entire cache for a cache point.
*
* @param int $id The cache point post ID.
*
* @return bool
*/
public function purge_cache( $id ) {
$return = false;
$cache_point = get_post( $id );
if ( ! is_null( $cache_point ) ) {
$items = $this->get_cache_items( $cache_point->ID, true );
foreach ( $items as $cache_item ) {
update_post_meta( $cache_item, self::META_KEYS['cached_urls'], array() );
}
update_post_meta( $cache_point->ID, self::META_KEYS['cached_urls'], array() );
$return = true;
}
return $return;
}
/**
* Filter out duplicate urls that have different query and fragments.
* We should only have a single base url per asset to prevent creating duplicate base items.
*
* @param string $url The url to test.
*
* @return bool
*/
protected function filter_duplicate_base( $url ) {
static $urls = array();
$clean = $this->clean_url( $url );
if ( isset( $urls[ $clean ] ) ) {
return false;
}
$urls[ $clean ] = true;
return true;
}
/**
* Version a URL.
*
* @param string $url The url to add a version to.
*
* @return string
*/
protected function version_url( $url ) {
$url = $this->clean_url( $url );
$cache_point = $this->get_cache_point( $url );
$version = get_post_meta( $cache_point->ID, self::META_KEYS['version'], true );
return add_query_arg( 'version', $version, $url );
}
/**
* Convert a list of local URLS to Cached.
*
* @param array $urls List of local URLS to get cached versions.
*
* @return array|null
*/
public function get_cached_urls( $urls ) {
$active_ids = $this->get_active_cache_points( true );
if ( empty( $active_ids ) ) {
return null;
}
$urls = array_filter( $urls, array( $this, 'can_cache_url' ) );
if ( empty( $urls ) ) {
return null;
}
$urls = $this->pre_cache_urls( array_map( array( $this, 'version_url' ), $urls ) );
$found_posts = $this->pre_cached;
if ( ! empty( $urls ) ) {
$queried_items = $this->query_cached_items( $urls );
if ( ! empty( $queried_items ) ) {
$found_posts += $queried_items;
}
}
$missing = array_diff( $urls, array_keys( $found_posts ) );
$missing = array_filter( $missing, array( $this, 'filter_duplicate_base' ) );
if ( ! empty( $missing ) ) {
$this->prepare_cache( $missing );
}
// Remove urls that are local to improve replace performance.
$found_posts = array_filter(
$found_posts,
function ( $key, $value ) {
return $key !== $value;
},
ARRAY_FILTER_USE_BOTH
);
return $found_posts;
}
/**
* Add item to be synced later.
*
* @param int $id The cloudinary_asset post type ID to sync.
*/
protected function prepare_for_sync( $id ) {
if ( count( $this->to_upload ) < $this->sync_limit ) {
$this->to_upload[] = $id;
}
}
/**
* Query cached items that are not cached in the cache point meta (purged, new, evaluated).
* This will add items to the to_upload to re-evaluate, and re-upload if needed.
*
* @param array $urls The urls to query.
*
* @return array
*/
public function query_cached_items( $urls ) {
$clean_urls = array_map( array( $this, 'clean_url' ), $urls );
$keys = array_map( array( $this, 'get_key_name' ), $urls );
$params = array(
'post_type' => self::POST_TYPE_SLUG,
'post_name__in' => array_unique( $keys ),
'posts_per_page' => 100,
'post_status' => array( 'enabled', 'disabled' ),
'post_parent__in' => $this->get_active_cache_points( true ),
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'paged' => 1,
);
$posts = new \WP_Query( $params );
do {
$all = $posts->get_posts();
$found_posts = array();
foreach ( $all as $index => $post ) {
$meta = get_post_meta( $post->ID );
$excludes = get_post_meta( $post->post_parent, self::META_KEYS['excluded_urls'], true );
if ( in_array( $meta[ self::META_KEYS['base_url'] ], $excludes, true ) ) {
// Add it as local, since this is being ignored.
$found_posts[ $meta[ self::META_KEYS['base_url'] ] ] = $meta[ self::META_KEYS['base_url'] ];
continue;
}
$indexes = array_keys( $clean_urls, $meta[ self::META_KEYS['base_url'] ], true );
if ( empty( $indexes ) ) {
continue; // Shouldn't happen, but bail in case.
}
foreach ( $indexes as $key ) {
$url = $urls[ $key ];
if (
! isset( $meta[ self::META_KEYS['cached_urls'] ][ $url ] )
|| (
$url === $meta[ self::META_KEYS['cached_urls'] ][ $url ]
&& $meta[ self::META_KEYS['last_updated'] ] < time() - MINUTE_IN_SECONDS * 10
)
) {
// Send to upload prep.
$this->prepare_for_sync( $post->ID );
$meta[ self::META_KEYS['cached_urls'] ][ $url ] = $url;
update_post_meta( $post->ID, self::META_KEYS['cached_urls'], $meta[ self::META_KEYS['cached_urls'] ] );
}
$found_posts[ $url ] = $meta[ self::META_KEYS['cached_urls'] ][ $url ];
}
}
++$params['paged'];
$posts = new \WP_Query( $params );
} while ( $posts->have_posts() );
return $found_posts;
}
/**
* Prepare a list of urls to be cached.
*
* @param array $urls List of urls to cache.
*/
public function prepare_cache( $urls ) {
foreach ( $urls as $url ) {
$base_url = $this->clean_url( $url );
$cache_point = $this->get_cache_point( $base_url );
if ( is_null( $cache_point ) || $this->exists( $base_url ) ) {
continue;
}
$file = $this->url_to_path( $url );
if ( is_null( $file ) ) {
$this->exclude_url( $cache_point->ID, $url );
continue;
}
$meta = array(
self::META_KEYS['base_url'] => $base_url,
self::META_KEYS['cached_urls'] => array(
$url => $url,
),
self::META_KEYS['src_file'] => $file,
self::META_KEYS['last_updated'] => time(),
);
$args = array(
'post_type' => self::POST_TYPE_SLUG,
'post_title' => $base_url,
'post_content' => wp_json_encode( $meta ),
'post_name' => $this->get_key_name( $base_url ), // Has the name for uniqueness, and length.
'post_status' => 'enabled',
'post_parent' => $cache_point->ID,
);
$id = wp_insert_post( $args );
$this->prepare_for_sync( $id );
}
}
/**
* Check if the post exists to prevent creating duplicates.
*
* @param string $url The url to test.
*
* @return bool
*/
public function exists( $url ) {
$cache_name = $this->get_key_name( $url );
$args = array(
'post_type' => self::POST_TYPE_SLUG,
'post_status' => array( 'enabled', 'disabled' ),
'posts_per_page' => 1,
'name' => $cache_name,
);
$query = new \WP_Query( $args );
return (bool) $query->found_posts;
}
/**
* Register the cache point type.
*/
protected function register_post_type() {
$args = array(
'label' => __( 'Cloudinary Asset', 'cloudinary' ),
'description' => __( 'Post type to represent a non-media library asset.', 'cloudinary' ),
'labels' => array(),
'supports' => false,
'hierarchical' => true,
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'capability_type' => 'page',
);
$this->post_type = register_post_type( self::POST_TYPE_SLUG, $args ); // phpcs:ignore WordPress.NamingConventions.ValidPostTypeSlug.NotStringLiteral
}
}