forked from nosilver4u/ewww-image-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique.php
2922 lines (2873 loc) · 133 KB
/
unique.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
/**
* Unique functions for Standard EWWW I.O. plugins.
*
* This file contains functions that are unique to the regular EWWW IO plugin.
*
* @link https://ewww.io
* @package EWWW_Image_Optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Installation routine for PNGOUT.
add_action( 'admin_action_ewww_image_optimizer_install_pngout', 'ewww_image_optimizer_install_pngout_wrapper' );
// AJAX action hook to dismiss the exec notice May be extended to other notices in the future.
add_action( 'wp_ajax_ewww_dismiss_exec_notice', 'ewww_image_optimizer_dismiss_exec_notice' );
// Removes the binaries when the plugin is deactivated.
register_deactivation_hook( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE, 'ewww_image_optimizer_remove_binaries' );
/**
* Make sure the cloud constant is defined.
*
* Check to see if the cloud constant is defined (which would mean we've already run init) and set it properly if not.
*/
function ewww_image_optimizer_cloud_init() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Basically this happens on settings save, so we need to check if the settings are being saved with an empty key, and pre-emptively set the constant to false.
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_CLOUD' ) && isset( $_POST['ewww_image_optimizer_cloud_key'] ) && ! $_POST['ewww_image_optimizer_cloud_key'] ) {
define( 'EWWW_IMAGE_OPTIMIZER_CLOUD', false );
return;
}
if (
! defined( 'EWWW_IMAGE_OPTIMIZER_CLOUD' ) &&
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
) {
define( 'EWWW_IMAGE_OPTIMIZER_CLOUD', true );
} elseif ( ! defined( 'EWWW_IMAGE_OPTIMIZER_CLOUD' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_CLOUD', false );
}
ewwwio_memory( __FUNCTION__ );
}
/**
* Initializes settings for the local tools, and runs the checks for tools on select pages.
*/
function ewww_image_optimizer_exec_init() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( defined( 'WPE_PLUGIN_VERSION' ) ) {
add_action( 'network_admin_notices', 'ewww_image_optimizer_notice_wpengine' );
add_action( 'admin_notices', 'ewww_image_optimizer_notice_wpengine' );
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_NOEXEC' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', true );
}
}
global $exactdn;
// If cloud is fully enabled, we're going to skip all the checks related to the bundled tools.
if ( EWWW_IMAGE_OPTIMIZER_CLOUD ) {
ewwwio_debug_message( 'cloud options enabled, shutting off binaries' );
ewww_image_optimizer_disable_tools();
} elseif ( defined( 'WPCOMSH_VERSION' ) || ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
if (
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) &&
empty( $_POST['ewww_image_optimizer_cloud_key'] ) &&
( ! is_object( $exactdn ) || ! $exactdn->get_exactdn_domain() )
) {
add_action( 'network_admin_notices', 'ewww_image_optimizer_notice_hosting_requires_api' );
add_action( 'admin_notices', 'ewww_image_optimizer_notice_hosting_requires_api' );
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_NOEXEC' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', true );
}
ewwwio_debug_message( 'wp.com/pantheon site, disabling tools' );
ewww_image_optimizer_disable_tools();
// Check if this is an unsupported OS (not Linux or Mac OSX or FreeBSD or Windows or SunOS).
} elseif ( 'Linux' !== PHP_OS && 'Darwin' !== PHP_OS && 'FreeBSD' !== PHP_OS && 'WINNT' !== PHP_OS && 'SunOS' !== PHP_OS ) {
// Call the function to display a notice.
add_action( 'network_admin_notices', 'ewww_image_optimizer_notice_os' );
add_action( 'admin_notices', 'ewww_image_optimizer_notice_os' );
// Turn off all the tools.
ewwwio_debug_message( 'unsupported OS, disabling tools: ' . PHP_OS );
ewww_image_optimizer_disable_tools();
} else {
add_action( 'load-upload.php', 'ewww_image_optimizer_tool_init', 9 );
add_action( 'load-media-new.php', 'ewww_image_optimizer_tool_init' );
add_action( 'load-media_page_ewww-image-optimizer-bulk', 'ewww_image_optimizer_tool_init' );
add_action( 'load-settings_page_' . str_replace( '.php', '', EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL ), 'ewww_image_optimizer_tool_init' );
add_action( 'load-plugins.php', 'ewww_image_optimizer_tool_init' );
add_action( 'load-ims_gallery_page_ewww-ims-optimize', 'ewww_image_optimizer_tool_init' );
add_action( 'load-media_page_ewww-image-optimizer-unoptimized', 'ewww_image_optimizer_tool_init' );
add_action( 'load-flagallery_page_flag-manage-gallery', 'ewww_image_optimizer_tool_init' );
}
ewwwio_memory( __FUNCTION__ );
}
/**
* Check for binary installation and availability.
*/
function ewww_image_optimizer_tool_init() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Make sure the bundled tools are installed.
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_skip_bundle' ) ) {
ewww_image_optimizer_install_tools();
}
// Check for optimization utilities and register a notice if something is missing.
add_action( 'network_admin_notices', 'ewww_image_optimizer_notice_utils' );
add_action( 'admin_notices', 'ewww_image_optimizer_notice_utils' );
if ( defined( 'EWWW_IMAGE_OPTIMIZER_CLOUD' ) && EWWW_IMAGE_OPTIMIZER_CLOUD ) {
ewwwio_debug_message( 'cloud options enabled, shutting off binaries' );
ewww_image_optimizer_disable_tools();
}
}
/**
* Set some default option values.
*/
function ewww_image_optimizer_set_defaults() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Set defaults for all options that need to be autoloaded.
add_option( 'ewww_image_optimizer_noauto', false );
add_option( 'ewww_image_optimizer_disable_editor', false );
add_option( 'ewww_image_optimizer_debug', false );
add_option( 'ewww_image_optimizer_metadata_remove', true );
add_option( 'ewww_image_optimizer_jpg_level', '10' );
add_option( 'ewww_image_optimizer_png_level', '10' );
add_option( 'ewww_image_optimizer_gif_level', '10' );
add_option( 'ewww_image_optimizer_pdf_level', '0' );
add_option( 'ewww_image_optimizer_exactdn', false );
add_option( 'exactdn_all_the_things', true );
add_option( 'exactdn_lossy', true );
add_option( 'ewww_image_optimizer_lazy_load', false );
add_option( 'ewww_image_optimizer_disable_pngout', true );
add_option( 'ewww_image_optimizer_optipng_level', 2 );
add_option( 'ewww_image_optimizer_pngout_level', 2 );
add_option( 'ewww_image_optimizer_webp_for_cdn', false );
// Set network defaults.
add_site_option( 'ewww_image_optimizer_metadata_remove', true );
add_site_option( 'ewww_image_optimizer_jpg_level', '10' );
add_site_option( 'ewww_image_optimizer_png_level', '10' );
add_site_option( 'ewww_image_optimizer_gif_level', '10' );
add_site_option( 'ewww_image_optimizer_pdf_level', '0' );
add_site_option( 'ewww_image_optimizer_disable_pngout', true );
add_site_option( 'ewww_image_optimizer_optipng_level', 2 );
add_site_option( 'ewww_image_optimizer_pngout_level', 2 );
add_site_option( 'exactdn_all_the_things', true );
add_site_option( 'exactdn_lossy', true );
}
/**
* Let the user know the plugin requires API/ExactDN to operate at their webhost.
*/
function ewww_image_optimizer_notice_hosting_requires_api() {
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 ) ) {
$options_page = 'network/settings.php';
} else {
$options_page = 'options-general.php';
}
if ( defined( 'WPCOMSH_VERSION' ) ) {
$webhost = 'WordPress.com';
} elseif ( ! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
$webhost = 'Pantheon';
} else {
return;
}
$settings_url = admin_url( "$options_page?page=" . plugin_basename( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ) );
echo "<div id='ewww-image-optimizer-cloud-key-required' class='notice notice-error'><p><strong>" .
/* translators: %s: Name of a web host, like WordPress.com or Pantheon. */
sprintf( esc_html__( 'The EWWW Image Optimizer requires an API key or an ExactDN subscription to optimize images on %s sites.', 'ewww-image-optimizer-cloud' ), $webhost ) .
"</strong> <a href='https://ewww.io/plans/'>" . esc_html__( 'Purchase a subscription.', 'ewww-image-optimizer-cloud' ) .
"</a> <a href='$settings_url'>" . esc_html__( 'Then, activate it on the settings page.', 'ewww-image-optimizer-cloud' ) . '</a></p></div>';
}
/**
* Tells the user they are on an unsupported operating system.
*/
function ewww_image_optimizer_notice_os() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
echo "<div id='ewww-image-optimizer-warning-os' class='notice notice-error'><p><strong>" . esc_html__( 'The free mode of EWWW Image Optimizer is only supported on Linux, FreeBSD, Mac OSX, Solaris, and Windows.', 'ewww-image-optimizer' ) . '</strong></p></div>';
}
/**
* Generates the source and destination paths for the executables that we bundle with the plugin.
*
* Paths are determined based on the operating system and architecture.
*/
function ewww_image_optimizer_install_paths() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$tool_path = trailingslashit( EWWW_IMAGE_OPTIMIZER_TOOL_PATH );
if ( PHP_OS === 'WINNT' ) {
$gifsicle_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'gifsicle.exe';
$optipng_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'optipng.exe';
$jpegtran_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'jpegtran.exe';
$pngquant_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'pngquant.exe';
$webp_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'cwebp.exe';
$gifsicle_dst = $tool_path . 'gifsicle.exe';
$optipng_dst = $tool_path . 'optipng.exe';
$jpegtran_dst = $tool_path . 'jpegtran.exe';
$pngquant_dst = $tool_path . 'pngquant.exe';
$webp_dst = $tool_path . 'cwebp.exe';
}
if ( PHP_OS === 'Darwin' ) {
$gifsicle_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'gifsicle-mac';
$optipng_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'optipng-mac';
$jpegtran_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'jpegtran-mac';
$pngquant_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'pngquant-mac';
$webp_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'cwebp-mac14';
$gifsicle_dst = $tool_path . 'gifsicle';
$optipng_dst = $tool_path . 'optipng';
$jpegtran_dst = $tool_path . 'jpegtran';
$pngquant_dst = $tool_path . 'pngquant';
$webp_dst = $tool_path . 'cwebp';
}
if ( PHP_OS === 'SunOS' ) {
$gifsicle_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'gifsicle-sol';
$optipng_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'optipng-sol';
$jpegtran_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'jpegtran-sol';
$pngquant_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'pngquant-sol';
$webp_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'cwebp-sol';
$gifsicle_dst = $tool_path . 'gifsicle';
$optipng_dst = $tool_path . 'optipng';
$jpegtran_dst = $tool_path . 'jpegtran';
$pngquant_dst = $tool_path . 'pngquant';
$webp_dst = $tool_path . 'cwebp';
}
if ( PHP_OS === 'FreeBSD' ) {
if ( ewww_image_optimizer_function_exists( 'php_uname' ) ) {
$arch_type = php_uname( 'm' );
ewwwio_debug_message( "CPU architecture: $arch_type" );
} else {
ewwwio_debug_message( 'CPU architecture unknown, php_uname disabled' );
}
$gifsicle_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'gifsicle-fbsd';
$optipng_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'optipng-fbsd';
$jpegtran_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'jpegtran-fbsd';
$pngquant_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'pngquant-fbsd';
$webp_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'cwebp-fbsd';
$gifsicle_dst = $tool_path . 'gifsicle';
$optipng_dst = $tool_path . 'optipng';
$jpegtran_dst = $tool_path . 'jpegtran';
$pngquant_dst = $tool_path . 'pngquant';
$webp_dst = $tool_path . 'cwebp';
}
if ( PHP_OS === 'Linux' ) {
if ( ewww_image_optimizer_function_exists( 'php_uname' ) ) {
$arch_type = php_uname( 'm' );
ewwwio_debug_message( "CPU architecture: $arch_type" );
} else {
ewwwio_debug_message( 'CPU architecture unknown, php_uname disabled' );
}
$gifsicle_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'gifsicle-linux';
$optipng_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'optipng-linux';
$jpegtran_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'jpegtran-linux';
$pngquant_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'pngquant-linux';
$webp_src = EWWW_IMAGE_OPTIMIZER_BINARY_PATH . 'cwebp-linux';
$gifsicle_dst = $tool_path . 'gifsicle';
$optipng_dst = $tool_path . 'optipng';
$jpegtran_dst = $tool_path . 'jpegtran';
$pngquant_dst = $tool_path . 'pngquant';
$webp_dst = $tool_path . 'cwebp';
}
ewwwio_debug_message( "generated paths:<br>$jpegtran_src<br>$optipng_src<br>$gifsicle_src<br>$pngquant_src<br>$webp_src<br>$jpegtran_dst<br>$optipng_dst<br>$gifsicle_dst<br>$pngquant_dst<br>$webp_dst" );
ewwwio_memory( __FUNCTION__ );
return array( $jpegtran_src, $optipng_src, $gifsicle_src, $pngquant_src, $webp_src, $jpegtran_dst, $optipng_dst, $gifsicle_dst, $pngquant_dst, $webp_dst );
}
/**
* Makes sure permissions on a file/folder are adequate.
*
* @param string $file The file or folder to test.
* @param string $minimum The minimum file permissions needed.
* @return bool True if permissions are equal to or better than what is required.
*/
function ewww_image_optimizer_check_permissions( $file, $minimum ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$perms = fileperms( $file );
ewwwio_debug_message( "permissions for $file: " . substr( sprintf( '%o', $perms ), -4 ) );
if ( ! ewwwio_is_file( $file ) ) {
ewwwio_debug_message( 'permissions check failed, file not found' );
return false;
}
if ( is_readable( $file ) && is_executable( $file ) ) {
ewwwio_debug_message( 'permissions ok' );
return true;
}
ewwwio_debug_message( 'permissions insufficient' );
return false;
}
/**
* Alert the user when the tool folder could not be created.
*/
function ewww_image_optimizer_tool_folder_notice() {
echo "<div id='ewww-image-optimizer-warning-tool-folder-create' class='notice notice-error'><p><strong>" . esc_html__( 'EWWW Image Optimizer could not create the tool folder', 'ewww-image-optimizer' ) . ': ' . htmlentities( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) . '.</strong> ' . esc_html__( 'Please adjust permissions or create the folder', 'ewww-image-optimizer' ) . '.</p></div>';
}
/**
* Alert the user when permissions on the tool folder are insufficient.
*/
function ewww_image_optimizer_tool_folder_permissions_notice() {
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 ) ) {
$settings_page = 'settings.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
} else {
$settings_page = 'options-general.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
}
echo "<div id='ewww-image-optimizer-warning-tool-folder-permissions' class='notice notice-error'><p><strong>" .
/* translators: %s: Folder location where executables should be installed */
sprintf( esc_html__( 'EWWW Image Optimizer could not install tools in %s', 'ewww-image-optimizer' ), htmlentities( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) . '.</strong> ' .
esc_html__( 'Please adjust permissions or create the folder. If you have installed the tools elsewhere on your system, use the override which allows you to skip the bundled tools.', 'ewww-image-optimizer' ) . ' ' .
/* translators: 1: Settings Page (link) 2: Installation Instructions (link) */
sprintf( esc_html__( 'For more details, visit the %1$s or the %2$s.', 'ewww-image-optimizer' ), "<a href='$settings_page'>" . esc_html__( 'Settings Page', 'ewww-image-optimizer' ) . '</a>', "<a href='https://docs.ewww.io/'>" . esc_html__( 'Installation Instructions', 'ewww-image-optimizer' ) . '</a>' ) . '</p></div>';
}
/**
* Alert the user when tool installation fails.
*/
function ewww_image_optimizer_tool_installation_failed_notice() {
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 ) ) {
$settings_page = 'settings.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
} else {
$settings_page = 'options-general.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
}
echo "<div id='ewww-image-optimizer-warning-tool-install' class='notice notice-error'><p><strong>" .
/* translators: %s: Folder location where executables should be installed */
sprintf( esc_html__( 'EWWW Image Optimizer could not install tools in %s', 'ewww-image-optimizer' ), htmlentities( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) . '.</strong> ' .
esc_html__( 'Please adjust permissions or create the folder. If you have installed the tools elsewhere on your system, check the option to Use System Paths.', 'ewww-image-optimizer' ) . ' ' .
/* translators: 1: Settings Page (link) 2: Installation Instructions (link) */
sprintf( esc_html__( 'For more details, visit the %1$s or the %2$s.', 'ewww-image-optimizer' ), "<a href='$settings_page'>" . esc_html__( 'Settings Page', 'ewww-image-optimizer' ) . '</a>', "<a href='https://docs.ewww.io/'>" . esc_html__( 'Installation Instructions', 'ewww-image-optimizer' ) . '</a>' ) . '</p></div>';
}
/**
* Installs the executables that are bundled with the plugin.
*/
function ewww_image_optimizer_install_tools() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
ewwwio_debug_message( 'Checking/Installing tools in ' . EWWW_IMAGE_OPTIMIZER_TOOL_PATH );
$toolfail = false;
if ( ! is_dir( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) && is_writable( dirname( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) ) {
ewwwio_debug_message( 'folder does not exist, creating...' );
if ( ! wp_mkdir_p( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) {
ewwwio_debug_message( 'could not create folder' );
return;
}
} elseif ( is_dir( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) {
if ( ! is_writable( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) {
ewwwio_debug_message( 'wp-content/ewww is not writable, not installing anything' );
return;
}
$ewww_perms = substr( sprintf( '%o', fileperms( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ), -4 );
ewwwio_debug_message( "wp-content/ewww permissions: $ewww_perms" );
} else {
ewwwio_debug_message( 'wp-content is not writable, and the ewww folder does not exist' );
return;
}
list ( $jpegtran_src, $optipng_src, $gifsicle_src, $pngquant_src, $webp_src, $jpegtran_dst, $optipng_dst, $gifsicle_dst, $pngquant_dst, $webp_dst ) = ewww_image_optimizer_install_paths();
$skip = ewww_image_optimizer_skip_tools();
if ( ! $skip['jpegtran'] && ( ! file_exists( $jpegtran_dst ) || filesize( $jpegtran_dst ) !== filesize( $jpegtran_src ) ) ) {
ewwwio_debug_message( 'jpegtran not found or different size, installing' );
if ( ! copy( $jpegtran_src, $jpegtran_dst ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not copy jpegtran' );
}
}
if ( ! $skip['gifsicle'] && ( ! file_exists( $gifsicle_dst ) || filesize( $gifsicle_dst ) !== filesize( $gifsicle_src ) ) ) {
ewwwio_debug_message( 'gifsicle not found or different size, installing' );
if ( ! copy( $gifsicle_src, $gifsicle_dst ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not copy gifsicle' );
}
}
if ( ! $skip['optipng'] && ( ! file_exists( $optipng_dst ) || filesize( $optipng_dst ) !== filesize( $optipng_src ) ) ) {
ewwwio_debug_message( 'optipng not found or different size, installing' );
if ( ! copy( $optipng_src, $optipng_dst ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not copy optipng' );
}
}
if ( ! $skip['pngquant'] && ( ! file_exists( $pngquant_dst ) || filesize( $pngquant_dst ) !== filesize( $pngquant_src ) ) ) {
ewwwio_debug_message( 'pngquant not found or different size, installing' );
if ( ! copy( $pngquant_src, $pngquant_dst ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not copy pngquant' );
}
}
if ( ! $skip['webp'] && ( ! file_exists( $webp_dst ) || filesize( $webp_dst ) !== filesize( $webp_src ) ) ) {
ewwwio_debug_message( 'webp not found or different size, installing' );
if ( ! copy( $webp_src, $webp_dst ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not copy webp' );
}
}
if ( PHP_OS !== 'WINNT' && ! $toolfail ) {
ewwwio_debug_message( 'Linux/UNIX style OS, checking permissions' );
if ( ! $skip['jpegtran'] && ! ewww_image_optimizer_check_permissions( $jpegtran_dst, 'rwxr-xr-x' ) ) {
if ( ! is_writable( $jpegtran_dst ) || ! chmod( $jpegtran_dst, 0755 ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not set jpegtran permissions' );
}
}
if ( ! $skip['gifsicle'] && ! ewww_image_optimizer_check_permissions( $gifsicle_dst, 'rwxr-xr-x' ) ) {
if ( ! is_writable( $gifsicle_dst ) || ! chmod( $gifsicle_dst, 0755 ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not set gifsicle permissions' );
}
}
if ( ! $skip['optipng'] && ! ewww_image_optimizer_check_permissions( $optipng_dst, 'rwxr-xr-x' ) ) {
if ( ! is_writable( $optipng_dst ) || ! chmod( $optipng_dst, 0755 ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not set optipng permissions' );
}
}
if ( ! $skip['pngquant'] && ! ewww_image_optimizer_check_permissions( $pngquant_dst, 'rwxr-xr-x' ) ) {
if ( ! is_writable( $pngquant_dst ) || ! chmod( $pngquant_dst, 0755 ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not set pngquant permissions' );
}
}
if ( ! $skip['webp'] && ! ewww_image_optimizer_check_permissions( $webp_dst, 'rwxr-xr-x' ) ) {
if ( ! is_writable( $webp_dst ) || ! chmod( $webp_dst, 0755 ) ) {
$toolfail = true;
ewwwio_debug_message( 'could not set webp permissions' );
}
}
}
if ( $toolfail ) {
add_action( 'network_admin_notices', 'ewww_image_optimizer_tool_installation_failed_notice' );
add_action( 'admin_notices', 'ewww_image_optimizer_tool_installation_failed_notice' );
}
ewwwio_memory( __FUNCTION__ );
}
/**
* Checks which tools should be skipped.
*
* @return array {
* A list of tools to skip.
*
* @type bool $jpegtran
* @type bool $optipng
* @type bool $gifsicle
* @type bool $pngout
* @type bool $pngquant
* @type bool $webp
* }
*/
function ewww_image_optimizer_skip_tools() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$skip['jpegtran'] = false;
$skip['optipng'] = false;
$skip['gifsicle'] = false;
$skip['pngout'] = false;
// Except these which are off by default.
$skip['pngquant'] = true;
$skip['webp'] = true;
// If the user has disabled a tool, we aren't going to bother checking to see if it is there.
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) || ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) > 10 ) {
$skip['jpegtran'] = true;
}
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) || ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) > 10 ) ) {
$skip['optipng'] = true;
}
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_gif_level' ) || ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
$skip['gifsicle'] = true;
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_disable_pngout' ) || ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) || ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) > 10 ) ) {
$skip['pngout'] = true;
}
if ( 40 === (int) ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
$skip['pngquant'] = false;
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp' ) && ! ( 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 ) ) {
$skip['webp'] = false;
}
foreach ( $skip as $tool => $disabled ) {
if ( ! $disabled ) {
ewwwio_debug_message( "enabled: $tool" );
}
}
return $skip;
}
/**
* See if the plugin should avoid the exec() function.
*/
function ewww_image_optimizer_define_noexec() {
if ( defined( 'EWWW_IMAGE_OPTIMIZER_NOEXEC' ) ) {
return;
}
// Check if exec is disabled.
if ( ewww_image_optimizer_exec_check() ) {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', true );
ewwwio_debug_message( 'exec seems to be disabled' );
ewww_image_optimizer_disable_tools();
} else {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', false );
}
}
/**
* Disables local compression when exec notice is dismissed by ExactDN user.
*/
function ewww_image_optimizer_dismiss_exec_notice() {
ewwwio_ob_clean();
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Verify that the user is properly authorized.
if ( ! current_user_can( apply_filters( 'ewww_image_optimizer_admin_permissions', 'manage_options' ) ) ) {
wp_die( esc_html__( 'Access denied.', 'ewww-image-optimizer' ) );
}
update_option( 'ewww_image_optimizer_jpg_level', 0 );
update_option( 'ewww_image_optimizer_png_level', 0 );
update_option( 'ewww_image_optimizer_gif_level', 0 );
update_option( 'ewww_image_optimizer_pdf_level', 0 );
wp_die();
}
/**
* Checks for safe mode and exec, then displays an error if needed.
*
* @param string $quiet Optional. Use 'quiet' to suppress warning messages.
*/
function ewww_image_optimizer_notice_utils( $quiet = null ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Check if exec is disabled.
if ( ewww_image_optimizer_exec_check() ) {
$no_compression = false;
if (
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_gif_level' )
) {
$no_compression = true;
}
// Need to be a little particular with the quiet parameter.
if ( 'quiet' !== $quiet && ! $no_compression ) {
$exactdn_dismiss = ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ? true : false;
// Display a warning if exec() is disabled, can't run local tools without it.
echo "<div id='ewww-image-optimizer-warning-exec' " . ( $exactdn_dismiss ? "class='notice notice-warning is-dismissible'" : "class='notice notice-error'" ) . '><p>' . esc_html__( 'EWWW Image Optimizer requires exec() to perform local compression. Your system administrator has disabled the exec() function, ask them to enable it.', 'ewww-image-optimizer' ) .
( ! $exactdn_dismiss ? '<br>' . esc_html__( 'An API key or ExactDN subscription will allow you to offload the compression to our dedicated servers instead.', 'ewww-image-optimizer' )
: '<br>' . esc_html__( 'Sites that use ExactDN already have built-in image optimization and may dismiss this notice to disable local compression.', 'ewww-image-optimizer' ) ) .
'</p></div>';
echo
"<script>\n" .
"jQuery(document).on('click', '#ewww-image-optimizer-warning-exec .notice-dismiss', function() {\n" .
"\tvar ewww_dismiss_exec_data = {\n" .
"\t\taction: 'ewww_dismiss_exec_notice',\n" .
"\t};\n" .
"\tjQuery.post(ajaxurl, ewww_dismiss_exec_data, function(response) {\n" .
"\t\tif (response) {\n" .
"\t\t\tconsole.log(response);\n" .
"\t\t}\n" .
"\t});\n" .
"});\n" .
"</script>\n";
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_NOEXEC' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', true );
}
ewwwio_debug_message( 'exec seems to be disabled' );
ewww_image_optimizer_disable_tools();
return;
} else {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_NOEXEC' ) ) {
define( 'EWWW_IMAGE_OPTIMIZER_NOEXEC', false );
}
}
$skip = ewww_image_optimizer_skip_tools();
// Attempt to retrieve values for utility paths, and store them in the appropriate variables.
$required = ewww_image_optimizer_path_check( ! $skip['jpegtran'], ! $skip['optipng'], ! $skip['gifsicle'], ! $skip['pngout'], ! $skip['pngquant'], ! $skip['webp'] );
$missing = array();
// Go through each of the required tools.
foreach ( $required as $key => $req ) {
// if the tool wasn't found, add it to the $missing array if we are supposed to check the tool in question.
switch ( $key ) {
case 'JPEGTRAN':
if ( ! $skip['jpegtran'] && empty( $req ) ) {
$missing[] = 'jpegtran';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
case 'OPTIPNG':
if ( ! $skip['optipng'] && empty( $req ) ) {
$missing[] = 'optipng';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
case 'GIFSICLE':
if ( ! $skip['gifsicle'] && empty( $req ) ) {
$missing[] = 'gifsicle';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
case 'PNGOUT':
if ( ! $skip['pngout'] && empty( $req ) ) {
$missing[] = 'pngout';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
case 'PNGQUANT':
if ( ! $skip['pngquant'] && empty( $req ) ) {
$missing[] = 'pngquant';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
case 'CWEBP':
if ( ! $skip['webp'] && empty( $req ) ) {
$missing[] = 'webp';
$req = false;
}
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_' . $key ) ) {
ewwwio_debug_message( "defining EWWW_IMAGE_OPTIMIZER_$key" );
define( 'EWWW_IMAGE_OPTIMIZER_' . $key, $req );
}
break;
} // End switch().
} // End foreach().
// Expand the missing utilities list for use in the error message.
$msg = implode( ', ', $missing );
// If there is a message, display the warning.
if ( ! empty( $msg ) && 'quiet' !== $quiet ) {
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 ) ) {
$settings_page = 'settings.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
} else {
$settings_page = 'options-general.php?page=' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL;
}
if ( ! is_dir( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) {
ewww_image_optimizer_tool_folder_notice();
} elseif ( ! is_writable( EWWW_IMAGE_OPTIMIZER_TOOL_PATH ) ) {
ewww_image_optimizer_tool_folder_permissions_notice();
}
if ( 'pngout' === $msg ) {
echo "<div id='ewww-image-optimizer-warning-opt-missing' class='notice notice-error'><p>" .
sprintf(
/* translators: 1: automatically (link) 2: manually (link) */
esc_html__( 'You are missing pngout. Install %1$s or %2$s.', 'ewww-image-optimizer' ),
'<a href="admin.php?action=ewww_image_optimizer_install_pngout">' . esc_html__( 'automatically', 'ewww-image-optimizer' ) . '</a>',
'<a href="https://docs.ewww.io/article/13-installing-pngout" data-beacon-article="5854531bc697912ffd6c1afa">' . esc_html__( 'manually', 'ewww-image-optimizer' ) . '</a>'
) .
'</p></div>';
} else {
echo "<div id='ewww-image-optimizer-warning-opt-missing' class='notice notice-error'><p>" .
sprintf(
/* translators: 1-6: jpegtran, optipng, pngout, pngquant, gifsicle, and cwebp (links) 7: Settings Page (link) 8: Installation Instructions (link) */
esc_html__( 'EWWW Image Optimizer uses %1$s, %2$s, %3$s, %4$s, %5$s, and %6$s. You are missing: %7$s. Please install via the %8$s or the %9$s.', 'ewww-image-optimizer' ),
"<a href='http://jpegclub.org/jpegtran/'>jpegtran</a>",
"<a href='http://optipng.sourceforge.net/'>optipng</a>",
"<a href='http://advsys.net/ken/utils.htm'>pngout</a>",
"<a href='http://pngquant.org/'>pngquant</a>",
"<a href='http://www.lcdf.org/gifsicle/'>gifsicle</a>",
"<a href='https://developers.google.com/speed/webp/'>cwebp</a>",
$msg,
"<a href='$settings_page'>" . esc_html__( 'Settings Page', 'ewww-image-optimizer' ) . '</a>',
"<a href='https://docs.ewww.io/article/6-the-plugin-says-i-m-missing-something' data-beacon-article='585371e3c697912ffd6c0ba1' target='_blank'>" . esc_html__( 'Installation Instructions', 'ewww-image-optimizer' ) . '</a>'
) .
'</p></div>';
}
ewwwio_memory( __FUNCTION__ );
}
}
/**
* Checks if exec() is disabled.
*
* @return bool True if exec() is disabled.
*/
function ewww_image_optimizer_exec_check() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( ! ewww_image_optimizer_function_exists( 'exec' ) ) {
return true;
}
return false;
}
/**
* Sends each tool to the binary checker appropriate for the operating system.
*
* @param bool $j True to check jpegtran.
* @param bool $o True to check optipng.
* @param bool $g True to check gifsicle.
* @param bool $p True to check pngout.
* @param bool $q True to check pngquant.
* @param bool $w True to check cwebp.
* @return array Path for each tool (indexes JPEGTRAN, OPTIPNG, GIFSICLE, PNGOUT, PNGQUANT, CWEBP),
* or false for disabled/missing tools.
*/
function ewww_image_optimizer_path_check( $j = true, $o = true, $g = true, $p = true, $q = true, $w = true ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$jpegtran = false;
$optipng = false;
$gifsicle = false;
$pngout = false;
$pngquant = false;
$webp = false;
ewww_image_optimizer_define_noexec();
if ( EWWW_IMAGE_OPTIMIZER_NOEXEC ) {
return array(
'JPEGTRAN' => false,
'OPTIPNG' => false,
'GIFSICLE' => false,
'PNGOUT' => false,
'PNGQUANT' => false,
'CWEBP' => false,
);
}
if ( 'WINNT' === PHP_OS ) {
if ( $j ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_JPEGTRAN' ) ) {
$jpegtran = ewww_image_optimizer_find_win_binary( 'jpegtran', 'j' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_JPEGTRAN' );
define( 'EWWW_IMAGE_OPTIMIZER_JPEGTRAN', $jpegtran );
} else {
$jpegtran = EWWW_IMAGE_OPTIMIZER_JPEGTRAN;
}
}
if ( $o ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_OPTIPNG' ) ) {
$optipng = ewww_image_optimizer_find_win_binary( 'optipng', 'o' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_OPTIPNG' );
define( 'EWWW_IMAGE_OPTIMIZER_OPTIPNG', $optipng );
} else {
$optipng = EWWW_IMAGE_OPTIMIZER_OPTIPNG;
}
}
if ( $g ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_GIFSICLE' ) ) {
$gifsicle = ewww_image_optimizer_find_win_binary( 'gifsicle', 'g' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_GIFSICLE' );
define( 'EWWW_IMAGE_OPTIMIZER_GIFSICLE', $gifsicle );
} else {
$gifsicle = EWWW_IMAGE_OPTIMIZER_GIFSICLE;
}
}
if ( $p ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGOUT' ) ) {
$pngout = ewww_image_optimizer_find_win_binary( 'pngout', 'p' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_PNGOUT' );
define( 'EWWW_IMAGE_OPTIMIZER_PNGOUT', $pngout );
} else {
$pngout = EWWW_IMAGE_OPTIMIZER_PNGOUT;
}
}
if ( $q ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGQUANT' ) ) {
$pngquant = ewww_image_optimizer_find_win_binary( 'pngquant', 'q' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_PNGQUANT' );
define( 'EWWW_IMAGE_OPTIMIZER_PNGQUANT', $pngquant );
} else {
$pngquant = EWWW_IMAGE_OPTIMIZER_PNGQUANT;
}
}
if ( $w ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_CWEBP' ) ) {
$webp = ewww_image_optimizer_find_win_binary( 'cwebp', 'w' );
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_CWEBP' );
define( 'EWWW_IMAGE_OPTIMIZER_CWEBP', $webp );
} else {
$webp = EWWW_IMAGE_OPTIMIZER_CWEBP;
}
}
} else {
if ( $j ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_JPEGTRAN' ) ) {
$jpegtran = ewww_image_optimizer_find_nix_binary( 'jpegtran', 'j' );
if ( ! $jpegtran ) {
$jpegtran = ewww_image_optimizer_find_nix_binary( 'jpegtran', 'jb' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_JPEGTRAN' );
define( 'EWWW_IMAGE_OPTIMIZER_JPEGTRAN', $jpegtran );
} else {
$jpegtran = EWWW_IMAGE_OPTIMIZER_JPEGTRAN;
}
}
if ( $o ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_OPTIPNG' ) ) {
$optipng = ewww_image_optimizer_find_nix_binary( 'optipng', 'o' );
if ( ! $optipng ) {
$optipng = ewww_image_optimizer_find_nix_binary( 'optipng', 'ob' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_OPTIPNG' );
define( 'EWWW_IMAGE_OPTIMIZER_OPTIPNG', $optipng );
} else {
$optipng = EWWW_IMAGE_OPTIMIZER_OPTIPNG;
}
}
if ( $g ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_GIFSICLE' ) ) {
$gifsicle = ewww_image_optimizer_find_nix_binary( 'gifsicle', 'g' );
if ( ! $gifsicle ) {
$gifsicle = ewww_image_optimizer_find_nix_binary( 'gifsicle', 'gb' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_GIFSICLE' );
define( 'EWWW_IMAGE_OPTIMIZER_GIFSICLE', $gifsicle );
} else {
$gifsicle = EWWW_IMAGE_OPTIMIZER_GIFSICLE;
}
}
if ( $p ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGOUT' ) ) {
// Pngout is special and has a dynamic and static binary to check.
$pngout = ewww_image_optimizer_find_nix_binary( 'pngout-static', 'p' );
if ( ! $pngout ) {
$pngout = ewww_image_optimizer_find_nix_binary( 'pngout', 'p' );
}
if ( ! $pngout ) {
$pngout = ewww_image_optimizer_find_nix_binary( 'pngout-static', 'pb' );
}
if ( ! $pngout ) {
$pngout = ewww_image_optimizer_find_nix_binary( 'pngout', 'pb' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_PNGOUT' );
define( 'EWWW_IMAGE_OPTIMIZER_PNGOUT', $pngout );
} else {
$pngout = EWWW_IMAGE_OPTIMIZER_PNGOUT;
}
}
if ( $q ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_PNGQUANT' ) ) {
$pngquant = ewww_image_optimizer_find_nix_binary( 'pngquant', 'q' );
if ( ! $pngquant ) {
$pngquant = ewww_image_optimizer_find_nix_binary( 'pngquant', 'qb' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_PNGQUANT' );
define( 'EWWW_IMAGE_OPTIMIZER_PNGQUANT', $pngquant );
} else {
$pngquant = EWWW_IMAGE_OPTIMIZER_PNGQUANT;
}
}
if ( $w ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_CWEBP' ) ) {
$webp = ewww_image_optimizer_find_nix_binary( 'cwebp', 'w' );
if ( ! $webp ) {
$webp = ewww_image_optimizer_find_nix_binary( 'cwebp', 'wb' );
}
ewwwio_debug_message( 'defining EWWW_IMAGE_OPTIMIZER_CWEBP' );
define( 'EWWW_IMAGE_OPTIMIZER_CWEBP', $webp );
} else {
$webp = EWWW_IMAGE_OPTIMIZER_CWEBP;
}
}
} // End if().
if ( $jpegtran ) {
ewwwio_debug_message( "using: $jpegtran" );
}
if ( $optipng ) {
ewwwio_debug_message( "using: $optipng" );
}
if ( $gifsicle ) {
ewwwio_debug_message( "using: $gifsicle" );
}
if ( $pngout ) {
ewwwio_debug_message( "using: $pngout" );
}
if ( $pngquant ) {
ewwwio_debug_message( "using: $pngquant" );
}
if ( $webp ) {
ewwwio_debug_message( "using: $webp" );
}
ewwwio_memory( __FUNCTION__ );
return array(
'JPEGTRAN' => $jpegtran,
'OPTIPNG' => $optipng,
'GIFSICLE' => $gifsicle,
'PNGOUT' => $pngout,
'PNGQUANT' => $pngquant,
'CWEBP' => $webp,
);
}
/**
* Checks the binary against a list of valid sha256 checksums (formerly md5sums).
*
* @param string $path The filename of a binary to check for a match.
* @return bool True if the sha256sum is validated.
*/
function ewww_image_optimizer_md5check( $path ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$binary_sum = hash_file( 'sha256', $path );
ewwwio_debug_message( "$path: $binary_sum" );
$valid_sums = array(
'463de9ba684d54d27185cb6487a0b22b7571a87419abde4dee72c9b107f23315', // jpegtran-mac 9, EWWW 1.3.0.
'0b94f82e3d740d1853281e9aaee5cc7122c27fd63da9d6d62ed3398997cbed1e', // jpegtran-linux 9, EWWW 1.4.0.
'f5f079bfe6f3f48c17738679292f35cdee44afe8f8413cdbc4f555cee7de4173', // jpegtran-linux64 9, EWWW 1.4.0.
'ec71f638d2101f08fab66f4d139746d4042352bc75d55bd093aa446081892e0c', // jpegtran-fbsd 9, EWWW 1.4.0.
'356532227fce51fcb9df29f143ab9d202fbd40f18e2b8234aee95937c93bd67e', // jpegtran-fbsd64 9, EWWW 1.4.0.
'7be857837764dff4f0d7d2c5d546bf4d2573af7f326ced908ac229d60fd054c6', // jpegtran.exe 9, EWWW 1.4.0.
'bce5205bb240532c01273b5442a44244a8a27a74fb47e2ce467c18b91fabea6b', // jpegtran-sol 9, EWWW 1.7.4.
'cadc7be4688632bf2860562a1596f1b2b54b9a9c8b27df7ecabca49b1dcd8a5f', // jpegtran-fbsd 9a, EWWW 2.0.0.
'bab4aa853c143534503464eeb35893d16799cf859ff22f9a4e62aa383f4fc99c', // jpegtran-fbsd64 9a, EWWW 2.0.0.
'deb7e0f579fac767196611aa110052864e3093017970ff74de709b41e265e8b1', // jpegtran-linux 9a, EWWW 2.0.0.
'b991fde396ebcc0e4f805df44b1797fe369f7f19e9392684dd4052e3f23c441e', // jpegtran-linux64 9a, EWWW 2.0.0.
'436835bd42b27d2f05440bc5dc5174f2a896d38f8a550d96704d39969951d9ac', // jpegtran-mac 9a, EWWW 2.0.0.
'bdf3c6b6cb16287a3f62e7cde8f69f8bda5d310abca28e00068c526f9f37cc89', // jpegtran-sol 9a, EWWW 2.0.0.
'3c2746d0b1ae150b13b767715af45ff601e394c01ada929cbe16e6dcd18fb199', // jpegtran.exe 9a, EWWW 2.0.0.
'8e11f7df5735b36d3ecc95c84b0e355355a766d3ccafbf751bcf343a8952432c', // jpegtran-fbsd 9b, EWWW 2.6.0.
'21d8046e07cb298dfd2f3b1e321c67c378a4d35fa8adc3521acc42b5b8088d64', // jpegtran-linux 9b, EWWW 2.6.0.
'4d1a1c601d291f96dc03ea7e42ab9137a17f93ebc391353db65b4e32c1e9fbdb', // jpegtran-mac 9b, EWWW 2.6.0.
'7e8719703d31e1ab9bf2b2ad7ab633649012ab6aae46ea40462365b9c00876d5', // jpegtran-sol 9b, EWWW 2.6.0.
'9767f05ae1b59d4fea25a73b276dcd1245f5281b53386dc03784539265bffbea', // jpegtran.exe 9b, EWWW 2.6.0.
// end jpegtran.
'6deddb5562ac13ffc3e46a0af79b592e92fb4553c5df294b6e0052bc890fd0e3', // optipng-linux 0.7.4, EWWW 1.2.0.
'51df81fa8c765efbe0aa4c1cf5293e25e7e2e7f6962f5161615239c54aec4c01', // optipng-linux 0.7.4, EWWW 1.3.0.
'7a56cca66471ce2b6cdff4460db0d75258ef05de8da1eda0448e4d4ad9ae252f', // optipng-mac 0.7.4, EWWW 1.3.0.
'2f9140cdc3ef1f7687baa710f0bba84c5f7f11e3f62c3ce43124e23b849ac5ff', // optipng-linux 0.7.4, EWWW 1.3.7.
'5d59467363c457bf743f4df121c365dd43365357f1cdea5f3752a7ca1b3e315a', // optipng-fbsd 0.7.4, EWWW 1.4.0.
'1af8077958a88a3064a71903841f901179e27fe137774085565619fb199c653a', // optipng.exe 0.7.4, EWWW 1.4.0.
'f692fef395b8689de033b9f2ce80c867c8a229c52e948df733377e20b62773a9', // optipng-sol 0.7.4, EWWW 1.7.4.
'e17d327cd89ab34eff7f994806fe9f2c124d6cc6cd309fa4c3911d5ce90312c9', // optipng-fbsd 0.7.5, EWWW 2.0.0.
'd263ecfb5b29ed08920e26cf604a86d3484daee5b80605e445cf97aa14d8aebc', // optipng-linux 0.7.5, EWWW 2.0.0.
'6f15cb2e8d25e51037efa7bcec7499c96eb11e576536a478edfee500207655ae', // optipng-mac 0.7.5, EWWW 2.0.0.
'1d2de40b009f16e9c709f9b0c15a47abb8da57668a918ac9a0723ddc6de6c30a', // optipng-sol 0.7.5, EWWW 2.0.0.
'fad3a0fd95706d53576f72593bf13d3e31d1c896c852bfd5b9ba602eca0bd2b6', // optipng.exe 0.7.5, EWWW 2.0.0.
'9d60eaeb9dc5167a57a5f3af236d56b4149d1043b543f2faa38a0936fa6b54b2', // optipng-fbsd 0.7.6, EWWW 2.8.0.
'853ca5936a2dd92a17b3518fd55db6be35e1b2bebfabca3949c34700072e08b8', // optipng-linux 0.7.6, EWWW 2.8.0.
'd4f11e96733aed64a72e744843dcd0929e144a7fc97f40d405a034a72eb9bbc6', // optipng-mac 0.7.6, EWWW 2.8.0.
'1ed9343194cfca0a1c32677c974192746adfd48cb4cea6a2df668452df0e68f7', // optipng-sol 0.7.6, EWWW 2.8.0.
'03b86ce2c08e2cc78d76d3d3dd173986b498b055c3c19e13a97a7c3c674772c6', // optipng.exe 0.7.6, EWWW 2.8.0.
'f01cba0ab658e08738315843ee635be273726bf102ae448416b3d8956843d864', // optipng-fbsd 0.7.7 EWWW 4.1.0.
'4404076a4f9119d4dfbb7acb00eb65345e804186a019c7136d8f8e87fb0cb997', // optipng-linux 0.7.7 EWWW 4.1.0.
'36535c1b262e0c457bbb0ed2bc71e812a49e26a6cada63b6acbd8d809c68a5a1', // optipng-mac 0.7.7 EWWW 4.1.0.
'41a4c78e6c97ea26836f4b021157b34f1812a9e5c2341502aad8cde942b18576', // optipng-sol 0.7.7 EWWW 4.1.0.
'6a321e07eca8e28fa8a969b5db3c1d3cc008a2064d636cf74762bbe4364b7b14', // optipng.exe 0.7.7 EWWW 4.1.0.
// end optipng.
'a2292c0085863a65c99cb41ff8418ce63033e162906df72e8fdde52f0633579b', // gifsicle linux 1.67, EWWW 1.2.0.
'd7f9609b6fd0000b2eaad2bd0c3cb85476988b18705762e915bda3f2e6007801', // gifsicle-linux 1.68, EWWW 1.3.0.
'204a839a50367adb8cd23fae5d1913a5ca8b41307f054156ed152748d3e7934d', // gifsicle-linux 1.68, EWWW 1.3.7.
'23e208099fa7ce75a3f98144190d6362d69b90c6f0a534ffa45dbbf789f7d99c', // gifsicle-mac 1.68, EWWW 1.3.0.
'8b08243a7cc655512a03403f6c3814176e28bbd140df7c059bd321a9a0151c18', // gifsicle-fbsd 1.70, EWWW 1.4.0.
'fd074673967ee9d387208f047c081a6331663b4076f4a6a608d6f646622af718', // gifsicle-linux 1.70, EWWW 1.4.0 - 1.7.4.
'bc32a390e86d2d8f40e970b2dc059015b51afe26794d92a936c1fe7216db805d', // gifsicle-mac 1.70, EWWW 1.4.0.
'41e67a35cd178f781b5224d196185e4243e6c2b3bece43277130fe07cdda402f', // gifsicle-sol 1.70, EWWW 1.7.4.
'3c6d9fabd1ea1014b8f58063dd00a653980c06bc1b45e96a47d866247263a1e1', // gifsicle.exe 1.70, EWWW 1.4.0.
'decba7a95b637bee53847af680fd37bde8bd568528412c514b7bd794056fd4ff', // gifsicle-fbsd 1.78, EWWW 1.7.5.
'c28e5e4b5344f77f415973d013e4cb393fc550e8de44117b090d534e98b30d1c', // gifsicle-linux 1.78, EWWW 1.7.5 - 1.9.3.
'fc2de863e8579b0d540003300e918cee450bc8e026018c631dffc0ed851a8c1c', // gifsicle-mac 1.78, EWWW 1.7.5.
'74d011ee1b6d9fe6d5d8bdb4cd17db0c5987fa6e3d495b42439cd70b0763c07a', // gifsicle-sol 1.78, EWWW 1.7.5.
'7c10da38f4afb28373779d40a30710aa9fb369e82f7f29363554bea965d132df', // gifsicle.exe 1.78, EWWW 1.7.5.
'e75acedd0725fba64ee72855b796cdfa8dac9959d63e89a9e0e5ba059ae013c2', // gifsicle-fbsd 1.84, EWWW 2.0.0.
'a4f0f21bc4bea51f5d304fe944262c12f671d70a3e5f688061da7bb036e84ff8', // gifsicle-linux 1.84, EWWW 2.0.0 - 2.4.3.
'5f4176b3fe69f975563d2ce7e76615ab558f5f1839b9bfa6f6de1b3c3fa11c02', // gifsicle-mac 1.84, EWWW 2.0.0.
'9f0027bed22d4be60012488ab726c3a131d9f3e1e276e9400c578173347a9a48', // gifsicle-sol 1.84, EWWW 2.0.0.
'72f0077e8591292d09efee09a181458b34fb3c0e9a6ac7e8e11cec574bf619ac', // gifsicle.exe 1.84, EWWW 2.0.0.
'c64936b429e46b6a75339df00eb8daa39d335844c906fa16d4d0af481851e91e', // gifsicle-fbsd 1.87, EWWW 2.4.4.
'deea065a91c8429edecf42ccef78636065f7ae0dad867df7696128c6711e4735', // gifsicle-linux 1.87, EWWW 2.4.4.
'2e0d8b7413173555bbec6e019c3cd7c55f7d582a017a0af7b14cfd24a6921f51', // gifsicle-mac 1.87, EWWW 2.4.4.
'3966e01474601059c6a13aefbe4f313c6cb6d49c799f7850966950892a9ab45a', // gifsicle-sol 1.87, EWWW 2.4.4.
'40b86b2ea6642f4c921152923af1e631922b624f7d23189f53c659506c7179b5', // gifsicle.exe 1.87, EWWW 2.4.4.
'3da9e1a764a459d78dc1468ba60d882ff042050a86f82d895777b172b50f2f19', // gifsicle.exe 1.87, EWWW 2.4.5.
'327c21635ea8c789e3e9533210e6baf372db27c7bbed3791881d74a7dd41cef9', // gifsicle-fbsd 1.91, EWWW 4.1.0.
'566f058b2043c4f3c8c049b0507bfa78dcb33dac52b132cade5f67bbb62d91e4', // gifsicle-linux 1.91, EWWW 4.1.0.
'03602b141432af2211882fc079ba15a773a7ec782c92755cb31279eb6d8b99d4', // gifsicle-mac 1.91, EWWW 4.1.0.
'5fcdd102146984e41b01a160d072dd36852d7be14ab569a323c47e7e56916d0d', // gifsicle-sol 1.91, EWWW 4.x.
'7156bfe16dc5e33af7facdc6847d268154ffeb75c0217517e4e188b58b293c6a', // gifsicle.exe 1.91, EWWW 4.1.0.
// end gifsicle.
'bdea95497d6e60aae8938cae8e999ef74a255ad603531bf523dcdb531f61fc8f', // 20110722-bsd/i686/pngout.
'57c09b3ebd7d4623d16f6056efd7951e8f98e2362a27993a7d865af677875c00', // 20110722-bsd-static/i686/pngout-static.
'17960599ca28a61aeb883a68b2eb52c513b730a410a0db75a7c2c22e0a3f925a', // 20110722-linux/i686/pngout.
'689f68bcbf39e68cdf0f0a350d59c0acafdbcf7ff122e25b5a8b58ed3a8f18ef', // 20110722-linux/x86_64/pngout.
'2028eea62f04b074b7693e5ce625c848ff6521206782616c893ca93637644a51', // 20110722-linux-static/i686/pngout-static.
'7d071c3a6ac9c4e8077f029dbba1cde49008d38adf897401e951f9c2e7ce8bb1', // 20110722-linux-static/x86_64/pngout-static.