-
Notifications
You must be signed in to change notification settings - Fork 26
/
common.php
15467 lines (14969 loc) · 686 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 plugin.
*
* This file contains functions that are shared by both EWWW IO plugin(s), back when we had a
* Cloud version. Functions that differed between the two are stored in unique.php.
*
* @link https://ewww.io
* @package EWWW_Image_Optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
/*
* Hooks
*/
// Runs other checks that need to run on 'init'.
add_action( 'init', 'ewww_image_optimizer_init', 9 );
// Load our front-end parsers for ExactDN, Lazy Load and WebP.
add_action( 'init', 'ewww_image_optimizer_parser_init', 99 );
// Check the current screen ID to see if temp debugging should still be enabled.
add_action( 'current_screen', 'ewww_image_optimizer_current_screen', 10, 1 );
// If automatic optimization is NOT disabled.
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_noauto' ) ) {
if ( ! defined( 'EWWW_IMAGE_OPTIMIZER_DISABLE_EDITOR' ) || ! EWWW_IMAGE_OPTIMIZER_DISABLE_EDITOR ) {
// Turns off the ewwwio_image_editor during uploads.
add_action( 'add_attachment', 'ewww_image_optimizer_add_attachment' );
// Turn off the editor when scaling down the original (core WP 5.3+).
add_filter( 'big_image_size_threshold', 'ewww_image_optimizer_image_sizes' );
// Turns off ewwwio_image_editor during Enable Media Replace.
add_filter( 'emr_unfiltered_get_attached_file', 'ewww_image_optimizer_image_sizes' );
// Checks to see if thumb regen or other similar operation is running via REST API.
add_action( 'rest_api_init', 'ewww_image_optimizer_restapi_compat_check' );
// Detect WP/LR Sync when it starts.
add_action( 'wplr_presync_media', 'ewww_image_optimizer_image_sizes' );
// Enables direct integration to the editor's save function.
add_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
}
// Resizes and auto-rotates images.
add_filter( 'wp_handle_upload', 'ewww_image_optimizer_handle_upload' );
// Processes an image via the metadata after upload.
add_filter( 'wp_generate_attachment_metadata', 'ewww_image_optimizer_resize_from_meta_data', 15, 2 );
// Add hook for PTE confirmation to make sure new resizes are optimized.
add_filter( 'wp_get_attachment_metadata', 'ewww_image_optimizer_pte_check' );
// Resizes and auto-rotates MediaPress images.
add_filter( 'mpp_handle_upload', 'ewww_image_optimizer_handle_mpp_upload' );
// Processes a MediaPress image via the metadata after upload.
add_filter( 'mpp_generate_metadata', 'ewww_image_optimizer_resize_from_meta_data', 15, 2 );
// Processes an attachment after IRSC has done a thumb regen.
add_filter( 'sirsc_attachment_images_ready', 'ewww_image_optimizer_resize_from_meta_data', 15, 2 );
// Processes an attachment after Crop Thumbnails plugin has modified the images.
add_filter( 'crop_thumbnails_before_update_metadata', 'ewww_image_optimizer_resize_from_meta_data', 15, 2 );
// Process BuddyPress uploads from Vikinger theme.
add_action( 'vikinger_file_uploaded', 'ewww_image_optimizer' );
// Process image after resize by Imsanity.
add_action( 'imsanity_post_process_attachment', 'ewww_image_optimizer_optimize_by_id', 10, 2 );
}
// Ensures we update the filesize data in the meta.
// add_filter( 'wp_update_attachment_metadata', 'ewww_image_optimizer_update_filesize_metadata', 9, 2 );
// Skips resizing for images with 'noresize' in the filename.
add_filter( 'ewww_image_optimizer_resize_dimensions', 'ewww_image_optimizer_noresize', 10, 2 );
// Makes sure the optimizer never optimizes it's own testing images.
add_filter( 'ewww_image_optimizer_bypass', 'ewww_image_optimizer_ignore_self', 10, 2 );
// Adds a column to the media library list view to display optimization results.
add_filter( 'manage_media_columns', 'ewww_image_optimizer_columns' );
// Outputs the actual column information for each attachment.
add_action( 'manage_media_custom_column', 'ewww_image_optimizer_custom_column', 10, 2 );
// Filters to set default permissions, admins 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 );
// Add a link to the plugins page so the user can go straight to the settings page.
add_filter( 'plugin_action_links_' . EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE_REL, 'ewww_image_optimizer_settings_link' );
// Filter the registered sizes so we can remove any that the user disabled.
add_filter( 'intermediate_image_sizes_advanced', 'ewww_image_optimizer_image_sizes_advanced' );
// Ditto for PDF files (or anything non-image).
add_filter( 'fallback_intermediate_image_sizes', 'ewww_image_optimizer_fallback_sizes' );
// Disable core WebP generation since we already do that.
add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
// Processes screenshots imported with MyArcadePlugin.
add_filter( 'myarcade_filter_screenshot', 'ewww_image_optimizer_myarcade_thumbnail' );
// Processes thumbnails created by MyArcadePlugin.
add_filter( 'myarcade_filter_thumbnail', 'ewww_image_optimizer_myarcade_thumbnail' );
// 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' );
// Allows the user to override the default JPG quality used by WordPress.
add_filter( 'jpeg_quality', 'ewww_image_optimizer_set_jpg_quality', PHP_INT_MAX - 1 );
// Allows the user to override the default WebP quality used by EWWW IO.
add_filter( 'webp_quality', 'ewww_image_optimizer_set_webp_quality' );
// Allows the user to override the default AVIF quality used by EWWW IO.
add_filter( 'avif_quality', 'ewww_image_optimizer_set_avif_quality' );
// Prevent WP from over-riding EWWW IO's resize settings.
add_filter( 'big_image_size_threshold', 'ewww_image_optimizer_adjust_big_image_threshold', 10, 3 );
// Makes sure the plugin bypasses any files affected by the Folders to Ignore setting.
add_filter( 'ewww_image_optimizer_bypass', 'ewww_image_optimizer_ignore_file', 10, 2 );
// Makes sure the plugin bypasses any WebP files generated by WebP Conversion.
add_filter( 'ewww_image_optimizer_bypass', 'ewww_image_optimizer_ignore_converted_webp_images', 10, 2 );
// Ensure we populate the queue with webp images for WP Offload S3.
add_filter( 'as3cf_attachment_file_paths', 'ewww_image_optimizer_as3cf_attachment_file_paths', 10, 2 );
// Make sure to remove webp images from remote storage when an attachment is deleted.
add_filter( 'as3cf_remove_source_files_from_provider', 'ewww_image_optimizer_as3cf_remove_source_files' );
// Fix the ContentType for WP Offload S3 on WebP images.
add_filter( 'as3cf_object_meta', 'ewww_image_optimizer_as3cf_object_meta' );
// Built-in check for whitelabel constant.
add_filter( 'ewwwio_whitelabel', 'ewwwio_is_whitelabel' );
// Get admin color scheme and save it for later.
add_action( 'admin_head', 'ewww_image_optimizer_save_admin_colors' );
// Legacy (non-AJAX) action hook for manually optimizing an image.
add_action( 'admin_action_ewww_image_optimizer_manual_optimize', 'ewww_image_optimizer_manual' );
// Legacy (non-AJAX) action hook for manually restoring a converted image.
add_action( 'admin_action_ewww_image_optimizer_manual_restore', 'ewww_image_optimizer_manual' );
// Legacy (non-AJAX) action hook for manually restoring a backup from the API.
add_action( 'admin_action_ewww_image_optimizer_manual_image_restore', 'ewww_image_optimizer_manual' );
// Cleanup routine when an attachment is deleted.
add_action( 'delete_attachment', 'ewww_image_optimizer_delete', 21 );
// Cleanup db records when Enable Media Replace replaces a file.
add_action( 'wp_handle_replace', 'ewww_image_optimizer_media_replace' );
// Cleanup db records when Phoenix Media Rename is finished.
add_action( 'pmr_renaming_successful', 'ewww_image_optimizer_media_rename', 10, 2 );
// Cleanup db records when Image Regenerate & Select Crop deletes a file.
add_action( 'sirsc_image_file_deleted', 'ewww_image_optimizer_irsc_file_deleted', 10, 2 );
// Cleanup db records when Force Regenerate Thumbnails deletes a file.
add_action( 'regenerate_thumbs_post_delete', 'ewww_image_optimizer_file_deleted' );
// Adds the EWWW IO pages to the admin menu.
add_action( 'admin_menu', 'ewww_image_optimizer_admin_menu', 60 );
// Adds the EWWW IO settings to the network admin menu.
add_action( 'network_admin_menu', 'ewww_image_optimizer_network_admin_menu' );
// Handle the bulk actions from the media library.
add_filter( 'handle_bulk_actions-upload', 'ewww_image_optimizer_bulk_action_handler', 10, 3 );
// Enables scheduled optimization via wp-cron.
add_action( 'ewww_image_optimizer_auto', 'ewww_image_optimizer_auto' );
// Correct records for files renamed by MFR.
add_action( 'mfrh_path_renamed', 'ewww_image_optimizer_path_renamed', 10, 3 );
// Correct any records in the table created during retina generation.
add_action( 'wr2x_retina_file_added', 'ewww_image_optimizer_retina', 20, 2 );
// Hook onto creation of new sites in multi-site for Easy IO registration.
add_action( 'wp_initialize_site', 'ewww_image_optimizer_initialize_site', 101, 1 );
// AJAX action hook to verify an API key.
add_action( 'wp_ajax_ewww_cloud_key_verify', 'ewww_image_optimizer_cloud_key_verify_ajax' );
// AJAX action hook to activate Easy IO.
add_action( 'wp_ajax_ewww_exactdn_activate', 'ewww_image_optimizer_exactdn_activate_ajax' );
// AJAX action hook to activate Easy IO on a specific site.
add_action( 'wp_ajax_ewww_exactdn_activate_site', 'ewww_image_optimizer_exactdn_activate_site_ajax' );
// AJAX action hook to register a specific site with Easy IO.
add_action( 'wp_ajax_ewww_exactdn_register_site', 'ewww_image_optimizer_exactdn_register_site_ajax' );
// AJAX action hook to remove a specific site from Easy IO.
add_action( 'wp_ajax_ewww_exactdn_deregister_site', 'ewww_image_optimizer_exactdn_deregister_site_ajax' );
// AJAX action hook for inserting WebP rewrite rules into .htaccess.
add_action( 'wp_ajax_ewww_webp_rewrite', 'ewww_image_optimizer_webp_rewrite' );
// AJAX action hook for removing WebP rewrite rules from .htaccess.
add_action( 'wp_ajax_ewww_webp_unwrite', 'ewww_image_optimizer_webp_unwrite' );
// AJAX action hook for manually optimizing/converting an image.
add_action( 'wp_ajax_ewww_manual_optimize', 'ewww_image_optimizer_manual' );
// AJAX action hook for manually restoring a converted image.
add_action( 'wp_ajax_ewww_manual_restore', 'ewww_image_optimizer_manual' );
// AJAX action hook for manually restoring an attachment from local/cloud backups.
add_action( 'wp_ajax_ewww_manual_image_restore', 'ewww_image_optimizer_manual' );
// AJAX action hook for fetching the optimization status of an image attachment.
add_action( 'wp_ajax_ewww_manual_get_status', 'ewww_image_optimizer_ajax_get_attachment_status' );
// AJAX action hook to dismiss the WooCommerce regen notice.
add_action( 'wp_ajax_ewww_dismiss_wc_regen', 'ewww_image_optimizer_dismiss_wc_regen' );
// AJAX action hook to dismiss the WP/LR Sync regen notice.
add_action( 'wp_ajax_ewww_dismiss_lr_sync', 'ewww_image_optimizer_dismiss_lr_sync' );
// AJAX action hook to disable the media library notice.
add_action( 'wp_ajax_ewww_dismiss_media_notice', 'ewww_image_optimizer_dismiss_media_notice' );
// AJAX action hook to disable the 'review request' notice.
add_action( 'wp_ajax_ewww_dismiss_review_notice', 'ewww_image_optimizer_dismiss_review_notice' );
// AJAX action hook to disable the newsletter signup banner.
add_action( 'wp_ajax_ewww_dismiss_newsletter', 'ewww_image_optimizer_dismiss_newsletter_signup' );
// Adds script to highlight mis-sized images on the front-end (for logged in admins only).
add_action( 'wp_head', 'ewww_image_optimizer_resize_detection_script' );
// Adds a button on the admin bar to allow highlighting mis-sized images on-demand.
add_action( 'admin_bar_init', 'ewww_image_optimizer_admin_bar_init' );
// Non-AJAX handler to delete the API key, and reroute back to the settings page.
add_action( 'admin_action_ewww_image_optimizer_remove_cloud_key', 'ewww_image_optimizer_remove_cloud_key' );
// Non-AJAX handler to disable Easy IO and reroute back to the settings page.
add_action( 'admin_action_ewww_image_optimizer_remove_easyio', 'ewww_image_optimizer_remove_easyio' );
// Non-AJAX handler to disable Easy IO network-wide.
add_action( 'admin_action_ewww_image_optimizer_network_remove_easyio', 'ewww_image_optimizer_network_remove_easyio' );
// Non-AJAX handler to enable Force WebP for GIF files.
add_action( 'admin_action_ewww_image_optimizer_enable_force_gif2webp', 'ewww_image_optimizer_enable_force_gif2webp' );
// Non-AJAX handler to retest async/background mode.
add_action( 'admin_action_ewww_image_optimizer_retest_background_optimization', 'ewww_image_optimizer_retest_background_optimization' );
// Non-AJAX handler to disable debugging mode.
add_action( 'admin_action_ewww_image_optimizer_disable_debugging', 'ewww_image_optimizer_disable_debugging' );
// Non-AJAX handler to view the debug log, and display it.
add_action( 'admin_action_ewww_image_optimizer_view_debug_log', 'ewww_image_optimizer_view_debug_log' );
// Non-AJAX handler to delete the debug log, and reroute back to the settings page.
add_action( 'admin_action_ewww_image_optimizer_delete_debug_log', 'ewww_image_optimizer_delete_debug_log' );
// Non-AJAX handler to download the debug log.
add_action( 'admin_action_ewww_image_optimizer_download_debug_log', 'ewww_image_optimizer_download_debug_log' );
// Non-AJAX handler to apply 6.2 current_timestamp db upgrade.
add_action( 'admin_action_ewww_image_optimizer_620_upgrade', 'ewww_image_optimizer_620_upgrade' );
// Check if WebP option was turned off and is now enabled.
add_action( 'update_option_ewww_image_optimizer_webp', 'ewww_image_optimizer_webp_maybe_enabled', 10, 2 );
// Check Scheduled Opt option has just been disabled and clear the queues/stop the process.
add_action( 'update_option_ewww_image_optimizer_auto', 'ewww_image_optimizer_scheduled_optimization_changed', 10, 2 );
// Check if image resize dimensions have been changed.
add_action( 'update_option_ewww_image_optimizer_maxmediawidth', 'ewww_image_optimizer_resize_dimensions_changed', 10, 2 );
add_action( 'update_option_ewww_image_optimizer_maxmediaheight', 'ewww_image_optimizer_resize_dimensions_changed', 10, 2 );
// Makes sure to flush out any scheduled jobs on deactivation.
register_deactivation_hook( EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE, 'ewww_image_optimizer_network_deactivate' );
// add_action( 'shutdown', 'ewwwio_memory_output' );.
// Makes sure we flush the debug info to the log on shutdown.
add_action( 'shutdown', 'ewww_image_optimizer_debug_log' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-ewwwio-cli.php';
}
/**
* Setup page parsing classes early, but after theme functions.php is loaded and plugins have loaded.
*/
function ewww_image_optimizer_parser_init() {
$buffer_start = false;
// If ExactDN is enabled.
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) && false === strpos( add_query_arg( '', '' ), 'exactdn_disable=1' ) ) {
if ( ! ewww_image_optimizer_get_option( 'exactdn_all_the_things' ) ) {
// Un-configure Autoptimize CDN domain.
if ( defined( 'AUTOPTIMIZE_PLUGIN_DIR' ) && strpos( ewww_image_optimizer_get_option( 'autoptimize_cdn_url' ), 'exactdn' ) ) {
ewww_image_optimizer_set_option( 'autoptimize_cdn_url', '' );
}
}
$buffer_start = true;
/**
* Page Parsing class for working with HTML content.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-page-parser.php';
/**
* ExactDN class for parsing image urls and rewriting them.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-exactdn.php';
} else {
// Un-configure Autoptimize CDN domain.
if ( defined( 'AUTOPTIMIZE_PLUGIN_DIR' ) && strpos( ewww_image_optimizer_get_option( 'autoptimize_cdn_url' ), 'exactdn' ) ) {
ewww_image_optimizer_set_option( 'autoptimize_cdn_url', '' );
}
}
// If Lazy Load is enabled.
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_lazy_load' ) ) {
$buffer_start = true;
/**
* Page Parsing class for working with HTML content.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-page-parser.php';
/**
* Lazy Load class for parsing image urls and rewriting them to defer off-screen images.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-lazy-load.php';
global $eio_lazy_load;
$eio_lazy_load = new EWWW\Lazy_Load();
}
// If JS WebP Rewriting is enabled.
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp_for_cdn' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
$buffer_start = true;
/**
* Page Parsing class for working with HTML content.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-page-parser.php';
/**
* JS WebP class for parsing image urls and rewriting them for WebP support.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-js-webp.php';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_picture_webp' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
$buffer_start = true;
/**
* Page Parsing class for working with HTML content.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-page-parser.php';
/**
* Picture WebP class for parsing img elements and rewriting them with WebP URLs.
*/
require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'classes/class-picture-webp.php';
}
if ( $buffer_start ) {
// Start an output buffer before any output starts.
add_action( 'template_redirect', 'ewww_image_optimizer_buffer_start', 0 );
if ( wp_doing_ajax() && apply_filters( 'eio_filter_admin_ajax_response', false ) ) {
add_action( 'admin_init', 'ewww_image_optimizer_buffer_start', 0 );
}
}
}
/**
* Starts an output buffer and registers the callback function to do WebP replacement.
*/
function ewww_image_optimizer_buffer_start() {
ob_start( 'ewww_image_optimizer_filter_page_output' );
}
/**
* Run the page through any registered EWWW IO filters.
*
* @param string $buffer The full HTML page generated since the output buffer was started.
* @return string The altered buffer containing the full page with WebP images inserted.
*/
function ewww_image_optimizer_filter_page_output( $buffer ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
return apply_filters( 'ewww_image_optimizer_filter_page_output', $buffer );
}
/**
* Skips optimization when a file is within EWWW IO's own folder.
*
* @param bool $skip Defaults to false.
* @param string $filename The name of the file about to be optimized.
* @return bool True if the file is within the EWWW IO folder, unaltered otherwise.
*/
function ewww_image_optimizer_ignore_self( $skip, $filename ) {
if ( 0 === strpos( $filename, EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH ) ) {
return true;
}
return $skip;
}
/**
* Skips optimization of WebP images that were created by EWWW IO (or possibly another IO plugin).
*
* @param bool $skip Defaults to false.
* @param string $filename The name of the file about to be optimized.
* @return bool True if the file is a WebP image EWWW IO created, unaltered otherwise.
*/
function ewww_image_optimizer_ignore_converted_webp_images( $skip, $filename ) {
if ( apply_filters( 'ewwwio_optimize_converted_webp_images', false ) ) {
return $skip;
}
if ( preg_match( '/\.(jpe?g|png|gif)\.webp$/i', $filename ) ) {
ewwwio_debug_message( "skipping converted WebP: $filename" );
return true;
}
if ( str_ends_with( $filename, '.webp' ) ) {
$extensionless = ewwwio()->remove_from_end( $filename, '.webp' );
$original_image_path = '';
$original_extensions = array(
'jpg',
'jpeg',
'png',
'gif',
);
foreach ( $original_extensions as $original_extension ) {
if ( is_file( $extensionless . '.' . $original_extension ) ) {
ewwwio_debug_message( "skipping converted WebP: $filename" );
return true;
}
if ( is_file( $extensionless . '.' . strtoupper( $original_extension ) ) ) {
ewwwio_debug_message( "skipping converted WebP: $filename" );
return true;
}
}
}
return $skip;
}
/**
* Gets the version number from a plugin file.
*
* @param string $plugin_file The path to the plugin's main file.
* @return array With a single index, 'Version'.
*/
function ewww_image_optimizer_get_plugin_version( $plugin_file ) {
$default_headers = array(
'Version' => 'Version',
);
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
return $plugin_data;
}
/**
* Checks to see if some other lazy loader is active.
*
* @return bool True if third-party lazy load detected, false otherwise.
*/
function ewwwio_other_lazy_detected() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( class_exists( 'RocketLazyLoadPlugin\Plugin' ) || defined( 'ROCKET_LL_VERSION' ) ) {
$rocketll_settings = get_option( 'rocket_lazyload_options' );
if ( is_array( $rocketll_settings ) && ! empty( $rocketll_settings['images'] ) ) {
ewwwio_debug_message( 'rocket lazy detected (standalone free plugin)' );
return true;
}
}
if ( class_exists( 'WP_Rocket\Plugin' ) || defined( 'WP_ROCKET_VERSION' ) ) {
$rocket_settings = get_option( 'wp_rocket_settings' );
if ( is_array( $rocket_settings ) && ! empty( $rocket_settings['lazyload'] ) ) {
ewwwio_debug_message( 'WP Rocket with lazy detected' );
return true;
}
}
if ( class_exists( 'autoptimizeExtra' ) || defined( 'AUTOPTIMIZE_PLUGIN_VERSION' ) ) {
$ao_extra = get_option( 'autoptimize_imgopt_settings' );
if ( ! empty( $ao_extra['autoptimize_imgopt_checkbox_field_3'] ) ) {
ewwwio_debug_message( 'Autoptimize lazy detected' );
return true;
}
}
if ( class_exists( 'SiteGround_Optimizer\Helper\Helper' ) && get_option( 'siteground_optimizer_lazyload_images' ) ) {
ewwwio_debug_message( 'SG Optimizer lazy detected' );
return true;
}
if ( class_exists( '\A3Rev\LazyLoad' ) || defined( 'A3_LAZY_VERSION' ) ) {
ewwwio_debug_message( 'A3 lazy detected' );
return true;
}
if ( class_exists( 'WpFastestCache' ) || defined( 'WPFC_WP_PLUGIN_DIR' ) ) {
if ( ! empty( $GLOBALS['wp_fastest_cache_options']->wpFastestCacheLazyLoad ) ) {
ewwwio_debug_message( 'WPFC lazy detected' );
return true;
}
}
if ( class_exists( '\W3TC\Dispatcher' ) || defined( 'W3TC_VERSION' ) ) {
if ( method_exists( '\W3TC\Dispatcher', 'config' ) ) {
$w3tc_config = \W3TC\Dispatcher::config();
if ( method_exists( $w3tc_config, 'get_boolean' ) && $w3tc_config->get_boolean( 'lazyload.enabled' ) ) {
ewwwio_debug_message( 'W3TC lazy detected' );
return true;
}
}
}
if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'lazy-images' ) ) {
ewwwio_debug_message( 'Jetpack lazy detected' );
return true;
}
if ( class_exists( '\Automattic\Jetpack_Boost\Jetpack_Boost' ) ) {
if ( \get_option( 'jetpack_boost_status_lazy-images' ) ) {
ewwwio_debug_message( 'Jetpack Boost lazy detected' );
return true;
}
}
return false;
}
/**
* Checks to see if the WebP option from the Cache Enabler plugin is enabled.
*
* @return bool True if the WebP option for CE is enabled.
*/
function ewww_image_optimizer_ce_webp_enabled() {
if ( class_exists( 'Cache_Enabler' ) ) {
$ce_options = get_option( 'cache_enabler', array() );
if ( ! empty( $ce_options['convert_image_urls_to_webp'] ) || ! empty( $ce_options['webp'] ) ) {
ewwwio_debug_message( 'Cache Enabler WebP option enabled' );
return true;
}
}
return false;
}
/**
* Checks to see if the WebP option from the SWIS Performance plugin is enabled.
*
* @return bool True if the WebP option for SWIS is enabled.
*/
function ewww_image_optimizer_swis_webp_enabled() {
if ( function_exists( 'swis' ) && class_exists( '\SWIS\Cache' ) ) {
$cache_settings = swis()->cache->get_settings();
if ( swis()->settings->get_option( 'cache' ) && ! empty( $cache_settings['webp'] ) ) {
ewwwio_debug_message( 'SWIS WebP option enabled' );
return true;
}
}
return false;
}
/**
* Checks to see if there is a method available for WebP conversion.
*
* @return bool True if a WebP Convertor is available.
*/
function ewww_image_optimizer_webp_available() {
return true; // Because API mode is the fallback and is free for everyone.
if (
! ewwwio()->local->exec_check() &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) &&
! ewwwio()->imagick_supports_webp() &&
! ewwwio()->gd_supports_webp()
) {
return false;
}
return true;
}
/**
* Checks to see if the WebP rules from WPFC are enabled.
*
* @return bool True if the WebP rules from WPFC are found.
*/
function ewww_image_optimizer_wpfc_webp_enabled() {
if ( class_exists( 'WpFastestCache' ) ) {
$wpfc_abspath = get_home_path() . '.htaccess';
ewwwio_debug_message( "looking for WPFC rules in $wpfc_abspath" );
$wpfc_rules = ewwwio_extract_from_markers( $wpfc_abspath, 'WEBPWpFastestCache' );
if ( empty( $wpfc_rules ) ) {
$wpfc_abspath = ABSPATH . '.htaccess';
ewwwio_debug_message( "looking for WPFC rules in $wpfc_abspath" );
$wpfc_rules = ewwwio_extract_from_markers( $wpfc_abspath, 'WEBPWpFastestCache' );
}
if ( ! empty( $wpfc_rules ) ) {
ewwwio_debug_message( 'WPFC webp rules enabled' );
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
ewwwio_debug_message( 'removing htaccess webp to prevent ExactDN problems' );
insert_with_markers( $wpfc_abspath, 'WEBPWpFastestCache', '' );
return false;
}
return true;
}
}
return false;
}
/**
* Set default permissions for manual operations (single image optimize, convert, restore).
*
* @param string $permissions A valid WP capability level.
* @return string Either the original value, unchanged, or the default capability level.
*/
function ewww_image_optimizer_manual_permissions( $permissions ) {
if ( empty( $permissions ) ) {
return 'edit_others_posts';
}
return $permissions;
}
/**
* Set default permissions for admin (configuration) and bulk operations.
*
* @param string $permissions A valid WP capability level.
* @return string Either the original value, unchanged, or the default capability level.
*/
function ewww_image_optimizer_admin_permissions( $permissions ) {
if ( empty( $permissions ) ) {
return 'activate_plugins';
}
return $permissions;
}
/**
* Set default permissions for multisite/network admin (configuration) operations.
*
* @param string $permissions A valid WP capability level.
* @return string Either the original value, unchanged, or the default capability level.
*/
function ewww_image_optimizer_superadmin_permissions( $permissions ) {
if ( empty( $permissions ) ) {
return 'manage_network_options';
}
return $permissions;
}
if ( ! function_exists( 'wp_getimagesize' ) ) {
/**
* Stub for WP prior to 5.7.
*
* @param string $filename The file path.
* @return array|false Array of image information or false on failure.
*/
function wp_getimagesize( $filename ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors
return @getimagesize( $filename );
}
}
if ( ! function_exists( 'str_ends_with' ) ) {
/**
* Polyfill for `str_ends_with()` function added in WP 5.9 or PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack ends with needle.
*
* @since 6.8.1
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` ends with `$needle`, otherwise false.
*/
function str_ends_with( $haystack, $needle ) {
if ( '' === $haystack && '' !== $needle ) {
return false;
}
$len = strlen( $needle );
return 0 === substr_compare( $haystack, $needle, -$len, $len );
}
}
/**
* Find out if set_time_limit() is allowed.
*/
function ewww_image_optimizer_stl_check() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( defined( 'EWWW_IMAGE_OPTIMIZER_DISABLE_STL' ) && EWWW_IMAGE_OPTIMIZER_DISABLE_STL ) {
ewwwio_debug_message( 'stl disabled by user' );
return false;
}
if ( function_exists( 'wp_is_ini_value_changeable' ) && ! wp_is_ini_value_changeable( 'max_execution_time' ) ) {
ewwwio_debug_message( 'max_execution_time not configurable' );
return false;
}
return ewwwio()->function_exists( '\set_time_limit' );
}
/**
* Save the multi-site settings, if this is the WP admin, and they've been POSTed.
*/
function ewww_image_optimizer_save_network_settings() {
if ( ! is_admin() ) {
return;
}
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 ) ) {
ewwwio_debug_message( 'saving network settings' );
// Set the common network settings if they have been POSTed.
if (
! empty( $_REQUEST['_wpnonce'] ) &&
wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' ) &&
isset( $_POST['option_page'] ) &&
false !== strpos( sanitize_text_field( wp_unslash( $_POST['option_page'] ) ), 'ewww_image_optimizer_options' ) &&
current_user_can( 'manage_network_options' ) &&
! get_site_option( 'ewww_image_optimizer_allow_multisite_override' ) &&
false === strpos( wp_get_referer(), 'options-general' )
) {
ewwwio_debug_message( 'network-wide settings, no override' );
$ewww_image_optimizer_debug = ( empty( $_POST['ewww_image_optimizer_debug'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_debug', $ewww_image_optimizer_debug );
$ewww_image_optimizer_metadata_remove = ( empty( $_POST['ewww_image_optimizer_metadata_remove'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_metadata_remove', $ewww_image_optimizer_metadata_remove );
$ewww_image_optimizer_jpg_level = empty( $_POST['ewww_image_optimizer_jpg_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_jpg_level'];
update_site_option( 'ewww_image_optimizer_jpg_level', $ewww_image_optimizer_jpg_level );
$ewww_image_optimizer_png_level = empty( $_POST['ewww_image_optimizer_png_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_png_level'];
update_site_option( 'ewww_image_optimizer_png_level', $ewww_image_optimizer_png_level );
$ewww_image_optimizer_gif_level = empty( $_POST['ewww_image_optimizer_gif_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_gif_level'];
update_site_option( 'ewww_image_optimizer_gif_level', $ewww_image_optimizer_gif_level );
$ewww_image_optimizer_pdf_level = empty( $_POST['ewww_image_optimizer_pdf_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_pdf_level'];
update_site_option( 'ewww_image_optimizer_pdf_level', $ewww_image_optimizer_pdf_level );
$ewww_image_optimizer_svg_level = empty( $_POST['ewww_image_optimizer_svg_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_svg_level'];
update_site_option( 'ewww_image_optimizer_svg_level', $ewww_image_optimizer_svg_level );
$ewww_image_optimizer_webp_level = empty( $_POST['ewww_image_optimizer_webp_level'] ) ? '' : (int) $_POST['ewww_image_optimizer_webp_level'];
update_site_option( 'ewww_image_optimizer_webp_level', $ewww_image_optimizer_webp_level );
$ewww_image_optimizer_delete_originals = ( empty( $_POST['ewww_image_optimizer_delete_originals'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_delete_originals', $ewww_image_optimizer_delete_originals );
$ewww_image_optimizer_jpg_to_png = ( empty( $_POST['ewww_image_optimizer_jpg_to_png'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_jpg_to_png', $ewww_image_optimizer_jpg_to_png );
$ewww_image_optimizer_png_to_jpg = ( empty( $_POST['ewww_image_optimizer_png_to_jpg'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_png_to_jpg', $ewww_image_optimizer_png_to_jpg );
$ewww_image_optimizer_gif_to_png = ( empty( $_POST['ewww_image_optimizer_gif_to_png'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_gif_to_png', $ewww_image_optimizer_gif_to_png );
$ewww_image_optimizer_bmp_convert = ( empty( $_POST['ewww_image_optimizer_bmp_convert'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_bmp_convert', $ewww_image_optimizer_bmp_convert );
$ewww_image_optimizer_webp = ( empty( $_POST['ewww_image_optimizer_webp'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_webp', $ewww_image_optimizer_webp );
$ewww_image_optimizer_jpg_background = empty( $_POST['ewww_image_optimizer_jpg_background'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['ewww_image_optimizer_jpg_background'] ) );
update_site_option( 'ewww_image_optimizer_jpg_background', ewww_image_optimizer_jpg_background( $ewww_image_optimizer_jpg_background ) );
$ewww_image_optimizer_sharpen = empty( $_POST['ewww_image_optimizer_sharpen'] ) ? false : true;
update_site_option( 'ewww_image_optimizer_sharpen', $ewww_image_optimizer_sharpen );
$ewww_image_optimizer_jpg_quality = empty( $_POST['ewww_image_optimizer_jpg_quality'] ) ? '' : (int) $_POST['ewww_image_optimizer_jpg_quality'];
update_site_option( 'ewww_image_optimizer_jpg_quality', ewww_image_optimizer_jpg_quality( $ewww_image_optimizer_jpg_quality ) );
$ewww_image_optimizer_webp_quality = empty( $_POST['ewww_image_optimizer_webp_quality'] ) ? '' : (int) $_POST['ewww_image_optimizer_webp_quality'];
update_site_option( 'ewww_image_optimizer_webp_quality', ewww_image_optimizer_webp_quality( $ewww_image_optimizer_webp_quality ) );
$ewww_image_optimizer_avif_quality = empty( $_POST['ewww_image_optimizer_avif_quality'] ) ? '' : (int) $_POST['ewww_image_optimizer_avif_quality'];
update_site_option( 'ewww_image_optimizer_avif_quality', ewww_image_optimizer_avif_quality( $ewww_image_optimizer_avif_quality ) );
$ewww_image_optimizer_disable_convert_links = ( empty( $_POST['ewww_image_optimizer_disable_convert_links'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_disable_convert_links', $ewww_image_optimizer_disable_convert_links );
$ewww_image_optimizer_backup_files = ( empty( $_POST['ewww_image_optimizer_backup_files'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['ewww_image_optimizer_backup_files'] ) ) );
update_site_option( 'ewww_image_optimizer_backup_files', $ewww_image_optimizer_backup_files );
$ewww_image_optimizer_auto = ( empty( $_POST['ewww_image_optimizer_auto'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_auto', $ewww_image_optimizer_auto );
$ewww_image_optimizer_aux_paths = empty( $_POST['ewww_image_optimizer_aux_paths'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['ewww_image_optimizer_aux_paths'] ) );
update_site_option( 'ewww_image_optimizer_aux_paths', ewww_image_optimizer_aux_paths_sanitize( $ewww_image_optimizer_aux_paths ) );
$ewww_image_optimizer_exclude_paths = empty( $_POST['ewww_image_optimizer_exclude_paths'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['ewww_image_optimizer_exclude_paths'] ) );
update_site_option( 'ewww_image_optimizer_exclude_paths', ewww_image_optimizer_exclude_paths_sanitize( $ewww_image_optimizer_exclude_paths ) );
$exactdn_all_the_things = ( empty( $_POST['exactdn_all_the_things'] ) ? false : true );
update_site_option( 'exactdn_all_the_things', $exactdn_all_the_things );
$exactdn_lossy = ( empty( $_POST['exactdn_lossy'] ) ? false : true );
update_site_option( 'exactdn_lossy', $exactdn_lossy );
$exactdn_hidpi = ( empty( $_POST['exactdn_hidpi'] ) ? false : true );
update_site_option( 'exactdn_hidpi', $exactdn_hidpi );
$exactdn_exclude = empty( $_POST['exactdn_exclude'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['exactdn_exclude'] ) );
update_site_option( 'exactdn_exclude', ewww_image_optimizer_exclude_paths_sanitize( $exactdn_exclude ) );
$ewww_image_optimizer_add_missing_dims = ( empty( $_POST['ewww_image_optimizer_add_missing_dims'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_add_missing_dims', $ewww_image_optimizer_add_missing_dims );
$ewww_image_optimizer_lazy_load = ( empty( $_POST['ewww_image_optimizer_lazy_load'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_lazy_load', $ewww_image_optimizer_lazy_load );
$ewww_image_optimizer_ll_autoscale = ( empty( $_POST['ewww_image_optimizer_ll_autoscale'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_ll_autoscale', $ewww_image_optimizer_ll_autoscale );
$ewww_image_optimizer_ll_abovethefold = ( empty( $_POST['ewww_image_optimizer_ll_abovethefold'] ) ? 0 : (int) $_POST['ewww_image_optimizer_ll_abovethefold'] );
update_site_option( 'ewww_image_optimizer_ll_abovethefold', $ewww_image_optimizer_ll_abovethefold );
$ewww_image_optimizer_use_lqip = ( empty( $_POST['ewww_image_optimizer_use_lqip'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_use_lqip', $ewww_image_optimizer_use_lqip );
$ewww_image_optimizer_use_dcip = ( empty( $_POST['ewww_image_optimizer_use_dcip'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_use_dcip', $ewww_image_optimizer_use_dcip );
// Using sanitize_text_field instead of textarea on purpose.
$ewww_image_optimizer_ll_all_things = empty( $_POST['ewww_image_optimizer_ll_all_things'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['ewww_image_optimizer_ll_all_things'] ) );
update_site_option( 'ewww_image_optimizer_ll_all_things', $ewww_image_optimizer_ll_all_things );
$ewww_image_optimizer_ll_exclude = empty( $_POST['ewww_image_optimizer_ll_exclude'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['ewww_image_optimizer_ll_exclude'] ) );
update_site_option( 'ewww_image_optimizer_ll_exclude', ewww_image_optimizer_exclude_paths_sanitize( $ewww_image_optimizer_ll_exclude ) );
$ewww_image_optimizer_maxmediawidth = empty( $_POST['ewww_image_optimizer_maxmediawidth'] ) ? 0 : (int) $_POST['ewww_image_optimizer_maxmediawidth'];
update_site_option( 'ewww_image_optimizer_maxmediawidth', $ewww_image_optimizer_maxmediawidth );
$ewww_image_optimizer_maxmediaheight = empty( $_POST['ewww_image_optimizer_maxmediaheight'] ) ? 0 : (int) $_POST['ewww_image_optimizer_maxmediaheight'];
update_site_option( 'ewww_image_optimizer_maxmediaheight', $ewww_image_optimizer_maxmediaheight );
$ewww_image_optimizer_resize_detection = ( empty( $_POST['ewww_image_optimizer_resize_detection'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_resize_detection', $ewww_image_optimizer_resize_detection );
$ewww_image_optimizer_resize_existing = ( empty( $_POST['ewww_image_optimizer_resize_existing'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_resize_existing', $ewww_image_optimizer_resize_existing );
$ewww_image_optimizer_resize_other_existing = ( empty( $_POST['ewww_image_optimizer_resize_other_existing'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_resize_other_existing', $ewww_image_optimizer_resize_other_existing );
$ewww_image_optimizer_include_media_paths = ( empty( $_POST['ewww_image_optimizer_include_media_paths'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_include_media_paths', $ewww_image_optimizer_include_media_paths );
$ewww_image_optimizer_include_originals = ( empty( $_POST['ewww_image_optimizer_include_originals'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_include_originals', $ewww_image_optimizer_include_originals );
$ewww_image_optimizer_webp_for_cdn = ( empty( $_POST['ewww_image_optimizer_webp_for_cdn'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_webp_for_cdn', $ewww_image_optimizer_webp_for_cdn );
$ewww_image_optimizer_picture_webp = ( empty( $_POST['ewww_image_optimizer_picture_webp'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_picture_webp', $ewww_image_optimizer_picture_webp );
$ewww_image_optimizer_webp_rewrite_exclude = empty( $_POST['ewww_image_optimizer_webp_rewrite_exclude'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['ewww_image_optimizer_webp_rewrite_exclude'] ) );
update_site_option( 'ewww_image_optimizer_webp_rewrite_exclude', ewww_image_optimizer_exclude_paths_sanitize( $ewww_image_optimizer_webp_rewrite_exclude ) );
$ewww_image_optimizer_webp_force = ( empty( $_POST['ewww_image_optimizer_webp_force'] ) ? false : true );
update_site_option( 'ewww_image_optimizer_webp_force', $ewww_image_optimizer_webp_force );
$ewww_image_optimizer_webp_paths = ( empty( $_POST['ewww_image_optimizer_webp_paths'] ) ? '' : sanitize_textarea_field( wp_unslash( $_POST['ewww_image_optimizer_webp_paths'] ) ) );
update_site_option( 'ewww_image_optimizer_webp_paths', ewww_image_optimizer_webp_paths_sanitize( $ewww_image_optimizer_webp_paths ) );
$ewww_image_optimizer_allow_multisite_override = empty( $_POST['ewww_image_optimizer_allow_multisite_override'] ) ? false : true;
update_site_option( 'ewww_image_optimizer_allow_multisite_override', $ewww_image_optimizer_allow_multisite_override );
$ewww_image_optimizer_enable_help = empty( $_POST['ewww_image_optimizer_enable_help'] ) ? false : true;
update_site_option( 'ewww_image_optimizer_enable_help', $ewww_image_optimizer_enable_help );
$ewww_image_optimizer_allow_tracking = empty( $_POST['ewww_image_optimizer_allow_tracking'] ) ? false : ewwwio()->tracking->check_for_settings_optin( (bool) $_POST['ewww_image_optimizer_allow_tracking'] );
update_site_option( 'ewww_image_optimizer_allow_tracking', $ewww_image_optimizer_allow_tracking );
add_action( 'network_admin_notices', 'ewww_image_optimizer_network_settings_saved' );
} elseif (
isset( $_POST['ewww_image_optimizer_allow_multisite_override_active'] ) &&
current_user_can( 'manage_network_options' ) &&
isset( $_REQUEST['_wpnonce'] ) &&
wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' )
) {
ewwwio_debug_message( 'network-wide settings, single-site overriding' );
$ewww_image_optimizer_allow_multisite_override = empty( $_POST['ewww_image_optimizer_allow_multisite_override'] ) ? false : true;
update_site_option( 'ewww_image_optimizer_allow_multisite_override', $ewww_image_optimizer_allow_multisite_override );
$ewww_image_optimizer_allow_tracking = empty( $_POST['ewww_image_optimizer_allow_tracking'] ) ? false : ewwwio()->tracking->check_for_settings_optin( (bool) $_POST['ewww_image_optimizer_allow_tracking'] );
update_site_option( 'ewww_image_optimizer_allow_tracking', $ewww_image_optimizer_allow_tracking );
add_action( 'network_admin_notices', 'ewww_image_optimizer_network_settings_saved' );
} // End if().
} // End if().
if ( is_multisite() && get_site_option( 'ewww_image_optimizer_allow_multisite_override' ) &&
! 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' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_svg_level' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp_level' )
) {
ewwwio()->set_defaults();
update_option( 'ewww_image_optimizer_disable_pngout', true );
update_option( 'ewww_image_optimizer_disable_svgcleaner', true );
update_option( 'ewww_image_optimizer_optipng_level', 2 );
update_option( 'ewww_image_optimizer_pngout_level', 2 );
update_option( 'ewww_image_optimizer_metadata_remove', true );
update_option( 'ewww_image_optimizer_jpg_level', '10' );
update_option( 'ewww_image_optimizer_png_level', '10' );
update_option( 'ewww_image_optimizer_gif_level', '10' );
update_option( 'ewww_image_optimizer_svg_level', 0 );
update_option( 'ewww_image_optimizer_webp_level', 0 );
}
}
/**
* Runs early for checks that need to happen on init before anything else.
*/
function ewww_image_optimizer_init() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// For the settings page, check for the enable-local param and take appropriate action.
if ( ! empty( $_GET['enable-local'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' ) ) {
update_option( 'ewww_image_optimizer_ludicrous_mode', true );
update_site_option( 'ewww_image_optimizer_ludicrous_mode', true );
} elseif ( isset( $_GET['enable-local'] ) && ! (bool) $_GET['enable-local'] && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' ) ) {
delete_option( 'ewww_image_optimizer_ludicrous_mode' );
delete_site_option( 'ewww_image_optimizer_ludicrous_mode' );
}
if ( ! empty( $_GET['complete_wizard'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' ) ) {
update_option( 'ewww_image_optimizer_wizard_complete', true, false );
}
if ( ! empty( $_GET['uncomplete_wizard'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ewww_image_optimizer_options-options' ) ) {
update_option( 'ewww_image_optimizer_wizard_complete', false, false );
}
if ( defined( 'CROP_THUMBNAILS_VERSION' ) ) {
add_filter( 'ewwwio_use_original_for_webp_thumbs', '__return_false', 9 ); // Early, so folks can turn it back on if they want for some reason.
}
if ( defined( 'DOING_WPLR_REQUEST' ) && DOING_WPLR_REQUEST ) {
// Unhook all automatic processing, and save an option that (does not autoload) tells the user LR Sync regenerated their images and they should run the bulk optimizer.
remove_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
remove_filter( 'wp_generate_attachment_metadata', 'ewww_image_optimizer_resize_from_meta_data', 15 );
add_action( 'wplr_add_media', 'ewww_image_optimizer_lr_sync_update' );
add_action( 'wplr_update_media', 'ewww_image_optimizer_lr_sync_update' );
add_filter( 'ewww_image_optimizer_allowed_reopt', '__return_true' );
}
}
/**
* Plugin upgrade function
*
* @global object $wpdb
*/
function ewww_image_optimizer_upgrade() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
global $ewwwio_upgrading;
$ewwwio_upgrading = false;
if ( get_option( 'ewww_image_optimizer_version' ) < EWWW_IMAGE_OPTIMIZER_VERSION ) {
if ( wp_doing_ajax() && ! empty( $_POST['ewwwio_test_verify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
}
$ewwwio_upgrading = true;
ewww_image_optimizer_install_table();
ewwwio()->set_defaults();
ewww_image_optimizer_enable_background_optimization();
// This will get re-enabled if things are too slow.
ewww_image_optimizer_set_option( 'exactdn_prevent_db_queries', false );
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn_verify_method' ) > 0 ) {
delete_option( 'ewww_image_optimizer_exactdn_verify_method' );
delete_site_option( 'ewww_image_optimizer_exactdn_verify_method' );
}
if ( get_option( 'ewww_image_optimizer_version' ) < 297.5 ) {
// Cleanup background test mess.
wp_clear_scheduled_hook( 'wp_ewwwio_test_optimize_cron' );
global $wpdb;
if ( is_multisite() ) {
$wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE 'wp_ewwwio_test_optimize_batch_%'" );
}
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'wp_ewwwio_test_optimize_batch_%'" );
}
if ( ! get_option( 'ewww_image_optimizer_version' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
add_option( 'exactdn_never_been_active', true, '', false );
}
if ( get_option( 'ewww_image_optimizer_version' ) < 280 ) {
ewww_image_optimizer_migrate_settings_to_levels();
}
if ( get_option( 'ewww_image_optimizer_version' ) > 0 && get_option( 'ewww_image_optimizer_version' ) < 434 && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpegtran_copy' ) ) {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_metadata_remove', false );
}
if ( get_option( 'ewww_image_optimizer_version' ) < 454 ) {
update_option( 'ewww_image_optimizer_bulk_resume', '' );
update_option( 'ewww_image_optimizer_aux_resume', '' );
ewww_image_optimizer_delete_pending();
// Make sure some of our options are not autoloaded (since they can be huge).
$bulk_attachments = get_option( 'ewww_image_optimizer_flag_attachments', '' );
delete_option( 'ewww_image_optimizer_flag_attachments' );
add_option( 'ewww_image_optimizer_flag_attachments', $bulk_attachments, '', 'no' );
$bulk_attachments = get_option( 'ewww_image_optimizer_ngg_attachments', '' );
delete_option( 'ewww_image_optimizer_ngg_attachments' );
add_option( 'ewww_image_optimizer_ngg_attachments', $bulk_attachments, '', 'no' );
}
if ( get_option( 'ewww_image_optimizer_version' ) < 530 ) {
ewww_image_optimizer_migrate_option_queue_to_table();
}
if ( get_option( 'ewww_image_optimizer_version' ) < 550 && ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp_force' ) ) {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_force_gif2webp', false );
} elseif ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_force_gif2webp' ) ) {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_force_gif2webp', true );
}
if (
get_option( 'ewww_image_optimizer_version' ) <= 601.0 &&
PHP_OS !== 'WINNT' &&
ewwwio_is_file( EWWW_IMAGE_OPTIMIZER_TOOL_PATH . '/pngout-static' ) &&
is_writable( EWWW_IMAGE_OPTIMIZER_TOOL_PATH . '/pngout-static' )
) {
ewwwio_debug_message( 'removing old version of pngout' );
ewwwio_delete_file( EWWW_IMAGE_OPTIMIZER_TOOL_PATH . '/pngout-static' );
}
if (
get_option( 'ewww_image_optimizer_version' ) < 661 &&
get_option( 'ewww_image_optimizer_exactdn' ) &&
! ewww_image_optimizer_get_option( 'ewww_image_optimizer_ludicrous_mode' ) &&
! ewww_image_optimizer_get_option( 'exactdn_lossy' )
) {
ewww_image_optimizer_set_option( 'exactdn_lossy', true );
}
if (
get_option( 'ewww_image_optimizer_version' ) <= 670 &&
ewww_image_optimizer_get_option( 'ewww_image_optimizer_backup_files' )
) {
$backup_mode = ewww_image_optimizer_get_option( 'ewww_image_optimizer_backup_files' );
if ( 'local' !== $backup_mode && 'cloud' !== $backup_mode ) {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_backup_files', 'cloud' );
}
}
if ( get_option( 'ewww_image_optimizer_local_mode' ) || get_site_option( 'ewww_image_optimizer_local_mode' ) ) {
update_option( 'ewww_image_optimizer_ludicrous_mode', true );
update_site_option( 'ewww_image_optimizer_ludicrous_mode', true );
delete_option( 'ewww_image_optimizer_local_mode' );
delete_site_option( 'ewww_image_optimizer_local_mode' );
}
if ( get_option( 'ewww_image_optimizer_version' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_review_time' ) ) {
$review_time = rand( time(), time() + 51 * DAY_IN_SECONDS );
add_option( 'ewww_image_optimizer_review_time', $review_time, '', false );
add_site_option( 'ewww_image_optimizer_review_time', $review_time );
} elseif ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_review_time' ) ) {
$review_time = time() + 7 * DAY_IN_SECONDS;
add_option( 'ewww_image_optimizer_review_time', $review_time, '', false );
add_site_option( 'ewww_image_optimizer_review_time', $review_time );
}
if ( get_option( 'ewww_image_optimizer_version' ) && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_wizard_complete' ) ) {
add_option( 'ewww_image_optimizer_wizard_complete', true, '', false );
add_site_option( 'ewww_image_optimizer_wizard_complete', true );
}
// Nuke the (legacy) bulk queue during an upgrade, if nothing is running.
if ( ! get_option( 'ewww_image_optimizer_bulk_resume' ) && ! get_option( 'ewww_image_optimizer_aux_resume' ) ) {
ewww_image_optimizer_delete_queue_images();
}
if ( is_file( EWWWIO_CONTENT_DIR . 'debug.log' ) && is_writable( EWWWIO_CONTENT_DIR . 'debug.log' ) ) {
unlink( EWWWIO_CONTENT_DIR . 'debug.log' );
}
ewww_image_optimizer_remove_obsolete_settings();
update_option( 'ewww_image_optimizer_version', EWWW_IMAGE_OPTIMIZER_VERSION );
}
ewwwio_memory( __FUNCTION__ );
}
/**
* Tests background optimization.
*
* Send a known packet to admin-ajax.php via the EWWW\Async_Test_Request class.
*/
function ewww_image_optimizer_enable_background_optimization() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
if ( ewww_image_optimizer_detect_wpsf_location_lock() ) {
return;
}
ewww_image_optimizer_set_option( 'ewww_image_optimizer_background_optimization', false );
ewwwio_debug_message( 'running test async handler' );
ewwwio()->async_test_request->data( array( 'ewwwio_test_verify' => '949c34123cf2a4e4ce2f985135830df4a1b2adc24905f53d2fd3f5df5b162932' ) )->dispatch();
ewww_image_optimizer_debug_log();
}
/**
* Re-tests background optimization at a user's request.
*/
function ewww_image_optimizer_retest_background_optimization() {
check_admin_referer( 'ewww_image_optimizer_options-options' );
if ( ! current_user_can( apply_filters( 'ewww_image_optimizer_admin_permissions', '' ) ) ) {
wp_die( esc_html__( 'Access denied.', 'ewww-image-optimizer' ) );
}
ewww_image_optimizer_enable_background_optimization();
sleep( 10 );
$sendback = wp_get_referer();
wp_safe_redirect( $sendback );
exit;
}
/**
* Apply 6.2.0+ current_timestamp db upgrade.
*/
function ewww_image_optimizer_620_upgrade() {
check_admin_referer( 'ewww_image_optimizer_options-options' );
if ( ! current_user_can( apply_filters( 'ewww_image_optimizer_admin_permissions', '' ) ) ) {
wp_die( esc_html__( 'Access denied.', 'ewww-image-optimizer' ) );
}
delete_transient( 'ewww_image_optimizer_620_upgrade_needed' );
global $wpdb;
$suppress = $wpdb->suppress_errors();
$wpdb->query( "ALTER TABLE $wpdb->ewwwio_images MODIFY updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" );
$wpdb->suppress_errors( $suppress );
wp_safe_redirect( wp_get_referer() );
exit;
}
/**
* Setup wp_cron tasks for scheduled optimization.
*
* @global object $wpdb
*
* @param string $event Name of cron hook to schedule.
*/
function ewww_image_optimizer_cron_setup( $event ) {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
// Setup scheduled optimization if the user has enabled it, and it isn't already scheduled.
if ( ewww_image_optimizer_get_option( $event ) && ! wp_next_scheduled( $event ) ) {
ewwwio_debug_message( "scheduling $event" );
wp_schedule_event( time(), apply_filters( 'ewww_image_optimizer_schedule', 'hourly', $event ), $event );
} elseif ( ewww_image_optimizer_get_option( $event ) ) {
ewwwio_debug_message( "$event already scheduled: " . wp_next_scheduled( $event ) );
} elseif ( wp_next_scheduled( $event ) ) {
ewwwio_debug_message( "un-scheduling $event" );