-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3-image-optimizer.php
1958 lines (1891 loc) · 77.6 KB
/
s3-image-optimizer.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
/**
* S3 Image Optimizer plugin.
*
* @link https://ewww.io
* @package S3_Image_Optimizer
*/
/*
Plugin Name: S3 Image Optimizer
Plugin URI: https://wordpress.org/plugins/s3-image-optimizer/
Description: Reduce file sizes for images in S3 buckets using lossless and lossy optimization methods via the EWWW Image Optimizer.
Author: Exactly WWW
Version: 2.6.1
Requires at least: 6.3
Requires PHP: 8.1
Requires Plugins: ewww-image-optimizer
Author URI: https://ewww.io/
License: GPLv3
*/
/**
* Constants
*/
define( 'S3IO_VERSION', 261 );
// This is the full path of the plugin file itself.
define( 'S3IO_PLUGIN_FILE', __FILE__ );
// This is the path of the plugin file relative to the plugins/ folder.
define( 'S3IO_PLUGIN_FILE_REL', 's3-image-optimizer/s3-image-optimizer.php' );
add_action( 'admin_init', 's3io_admin_init' );
add_action( 'admin_menu', 's3io_admin_menu', 60 );
add_filter( 'aws_get_client_args', 's3io_addv4_args', 8 );
add_filter( 'aws_get_client_args', 's3io_dospaces' );
require_once plugin_dir_path( __FILE__ ) . 'vendor/Aws3/aws-autoloader.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/class-amazon-web-services.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/class-s3io-cli.php';
}
global $wpdb;
if ( ! isset( $wpdb->s3io_images ) ) {
$wpdb->s3io_images = $wpdb->prefix . 's3io_images';
}
/**
* Register settings and perform any upgrades during admin_init hook.
*/
function s3io_admin_init() {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
register_setting( 's3io_options', 's3io_verion' );
register_setting( 's3io_options', 's3io_bucketlist', 's3io_bucketlist_sanitize' );
register_setting( 's3io_options', 's3io_dospaces', 'trim' );
register_setting( 's3io_options', 's3io_aws_access_key_id', 'trim' );
register_setting( 's3io_options', 's3io_aws_secret_access_key', 'trim' );
if ( get_option( 's3io_version' ) < S3IO_VERSION ) {
s3io_install_table();
update_option( 's3io_version', S3IO_VERSION );
}
$aws_settings = get_option( 'aws_settings' );
if ( $aws_settings && is_array( $aws_settings ) ) {
if ( ! get_option( 's3io_aws_access_key_id' ) && ! empty( $aws_settings['access_key_id'] ) ) {
update_option( 's3io_aws_access_key_id', $aws_settings['access_key_id'] );
}
if ( ! get_option( 's3io_aws_secret_access_key' ) && ! empty( $aws_settings['secret_access_key'] ) ) {
update_option( 's3io_aws_secret_access_key', $aws_settings['secret_access_key'] );
}
}
global $wp_version;
if ( substr( $wp_version, 0, 3 ) >= 3.8 ) {
add_action( 'admin_enqueue_scripts', 's3io_progressbar_style' );
}
if ( ! function_exists( 'ewww_image_optimizer' ) ) {
add_action( 'network_admin_notices', 's3io_missing_ewww_plugin' );
add_action( 'admin_notices', 's3io_missing_ewww_plugin' );
}
if ( function_exists( 'ewww_image_optimizer' ) && ( ! function_exists( 'ewwwio' ) || ! function_exists( 'ewww_image_optimizer_filesize' ) ) ) {
add_action( 'network_admin_notices', 's3io_ewww_plugin_outdated' );
add_action( 'admin_notices', 's3io_ewww_plugin_outdated' );
}
}
/**
* Install the s3io_images table into the db for tracking image optimization.
*/
function s3io_install_table() {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $wpdb;
// See if the path column exists, and what collation it uses to determine the column index size.
if ( $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->s3io_images'" ) === $wpdb->s3io_images ) {
$current_collate = $wpdb->get_results( "SHOW FULL COLUMNS FROM $wpdb->s3io_images", ARRAY_A );
if ( ! empty( $current_collate[1]['Field'] ) && 'path' === $current_collate[1]['Field'] && strpos( $current_collate[1]['Collation'], 'utf8mb4' ) === false ) {
$path_index_size = 255;
}
}
$charset_collate = $wpdb->get_charset_collate();
if ( empty( $path_index_size ) && strpos( $charset_collate, 'utf8mb4' ) ) {
$path_index_size = 191;
} else {
$path_index_size = 255;
}
// Create a table with 6 columns: an id, the bucket name, the file path, the optimization results, optimized image size, and original image size.
$sql = "CREATE TABLE $wpdb->s3io_images (
id int(14) NOT NULL AUTO_INCREMENT,
bucket VARCHAR(100),
path text NOT NULL,
results VARCHAR(75) NOT NULL,
image_size int(10) unsigned,
orig_size int(10) unsigned,
UNIQUE KEY id (id),
KEY path_image_size (path($path_index_size),image_size)
) $charset_collate;";
// Include the upgrade library to initialize a table.
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
add_option( 's3io_optimize_urls', '', '', 'no' );
}
/**
* Generates css include for progressbars to match admin style.
*/
function s3io_progressbar_style() {
if ( function_exists( 'wp_add_inline_style' ) ) {
wp_add_inline_style( 'jquery-ui-progressbar', '.ui-widget-header { background-color: ' . s3io_admin_background() . '; }' );
}
}
/**
* Determines the background color to use based on the selected admin theme.
*
* @return string The background color in hex notation.
*/
function s3io_admin_background() {
global $s3io_admin_color;
if ( ! empty( $s3io_admin_color ) && preg_match( '/^\#([0-9a-fA-F]){3,6}$/', $s3io_admin_color ) ) {
return $s3io_admin_color;
}
if ( function_exists( 'wp_add_inline_style' ) ) {
$user_info = wp_get_current_user();
global $_wp_admin_css_colors;
if (
is_array( $_wp_admin_css_colors ) &&
! empty( $user_info->admin_color ) &&
isset( $_wp_admin_css_colors[ $user_info->admin_color ] ) &&
is_object( $_wp_admin_css_colors[ $user_info->admin_color ] ) &&
is_array( $_wp_admin_css_colors[ $user_info->admin_color ]->colors ) &&
! empty( $_wp_admin_css_colors[ $user_info->admin_color ]->colors[2] ) &&
preg_match( '/^\#([0-9a-fA-F]){3,6}$/', $_wp_admin_css_colors[ $user_info->admin_color ]->colors[2] )
) {
$s3io_admin_color = $_wp_admin_css_colors[ $user_info->admin_color ]->colors[2];
return $s3io_admin_color;
}
switch ( $user_info->admin_color ) {
case 'midnight':
return '#e14d43';
case 'blue':
return '#096484';
case 'light':
return '#04a4cc';
case 'ectoplasm':
return '#a3b745';
case 'coffee':
return '#c7a589';
case 'ocean':
return '#9ebaa0';
case 'sunrise':
return '#dd823b';
default:
return '#0073aa';
}
}
return '#0073aa';
}
/**
* Adjusts the signature/version and region from the defaults.
*
* @param array $args A list of arguments sent to the AWS SDK.
* @return array The arguments for the AWS connection, possibly modified.
*/
function s3io_addv4_args( $args ) {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$args['signature'] = 'v4';
$args['region'] = 'us-east-1';
$args['version'] = '2006-03-01';
if ( defined( 'S3_IMAGE_OPTIMIZER_REGION' ) && S3_IMAGE_OPTIMIZER_REGION ) {
$args['region'] = S3_IMAGE_OPTIMIZER_REGION;
}
if ( defined( 'S3_IMAGE_OPTIMIZER_ENDPOINT' ) && S3_IMAGE_OPTIMIZER_ENDPOINT ) {
$args['endpoint'] = S3_IMAGE_OPTIMIZER_ENDPOINT;
}
if ( defined( 'S3_IMAGE_OPTIMIZER_PATH_STYLE' ) && S3_IMAGE_OPTIMIZER_PATH_STYLE ) {
$args['use_path_style_endpoint'] = true;
}
return $args;
}
/**
* Adjusts the endpoint and region for DO Spaces connection.
*
* @param array $args A list of arguments sent to the AWS SDK.
* @return array The arguments for the AWS connection, possibly modified.
*/
function s3io_dospaces( $args ) {
if ( get_option( 's3io_dospaces' ) || defined( 'S3IO_DOSPACES' ) ) {
$region = defined( 'S3IO_DOSPACES' ) ? S3IO_DOSPACES : get_option( 's3io_dospaces' );
$args['endpoint'] = 'https://' . $region . '.digitaloceanspaces.com';
$args['region'] = $region;
}
return $args;
}
/**
* Let the user know that they need the EWWW IO plugin before S3 IO can do anything.
*/
function s3io_missing_ewww_plugin() {
?>
<div id='s3io-error-ewww' class='error'>
<p>
<?php /* translators: %s: EWWW Image Optimizer (link) */ ?>
<?php printf( esc_html__( 'Could not detect the %s plugin, please install and configure it first.', 's3-image-optimizer' ), '<a href="' . esc_url( admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) ) . '">EWWW Image Optimizer</a>' ); ?>
</p>
</div>
<?php
}
/**
* Let the user know that they need to update the EWWW IO plugin before S3 IO can do anything.
*/
function s3io_ewww_plugin_outdated() {
?>
<div id='s3io-error-ewww' class='error'>
<p>
<?php esc_html_e( 'Please update EWWW Image Optimizer to the latest version.', 's3-image-optimizer' ); ?>
</p>
</div>
<?php
}
/**
* Setup the admin menu items for the plugin.
*/
function s3io_admin_menu() {
if ( ! function_exists( 'ewww_image_optimizer' ) ) {
return;
}
add_media_page( esc_html__( 'S3 Bulk Image Optimizer', 's3-image-optimizer' ), esc_html__( 'S3 Bulk Optimizer', 's3-image-optimizer' ), 'activate_plugins', 's3io-bulk-display', 's3io_bulk_display' );
add_media_page( esc_html__( 'S3 Bulk URL Optimizer', 's3-image-optimizer' ), esc_html__( 'S3 URL Optimizer', 's3-image-optimizer' ), 'activate_plugins', 's3io-url-display', 's3io_url_display' );
// Add options page to the settings menu.
add_options_page(
esc_html__( 'S3 Image Optimizer', 's3-image-optimizer' ), // Title.
esc_html__( 'S3 Image Optimizer', 's3-image-optimizer' ), // Sub-menu title.
'manage_options', // Security.
S3IO_PLUGIN_FILE, // File to open.
's3io_options_page' // Function to call.
);
}
/**
* Display settings page for the plugin.
*/
function s3io_options_page() {
global $s3io_amazon_web_services;
$bucket_list = get_option( 's3io_bucketlist' );
?>
<div class='wrap'>
<h1><?php esc_html_e( 'S3 Image Optimizer', 's3-image-optimizer' ); ?></h1>
<p>
<a href="https://docs.ewww.io/article/22-how-to-use-s3-image-optimizer"><?php esc_html_e( 'Installation Instructions', 's3-image-optimizer' ); ?></a> |
<a href="https://ewww.io/contact-us/"><?php esc_html_e( 'Support', 's3-image-optimizer' ); ?></a>
</p>
<form method='post' action='options.php'>
<?php settings_fields( 's3io_options' ); ?>
<table class='form-table'>
<?php if ( $s3io_amazon_web_services->needs_access_keys() ) : ?>
<tr>
<th><?php esc_html_e( 'AWS Access Keys', 's3-image-optimizer' ); ?></th>
<td>
<i><?php esc_html_e( 'We recommend defining your Access Keys in wp-config.php so long as you don’t commit it to source control (you shouldn’t be).', 's3-image-optimizer' ); ?></i><br>
<?php esc_html_e( 'Simply copy the following snippet and replace the stars with the keys.' ); ?>
<a href="https://docs.ewww.io/article/61-creating-an-amazon-web-services-aws-user" target="_blank"><?php esc_html_e( 'Not sure where to find your access keys?', 's3-image-optimizer' ); ?></a><br>
<pre>define( 'DBI_AWS_ACCESS_KEY_ID', '********************' );
define( 'DBI_AWS_SECRET_ACCESS_KEY', '****************************************' );</pre>
</td>
</tr>
<tr>
<th><label for="s3io_aws_access_key_id"><?php esc_html_e( 'AWS Access Key ID', 's3-image-optimizer' ); ?></label></th>
<td><input type="text" id="s3io_aws_access_key_id" name="s3io_aws_access_key_id" value="<?php echo esc_attr( get_option( 's3io_aws_access_key_id' ) ); ?>" size="64" /></td>
</tr>
<tr>
<th><label for="s3io_aws_secret_access_key"><?php esc_html_e( 'AWS Secret Access Key', 's3-image-optimizer' ); ?></label></th>
<td><input type="text" id="s3io_aws_secret_access_key" name="s3io_aws_secret_access_key" value="<?php echo esc_attr( get_option( 's3io_aws_secret_access_key' ) ); ?>" size="64" /></td>
</tr>
<?php else : ?>
<?php if ( get_option( 's3io_aws_access_key_id' ) ) : ?>
<tr>
<th><label for="s3io_aws_access_key_id"><?php esc_html_e( 'AWS Access Key ID', 's3-image-optimizer' ); ?></label></th>
<td><input type="text" id="s3io_aws_access_key_id" name="s3io_aws_access_key_id" value="<?php echo esc_attr( get_option( 's3io_aws_access_key_id' ) ); ?>" size="64" /></td>
</tr>
<?php endif; ?>
<?php if ( get_option( 's3io_aws_secret_access_key' ) ) : ?>
<tr>
<th><label for="s3io_fake_secret_access_key"><?php esc_html_e( 'AWS Secret Access Key', 's3-image-optimizer' ); ?></label></th>
<td><input type="text" id="s3io_fake_secret_access_key" name="s3io_fake_secret_access_key" readonly='readonly' value="********************" size="64" />
<a href="<?php echo esc_url( admin_url( 'admin.php?action=s3io_remove_aws_keys' ) ); ?>"><?php esc_html_e( 'Remove Access Keys', 's3-image-optimizer' ); ?></a>
<input type="hidden" id="s3io_aws_secret_access_key" name="s3io_aws_secret_access_key" value="<?php echo esc_attr( get_option( 's3io_aws_secret_access_key' ) ); ?>" size="64" />
</td>
</tr>
<?php endif; ?>
<?php endif; ?>
<tr>
<th><label for="s3io_dospaces"><?php esc_html_e( 'Digital Ocean Spaces Region', 's3-image-optimizer' ); ?></label></th>
<td>
<input type="text" id="s3io_dospaces" name="s3io_dospaces" value="<?php echo esc_attr( get_option( 's3io_dospaces' ) ); ?>" size="10" />
<p class='description'><?php esc_html_e( 'To use Digital Ocean Spaces, enter the region for your space, or define the S3IO_DOSPACES constant.', 's3-image-optimizer' ); ?></p>
</td>
</tr>
<?php
try {
$client = $s3io_amazon_web_services->get_client();
} catch ( Exception $e ) {
echo '</table><p>' . wp_kses_post( $e->getMessage() ) . '</p>';
echo "<p class='submit'><input type='submit' class='button-primary' value='" . esc_attr__( 'Save Changes', 's3-image-optimizer' ) . "' /></p>";
return;
}
if ( is_wp_error( $client ) ) {
echo '</table><p>' . wp_kses_post( $aws->get_error_message() ) . '</p>';
echo "<p class='submit'><input type='submit' class='button-primary' value='" . esc_attr__( 'Save Changes', 's3-image-optimizer' ) . "' /></p>";
return;
}
try {
$buckets = $client->listBuckets();
} catch ( Exception $e ) {
$buckets = new WP_Error( 'exception', $e->getMessage() );
}
?>
<tr>
<th><label for="s3io_bucketlist"><?php esc_html_e( 'Buckets to optimize', 's3-image-optimizer' ); ?></label></th>
<td>
<?php if ( defined( 'S3_IMAGE_OPTIMIZER_BUCKET' ) && S3_IMAGE_OPTIMIZER_BUCKET ) : ?>
<p>
<?php esc_html_e( 'You have currently defined the bucket constant (S3_IMAGE_OPTIMIZER_BUCKET):', 's3-image-optimizer' ); ?>
<pre><?php echo esc_html( S3_IMAGE_OPTIMIZER_BUCKET ); ?></pre>
</p>
<?php else : ?>
<?php if ( is_wp_error( $buckets ) ) : ?>
<?php esc_html_e( 'One bucket per line. If empty, all available buckets will be optimized.', 's3-image-optimizer' ); ?><br>
<?php else : ?>
<?php esc_html_e( 'One bucket per line, must match one of the buckets listed below. If empty, all available buckets will be optimized.', 's3-image-optimizer' ); ?><br>
<?php endif; ?>
<?php
echo "<textarea id='s3io_bucketlist' name='s3io_bucketlist' rows='3' cols='40'>";
if ( ! empty( $bucket_list ) && is_array( $bucket_list ) ) {
foreach ( $bucket_list as $bucket ) {
echo esc_html( $bucket ) . "\n";
}
}
echo '</textarea>';
?>
<?php endif; ?>
<p class='description'>
<?php
if ( is_wp_error( $buckets ) && ! defined( 'S3_IMAGE_OPTIMIZER_BUCKET' ) ) {
/* translators: %s: AWS error message */
printf( esc_html__( 'Could not list buckets due to AWS error: %s', 's3-image-optimizer' ), '<br>' . wp_kses_post( $buckets->get_error_message() ) );
echo '<br>';
} elseif ( ! is_wp_error( $buckets ) ) {
esc_html_e( 'These are the buckets that we have access to optimize:', 's3-image-optimizer' );
echo '<br>';
foreach ( $buckets['Buckets'] as $bucket ) {
echo esc_html( $bucket['Name'] ) . "<br>\n";
}
}
?>
</p>
</td>
</tr>
<tr>
<th><?php esc_html_e( 'Custom endpoint', 's3-image-optimizer' ); ?></th>
<td>
<?php if ( defined( 'S3_IMAGE_OPTIMIZER_ENDPOINT' ) && S3_IMAGE_OPTIMIZER_ENDPOINT ) : ?>
<?php /* translators: %s: S3-compatible endpoint/URL */ ?>
<?php printf( esc_html__( 'Using custom S3-compatible endpoint: %s', 's3-image-optimizer' ), '<pre>' . esc_url( S3_IMAGE_OPTIMIZER_ENDPOINT ) . '</pre>' ); ?>
<?php if ( defined( 'S3_IMAGE_OPTIMIZER_REGION' ) && S3_IMAGE_OPTIMIZER_REGION ) : ?>
<?php /* translators: %s: S3 region, like 'nyc3' */ ?>
<?php printf( esc_html__( 'User-defined region: %s', 's3-image-optimizer' ), '<pre>' . esc_html( S3_IMAGE_OPTIMIZER_REGION ) . '</pre>' ); ?>
<?php endif; ?>
<?php else : ?>
<?php esc_html_e( 'Set the S3_IMAGE_OPTIMIZER_ENDPOINT and S3_IMAGE_OPTIMIZER_REGION constants to use other S3-compatible providers.', 's3-image-optimizer' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<th><?php esc_html_e( 'Sub-folders', 's3-image-optimizer' ); ?></th>
<td>
<?php if ( defined( 'S3_IMAGE_OPTIMIZER_FOLDER' ) && S3_IMAGE_OPTIMIZER_FOLDER ) : ?>
<?php /* translators: %s: folder or path in S3 bucket */ ?>
<?php printf( esc_html__( 'Optimization has been restricted to this folder: %s', 's3-image-optimizer' ), '<pre>' . esc_html( ltrim( S3_IMAGE_OPTIMIZER_FOLDER, '/' ) ) . '</pre>' ); ?>
<?php else : ?>
<?php esc_html_e( 'You may set the S3_IMAGE_OPTIMIZER_FOLDER constant to restrict optimization to a specific sub-directory of the bucket(s) above.', 's3-image-optimizer' ); ?>
<?php endif; ?>
</td>
</tr>
</table>
<p class='submit'><input type='submit' class='button-primary' value='<?php esc_attr_e( 'Save Changes', 's3-image-optimizer' ); ?>' /></p>
</form>
</div>
<?php
}
/**
* Removes AWS keys from the database.
*/
function s3io_remove_aws_keys() {
if ( false === current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Access denied', 's3-image-optimizer' ) );
}
delete_option( 's3io_aws_access_key_id' );
delete_option( 's3io_aws_secret_access_key' );
$sendback = wp_get_referer();
wp_safe_redirect( $sendback );
exit;
}
/**
* Sanitize the bucket list provided by the user.
*
* @param string $input A list of buckets, separated by newline characters.
* @return array An array of buckets verified to be accurate for the user's account.
*/
function s3io_bucketlist_sanitize( $input ) {
if ( empty( $input ) ) {
return '';
}
global $s3io_amazon_web_services;
try {
$client = $s3io_amazon_web_services->get_client();
} catch ( Exception $e ) {
return false;
}
try {
$buckets = $client->listBuckets();
} catch ( Exception $e ) {
$buckets = new WP_Error( 'exception', $e->getMessage() );
}
$bucket_array = array();
if ( is_array( $input ) ) {
$input_buckets = $input;
}
$input_buckets = explode( "\n", $input );
foreach ( $input_buckets as $input_bucket ) {
$input_bucket = trim( $input_bucket );
if ( is_wp_error( $buckets ) ) {
if ( strlen( $input_bucket ) < 3 || strlen( $input_bucket ) > 63 ) {
continue;
}
if ( preg_match( '/^([a-z0-9]+(-[a-z0-9]+)*\.*)+/', $input_bucket ) ) {
$bucket_array[] = $input_bucket;
}
} else {
foreach ( $buckets['Buckets'] as $bucket ) {
if ( $input_bucket === $bucket['Name'] ) {
$bucket_array[] = $input_bucket;
}
}
}
}
return $bucket_array;
}
/**
* Alert the user if the s3io folder could not be created within the uploads folder.
*/
function s3io_make_upload_dir_remote_error() {
echo "<div id='s3io-error-mkdir' class='error'><p>" . esc_html__( 'Unable to create the /s3io/ working directory: could not determine local upload directory path.', 's3-image-optimizer' ) . '</p></div>';
}
/**
* Alert the user if the s3io folder could not be created within the uploads folder.
*/
function s3io_make_upload_dir_failed() {
echo "<div id='s3io-error-mkdir' class='error'><p>" . esc_html__( 'Could not create the /s3io/ folder within the WordPress uploads folder, please adjust the permissions and try again.', 's3-image-optimizer' ) . '</p></div>';
}
/**
* Alert the user if the s3io folder is not writable.
*/
function s3io_make_upload_dir_write_error() {
echo "<div id='s3io-error-mkdir' class='error'><p>" . esc_html__( 'The /s3io/ working directory is not writable, please check permissions for the WordPress uploads folder and /s3io/ sub-folder.', 's3-image-optimizer' ) . '</p></div>';
}
/**
* Attempt to create the s3io/ folder within the uploads directory.
*
* @return string The absolute filesystem path to the s3io/ directory.
*/
function s3io_make_upload_dir() {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Unlook S3 Uploads from upload_dir.
if ( class_exists( 'S3_Uploads' ) || class_exists( 'S3_Uploads\Plugin' ) ) {
s3io_debug_message( 'S3_Uploads detected, removing upload_dir filters' );
remove_all_filters( 'upload_dir' );
}
$upload_dir = wp_upload_dir( null, false, true );
if ( false !== strpos( $upload_dir['basedir'], 's3://' ) ) {
s3io_debug_message( "upload_dir has an s3 prefix: {$upload_dir['basedir']}" );
add_action( 'admin_notices', 's3io_make_upload_dir_remote_error' );
return trailingslashit( $upload_dir['basedir'] ) . 's3io/';
}
if ( ! is_writable( $upload_dir['basedir'] ) ) {
s3io_debug_message( 'upload_dir is not writable' );
add_action( 'admin_notices', 's3io_make_upload_dir_failed' );
return false;
}
$upload_dir = trailingslashit( $upload_dir['basedir'] ) . 's3io/';
if ( ! is_dir( $upload_dir ) ) {
$mkdir = wp_mkdir_p( $upload_dir );
if ( ! $mkdir ) {
s3io_debug_message( 'could not create /s3io/ working dir' );
add_action( 'admin_notices', 's3io_make_upload_dir_failed' );
return false;
}
}
if ( ! is_writable( $upload_dir ) ) {
s3io_debug_message( "$upload_dir is not writable" );
add_action( 'admin_notices', 's3io_make_upload_dir_write_error' );
return false;
}
s3io_debug_message( "using $upload_dir as working dir" );
return $upload_dir;
}
/**
* Prepares the bulk operation and includes the javascript functions.
*
* @param string $hook The hook/suffix for the current page.
*/
function s3io_bulk_script( $hook ) {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Make sure we are being called from the proper page.
if ( 's3io-auto' !== $hook && 'media_page_s3io-bulk-display' !== $hook ) {
return;
}
s3io_make_upload_dir();
// Check to see if the user has asked to reset (empty) the optimized images table.
if ( ! empty( $_REQUEST['s3io_force_empty'] ) && ! empty( $_REQUEST['s3io_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['s3io_wpnonce'] ), 's3io-bulk-empty' ) ) {
s3io_table_truncate();
}
// Check to see if we are supposed to reset the bulk operation and verify we are authorized to do so.
if ( ! empty( $_REQUEST['s3io_reset_bulk'] ) && ! empty( $_REQUEST['s3io_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['s3io_wpnonce'] ), 's3io-bulk-reset' ) ) {
update_option( 's3io_resume', '', false );
}
// Check the 'bulk resume' option.
$resume = get_option( 's3io_resume' );
update_option( 's3io_bucket_paginator', '', false );
update_option( 's3io_buckets_scanned', '', false );
if ( empty( $resume ) ) {
s3io_table_delete_pending();
}
if ( 'media_page_s3io_bulk-display' !== $hook ) {
// Submit a couple variables to the javascript to work with.
wp_enqueue_script( 's3iobulkscript', plugins_url( '/s3io.js', __FILE__ ), array( 'jquery', 'jquery-ui-slider', 'jquery-ui-progressbar', 'postbox', 'dashboard' ), S3IO_VERSION );
$image_count = s3io_table_count_optimized();
wp_localize_script(
's3iobulkscript',
's3io_vars',
array(
'_wpnonce' => wp_create_nonce( 's3io-bulk' ),
'image_count' => $image_count, // Number of images completed.
'attachments' => s3io_table_count_pending(), // Number of pending images, will be 0 unless resuming.
/* translators: %s: number of items completed (includes HTML markup) */
'completed_string' => sprintf( esc_html__( 'Checked %s files so far', 's3-image-optimizer' ), '<span id="s3io-completed-count"></span>' ),
/* translators: %d: number of images */
'count_string' => sprintf( esc_html__( '%d images', 's3-image-optimizer' ), $image_count ),
'starting_scan' => esc_html__( 'Scanning buckets...', 's3-image-optimizer' ),
'operation_stopped' => esc_html__( 'Optimization stopped, reload page to resume.', 's3-image-optimizer' ),
'operation_interrupted' => esc_html__( 'Operation Interrupted', 's3-image-optimizer' ),
'temporary_failure' => esc_html__( 'Temporary failure, seconds left to retry:', 's3-image-optimizer' ),
'remove_failed' => esc_html__( 'Could not remove image from table.', 's3-image-optimizer' ),
'optimized' => esc_html__( 'Optimized', 's3-image-optimizer' ),
)
);
wp_enqueue_style( 'jquery-ui-progressbar', plugins_url( 'jquery-ui-1.10.1.custom.css', __FILE__ ), array(), S3IO_VERSION );
} else {
return;
}
}
/**
* Prepares the bulk URL operation and includes the javascript functions.
*
* @param string $hook The hook/suffix for the current page.
*/
function s3io_url_script( $hook ) {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Make sure we are being called from the proper page.
if ( 'media_page_s3io-url-display' !== $hook ) {
return;
}
s3io_make_upload_dir();
$loading_image = plugins_url( '/wpspin.gif', __FILE__ );
// Submit a couple variables to the javascript to work with.
wp_enqueue_script( 's3iobulkscript', plugins_url( '/s3io.js', __FILE__ ), array( 'jquery', 'jquery-ui-slider', 'jquery-ui-progressbar', 'postbox', 'dashboard' ), S3IO_VERSION );
wp_localize_script(
's3iobulkscript',
's3io_vars',
array(
'_wpnonce' => wp_create_nonce( 's3io-url' ),
'operation_stopped' => esc_html__( 'Optimization stopped, reload page to optimize more images by url.', 's3-image-optimizer' ),
'operation_interrupted' => esc_html__( 'Operation Interrupted', 's3-image-optimizer' ),
'temporary_failure' => esc_html__( 'Temporary failure, seconds left to retry:', 's3-image-optimizer' ),
'optimized' => esc_html__( 'Optimized', 's3-image-optimizer' ),
'finished' => esc_html__( 'Finished', 's3-image-optimizer' ),
'optimizing' => esc_html__( 'Optimizing', 's3-image-optimizer' ),
'spinner' => "<img src='$loading_image' alt='loading'/>",
)
);
wp_enqueue_style( 'jquery-ui-progressbar', plugins_url( 'jquery-ui-1.10.1.custom.css', __FILE__ ), array(), S3IO_VERSION );
}
/**
* Get list of buckets.
*
* @return array A list of bucket names.
*/
function s3io_get_selected_buckets() {
global $s3io_errors;
if ( defined( 'S3_IMAGE_OPTIMIZER_BUCKET' ) && S3_IMAGE_OPTIMIZER_BUCKET ) {
$bucket_list = array( S3_IMAGE_OPTIMIZER_BUCKET );
} else {
$bucket_list = get_option( 's3io_bucketlist' );
}
if ( empty( $bucket_list ) ) {
global $s3io_amazon_web_services;
$bucket_list = array();
try {
$client = $s3io_amazon_web_services->get_client();
} catch ( Exception $e ) {
$s3io_errors[] = $e->getMessage();
return $bucket_list;
}
try {
$buckets = $client->listBuckets();
} catch ( Exception $e ) {
$buckets = new WP_Error( 'exception', $e->getMessage() );
}
if ( is_wp_error( $buckets ) ) {
/* translators: %s: AWS error message */
$s3io_errors[] = sprintf( esc_html__( 'Could not list buckets: %s', 's3-image-optimizer' ), wp_kses_post( $buckets->get_error_message() ) );
} else {
foreach ( $buckets['Buckets'] as $aws_bucket ) {
$bucket_list[] = $aws_bucket['Name'];
}
}
}
return $bucket_list;
}
/**
* Scan buckets for images and store in database.
*
* @param bool $verbose Enable (true) to output WP_CLI logging. Default false.
*/
function s3io_image_scan( $verbose = false ) {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$wpcli = false;
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$wpcli = true;
}
$permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' );
if ( ! $wpcli && ( empty( $_REQUEST['s3io_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['s3io_wpnonce'] ), 's3io-bulk' ) || ! current_user_can( $permissions ) ) ) {
die( wp_json_encode( array( 'error' => esc_html__( 'Access token has expired, please reload the page.', 's3-image-optimizer' ) ) ) );
}
global $wpdb;
global $s3io_errors;
$s3io_errors = array();
$images = array();
$image_count = 0;
$scan_count = 0;
$field_formats = array(
'%s', // bucket.
'%s', // path.
'%d', // image_size.
);
/* $start = microtime( true ); */
global $s3io_amazon_web_services;
try {
$client = $s3io_amazon_web_services->get_client();
} catch ( Exception $e ) {
$s3io_errors[] = $e->getMessage();
if ( $wpcli ) {
return 0;
}
die( wp_json_encode( array( 'error' => $s3io_errors[0] ) ) );
}
$bucket_list = s3io_get_selected_buckets();
if ( ! empty( $s3io_errors ) ) {
if ( $wpcli ) {
return 0;
}
die( wp_json_encode( array( 'error' => $s3io_errors[0] ) ) );
}
$completed_buckets = get_option( 's3io_buckets_scanned' ) ? get_option( 's3io_buckets_scanned' ) : array();
$paginator = get_option( 's3io_bucket_paginator' );
foreach ( $bucket_list as $bucket ) {
s3io_debug_message( "scanning $bucket" );
if ( $verbose && $wpcli ) {
/* translators: %s: S3 bucket name */
WP_CLI::line( sprintf( __( 'Scanning bucket %s...', 's3-image-optimizer' ), $bucket ) );
}
foreach ( $completed_buckets as $completed_bucket ) {
if ( $bucket === $completed_bucket ) {
s3io_debug_message( "skipping $bucket, already done" );
continue 2;
}
}
try {
$location = $client->getBucketLocation( array( 'Bucket' => $bucket ) );
} catch ( Exception $e ) {
$location = new WP_Error( 'exception', $e->getMessage() );
}
$region = 'us-east-1';
if ( is_wp_error( $location ) && ( ! defined( 'S3_IMAGE_OPTIMIZER_REGION' ) || ! S3_IMAGE_OPTIMIZER_REGION ) ) {
/* translators: 1: bucket name 2: AWS error message */
$s3io_errors[] = sprintf( esc_html__( 'Could not get bucket location for %1$s, error: %2$s. Will assume us-east-1 region for all buckets. You may set the region manually using the S3_IMAGE_OPTIMIZER_REGION constant in wp-config.php.', 's3-image-optimizer' ), $bucket, wp_kses_post( $location->get_error_message() ) );
if ( ! $wpcli ) {
die( wp_json_encode( array( 'error' => $s3io_errors[0] ) ) );
}
} elseif ( empty( get_option( 's3io_dospaces' ) ) ) {
if ( ! is_wp_error( $location ) && ! empty( $location['Location'] ) ) {
$region = $location['Location'];
} elseif ( defined( 'S3_IMAGE_OPTIMIZER_REGION' ) && S3_IMAGE_OPTIMIZER_REGION ) {
$region = S3_IMAGE_OPTIMIZER_REGION;
}
}
$paginator_args = array( 'Bucket' => $bucket );
if ( defined( 'S3_IMAGE_OPTIMIZER_FOLDER' ) && S3_IMAGE_OPTIMIZER_FOLDER ) {
$paginator_args['Prefix'] = ltrim( S3_IMAGE_OPTIMIZER_FOLDER, '/' );
}
if ( $paginator ) {
s3io_debug_message( "starting from $paginator" );
$paginator_args['Marker'] = $paginator;
}
// In case you need to modify the arguments to the $client->getPaginator() call before they are used.
$paginator_args = apply_filters( 's3io_scan_iterator_args', $paginator_args );
$results = $client->getPaginator( 'ListObjects', $paginator_args );
$already_optimized = $wpdb->get_results( $wpdb->prepare( "SELECT path,image_size FROM $wpdb->s3io_images WHERE bucket LIKE %s", $bucket ), ARRAY_A );
$optimized_list = array();
foreach ( $already_optimized as $optimized ) {
$optimized_path = $optimized['path'];
$optimized_list[ $optimized_path ] = (int) $optimized['image_size'];
}
if ( function_exists( 'ewww_image_optimizer_stl_check' ) && ewww_image_optimizer_stl_check() ) {
set_time_limit( 0 );
}
try {
foreach ( $results as $result ) {
foreach ( $result['Contents'] as $object ) {
++$scan_count;
$skip_optimized = false;
$path = $object['Key'];
s3io_debug_message( "$scan_count: checking $path" );
if ( preg_match( '/\.(jpe?g|png|gif)$/i', $path ) ) {
$image_size = (int) $object['Size'];
if ( isset( $optimized_list[ $path ] ) && $optimized_list[ $path ] === $image_size ) {
s3io_debug_message( 'size matches db, skipping' );
$skip_optimized = true;
}
} else {
s3io_debug_message( 'not an image, skipping' );
continue;
}
if ( ! $skip_optimized || ! empty( $_REQUEST['s3io_force'] ) ) {
$images[ $path ] = array(
'bucket' => $bucket,
'path' => $path,
'orig_size' => $image_size,
);
if ( $verbose && $wpcli ) {
/* translators: 1: image name 2: S3 bucket name */
WP_CLI::line( sprintf( __( 'Queueing %1$s in %2$s.', 's3-image-optimizer' ), $path, $bucket ) );
}
s3io_debug_message( "queuing $path in $bucket" );
++$image_count;
}
if ( $scan_count >= 4000 && count( $images ) ) {
// let's dump what we have so far to the db.
if ( ! function_exists( 'ewww_image_optimizer_mass_insert' ) ) {
die( wp_json_encode( array( 'error' => esc_html__( 'Please update EWWW Image Optimizer to the latest version.', 's3-image-optimizer' ) ) ) );
}
ewww_image_optimizer_mass_insert( $wpdb->s3io_images, $images, $field_formats );
s3io_debug_message( "saved queue to db after checking $scan_count and finding $image_count" );
if ( $verbose && $wpcli ) {
WP_CLI::line( __( 'Saved queue to database.', 's3-image-optimizer' ) );
}
if ( ! $wpcli ) {
s3io_debug_message( "stashing $path as last marker" );
update_option( 's3io_bucket_paginator', $path, false );
die(
wp_json_encode(
array(
/* translators: %s: S3 bucket name */
'current' => sprintf( esc_html__( 'Scanning bucket %s', 's3-image-optimizer' ), "<strong>$bucket</strong>" ),
'completed' => $scan_count, // Number of images scanned in this pass.
)
)
);
}
$image_count = 0;
$images = array();
} elseif ( $scan_count >= 4000 && ! $wpcli ) {
s3io_debug_message( "stashing $path as last marker" );
update_option( 's3io_bucket_paginator', $path, false );
die(
wp_json_encode(
array(
/* translators: %s: S3 bucket name */
'current' => sprintf( esc_html__( 'Scanning bucket %s', 's3-image-optimizer' ), "<strong>$bucket</strong>" ),
'completed' => $scan_count, // Number of images scanned in this pass.
)
)
);
}
}
}
} catch ( Exception $e ) {
/* translators: 1: bucket name 2: AWS error message */
$s3io_errors[] = sprintf( esc_html__( 'Incorrect region for %1$s, please set the region using the S3_IMAGE_OPTIMIZER_REGION constant in wp-config.php. Error: %2$s.', 's3-image-optimizer' ), $bucket, wp_kses_post( $e->getMessage() ) );
if ( ! $wpcli ) {
die( wp_json_encode( array( 'error' => $s3io_errors[0] ) ) );
}
}
$paginator = '';
update_option( 's3io_bucket_paginator', $paginator, false );
$completed_buckets[] = $bucket;
s3io_debug_message( "adding $bucket to the completed list" );
update_option( 's3io_buckets_scanned', $completed_buckets, false );
}
if ( ! empty( $images ) ) {
if ( ! function_exists( 'ewww_image_optimizer_mass_insert' ) ) {
die( wp_json_encode( array( 'error' => esc_html__( 'Please update EWWW Image Optimizer to the latest version.', 's3-image-optimizer' ) ) ) );
}
s3io_debug_message( 'saving queue to db' );
ewww_image_optimizer_mass_insert( $wpdb->s3io_images, $images, $field_formats );
}
s3io_debug_message( "found $image_count images to optimize" );
update_option( 's3io_buckets_scanned', '', false );
if ( ! $wpcli ) {
$pending = s3io_table_count_pending();
/* translators: %d: number of images */
$message = $pending ? sprintf( esc_html__( 'There are %d images to be optimized.', 's3-image-optimizer' ), $pending ) : esc_html__( 'There is nothing left to optimize.', 's3-image-optimizer' );
if ( function_exists( 'ewww_image_optimizer_get_option' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp' ) ) {
$message .= ' *' . esc_html__( 'WebP versions will be generated and uploaded in accordance with EWWW IO settings.', 's3-image-optimizer' );
}
die(
wp_json_encode(
array(
'message' => $message,
'pending' => $pending, // Number of images to do.
'completed' => $scan_count, // Number of images scanned in this pass.
)
)
);
}
return $image_count;
}
/**
* Display the bulk S3 optimization page for URLs.
*/
function s3io_url_display() {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $wpdb;
$loading_image = plugins_url( '/wpspin.gif', __FILE__ );
?>
<div class="wrap">
<h1><?php esc_html_e( 'S3 URL Optimizer', 's3-image-optimizer' ); ?></h1>
<div id="s3io-bulk-loading">
<p id="s3io-loading" class="s3io-bulk-info" style="display:none"> <img src="<?php echo esc_url( $loading_image ); ?>" /></p>
</div>
<div id="s3io-bulk-progressbar"></div>
<div id="s3io-bulk-counter"></div>
<form id="s3io-bulk-stop" style="display:none;" method="post" action="">
<br /><input type="submit" class="button-secondary action" value="<?php esc_attr_e( 'Stop Optimizing', 's3-image-optimizer' ); ?>" />
</form>
<div id="s3io-bulk-widgets" class="metabox-holder" style="display:none">
<div class="meta-box-sortables">
<div id="s3io-bulk-status" class="postbox">
<button type="button" class="s3io-handlediv button-link" aria-expanded="true">
<span class="screen-reader-text"><?php esc_html_e( 'Click to toggle', 's3-image-optimizer' ); ?></span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
<h2 class="s3io-hndle"><span><?php esc_html_e( 'Optimization Log', 's3-image-optimizer' ); ?></span></h2>
<div class="inside"></div>
</div>
</div>
</div>
<form class="s3io-bulk-form">
<p><label for="s3io-delay" style="font-weight: bold"><?php esc_html_e( 'Choose how long to pause between images (in seconds, 0 = disabled)', 's3-image-optimizer' ); ?></label> <input type="text" id="s3io-delay" name="s3io-delay" value="0"></p>
<div id="s3io-delay-slider" style="width:50%"></div>
</form>
<div id="s3io-bulk-forms"><p class="s3io-bulk-info">
<p class="s3io-media-info s3io-bulk-info">
<?php esc_html_e( 'Previously optimized images will not be skipped.', 's3-image-optimizer' ); ?>
<?php echo ( function_exists( 'ewww_image_optimizer_get_option' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp' ) ? ' *' . esc_html__( 'WebP versions will be generated and uploaded in accordance with EWWW IO settings.', 's3-image-optimizer' ) : '' ); ?>
</p>
<form id="s3io-url-start" class="s3io-bulk-form" method="post" action="">
<p><label><?php esc_html_e( 'List images to be processed by URL (1 per line), for example:', 's3-image-optimizer' ); ?> https://bucket-name.s3.amazonaws.com/uploads/<?php echo esc_html( gmdate( 'Y' ) . '/' . gmdate( 'm' ) ); ?>/test-image.jpg<br><textarea id="s3io-url-image-queue" name="s3io-url-image-queue" style="resize:both; height: 300px; width: 60%;"></textarea></label></p>
<input id="s3io-first" type="submit" class="button-primary action" value="<?php esc_attr_e( 'Start optimizing', 's3-image-optimizer' ); ?>" />
</form>
</div>
</div>
<?php
}
/**
* Display the bulk S3 optimization page.
*/
function s3io_bulk_display() {
s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $wpdb;
global $s3io_errors;
// Retrieve the value of the 'aux resume' option and set the button text for the form to use.
$s3io_resume = get_option( 's3io_resume' );
$start_text = __( 'Start optimizing', 's3-image-optimizer' );
if ( empty( $s3io_resume ) ) {
$button_text = __( 'Scan for unoptimized images', 's3-image-optimizer' );
} else {
$button_text = __( 'Resume where you left off', 's3-image-optimizer' );
}
// find out if the auxiliary image table has anything in it.
$already_optimized = s3io_table_count_optimized();
// generate the WP spinner image for display.
$loading_image = plugins_url( '/wpspin.gif', __FILE__ );
echo "\n";
?>
<div class="wrap">
<h1><?php esc_html_e( 'S3 Bulk Optimizer', 's3-image-optimizer' ); ?></h1>
<?php
if ( ! empty( $s3io_errors ) && is_array( $s3io_errors ) ) {
foreach ( $s3io_errors as $s3io_error ) {
echo '<p style="color: red"><strong>' . esc_html( $s3io_error ) . '</strong></p>';
}
}
$s3io_errors = array();
?>
<div id="s3io-bulk-loading">
<p id="s3io-loading" class="s3io-bulk-info" style="display:none"> <img src="<?php echo esc_url( $loading_image ); ?>" /></p>
</div>
<div id="s3io-bulk-progressbar"></div>
<div id="s3io-bulk-counter"></div>
<form id="s3io-bulk-stop" style="display:none;" method="post" action="">
<br /><input type="submit" class="button-secondary action" value="<?php esc_attr_e( 'Stop Optimizing', 's3-image-optimizer' ); ?>" />
</form>
<div id="s3io-bulk-widgets" class="metabox-holder" style="display:none">
<div class="meta-box-sortables">
<div id="s3io-bulk-last" class="postbox">
<button type="button" class="s3io-handlediv button-link" aria-expanded="true">
<span class="screen-reader-text"><?php esc_html_e( 'Click to toggle', 's3-image-optimizer' ); ?></span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
<h2 class="s3io-hndle"><span><?php esc_html_e( 'Last Image Optimized', 's3-image-optimizer' ); ?></span></h2>
<div class="inside"></div>
</div>
</div>
<div class="meta-box-sortables">
<div id="s3io-bulk-status" class="postbox">
<button type="button" class="s3io-handlediv button-link" aria-expanded="true">
<span class="screen-reader-text"><?php esc_html_e( 'Click to toggle', 's3-image-optimizer' ); ?></span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
<h2 class="s3io-hndle"><span><?php esc_html_e( 'Optimization Log', 's3-image-optimizer' ); ?></span></h2>
<div class="inside"></div>
</div>
</div>
</div>
<form id="s3io-delay-slider-form" class="s3io-bulk-form">
<p><label for="s3io-delay" style="font-weight: bold"><?php esc_html_e( 'Choose how long to pause between images (in seconds, 0 = disabled)', 's3-image-optimizer' ); ?></label> <input type="text" id="s3io-delay" name="s3io-delay" value="0"></p>
<div id="s3io-delay-slider" style="width:50%"></div>
</form>
<div id="s3io-bulk-forms">
<p class="s3io-media-info s3io-bulk-info"><strong><?php esc_html_e( 'Currently selected buckets:', 's3-image-optimizer' ); ?></strong>
<?php
$bucket_list = s3io_get_selected_buckets();
if ( ! empty( $s3io_errors ) ) {
echo '<span style="color: red"><strong>' . esc_html( $s3io_error[0] ) . '</strong></p>';
} elseif ( empty( $bucket_list ) ) {
echo '<strong>' . esc_html__( 'Unable to find any buckets to scan.', 's3-image-optimizer' ) . '</strong>';