forked from nosilver4u/ewwwio-silo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
3339 lines (3162 loc) · 175 KB
/
common.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
// common functions for Standard and Cloud plugins
// TODO: make sure to update timestamp field in table for image record
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Hooks
*/
// used to move the W3TC CDN uploads filter
/* add_filter( 'wp_handle_upload', 'ewww_image_optimizer_handle_upload' );
// used to turn off ewwwio_image_editor during uploads
add_action( 'add_attachment', 'ewww_image_optimizer_add_attachment' );
add_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
add_filter( 'wp_generate_attachment_metadata', 'ewww_image_optimizer_resize_from_meta_data', 15, 2 );
// this filter turns off ewwwio_image_editor during save from the actual image editor
// and ensures that we parse the resizes list during the image editor save function
add_filter( 'load_image_to_edit_path', 'ewww_image_optimizer_editor_save_pre' );
// this hook is used to ensure we populate the metadata with webp images
add_filter( 'wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment_metadata', 8, 2 );
add_filter( 'manage_media_columns', 'ewww_image_optimizer_columns' );
// filters to set default permissions, anyone can override these if they wish
add_filter( 'ewww_image_optimizer_manual_permissions', 'ewww_image_optimizer_manual_permissions', 8 );
add_filter( 'ewww_image_optimizer_bulk_permissions', 'ewww_image_optimizer_admin_permissions', 8 );
add_filter( 'ewww_image_optimizer_admin_permissions', 'ewww_image_optimizer_admin_permissions', 8 );
add_filter( 'ewww_image_optimizer_superadmin_permissions', 'ewww_image_optimizer_superadmin_permissions', 8 );
// variable for plugin settings link
$plugin = plugin_basename ( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE );
add_filter( "plugin_action_links_$plugin", 'ewww_image_optimizer_settings_link' );
add_filter( 'intermediate_image_sizes_advanced', 'ewww_image_optimizer_image_sizes_advanced' );
add_filter( 'ewww_image_optimizer_settings', 'ewww_image_optimizer_filter_settings_page' );
add_filter( 'myarcade_filter_screenshot', 'ewww_image_optimizer_myarcade_thumbnail' );
add_filter( 'myarcade_filter_thumbnail', 'ewww_image_optimizer_myarcade_thumbnail' );
add_filter( 'jpeg_quality', 'ewww_image_optimizer_set_jpg_quality' );
add_action( 'manage_media_custom_column', 'ewww_image_optimizer_custom_column', 10, 2 );
add_action( 'plugins_loaded', 'ewww_image_optimizer_preinit' );
add_action( 'init', 'ewww_image_optimizer_gallery_support' );
add_action( 'admin_init', 'ewww_image_optimizer_admin_init' );
add_action( 'admin_action_ewww_image_optimizer_manual_optimize', 'ewww_image_optimizer_manual' );
add_action( 'admin_action_ewww_image_optimizer_manual_restore', 'ewww_image_optimizer_manual' );
add_action( 'admin_action_ewww_image_optimizer_manual_convert', 'ewww_image_optimizer_manual' );
add_action( 'delete_attachment', 'ewww_image_optimizer_delete' );
add_action( 'admin_menu', 'ewww_image_optimizer_admin_menu', 60 );
add_action( 'network_admin_menu', 'ewww_image_optimizer_network_admin_menu' );
add_action( 'load-upload.php', 'ewww_image_optimizer_load_admin_js' );
add_action( 'admin_action_bulk_optimize', 'ewww_image_optimizer_bulk_action_handler' );
add_action( 'admin_action_-1', 'ewww_image_optimizer_bulk_action_handler' );
add_action( 'admin_enqueue_scripts', 'ewww_image_optimizer_media_scripts' );
add_action( 'admin_enqueue_scripts', 'ewww_image_optimizer_settings_script' );
add_action( 'ewww_image_optimizer_auto', 'ewww_image_optimizer_auto' );
add_action( 'wr2x_retina_file_added', 'ewww_image_optimizer_retina', 20, 2 );
add_action( 'wp_ajax_ewww_webp_rewrite', 'ewww_image_optimizer_webp_rewrite' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'iocli.php' );
}*/
function ewwwio_memory( $function ) {
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
global $ewww_memory;
// $ewww_memory .= $function . ': ' . memory_get_usage(true) . "\n";
}
}
if ( ! function_exists( 'boolval' ) ) {
function boolval( $value ) {
return (bool) $value;
}
}
// function to check if set_time_limit() is disabled
function ewww_image_optimizer_stl_check() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( ewww_image_optimizer_safemode_check() ) {
return false;
}
if ( defined( 'EWWW_IMAGE_OPTIMIZER_DISABLE_STL' ) && EWWW_IMAGE_OPTIMIZER_DISABLE_STL ) {
ewwwio_debug_message( 'stl disabled by user' );
return false;
}
$disabled = ini_get('disable_functions');
ewwwio_debug_message( "disable_functions = $disabled" );
if ( preg_match( '/set_time_limit/', $disabled ) ) {
ewwwio_memory( __FUNCTION__ );
return false;
} elseif ( function_exists( 'set_time_limit' ) ) {
ewwwio_memory( __FUNCTION__ );
return true;
} else {
ewwwio_debug_message( 'set_time_limit does not exist' );
return false;
}
}
function ewww_image_optimizer_preinit() {
// TODO: for future translation capability
load_plugin_textdomain( EWWW_IMAGE_OPTIMIZER_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Plugin initialization function
*/
function ewww_image_optimizer_upgrade() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
ewwwio_memory( __FUNCTION__ );
if ( get_option( 'ewww_image_optimizer_version' ) < EWWW_IMAGE_OPTIMIZER_VERSION ) {
//ewww_image_optimizer_install_table();
ewww_image_optimizer_upgrade_table();
ewww_image_optimizer_set_defaults();
update_option( 'ewww_image_optimizer_version', EWWW_IMAGE_OPTIMIZER_VERSION );
}
ewwwio_memory( __FUNCTION__ );
// ewww_image_optimizer_debug_log();
}
// Plugin initialization for admin area
function ewww_image_optimizer_admin_init() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
ewwwio_memory( __FUNCTION__ );
ewww_image_optimizer_cloud_init();
ewww_image_optimizer_upgrade();
// set the common network settings if they have been POSTed
if ( isset( $_POST['_ewww_nonce'] ) && current_user_can( 'manage_options' ) && wp_verify_nonce( $_REQUEST['_ewww_nonce'], 'ewww_image_optimizer_options' ) ) {
ewwwio_debug_message( print_r( $_POST, true ) );
$_POST['ewww_image_optimizer_debug'] = ( empty( $_POST['ewww_image_optimizer_debug'] ) ? false : true );
update_option( 'ewww_image_optimizer_debug', $_POST['ewww_image_optimizer_debug'] );
$_POST['ewww_image_optimizer_remove_meta'] = ( empty( $_POST['ewww_image_optimizer_remove_meta'] ) ? false : true );
update_option( 'ewww_image_optimizer_remove_meta', $_POST['ewww_image_optimizer_remove_meta'] );
if ( empty( $_POST['ewww_image_optimizer_jpg_level'] ) ) $_POST['ewww_image_optimizer_jpg_level'] = '';
update_option( 'ewww_image_optimizer_jpg_level', (int) $_POST['ewww_image_optimizer_jpg_level'] );
if ( empty( $_POST[ 'ewww_image_optimizer_png_level'] ) ) $_POST['ewww_image_optimizer_png_level'] = '';
update_option( 'ewww_image_optimizer_png_level', (int) $_POST['ewww_image_optimizer_png_level'] );
if ( empty( $_POST['ewww_image_optimizer_gif_level'] ) ) $_POST['ewww_image_optimizer_gif_level'] = '';
update_option( 'ewww_image_optimizer_gif_level', (int) $_POST['ewww_image_optimizer_gif_level'] );
if ( empty( $_POST['ewww_image_optimizer_pdf_level'] ) ) $_POST['ewww_image_optimizer_pdf_level'] = '';
update_option( 'ewww_image_optimizer_pdf_level', (int) $_POST['ewww_image_optimizer_pdf_level'] );
$_POST['ewww_image_optimizer_delete_originals'] = ( empty( $_POST['ewww_image_optimizer_delete_originals'] ) ? false : true );
update_option( 'ewww_image_optimizer_delete_originals', $_POST['ewww_image_optimizer_delete_originals'] );
$_POST['ewww_image_optimizer_jpg_to_png'] = ( empty( $_POST['ewww_image_optimizer_jpg_to_png'] ) ? false : true );
update_option( 'ewww_image_optimizer_jpg_to_png', $_POST['ewww_image_optimizer_jpg_to_png'] );
$_POST['ewww_image_optimizer_png_to_jpg'] = ( empty( $_POST['ewww_image_optimizer_png_to_jpg'] ) ? false : true );
update_option( 'ewww_image_optimizer_png_to_jpg', $_POST['ewww_image_optimizer_png_to_jpg'] );
$_POST['ewww_image_optimizer_gif_to_png'] = ( empty( $_POST['ewww_image_optimizer_gif_to_png'] ) ? false : true );
update_option( 'ewww_image_optimizer_gif_to_png', $_POST['ewww_image_optimizer_gif_to_png'] );
$_POST['ewww_image_optimizer_webp'] = ( empty( $_POST['ewww_image_optimizer_webp'] ) ? false : true );
update_option( 'ewww_image_optimizer_webp', $_POST['ewww_image_optimizer_webp'] );
if (empty($_POST['ewww_image_optimizer_jpg_background'])) $_POST['ewww_image_optimizer_jpg_background'] = '';
update_option( 'ewww_image_optimizer_jpg_background', ewww_image_optimizer_jpg_background( $_POST['ewww_image_optimizer_jpg_background'] ) );
if (empty($_POST['ewww_image_optimizer_jpg_quality'])) $_POST['ewww_image_optimizer_jpg_quality'] = '';
update_option( 'ewww_image_optimizer_jpg_quality', ewww_image_optimizer_jpg_quality( $_POST['ewww_image_optimizer_jpg_quality'] ) );
if ( empty( $_POST['ewww_image_optimizer_cloud_key'] ) ) $_POST['ewww_image_optimizer_cloud_key'] = '';
update_option( 'ewww_image_optimizer_cloud_key', ewww_image_optimizer_cloud_key_sanitize( $_POST['ewww_image_optimizer_cloud_key'] ) );
if ( empty( $_POST['ewww_image_optimizer_aux_paths'] ) ) $_POST['ewww_image_optimizer_aux_paths'] = '';
update_option( 'ewww_image_optimizer_aux_paths', ewww_image_optimizer_aux_paths_sanitize( $_POST['ewww_image_optimizer_aux_paths'] ) );
if ( empty( $_POST['ewww_image_optimizer_delay'] ) ) $_POST['ewww_image_optimizer_delay'] = '';
update_option( 'ewww_image_optimizer_delay', (int) $_POST['ewww_image_optimizer_delay'] );
if ( empty( $_POST['ewww_image_optimizer_maxmediawidth'] ) ) $_POST['ewww_image_optimizer_maxmediawidth'] = 0;
update_option( 'ewww_image_optimizer_maxmediawidth', (int) $_POST['ewww_image_optimizer_maxmediawidth'] );
if ( empty( $_POST['ewww_image_optimizer_maxmediaheight'] ) ) $_POST['ewww_image_optimizer_maxmediaheight'] = 0;
update_option( 'ewww_image_optimizer_maxmediaheight', (int) $_POST['ewww_image_optimizer_maxmediaheight'] );
/*if ( empty( $_POST['ewww_image_optimizer_maxotherwidth'] ) ) $_POST['ewww_image_optimizer_maxotherwidth'] = 0;
update_option( 'ewww_image_optimizer_maxotherwidth', (int) $_POST['ewww_image_optimizer_maxotherwidth'] );
if ( empty( $_POST['ewww_image_optimizer_maxotherheight'] ) ) $_POST['ewww_image_optimizer_maxotherheight'] = 0;
update_option( 'ewww_image_optimizer_maxotherheight', (int) $_POST['ewww_image_optimizer_maxotherheight'] );*/
if ( empty( $_POST['ewww_image_optimizer_skip_size'] ) ) $_POST['ewww_image_optimizer_skip_size'] = '';
update_option( 'ewww_image_optimizer_skip_size', (int) $_POST['ewww_image_optimizer_skip_size'] );
if ( empty( $_POST['ewww_image_optimizer_skip_png_size'] ) ) $_POST['ewww_image_optimizer_skip_png_size'] = '';
update_option('ewww_image_optimizer_skip_png_size', (int) $_POST['ewww_image_optimizer_skip_png_size'] );
$_POST['ewww_image_optimizer_skip_bundle'] = ( empty( $_POST['ewww_image_optimizer_skip_bundle'] ) ? false : true );
update_option( 'ewww_image_optimizer_skip_bundle', $_POST['ewww_image_optimizer_skip_bundle'] );
if ( ! empty( $_POST['ewww_image_optimizer_optipng_level'] ) ) {
update_option( 'ewww_image_optimizer_optipng_level', (int) $_POST['ewww_image_optimizer_optipng_level'] );
update_option( 'ewww_image_optimizer_pngout_level', (int) $_POST['ewww_image_optimizer_pngout_level'] );
}
$_POST['ewww_image_optimizer_disable_pngout'] = ( empty( $_POST['ewww_image_optimizer_disable_pngout'] ) ? false : true );
update_option( 'ewww_image_optimizer_disable_pngout', $_POST['ewww_image_optimizer_disable_pngout'] );
// TODO: display the confirmation for saving settings
ewww_image_optimizer_network_settings_saved(); // rename this without 'network'
}
ewww_image_optimizer_exec_init();
// require the files that do the bulk processing
// require_once( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'bulk.php' );
require_once( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'aux-optimize.php' );
// queue the function that contains custom styling for our progressbars
add_action('admin_enqueue_scripts', 'ewww_image_optimizer_progressbar_style');
ewwwio_memory( __FUNCTION__ );
// ewww_image_optimizer_debug_log();
}
// sets all the tool constants to false
function ewww_image_optimizer_disable_tools() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_JPEGTRAN' ) ) {
define('EWWW_IMAGE_OPTIMIZER_JPEGTRAN', false);
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_OPTIPNG' ) ) {
define('EWWW_IMAGE_OPTIMIZER_OPTIPNG', false);
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGOUT' ) ) {
define('EWWW_IMAGE_OPTIMIZER_PNGOUT', false);
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_GIFSICLE' ) ) {
define('EWWW_IMAGE_OPTIMIZER_GIFSICLE', false);
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGQUANT' ) ) {
define('EWWW_IMAGE_OPTIMIZER_PNGQUANT', false);
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_WEBP' ) ) {
define('EWWW_IMAGE_OPTIMIZER_WEBP', false);
}
ewwwio_memory( __FUNCTION__ );
}
// generates css include for progressbars to match admin style
function ewww_image_optimizer_progressbar_style() {
wp_add_inline_style( 'jquery-ui-progressbar', ".ui-widget-header { background-color: " . ewww_image_optimizer_admin_background() . "; }" );
ewwwio_memory( __FUNCTION__ );
}
// determines the background color to use based on the selected theme
function ewww_image_optimizer_admin_background() {
if ( function_exists( 'wp_add_inline_style' ) ) {
$user_info = wp_get_current_user();
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";
}
}
ewwwio_memory( __FUNCTION__ );
}
// tells WP to ignore the 'large network' detection by filtering the results of wp_is_large_network()
function ewww_image_optimizer_large_network() {
return false;
}
// lets the user know their network settings have been saved
function ewww_image_optimizer_network_settings_saved() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
echo "<div id='ewww-image-optimizer-settings-saved' class='updated fade'><p><strong>" . esc_html__('Settings saved', EWWW_IMAGE_OPTIMIZER_DOMAIN) . ".</strong></p></div>";
}
// adds a global settings page to the network admin settings menu
function ewww_image_optimizer_network_admin_menu() {
if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
// need to include the plugin library for the is_plugin_active function
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
if ( is_multisite() && is_plugin_active_for_network( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL ) ) {
// add options page to the settings menu
$permissions = apply_filters( 'ewww_image_optimizer_superadmin_permissions', '' );
$ewww_network_options_page = add_submenu_page(
'settings.php', //slug of parent
'EWWW Image Optimizer', //Title
'EWWW Image Optimizer', //Sub-menu title
$permissions, //Security
EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE, //File to open
'ewww_image_optimizer_options' //Function to call
);
}
}
// adds the bulk optimize and settings page to the admin menu
function ewww_image_optimizer_admin_menu() {
$permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' );
// adds bulk optimize to the media library menu
$ewww_bulk_page = add_media_page( esc_html__( 'Bulk Optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN ), esc_html__( 'Bulk Optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN ), $permissions, 'ewww-image-optimizer-bulk', 'ewww_image_optimizer_bulk_preview' );
$ewww_unoptimized_page = add_media_page( esc_html__( 'Unoptimized Images', EWWW_IMAGE_OPTIMIZER_DOMAIN ), esc_html__( 'Unoptimized Images', EWWW_IMAGE_OPTIMIZER_DOMAIN ), $permissions, 'ewww-image-optimizer-unoptimized', 'ewww_image_optimizer_display_unoptimized_media' );
$ewww_webp_migrate_page = add_submenu_page( null, esc_html__( 'Migrate WebP Images', EWWW_IMAGE_OPTIMIZER_DOMAIN ), esc_html__( 'Migrate WebP Images', EWWW_IMAGE_OPTIMIZER_DOMAIN ), $permissions, 'ewww-image-optimizer-webp-migrate', 'ewww_image_optimizer_webp_migrate_preview' );
if ( ! function_exists( 'is_plugin_active' ) ) {
// need to include the plugin library for the is_plugin_active function
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
if ( ! is_plugin_active_for_network( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL ) ) {
// add options page to the settings menu
$ewww_options_page = add_options_page(
'EWWW Image Optimizer', //Title
'EWWW Image Optimizer', //Sub-menu title
apply_filters( 'ewww_image_optimizer_admin_permissions', '' ), //Security
EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE, //File to open
'ewww_image_optimizer_options' //Function to call
);
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
add_media_page( esc_html__( 'Dynamic Image Debugging', EWWW_IMAGE_OPTIMIZER_DOMAIN ), esc_html__( 'Dynamic Image Debugging', EWWW_IMAGE_OPTIMIZER_DOMAIN ), $permissions, 'ewww-image-optimizer-dynamic-debug', 'ewww_image_optimizer_dynamic_image_debug' );
}
if ( is_plugin_active( 'image-store/ImStore.php' ) || is_plugin_active_for_network( 'image-store/ImStore.php' ) ) {
$ims_menu ='edit.php?post_type=ims_gallery';
$ewww_ims_page = add_submenu_page( $ims_menu, esc_html__( 'Image Store Optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN ), esc_html__( 'Optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN ), 'ims_change_settings', 'ewww-ims-optimize', 'ewww_image_optimizer_ims');
// add_action( 'admin_footer-' . $ewww_ims_page, 'ewww_image_optimizer_debug' );
}
}
// enqueue custom jquery stylesheet for bulk optimizer
function ewww_image_optimizer_media_scripts( $hook ) {
if ( $hook == 'upload.php' ) {
wp_enqueue_script( 'jquery-ui-tooltip' );
wp_enqueue_style( 'jquery-ui-tooltip-custom', plugins_url( '/includes/jquery-ui-1.10.1.custom.css', __FILE__ ) );
}
}
// used to output debug messages to a logfile in the plugin folder in cases where output to the screen is a bad idea
function ewww_image_optimizer_debug_log() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $ewww_debug;
if (! empty( $ewww_debug ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
$timestamp = date( 'y-m-d h:i:s.u' ) . "\n";
if ( ! file_exists( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'debug.log' ) ) {
touch( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'debug.log' );
}
$ewww_debug_log = str_replace( '<br>', "\n", $ewww_debug );
file_put_contents( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'debug.log', $timestamp . $ewww_debug_log, FILE_APPEND );
}
$ewww_debug = '';
ewwwio_memory( __FUNCTION__ );
}
// adds a link on the Plugins page for the EWWW IO settings
function ewww_image_optimizer_settings_link( $links ) {
if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
// need to include the plugin library for the is_plugin_active function
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
// load the html for the settings link
if ( is_multisite() && is_plugin_active_for_network( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL ) ) {
$settings_link = '<a href="network/settings.php?page=' . plugin_basename( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ) . '">' . esc_html__( 'Settings', EWWW_IMAGE_OPTIMIZER_DOMAIN ) . '</a>';
} else {
$settings_link = '<a href="options-general.php?page=' . plugin_basename( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ) . '">' . esc_html__( 'Settings', EWWW_IMAGE_OPTIMIZER_DOMAIN ) . '</a>';
}
// load the settings link into the plugin links array
array_unshift( $links, $settings_link );
// send back the plugin links array
return $links;
}
// check for GD support of both PNG and JPG
function ewww_image_optimizer_gd_support() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( function_exists( 'gd_info' ) ) {
$gd_support = gd_info();
ewwwio_debug_message( "GD found, supports:" );
foreach ( $gd_support as $supports => $supported ) {
ewwwio_debug_message( "$supports: $supported" );
}
ewwwio_memory( __FUNCTION__ );
if ( ( ! empty( $gd_support["JPEG Support"] ) || ! empty( $gd_support["JPG Support"] ) ) && ! empty( $gd_support["PNG Support"] ) ) {
return TRUE;
}
}
return FALSE;
}
// check for IMagick support of both PNG and JPG
function ewww_image_optimizer_imagick_support() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( extension_loaded( 'imagick' ) ) {
$imagick = new Imagick();
$formats = $imagick->queryFormats();
if ( in_array( 'PNG', $formats ) && in_array( 'JPG', $formats ) ) {
return true;
}
}
return false;
}
// check for IMagick support of both PNG and JPG
function ewww_image_optimizer_gmagick_support() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( extension_loaded( 'gmagick' ) ) {
$gmagick = new Gmagick();
$formats = $gmagick->queryFormats();
if ( in_array( 'PNG', $formats ) && in_array( 'JPG', $formats ) ) {
return true;
}
}
return false;
}
function ewww_image_optimizer_disable_resizes_sanitize( $disabled_resizes ) {
if ( is_array( $disabled_resizes ) ) {
return $disabled_resizes;
} else {
return '';
}
}
function ewww_image_optimizer_aux_paths_sanitize( $input ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if (empty($input)) {
return '';
}
$path_array = array();
$paths = explode("\n", $input);
foreach ($paths as $path) {
$path = sanitize_text_field( $path );
ewwwio_debug_message( "validating auxiliary path: $path" );
// retrieve the location of the wordpress upload folder
$upload_dir = apply_filters( 'ewww_image_optimizer_folder_restriction', wp_upload_dir() );
// retrieve the path of the upload folder
$upload_path = trailingslashit($upload_dir['basedir']);
if ( is_dir( $path ) && ( strpos( $path, ABSPATH ) === 0 || strpos( $path, $upload_path ) === 0 ) ) {
$path_array[] = $path;
}
}
// ewww_image_optimizer_debug_log();
ewwwio_memory( __FUNCTION__ );
return $path_array;
}
// replacement for escapeshellarg() that won't kill non-ASCII characters
function ewww_image_optimizer_escapeshellarg( $arg ) {
if ( PHP_OS === 'WINNT' ) {
$safe_arg = '"' . $arg . '"';
} else {
$safe_arg = "'" . str_replace("'", "'\"'\"'", $arg) . "'";
}
ewwwio_memory( __FUNCTION__ );
return $safe_arg;
}
// Retrieves/sanitizes jpg background fill setting or returns null for png2jpg conversions
function ewww_image_optimizer_jpg_background( $background = null ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( $background === null ) {
// retrieve the user-supplied value for jpg background color
$background = ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_background' );
}
//verify that the supplied value is in hex notation
if ( preg_match( '/^\#*([0-9a-fA-F]){6}$/', $background ) ) {
// we remove a leading # symbol, since we take care of it later
preg_replace( '/#/', '', $background );
// send back the verified, cleaned-up background color
ewwwio_debug_message( "background: $background" );
ewwwio_memory( __FUNCTION__ );
return $background;
} else {
// send back a blank value
ewwwio_memory( __FUNCTION__ );
return NULL;
}
}
// Retrieves/sanitizes the jpg quality setting for png2jpg conversion or returns null
function ewww_image_optimizer_jpg_quality( $quality = null ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( $quality === null ) {
// retrieve the user-supplied value for jpg quality
$quality = ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_quality' );
}
// verify that the quality level is an integer, 1-100
if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
ewwwio_debug_message( "quality: $quality" );
// send back the valid quality level
ewwwio_memory( __FUNCTION__ );
return $quality;
} else {
// send back nothing
ewwwio_memory( __FUNCTION__ );
return NULL;
}
}
function ewww_image_optimizer_set_jpg_quality( $quality ) {
$new_quality = ewww_image_optimizer_jpg_quality();
if ( ! empty( $new_quality ) ) {
return $new_quality;
}
return $quality;
}
// check filesize, and prevent errors by ensuring file exists, and that the cache has been cleared
function ewww_image_optimizer_filesize( $file ) {
if ( is_file( $file ) ) {
// flush the cache for filesize
clearstatcache();
// find out the size of the new PNG file
return filesize( $file );
} else {
return 0;
}
}
/**
* Manually process an image from the Media Library
*/
function ewww_image_optimizer_manual() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $ewww_defer;
$ewww_defer = false;
// check permissions of current user
$permissions = apply_filters( 'ewww_image_optimizer_manual_permissions', '' );
if ( FALSE === current_user_can( $permissions ) ) {
// display error message if insufficient permissions
wp_die( esc_html__( 'You do not have permission to optimize images.', EWWW_IMAGE_OPTIMIZER_DOMAIN ) );
}
// make sure we didn't accidentally get to this page without an attachment to work on
if ( FALSE === isset($_GET['ewww_attachment_ID'])) {
// display an error message since we don't have anything to work on
wp_die( esc_html__('No attachment ID was provided.', EWWW_IMAGE_OPTIMIZER_DOMAIN));
}
session_write_close();
// store the attachment ID value
$attachment_ID = intval($_GET['ewww_attachment_ID']);
if ( empty( $_REQUEST['ewww_manual_nonce'] ) || ! wp_verify_nonce( $_REQUEST['ewww_manual_nonce'], "ewww-manual-$attachment_ID" ) ) {
wp_die( esc_html__( 'Access denied.', EWWW_IMAGE_OPTIMIZER_DOMAIN ) );
}
// retrieve the existing attachment metadata
$original_meta = wp_get_attachment_metadata( $attachment_ID );
// if the call was to optimize...
if ($_REQUEST['action'] === 'ewww_image_optimizer_manual_optimize') {
// call the optimize from metadata function and store the resulting new metadata
$new_meta = ewww_image_optimizer_resize_from_meta_data($original_meta, $attachment_ID);
} elseif ($_REQUEST['action'] === 'ewww_image_optimizer_manual_restore') {
$new_meta = ewww_image_optimizer_restore_from_meta_data($original_meta, $attachment_ID);
}
global $ewww_attachment;
$ewww_attachment['id'] = $attachment_ID;
$ewww_attachment['meta'] = $new_meta;
add_filter( 'w3tc_cdn_update_attachment_metadata', 'ewww_image_optimizer_w3tc_update_files' );
// update the attachment metadata in the database
wp_update_attachment_metadata( $attachment_ID, $new_meta );
ewww_image_optimizer_debug_log();
// store the referring webpage location
$sendback = wp_get_referer();
// sanitize the referring webpage location
$sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
// send the user back where they came from
wp_redirect( $sendback );
// we are done, nothing to see here
ewwwio_memory( __FUNCTION__ );
exit(0);
}
/**
* Manually restore a converted image
*/
function ewww_image_optimizer_restore_from_meta_data( $meta, $id ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// get the filepath
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$file_path = get_attached_file( $id );
if ( ! empty( $meta['converted'] ) ) {
if ( file_exists( $meta['orig_file'] ) ) {
// update the filename in the metadata
$meta['file'] = $meta['orig_file'];
// update the optimization results in the metadata
$meta['ewww_image_optimizer'] = __( 'Original Restored', EWWW_IMAGE_OPTIMIZER_DOMAIN );
$meta['orig_file'] = $file_path;
$meta['real_orig_file'] = $file_path;
$meta['converted'] = 0;
unlink( $meta['orig_file'] );
unset( $meta['orig_file'] );
$meta['file'] = str_replace($upload_path, '', $meta['file']);
// if we don't already have the update attachment filter
if (FALSE === has_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment'))
// add the update attachment filter
add_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10, 2);
} else {
remove_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10);
}
}
if ( isset( $meta['sizes'] ) ) {
// process each resized version
$processed = array();
// meta sizes don't contain a path, so we calculate one
$base_dir = trailingslashit( dirname( $file_path ) );
foreach( $meta['sizes'] as $size => $data ) {
// check through all the sizes we've processed so far
foreach( $processed as $proc => $scan ) {
// if a previous resize had identical dimensions
if ( $scan['height'] == $data['height'] && $scan['width'] == $data['width'] && isset( $meta['sizes'][ $proc ]['converted'] ) ) {
// point this resize at the same image as the previous one
$meta['sizes'][ $size ]['file'] = $meta['sizes'][ $proc ]['file'];
}
}
if ( isset( $data['converted'] ) ) {
// if this is a unique size
if ( file_exists( $base_dir . $data['orig_file'] ) ) {
// update the filename
$meta['sizes'][ $size ]['file'] = $data['orig_file'];
// update the optimization results
$meta['sizes'][ $size ]['ewww_image_optimizer'] = __( 'Original Restored', EWWW_IMAGE_OPTIMIZER_DOMAIN );
$meta['sizes'][ $size ]['orig_file'] = $data['file'];
$meta['sizes'][ $size ]['real_orig_file'] = $data['file'];
$meta['sizes'][ $size ]['converted'] = 0;
// if we don't already have the update attachment filter
if ( FALSE === has_filter( 'wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment' ) ) {
// add the update attachment filter
add_filter( 'wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10, 2 );
}
unlink( $base_dir . $data['file'] );
unset( $meta['sizes'][ $size ]['orig_file'] );
}
// store info on the sizes we've processed, so we can check the list for duplicate sizes
$processed[$size]['width'] = $data['width'];
$processed[$size]['height'] = $data['height'];
}
}
}
ewwwio_memory( __FUNCTION__ );
return $meta;
}
// deletes 'orig_file' when an attachment is being deleted
function ewww_image_optimizer_delete ($id) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $wpdb;
// retrieve the image metadata
$meta = wp_get_attachment_metadata($id);
// if the attachment has an original file set
if ( ! empty( $meta['orig_file'] ) ) {
unset($rows);
// get the filepath from the metadata
$file_path = $meta['orig_file'];
// get the filename
$filename = basename($file_path);
// delete any residual webp versions
$webpfile = $filename . '.webp';
$webpfileold = preg_replace( '/\.\w+$/', '.webp', $filename );
if ( file_exists( $webpfile) ) {
unlink( $webpfile );
}
if ( file_exists( $webpfileold) ) {
unlink( $webpfileold );
}
// retrieve any posts that link the original image
$esql = "SELECT ID, post_content FROM $wpdb->posts WHERE post_content LIKE '%$filename%'";
$rows = $wpdb->get_row($esql);
// if the original file still exists and no posts contain links to the image
if ( file_exists( $file_path ) && empty( $rows ) ) {
unlink( $file_path );
$wpdb->delete( $wpdb->ewwwio_images, array( 'path' => $file_path ) );
}
}
// remove the regular image from the ewwwio_images tables
list( $file_path, $upload_path ) = ewww_image_optimizer_attachment_path( $meta, $id );
$wpdb->delete($wpdb->ewwwio_images, array('path' => $file_path));
// resized versions, so we can continue
if (isset($meta['sizes']) ) {
// one way or another, $file_path is now set, and we can get the base folder name
$base_dir = dirname($file_path) . '/';
// check each resized version
foreach($meta['sizes'] as $size => $data) {
// delete any residual webp versions
$webpfile = $base_dir . $data['file'] . '.webp';
$webpfileold = preg_replace( '/\.\w+$/', '.webp', $base_dir . $data['file'] );
if ( file_exists( $webpfile) ) {
unlink( $webpfile );
}
if ( file_exists( $webpfileold) ) {
unlink( $webpfileold );
}
$wpdb->delete($wpdb->ewwwio_images, array('path' => $base_dir . $data['file']));
// if the original resize is set, and still exists
if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) {
unset($srows);
// retrieve the filename from the metadata
$filename = $data['orig_file'];
// retrieve any posts that link the image
$esql = "SELECT ID, post_content FROM $wpdb->posts WHERE post_content LIKE '%$filename%'";
$srows = $wpdb->get_row($esql);
// if there are no posts containing links to the original, delete it
if(empty($srows)) {
unlink($base_dir . $data['orig_file']);
$wpdb->delete($wpdb->ewwwio_images, array('path' => $base_dir . $data['orig_file']));
}
}
}
}
ewwwio_memory( __FUNCTION__ );
return;
}
function ewww_image_optimizer_cloud_key_sanitize( $key ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$key = trim( $key );
ewwwio_debug_message( print_r( $_REQUEST, true ) );
if ( ewww_image_optimizer_cloud_verify( false, $key ) ) {
ewwwio_debug_message( 'sanitize (verification) successful' );
ewwwio_memory( __FUNCTION__ );
// ewww_image_optimizer_debug_log();
return $key;
} else {
ewwwio_debug_message( 'sanitize (verification) failed' );
ewwwio_memory( __FUNCTION__ );
// ewww_image_optimizer_debug_log();
return '';
}
}
function ewww_image_optimizer_full_cloud() {
// ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) > 10 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) > 10 ) {
// ewwwio_debug_message( 'all cloud mode enabled, no local' );
return true;
} elseif ( EWWW_IMAGE_OPTIMIZER_DOMAIN == 'ewww-image-optimizer-cloud' ) {
// ewwwio_debug_message( 'cloud-only plugin, no local' );
return true;
}
// ewwwio_debug_message( 'local mode allowed' );
return false;
}
// turns on the cloud settings when they are all disabled
function ewww_image_optimizer_cloud_enable() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
add_option( 'ewww_image_optimizer_jpg_level', '20' );
add_option( 'ewww_image_optimizer_png_level', '20' );
add_option( 'ewww_image_optimizer_gif_level', '10' );
add_option( 'ewww_image_optimizer_pdf_level', '10' );
// just to make sure they get set with & without a database
ewww_image_optimizer_set_option('ewww_image_optimizer_jpg_level', 20);
ewww_image_optimizer_set_option('ewww_image_optimizer_png_level', 20);
ewww_image_optimizer_set_option('ewww_image_optimizer_gif_level', 10);
ewww_image_optimizer_set_option('ewww_image_optimizer_pdf_level', 10);
}
// adds our version to the useragent for http requests
function ewww_image_optimizer_cloud_useragent() {
return 'EWWW SILO/' . EWWW_IMAGE_OPTIMIZER_VERSION;
}
// submits the api key for verification
function ewww_image_optimizer_cloud_verify( $cache = true, $api_key = '' ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( empty( $api_key ) ) {
$api_key = ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' );
} elseif ( empty( $api_key ) && ! empty( $_POST['ewww_image_optimizer_cloud_key'] ) ) {
$api_key = $_POST['ewww_image_optimizer_cloud_key'];
}
if ( empty( $api_key ) ) {
ewwwio_debug_message( 'no api key' );
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) > 10 ) {
update_option( 'ewww_image_optimizer_jpg_level', 10 );
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) > 10 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) != 40 ) {
update_option( 'ewww_image_optimizer_png_level', 10 );
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) > 0 ) {
update_option( 'ewww_image_optimizer_pdf_level', 0 );
}
return false;
}
$ewww_cloud_status = get_transient( 'ewww_image_optimizer_cloud_status' );
if ( false && $cache && preg_match( '/great/', $ewww_cloud_status ) ) {
ewwwio_debug_message( 'using cached verification' );
return $ewww_cloud_status;
}
$result = ewww_image_optimizer_cloud_post_key( 'optimize.exactlywww.com', 'https', $api_key );
if ( empty( $result->success ) ) {
$result->throw_for_status( false );
ewwwio_debug_message( "verification failed" );
} elseif ( ! empty( $result->body ) && preg_match( '/(great|exceeded)/', $result->body ) ) {
$verified = $result->body;
ewwwio_debug_message( "verification success" );
} else {
ewwwio_debug_message( "verification failed" );
ewwwio_debug_message( print_r( $result, true ) );
}
if ( empty( $verified ) ) {
ewwwio_memory( __FUNCTION__ );
return FALSE;
} else {
set_transient( 'ewww_image_optimizer_cloud_status', $verified, 3600 );
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) < 20 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) < 20 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_gif_level' ) < 20 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) == 0 ) {
ewww_image_optimizer_cloud_enable();
}
ewwwio_debug_message( "verification body contents: " . $result->body );
ewwwio_memory( __FUNCTION__ );
return $verified;
}
}
function ewww_image_optimizer_cloud_post_key( $ip, $transport, $key ) {
$useragent = ewww_image_optimizer_cloud_useragent();
$result = Requests::post( "$transport://$ip/verify/", array(), array( 'api_key' => $key ), array( 'timeout' => 5, 'useragent' => $useragent ) );
return $result;
}
// checks the provided api key for quota information
function ewww_image_optimizer_cloud_quota() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$api_key = ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' );
$url = "https://optimize.exactlywww.com/quota/";
$useragent = ewww_image_optimizer_cloud_useragent();
$result = Requests::post( $url, array(), array( 'api_key' => $api_key ), array( 'timeout' => 5, 'useragent' => $useragent ) );
/*$result = wp_remote_post( $url, array(
'timeout' => 5,
'sslverify' => false,
'body' => array( 'api_key' => $api_key )
) );*/
if ( ! $result->success ) {
$result->throw_for_status( false );
ewwwio_debug_message( "quota request failed: $error_message" );
ewwwio_memory( __FUNCTION__ );
return '';
} elseif ( ! empty( $result->body ) ) {
ewwwio_debug_message( "quota data retrieved: " . $result->body );
$quota = explode( ' ', $result->body );
ewwwio_memory( __FUNCTION__ );
if ( $quota[0] == 0 && $quota[1] > 0 ) {
return esc_html( sprintf( _n( 'optimized %1$d images, usage will reset in %2$d day.', 'optimized %1$d images, usage will reset in %2$d days.', $quota[2], EWWW_IMAGE_OPTIMIZER_DOMAIN ), $quota[1], $quota[2] ) );
} elseif ( $quota[0] == 0 && $quota[1] < 0 ) {
return esc_html( sprintf( _n( '%1$d image credit remaining.', '%1$d image credits remaining.', abs( $quota[1] ), EWWW_IMAGE_OPTIMIZER_DOMAIN ), abs( $quota[1] ) ) );
} elseif ( $quota[0] > 0 && $quota[1] < 0 ) {
$real_quota = $quota[0] - $quota[1];
return esc_html( sprintf( _n( '%1$d image credit remaining.', '%1$d image credits remaining.', $real_quota, EWWW_IMAGE_OPTIMIZER_DOMAIN ), $real_quota ) );
} else {
return esc_html( sprintf( _n( 'used %1$d of %2$d, usage will reset in %3$d day.', 'used %1$d of %2$d, usage will reset in %3$d days.', $quota[2], EWWW_IMAGE_OPTIMIZER_DOMAIN ), $quota[1], $quota[0], $quota[2] ) );
}
}
}
/* submits an image to the cloud optimizer and saves the optimized image to disk
*
* Returns an array of the $file, $results, $converted to tell us if an image changes formats, and the $original file if it did.
*
* @param string $file Full absolute path to the image file
* @param string $type mimetype of $file
* @param boolean $convert true says we want to attempt conversion of $file
* @param string $newfile filename of new converted image
* @param string $newtype mimetype of $newfile
* @param boolean $fullsize is this the full-size original?
* @param array $jpg_params r, g, b values and jpg quality setting for conversion
* @returns array
*/
function ewww_image_optimizer_cloud_optimizer( $file, $type, $convert = false, $newfile = null, $newtype = null, $fullsize = false, $jpg_params = array( 'r' => '255', 'g' => '255', 'b' => '255', 'quality' => null ) ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// $ewww_cloud_ip = get_transient( 'ewww_image_optimizer_cloud_ip' );
// $ewww_cloud_transport = get_transient( 'ewww_image_optimizer_cloud_transport' );
$ewww_status = get_transient( 'ewww_image_optimizer_cloud_status' );
$started = microtime( true );
if ( preg_match( '/exceeded/', $ewww_status ) ) {
if ( ! ewww_image_optimizer_cloud_verify() ) {
return array( $file, false, 'key verification failed', 0 );
/* } else {
$ewww_cloud_ip = get_transient( 'ewww_image_optimizer_cloud_ip' );
$ewww_cloud_transport = get_transient( 'ewww_image_optimizer_cloud_transport' );*/
}
}
EWWWIO_CLI::line( ewww_image_optimizer_cloud_quota() );
// calculate how much time has elapsed since we started
$elapsed = microtime( true ) - $started;
// output how much time has elapsed since we started
ewwwio_debug_message( sprintf( 'Cloud verify took %.3f seconds', $elapsed ) );
$ewww_status = get_transient( 'ewww_image_optimizer_cloud_status' );
if ( ! empty ( $ewww_status ) && preg_match( '/exceeded/', $ewww_status ) ) {
ewwwio_debug_message( 'license exceeded, image not processed' );
return array($file, false, 'exceeded', 0);
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_metadata_skip_full' ) && $fullsize ) {
$metadata = 1;
} elseif ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpegtran_copy' ) ){
// don't copy metadata
$metadata = 0;
} else {
// copy all the metadata
$metadata = 1;
}
if ( empty( $convert ) ) {
$convert = 0;
} else {
$convert = 1;
}
$lossy_fast = 0;
if ( ewww_image_optimizer_get_option('ewww_image_optimizer_lossy_skip_full') && $fullsize ) {
$lossy = 0;
} elseif ( $type == 'image/png' && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) >= 40 ) {
$lossy = 1;
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) == 40 ) {
$lossy_fast = 1;
}
} elseif ( $type == 'image/jpeg' && ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) >= 30 ) {
$lossy = 1;
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) == 30 ) {
$lossy_fast = 1;
}
} elseif ( $type == 'application/pdf' && ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) == 20 ) {
$lossy = 1;
} else {
$lossy = 0;
}
if ( $newtype == 'image/webp' ) {
$webp = 1;
} else {
$webp = 0;
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) == 30 ) {
$png_compress = 1;
} else {
$png_compress = 0;
}
ewwwio_debug_message( "file: $file " );
ewwwio_debug_message( "type: $type" );
ewwwio_debug_message( "convert: $convert" );
ewwwio_debug_message( "newfile: $newfile" );
ewwwio_debug_message( "newtype: $newtype" );
ewwwio_debug_message( "webp: $webp" );
ewwwio_debug_message( "jpg_params: " . print_r($jpg_params, true) );
$api_key = ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_key');
$url = "https://optimize.exactlywww.com/";
$boundary = generate_password( 24 );
$useragent = ewww_image_optimizer_cloud_useragent();
$headers = array(
'content-type' => 'multipart/form-data; boundary=' . $boundary,
// 'timeout' => 90,
// 'httpversion' => '1.0',
// 'blocking' => true
);
$post_fields = array(
'filename' => $file,
'convert' => $convert,
'metadata' => $metadata,
'api_key' => $api_key,
'red' => $jpg_params['r'],
'green' => $jpg_params['g'],
'blue' => $jpg_params['b'],
'quality' => $jpg_params['quality'],
'compress' => $png_compress,
'lossy' => $lossy,
'lossy_fast' => $lossy_fast,
'webp' => $webp,
);
$payload = '';
foreach ($post_fields as $name => $value) {
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="' . $name .'"' . "\r\n\r\n";
$payload .= $value;
$payload .= "\r\n";
}
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="file"; filename="' . basename($file) . '"' . "\r\n";
$payload .= 'Content-Type: ' . $type . "\r\n";
$payload .= "\r\n";
$payload .= file_get_contents($file);
$payload .= "\r\n";
$payload .= '--' . $boundary;
$payload .= 'Content-Disposition: form-data; name="submitHandler"' . "\r\n";
$payload .= "\r\n";
$payload .= "Upload\r\n";
$payload .= '--' . $boundary . '--';
// retrieve the time when the optimizer starts
// $started = microtime(true);
$response = Requests::post(
$url,
$headers,
$payload,
array(
'timeout' => 90,
'useragent' => $useragent,
)
);
/* $response = wp_remote_post( $url, array(
'timeout' => 90,
'headers' => $headers,
'sslverify' => false,
'body' => $payload,
) );*/
// $elapsed = microtime(true) - $started;
// $ewww_debug .= "processing image via cloud took $elapsed seconds<br>";
if ( ! $response->success ) {
$response->throw_for_status( false );
ewwwio_debug_message( "optimize failed, see exception" );
return array( $file, false, 'cloud optimize failed', 0 );
} else {
$tempfile = $file . ".tmp";
file_put_contents( $tempfile, $response->body );
$orig_size = filesize( $file );
$newsize = $orig_size;
$converted = false;
$msg = '';
if ( preg_match( '/exceeded/', $response->body ) ) {
ewwwio_debug_message( 'License Exceeded' );
set_transient( 'ewww_image_optimizer_cloud_status', 'exceeded', 3600 );