-
Notifications
You must be signed in to change notification settings - Fork 37
/
amazon-s3-and-cloudfront-tweaks.php
1283 lines (1164 loc) · 42.3 KB
/
amazon-s3-and-cloudfront-tweaks.php
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
/*
Plugin Name: WP Offload Media Tweaks
Plugin URI: http://github.com/deliciousbrains/wp-amazon-s3-and-cloudfront-tweaks
Description: Examples of using WP Offload Media's filters
Author: Delicious Brains
Version: 0.6.0
Author URI: http://deliciousbrains.com
Network: True
*/
// Copyright (c) 2015 Delicious Brains. All rights reserved.
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************
use DeliciousBrains\WP_Offload_Media\Items\Item;
use DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item;
class Amazon_S3_and_CloudFront_Tweaks {
/**
* The constructor holds the `add_filter` and `add_action` statements that can be uncommented to activate them.
*
* Please only uncomment the statements you need after making sure their respective functions are correctly
* updated for your needs.
*/
public function __construct() {
/*
* WP Offload Media & WP Offload Media Lite
*
* https://deliciousbrains.com/wp-offload-media/
* https://wordpress.org/plugins/amazon-s3-and-cloudfront/
*/
/**
* Performance tuning for upgrade to custom tables in WP Offload Media 2.3.
*
* This upgrade is called `as3cf_items_table`.
*/
//add_filter( 'as3cf_update_as3cf_items_table_interval', array( $this, 'update_as3cf_items_table_interval' ) );
//add_filter( 'as3cf_update_as3cf_items_table_batch_size', array( $this, 'update_as3cf_items_table_batch_size' ) );
//add_filter( 'as3cf_update_as3cf_items_table_time_limit', array( $this, 'update_as3cf_items_table_time_limit' ) );
/*
* Settings related filters.
*
* Each setting has a filter, e.g, object-prefix ("Path" prefix in bucket) would be the following.
*/
//add_filter( 'as3cf_setting_object-prefix', array( $this, 'get_setting_object_prefix' ), 10, 1 );
//add_filter( 'as3cf_show_deprecated_domain_setting', array( $this, 'show_deprecated_domain_setting' ) );
/*
* Storage Provider related filters.
*
* Each supported Storage Provider can have client and service client args specified.
*
* The `as3cf_${provider_key}_init_client_args` filter is good for setting 'endpoint' and other settings that change how to access the provider's API.
* The `as3cf_${provider_key}_${service_key}_client_args` filter is for setting service specific changes, such as 'use_path_style_endpoint' to force bucket to be in URL path rather than domain.
*
* Amazon S3: provider_key => aws, service_key => s3
* DigitalOcean Spaces: provider_key => do, service_key => spaces
*/
//add_filter( 'as3cf_aws_init_client_args', array( $this, 'aws_init_client_args' ), 10, 1 );
//add_filter( 'as3cf_aws_s3_client_args', array( $this, 'aws_s3_client_args' ), 10, 1 );
/*
* Custom S3 API Example: MinIO
* @see https://min.io/
*/
//add_filter( 'as3cf_aws_s3_client_args', array( $this, 'minio_s3_client_args' ) );
//add_filter( 'as3cf_aws_get_regions', array( $this, 'minio_get_regions' ) );
//add_filter( 'as3cf_aws_s3_url_domain', array( $this, 'minio_s3_url_domain' ), 10, 5 );
//add_filter( 'as3cf_upload_acl', array( $this, 'minio_upload_acl' ), 10, 1 );
//add_filter( 'as3cf_upload_acl_sizes', array( $this, 'minio_upload_acl' ), 10, 1 );
//add_filter( 'as3cf_aws_s3_console_url', array( $this, 'minio_s3_console_url' ) );
//add_filter( 'as3cf_aws_s3_console_url_prefix_param', array( $this, 'minio_s3_console_url_prefix_param' ) );
/*
* Custom S3 API Example: Wasabi
* @see https://wasabi.com/
*/
//add_filter( 'as3cf_aws_s3_client_args', array( $this, 'wasabi_s3_client_args' ) );
//add_filter( 'as3cf_aws_get_regions', array( $this, 'wasabi_get_regions' ) );
//add_filter( 'as3cf_aws_s3_bucket_in_path', '__return_true' );
//add_filter( 'as3cf_aws_s3_domain', array( $this, 'wasabi_domain' ) );
//add_filter( 'as3cf_aws_s3_console_url', array( $this, 'wasabi_s3_console_url' ) );
/*
* Custom S3 API Example: Vultr Object Storage
* @see https://vultr.com/
*
* NOTE: Select "DigitalOcean" storage provider in UI,
* or use "do" provider in AS3CF_SETTINGS define,
* as this example requires that due to how Vultr uses
* region specific endpoints, and does not support
* Block All Public Access or Object Ownership,
* just like DigitalOcean.
* Please use WP Offload Media 3.2.6 or later to avoid
* getting a "403 Unauthorized" error in the delivery
* settings validation due to Vultr's hot-link protection.
*/
/* >>> REMOVE THIS COMMENT START LINE AND THE BELOW COMMENT END LINE TO ENABLE Vultr. >>>
add_filter( 'as3cf_do_get_regions', array( $this, 'vultr_get_regions' ) );
//add_filter( 'as3cf_do_spaces_bucket_in_path', '__return_true' ); // Optional
add_filter( 'as3cf_do_spaces_domain', array( $this, 'vultr_domain' ) );
// Update subscription_id in vultr_console_url function.
add_filter( 'as3cf_do_spaces_console_url', array( $this, 'vultr_console_url' ) );
add_filter( 'as3cf_do_spaces_console_url_prefix_param', array( $this, 'vultr_console_url_prefix_param' ) );
add_filter( 'as3cf_do_spaces_console_url_suffix_param', array( $this, 'vultr_console_url_suffix_param' ) );
<<< REMOVE THIS COMMENT END LINE AND THE ABOVE COMMENT START LINE TO ENABLE Vultr. <<< */
/*
* Storage related filters.
*/
//add_filter( 'as3cf_allowed_mime_types', array( $this, 'allowed_mime_types' ), 10, 1 );
//add_filter( 'as3cf_pre_update_attachment_metadata', array( $this, 'pre_update_attachment_metadata' ), 10, 4 );
//add_filter( 'as3cf_pre_upload_attachment', array( $this, 'pre_upload_attachment' ), 10, 3 );
//add_filter( 'as3cf_legacy_ms_subsite_prefix', array( $this, 'legacy_ms_subsite_prefix' ), 10, 2 );
//add_filter( 'as3cf_get_object_version_string', array( $this, 'get_object_version_string' ), 10, 1 );
//add_filter( 'as3cf_upload_object_key_as_private', array( $this, 'upload_object_key_as_private' ), 10, 3 );
//add_filter( 'as3cf_upload_object_key_as_private', array( $this, 'upload_every_object_key_as_private' ) );
//add_filter( 'as3cf_gzip_mime_types', array( $this, 'gzip_mime_types' ), 10, 2 );
//add_filter( 'as3cf_object_meta', array( $this, 'object_meta' ), 10, 4 );
//add_filter( 'as3cf_attachment_file_paths', array( $this, 'attachment_file_paths' ), 10, 3 );
//add_filter( 'as3cf_upload_attachment_local_files_to_remove', array( $this, 'upload_attachment_local_files_to_remove' ), 10, 3 );
//add_filter( 'as3cf_preserve_file_from_local_removal', array( $this, 'preserve_file_from_local_removal' ), 10, 2 );
/*
* Legacy storage related filters.
*/
//add_filter( 'as3cf_upload_acl', array( $this, 'upload_acl' ), 10, 3 );
//add_filter( 'as3cf_upload_acl_sizes', array( $this, 'upload_acl_sizes' ), 10, 4 );
/*
* URL Rewrite related filters.
*/
//add_filter( 'as3cf_local_domains', array( $this, 'local_domains' ), 10, 1 );
//add_filter( 'as3cf_use_ssl', array( $this, 'use_ssl' ), 10, 1 );
//add_filter( 'as3cf_get_attachment_url', array( $this, 'get_attachment_url' ), 10, 4 );
//add_filter( 'as3cf_wp_get_attachment_url', array( $this, 'wp_get_attachment_url' ), 10, 2 );
//add_filter( 'as3cf_get_attached_file_copy_back_to_local', array( $this, 'get_attached_file_copy_back_to_local' ), 10, 3 );
//add_filter( 'as3cf_expires', array( $this, 'default_expires' ), 10, 1 );
//add_filter( 'as3cf_cloudfront_path_parts', array( $this, 'cloudfront_path_parts' ), 10, 2 );
/*
* WP Offload Media (Pro)
*
* https://deliciousbrains.com/wp-offload-media/
*/
//add_filter( 'as3cfpro_media_actions_capability', array( $this, 'media_actions_capability' ), 10, 1 );
//add_filter( 'as3cf_seconds_between_batches', array( $this, 'seconds_between_batches' ) );
//add_filter( 'as3cf_default_time_limit', array( $this, 'default_time_limit' ) );
//add_filter( 'as3cf_tool_uploader_batch_size', array( $this, 'tool_uploader_batch_size' ) );
//add_filter( 'as3cf_tool_downloader_batch_size', array( $this, 'tool_downloader_batch_size' ) );
//add_filter( 'as3cf_tool_downloader_and_remover_batch_size', array( $this, 'tool_downloader_and_remover_batch_size' ) );
//add_filter( 'as3cf_tool_copy_buckets_batch_size', array( $this, 'tool_copy_buckets_batch_size' ) );
//add_filter( 'as3cf_tool_remove_local_files_batch_size', array( $this, 'tool_remove_local_files_batch_size' ) );
/*
* WP Offload Media - Assets Pull
*
* https://deliciousbrains.com/wp-offload-media/doc/assets-quick-start-guide/
*/
//add_filter( 'as3cf_assets_pull_test_endpoint_sslverify', array( $this, 'assets_pull_test_endpoint_sslverify' ), 10, 2 );
// If you're really brave, you can have Assets Pull also rewrite enqueued assets within the WordPress admin dashboard.
//add_filter( 'as3cf_assets_enable_wp_admin_rewrite', '__return_true' );
}
/**
* Change the interval between upgrade batch runs. Default: 2 (mins).
*
* @handles `as3cf_update_as3cf_items_table_interval`
*
* @param int $mins
*
* @return int
*/
public function update_as3cf_items_table_interval( $mins ) {
// For faster processing, set to smallest cron interval, 1 minute.
return 1;
}
/**
* Change the max number of attachments to be processed per batch run. Default: 500.
*
* @handles `as3cf_update_as3cf_items_table_batch_size`
*
* @param int $batch_size
*
* @return int
*/
public function update_as3cf_items_table_batch_size( $batch_size ) {
// For faster processing, process up to 1,000 items per batch run.
return 1000;
}
/**
* Change the max time each batch run can last. Default: 20 (seconds)
*
* @handles `as3cf_update_as3cf_items_table_time_limit`
*
* @param int $seconds
*
* @return int
*/
public function update_as3cf_items_table_time_limit( $seconds ) {
// Give each batch a few more seconds to run.
return 25;
}
/**
* The "as3cf_setting_{key} filter allows your to override specific settings before they are used.
*
* @handles `as3cf_setting_object-prefix`
*
* @param mixed $value
*
* @return string
*
* Note: Settings keys can be found in the Settings Constants doc.
* https://deliciousbrains.com/wp-offload-media/doc/settings-constants/
*/
public function get_setting_object_prefix( $value ) {
return '/my/custompath/';
}
/**
* Show the old Domain options in the Media Library settings tab.
*
* @handles `as3cf_show_deprecated_domain_setting`
*
* @param bool $show
*
* @return bool
*/
public function show_deprecated_domain_setting( $show ) {
return true;
}
/**
* This filter allows you to adjust the arguments passed to the provider's SDK client.
*
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#___construct
*
* @handles `as3cf_aws_init_client_args`
*
* @param array $args
*
* @return array
*
* Note: A good place for changing 'endpoint', 'credentials' or 'signature_version' for all API requests.
*/
public function aws_init_client_args( $args ) {
// Example forces SDK to use the restricted 'cn-north-1' region.
$args['region'] = 'cn-north-1';
return $args;
}
/**
* This filter allows you to adjust the arguments passed to the provider's service specific SDK client.
*
* The service specific SDK client is created from the initial provider SDK client, and inherits most of its config.
* The service specific SDK client is re-created more often than the provider SDK client for specific scenarios, so if possible
* set overrides in the provider client rather than service client for a slight improvement in performance.
*
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#___construct
*
* @handles `as3cf_aws_s3_client_args`
*
* @param array $args
*
* @return array
*
* Note: A good place for changing 'signature_version', 'use_path_style_endpoint' etc. for specific bucket/object actions.
*/
public function aws_s3_client_args( $args ) {
// Example forces SDK to use endpoint URLs with bucket name in path rather than domain name.
$args['use_path_style_endpoint'] = true;
return $args;
}
/*
* >>> MinIO Examples Start
*/
/**
* This filter allows you to adjust the arguments passed to the provider's service specific SDK client.
*
* The service specific SDK client is created from the initial provider SDK client, and inherits most of its config.
* The service specific SDK client is re-created more often than the provider SDK client for specific scenarios, so if possible
* set overrides in the provider client rather than service client for a slight improvement in performance.
*
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#___construct
* @see https://docs.min.io/docs/how-to-use-aws-sdk-for-php-with-minio-server.html
*
* @handles `as3cf_aws_s3_client_args`
*
* @param array $args
*
* @return array
*
* Note: A good place for changing 'signature_version', 'use_path_style_endpoint' etc. for specific bucket/object actions.
*/
public function minio_s3_client_args( $args ) {
// Example changes endpoint to connect to a local MinIO server configured to use port 54321 (the default MinIO port is 9000).
$args['endpoint'] = 'http://127.0.0.1:54321';
// Example forces SDK to use endpoint URLs with bucket name in path rather than domain name as required by MinIO.
$args['use_path_style_endpoint'] = true;
return $args;
}
/**
* This filter allows you to add or remove regions for the provider.
*
* @handles `as3cf_aws_get_regions`
*
* @param array $regions
*
* @return array
*
* MinIO regions, like Immortals in Highlander, there can be only one.
*/
public function minio_get_regions( $regions ) {
$regions = array(
'us-east-1' => 'Default',
);
return $regions;
}
/**
* This filter allows you to change the URL used for serving the files.
*
* @handles `as3cf_aws_s3_url_domain`
*
* @param string $domain
* @param string $bucket
* @param string $region
* @param int $expires
* @param array $args Allows you to specify custom URL settings
*
* @return string
*/
public function minio_s3_url_domain( $domain, $bucket, $region, $expires, $args ) {
// MinIO doesn't need a region prefix, and always puts the bucket in the path.
return '127.0.0.1:54321/' . $bucket;
}
/**
* Normally these filters allow you to change the default Access Control List (ACL)
* permission for an original file and its thumbnails when offloaded to bucket.
* However, MinIO doesn't do ACLs and defaults to private. So while this filter handler
* doesn't change anything in the bucket, it does tell WP Offload Media it needs sign URLs.
* In this handler we're just accepting the ACL and not bothering with any other params
* from the two filters.
*
* @handles `as3cf_upload_acl`
* @handles `as3cf_upload_acl_sizes`
*
* @param string $acl defaults to 'public-read'
*
* @return string
*
* Note: Only enable this if you are happy with signed URLs and haven't changed the bucket's policy to "Read Only" or similar.
*/
public function minio_upload_acl( $acl ) {
return 'private';
}
/**
* This filter allows you to change the base URL used to take you to the provider's console from WP Offload Media's settings.
*
* @handles `as3cf_aws_s3_console_url`
*
* @param string $url
*
* @return string
*/
public function minio_s3_console_url( $url ) {
return 'http://127.0.0.1:54321/minio/';
}
/**
* The "prefix param" denotes what should be in the console URL before the path prefix value.
*
* For example, the default for AWS/S3 is "?prefix=".
*
* The prefix is usually added to the console URL just after the bucket name.
*
* @handles `as3cf_aws_s3_console_url_prefix_param`
*
* @param $param
*
* @return string
*
* MinIO just appends the path prefix directly after the bucket name.
*/
public function minio_s3_console_url_prefix_param( $param ) {
return '/';
}
/*
* <<< MinIO Examples End
*/
/*
* >>> Wasabi Examples Start
*/
/**
* This filter allows you to adjust the arguments passed to the provider's service specific SDK client.
*
* The service specific SDK client is created from the initial provider SDK client, and inherits most of its config.
* The service specific SDK client is re-created more often than the provider SDK client for specific scenarios, so if possible
* set overrides in the provider client rather than service client for a slight improvement in performance.
*
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#___construct
* @see https://wasabi-support.zendesk.com/hc/en-us/articles/360000363572-How-do-I-use-AWS-SDK-for-PHP-with-Wasabi-
*
* @handles `as3cf_aws_s3_client_args`
*
* @param array $args
*
* @return array
*
* Note: A good place for changing 'signature_version', 'use_path_style_endpoint' etc. for specific bucket/object actions.
* Change the "eu-central-1" region in this example to match your preferred region.
*/
public function wasabi_s3_client_args( $args ) {
$args['endpoint'] = 'https://s3.eu-central-1.wasabisys.com';
$args['region'] = 'eu-central-1';
$args['use_path_style_endpoint'] = true;
return $args;
}
/**
* This filter allows you to add or remove regions for the provider.
*
* @handles `as3cf_aws_get_regions`
*
* @param array $regions
*
* @return array
*/
public function wasabi_get_regions( $regions ) {
$regions = array(
'ap-northeast-1' => 'Wasabi AP Northeast 1 (Tokyo)',
'ap-northeast-2' => 'Wasabi AP Northeast 2 (Osaka)',
'ap-southeast-1' => 'Wasabi AP Southeast 1 (Singapore)',
'ap-southeast-2' => 'Wasabi AP Southeast 2 (Sydney)',
'ca-central-1' => 'Wasabi CA Central 1 (Toronto)',
'eu-central-1' => 'Wasabi EU Central 1 (Amsterdam)',
'eu-central-2' => 'Wasabi EU Central 2 (Frankfurt)',
'eu-west-1' => 'Wasabi EU West 1 (London)',
'eu-west-2' => 'Wasabi EU West 2 (Paris)',
'us-west-1' => 'Wasabi US West 1 (Oregon)',
'us-central-1' => 'Wasabi US Central 1 (Texas)',
'us-east-1' => 'Wasabi US East 1 (N. Virginia)',
'us-east-2' => 'Wasabi US East 2 (N. Virginia)',
);
return $regions;
}
/**
* This filter allows you to change the default delivery domain for a storage provider.
*
* @handles `as3cf_aws_s3_domain`
*
* @param string $domain
*
* @return string
*/
public function wasabi_domain( $domain ) {
return 'wasabisys.com';
}
/**
* This filter allows you to change the base URL used to take you to the provider's console from WP Offload Media's settings.
*
* @handles `as3cf_aws_s3_console_url`
*
* @param string $url
*
* @return string
*/
public function wasabi_s3_console_url( $url ) {
return 'https://console.wasabisys.com/#/file_manager/';
}
/*
* <<< Wasabi Examples End
*/
/*
* >>> Vultr Examples Start
*
* NOTE: Use DigitalOcean as provider, see note in __construct.
*/
/**
* This filter allows you to add or remove regions for the provider.
*
* @handles `as3cf_do_get_regions`
*
* @param array $regions
*
* @return array
*/
public function vultr_get_regions( $regions ) {
$regions = array(
'ams1' => 'Amsterdam',
'blr1' => 'Bangalore',
'del1' => 'New Delhi',
'ewr1' => 'New Jersey',
'sjc1' => 'Silicon Valley',
'sgp1' => 'Singapore',
);
return $regions;
}
/**
* This filter allows you to change the default delivery domain for a storage provider.
*
* @handles `as3cf_do_spaces_domain`
*
* @param string $domain
*
* @return string
*/
public function vultr_domain( $domain ) {
return 'vultrobjects.com';
}
/**
* This filter allows you to change the base URL used to take you to the
* provider's console from WP Offload Media's settings.
*
* @handles `as3cf_do_spaces_console_url`
*
* @param string $url
*
* @return string
*/
public function vultr_console_url( $url ) {
// Add your subscription id here, as seen in Vultr console's URL as "id".
$subscription_id = '12345678-90ab-cdef-1234-567890abcdef';
return 'https://my.vultr.com/objectstorage/subs/detail/?id=' . $subscription_id . '&';
}
/**
* The "prefix param" denotes what should be in the console URL before the path prefix value.
*
* For example, the default for DigitalOcean/Spaces is "?path=".
*
* The prefix is usually added to the console URL just after the bucket name.
*
* @handles `as3cf_do_spaces_console_url_prefix_param`
*
* @param string $param
*
* @return string
*/
public function vultr_console_url_prefix_param( $param ) {
return '=';
}
/**
* The "suffix param" denotes what should be in the console URL after the path prefix value.
*
* The suffix is usually added to the console URL just after the bucket name and path prefix.
*
* @handles `as3cf_do_spaces_console_url_suffix_param`
*
* @param string $param
*
* @return string
*/
public function vultr_console_url_suffix_param( $param ) {
return '#buckets';
}
/*
* <<< Vultr Examples End
*/
/*
* Storage related filters.
*/
/**
* This filter allows your limit specific mime types of files that
* can be uploaded to the bucket. They will still be uploaded to the
* WordPress media library but ignored from the offload process.
*
* @handles `as3cf_allowed_mime_types`
*
* @param array $types
*
* @return array
*/
public function allowed_mime_types( $types ) {
// Disallow offload of PDFs.
unset( $types['pdf'] );
// Allow offload of PDFs.
$types['pdf'] = 'application/pdf';
return $types;
}
/**
* This filter allows the offload to the bucket to be aborted on a per attachment basis.
*
* @handles `as3cf_pre_update_attachment_metadata`
*
* @param bool $abort
* @param array $data attachment metadata
* @param int $post_id attachment ID
* @param DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item|null $old_as3cf_item
*
* @return mixed
*
* Note: Filter fires when attachment uploaded to Media Library, edited or metadata otherwise
* updated by some process.
*/
public function pre_update_attachment_metadata( $abort, $data, $post_id, $old_as3cf_item ) {
// Example stops movie files from being offloaded when added to library or metadata updated.
$file = get_post_meta( $post_id, '_wp_attached_file', true );
$extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
if ( is_string( $extension ) && in_array( $extension, array( 'mp4', 'mov' ) ) ) {
$abort = true; // abort the upload
}
return $abort;
}
/**
* This filter allows the offload to the bucket to be aborted on a per attachment basis.
*
* @handles `as3cf_pre_upload_attachment`
*
* @param bool $abort
* @param int $post_id attachment ID
* @param array $metadata attachment metadata
*
* @return mixed
*
* Note: Filter fires when attachment is about to be offloaded for any reason,
* including using Pro's bulk offload tools.
*/
public function pre_upload_attachment( $abort, $post_id, $metadata ) {
// Example stops movie files from being offloaded.
$file = get_post_meta( $post_id, '_wp_attached_file', true );
$extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
if ( is_string( $extension ) && in_array( $extension, array( 'mp4', 'mov' ) ) ) {
$abort = true; // abort the upload
}
// Example helps bulk offload tool on severely resource restricted shared hosting.
// WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors
// as `gc_collect_cycles()` could potentially impact performance of the bulk offload and WordPress.
/*
if ( false === $abort ) {
gc_collect_cycles();
}
*/
return $abort;
}
/**
* This filter allows you to change the Multisite subsite prefix used to store the object in the bucket.
*
* @handles `legacy_ms_subsite_prefix`
*
* @param string $legacy_ms_prefix defaults to '<sitename>/files/'
* @param object $details MS subsite details object
*
* @return string
*
* Note: Only fires when multisite still configured with pre WP 3.5 file paths, e.g. not using "sites/NN/" paths.
* Overrides WP Offload Media's Year/Month setting etc, but is appended to custom Path Prefix and suffixed by
* Object Versioning path if turned on.
* The `$legacy_ms_prefix` should not start with "/".
* The `$legacy_ms_prefix` should end with "/".
*/
public function legacy_ms_subsite_prefix( $legacy_ms_prefix, $details ) {
$legacy_ms_prefix = 'sites/' . $details->blog_id . '/';
return $legacy_ms_prefix;
}
/**
* This filter allows you to change the object version prefix added to files
* as they are offloaded to the bucket.
*
* @handles `as3cf_get_object_version_string`
*
* @param string $object_version
*
* @return string
*
* Note: THis filter only fires when "Object Versioning" is turn on.
* The `$object_version` contains just the version segment of the object path, not the entire key path prefix.
* The `$object_version` should not start with "/".
* The `$object_version` should end with "/".
*/
public function get_object_version_string( $object_version ) {
// This appends "my-string/" to the current object version string.
// e.g. "235959/" becomes "235959/my-string/".
$object_version .= 'my-string/';
return $object_version;
}
/**
* This filter allows you to change the public/private status of an individual file associated
* with an uploaded item before it's uploaded to the provider.
*
* This example makes primary file for every newly offloaded Media Library object private in the bucket.
*
* @handles `as3cf_upload_object_key_as_private`
*
* @param bool $is_private Should the object be private?
* @param string $object_key A unique file identifier for a composite item, e.g. image's "size" such as full, small, medium, large.
* @param Item $as3cf_item The item being uploaded.
*
* @return bool
*/
public function upload_object_key_as_private( $is_private, $object_key, $as3cf_item ) {
if ( Media_Library_Item::source_type() !== $as3cf_item->source_type() ) {
return $is_private;
}
if ( Item::primary_object_key() === $object_key ) {
return true;
}
return $is_private;
}
/**
* This filter allows you to change the public/private status of an individual file associated
* with an uploaded item before it's uploaded to the provider.
*
* This example makes every newly offloaded object private in the bucket.
*
* @handles `as3cf_upload_object_key_as_private`
*
* @param bool $is_private Should the object be private?
*
* @return bool
*/
public function upload_every_object_key_as_private( $is_private ) {
return true;
}
/**
* This filter allows you to change which file types are gzipped during offload.
*
* @handles `as3cf_gzip_mime_types`
*
* @param array $mime_types
* @param bool $media_library
*
* @return array
*/
public function gzip_mime_types( $mime_types, $media_library ) {
// Don't GZip any offloads, keep them pristine.
$mime_types = array();
// Add SVG (already is by default).
//$mime_types['svg'] = 'image/svg+xml';
// Remove SVG.
//unset( $mime_types['svg'] );
return $mime_types;
}
/**
* This filter allows your to change the arguments passed to the cloud storage SDK client when
* offloading a file to the bucket.
*
* @handles `as3cf_object_meta`
*
* @param array $args
* @param int $post_id
* @param string $image_size small, medium, large
* @param bool $copy true if the object is being copied between buckets
*
* @return array
*
* Note: Only fires for the "original" media file, image sizes etc. will be placed next to original in bucket.
*/
public function object_meta( $args, $post_id, $image_size, $copy ) {
$extension = pathinfo( $args['Key'], PATHINFO_EXTENSION );
// Example places (potentially large) movie files in a different bucket than configured.
// Also changes path prefix to match that used in CDN behavior's "Path Prefix" for this second origin.
/*
if ( in_array( $extension, array( 'mp4', 'mov' ) ) ) {
// Change bucket.
$args['Bucket'] = 'my-cheaper-infrequent-access-bucket';
// Change key (don't do this for images, thumbnails will not get new prefix and will not be usable).
$filename = pathinfo( $args['Key'], PATHINFO_FILENAME ) . '.' . $extension;
$args['Key'] = 'movies/' . $filename;
}
*/
// Example sets "Content-Disposition" header to "attachment" so that browsers download rather than play audio files.
/*
if ( in_array( $extension, array( 'mp3', 'wav' ) ) ) {
// Note, S3 format trims "-" from header names.
$args['ContentDisposition'] = 'attachment';
}
*/
return $args;
}
/**
* This filter allows you to add or remove paths of files that will be uploaded
* to the bucket. This can be used to upload associated images to an attachment used by a plugin.
*
* @handles `as3cf_attachment_file_paths`
*
* @param array $paths
* @param int $attachment_id
* @param array $metadata attachment metadata
*
* @return array
*/
public function attachment_file_paths( $paths, $attachment_id, $metadata ) {
// Example adds some backup files created for original and all thumbnails by some plugin, if they exist.
foreach ( $paths as $file ) {
$pathinfo = pathinfo( $file );
$extra_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '-backup-copy.' . $pathinfo['extension'];
if ( file_exists( $extra_file ) ) {
$paths[] = $extra_file;
}
}
return $paths;
}
/**
* This filter allows you to control the files that are being removed from the server
* after offload to the bucket.
*
* @handles `as3cf_upload_attachment_local_files_to_remove`
*
* @param array $files_to_remove
* @param int $post_id
* @param string $file_path
*
* @return array
*
* Note: Filter only fires when a media item is being (re)offloaded to bucket and "Remove Files From Server" is turned on.
*/
public function upload_attachment_local_files_to_remove( $files_to_remove, $post_id, $file_path ) {
// Example stops the original path/to/file.jpg from being removed from server when copying to bucket.
if ( 'path/to/file.jpg' === $file_path ) {
$files_to_remove = array_diff( $files_to_remove, array( $file_path ) );
}
return $files_to_remove;
}
/**
* This filter allows you to stop files from being removed from the local server
* even when using WP Offload Media's "Remove all files from server" tool.
*
* @handles `as3cf_preserve_file_from_local_removal`
*
* @param bool $preserve
* @param string $file_path
*
* @return bool
*/
public function preserve_file_from_local_removal( $preserve, $file_path ) {
// Example stops movie files from being removed from the local server.
$extension = pathinfo( $file_path, PATHINFO_EXTENSION );
if ( in_array( $extension, array( 'mp4', 'mov' ) ) ) {
return true;
}
return $preserve;
}
/*
* Legacy storage related filters.
*/
/**
* This filter allows your to change the default Access Control List (ACL)
* permission for an original file when offloaded to bucket.
*
* @handles `as3cf_upload_acl`
*
* @param string $acl defaults to 'public-read'
* @param array $data
* @param int $post_id
*
* @return string
*
* Note: This is a legacy filter, please use `as3cf_upload_object_key_as_private`.
*/
public function upload_acl( $acl, $data, $post_id ) {
return 'private';
}
/**
* This filter allows your to change the default Access Control List (ACL)
* permission for intermediate image sizes when offloaded to bucket.
*
* @handles `as3cf_upload_acl_sizes`
*
* @param string $acl defaults to 'public-read'
* @param string $size
* @param int $post_id
* @param array $metadata attachment metadata
*
* @return string
*
* Note: This is a legacy filter, please use `as3cf_upload_object_key_as_private`.
*/
public function upload_acl_sizes( $acl, $size, $post_id, $metadata ) {
// Make only thumbnail and medium image sizes private in bucket.
if ( 'medium' === $size || 'thumbnail' === $size ) {
return 'private';
}
return $acl;
}
/*
* URL Rewrite related filters.
*/
/**
* This filter allows you to alter the local domains that can be filtered to bucket URLs.
*
* If you're dynamically altering the site's URL with something like the following...
*
* define( 'WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] );
* define( 'WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] );
*
* ... then you'll need to append all known domains with this filter so that
* any URLs inserted into content with an alternate domain are matched as local.
*
* @handles `as3cf_local_domains`
*
* @param array $domains
*
* @return array
*
* Note: First entry in `$domains` *should* be akin to `siteurl`, but as returned by `wp_upload_dir()`.
* This however can be altered by domain mapping plugins or custom code as shown above.
* Therefore it's a good idea to "double down" and include configured domain as well as alternates here.
*/
public function local_domains( $domains ) {
// Example allows local URLs to be matched when site accessed as any of the 3 examples.
$domains[] = 'example.com';
$domains[] = 'example-pro.com';
$domains[] = 'example-deluxe.com';
// Example makes sure that the current multisite's canonical domain is included
// in match check even if domain mapping etc. has changed the URL of site.
if ( is_multisite() ) {
$blog_details = get_blog_details();
if ( false !== $blog_details && ! in_array( $blog_details->domain, $domains ) ) {
$domains[] = $blog_details->domain;
}
}
return $domains;
}
/**
* This filter allows you to control the scheme for bucket URLs, overrides "force-https" setting.
*