/
autoptimizeImages.php
1314 lines (1144 loc) · 58.3 KB
/
autoptimizeImages.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
/**
* Handles optimizing images.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class autoptimizeImages
{
/**
* Options.
*
* @var array
*/
protected $options = array();
/**
* Singleton instance.
*
* @var self|null
*/
protected static $instance = null;
public function __construct( array $options = array() )
{
// If options are not provided, fetch them.
if ( empty( $options ) ) {
$options = $this->fetch_options();
}
$this->set_options( $options );
}
public function set_options( array $options )
{
$this->options = $options;
return $this;
}
public static function fetch_options()
{
$value = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings' );
if ( empty( $value ) ) {
// Fallback to returning defaults when no stored option exists yet.
$value = autoptimizeConfig::get_ao_imgopt_default_options();
}
// get service availability and add it to the options-array.
$value['availabilities'] = autoptimizeOptionWrapper::get_option( 'autoptimize_service_availablity' );
if ( empty( $value['availabilities'] ) ) {
$value['availabilities'] = autoptimizeUtils::check_service_availability( true );
}
return $value;
}
public static function imgopt_active()
{
// function to quickly check if imgopt is active, used below but also in
// autoptimizeMain.php to start ob_ even if no HTML, JS or CSS optimizing is done
// and does not use/ request the availablity data (which could slow things down).
static $imgopt_active = null;
if ( null === $imgopt_active ) {
$opts = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings', '' );
if ( ! empty( $opts ) && is_array( $opts ) && array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $opts ) && ! empty( $opts['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $opts['autoptimize_imgopt_checkbox_field_1'] ) {
$imgopt_active = true;
} else {
$imgopt_active = false;
}
}
return $imgopt_active;
}
/**
* Helper for getting a singleton instance. While being an
* anti-pattern generally, it comes in handy for now from a
* readability/maintainability perspective, until we get some
* proper dependency injection going.
*
* @return self
*/
public static function instance()
{
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function run()
{
if ( is_admin() ) {
if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) {
add_action( 'network_admin_menu', array( $this, 'imgopt_admin_menu' ) );
} else {
add_action( 'admin_menu', array( $this, 'imgopt_admin_menu' ) );
}
add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_imgopt_tab' ), 9 );
} else {
add_action( 'wp', array( $this, 'run_on_frontend' ) );
}
}
public function run_on_frontend() {
if ( ! $this->should_run() ) {
if ( $this->should_lazyload() ) {
add_filter(
'wp_lazy_loading_enabled',
'__return_false'
);
add_filter(
'autoptimize_html_after_minify',
array( $this, 'filter_lazyload_images' ),
10,
1
);
add_action(
'wp_footer',
array( $this, 'add_lazyload_js_footer' ),
10,
0
);
}
return;
}
$active = false;
if ( apply_filters( 'autoptimize_filter_imgopt_do', true ) ) {
add_filter(
'autoptimize_html_after_minify',
array( $this, 'filter_optimize_images' ),
10,
1
);
$active = true;
}
if ( apply_filters( 'autoptimize_filter_imgopt_do_css', true ) ) {
add_filter(
'autoptimize_filter_base_replace_cdn',
array( $this, 'filter_optimize_css_images' ),
10,
1
);
$active = true;
}
if ( $active ) {
add_filter(
'autoptimize_extra_filter_tobepreconn',
array( $this, 'filter_preconnect_imgopt_url' ),
10,
1
);
}
if ( $this->should_lazyload() ) {
add_filter(
'wp_lazy_loading_enabled',
'__return_false'
);
add_action(
'wp_footer',
array( $this, 'add_lazyload_js_footer' ),
10,
0
);
}
}
/**
* Basic checks before we can run.
*
* @return bool
*/
protected function should_run()
{
$opts = $this->options;
$service_not_down = ( 'down' !== $opts['availabilities']['extra_imgopt']['status'] );
$not_launch_status = ( 'launch' !== $opts['availabilities']['extra_imgopt']['status'] );
$do_cdn = true;
$_userstatus = $this->get_imgopt_provider_userstatus();
if ( isset( $_userstatus['Status'] ) && ( -2 == $_userstatus['Status'] || -3 == $_userstatus['Status'] ) ) {
// don't even attempt to put images on CDN if heavily exceeded threshold or if site not reachable.
$do_cdn = false;
}
if (
$this->imgopt_active()
&& $do_cdn
&& $service_not_down
&& ( $not_launch_status || $this->launch_ok() )
) {
return true;
}
return false;
}
public function get_imgopt_host()
{
static $imgopt_host = null;
if ( null === $imgopt_host ) {
$imgopt_host = 'https://cdn.shortpixel.ai/';
$avail_imgopt = $this->options['availabilities']['extra_imgopt'];
if ( ! empty( $avail_imgopt ) && array_key_exists( 'hosts', $avail_imgopt ) && is_array( $avail_imgopt['hosts'] ) ) {
$imgopt_host = array_rand( array_flip( $avail_imgopt['hosts'] ) );
}
$imgopt_host = apply_filters( 'autoptimize_filter_imgopt_host', $imgopt_host );
}
return $imgopt_host;
}
public static function get_imgopt_host_wrapper()
{
// needed for CI tests.
$self = new self();
return $self->get_imgopt_host();
}
public static function get_service_url_suffix()
{
$suffix = '/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN;
return $suffix;
}
public function get_img_quality_string()
{
static $quality = null;
if ( null === $quality ) {
$q_array = $this->get_img_quality_array();
$setting = $this->get_img_quality_setting();
$quality = apply_filters(
'autoptimize_filter_imgopt_quality',
'q_' . $q_array[ $setting ]
);
}
return $quality;
}
public function get_img_quality_array()
{
static $map = null;
if ( null === $map ) {
$map = array(
'1' => 'lossy',
'2' => 'glossy',
'3' => 'lossless',
);
$map = apply_filters(
'autoptimize_filter_imgopt_quality_array',
$map
);
}
return $map;
}
public function get_img_quality_setting()
{
static $q = null;
if ( null === $q ) {
if ( is_array( $this->options ) && array_key_exists( 'autoptimize_imgopt_select_field_2', $this->options ) ) {
$setting = $this->options['autoptimize_imgopt_select_field_2'];
}
if ( ! isset( $setting ) || empty( $setting ) || ( '1' !== $setting && '3' !== $setting ) ) {
// default image opt. value is 2 ("glossy").
$q = '2';
} else {
$q = $setting;
}
}
return $q;
}
public function filter_preconnect_imgopt_url( array $in )
{
$url_parts = parse_url( $this->get_imgopt_base_url() );
$in[] = $url_parts['scheme'] . '://' . $url_parts['host'];
return $in;
}
/**
* Makes sure given url contains the full scheme and hostname
* in case they're not present already.
*
* @param string $in Image url to normalize.
*
* @return string
*/
private function normalize_img_url( $in )
{
// Only parse the site url once.
static $parsed_site_url = null;
if ( null === $parsed_site_url ) {
$parsed_site_url = parse_url( site_url() );
}
// get CDN domain once.
static $cdn_domain = null;
if ( is_null( $cdn_domain ) ) {
$cdn_url = $this->get_cdn_url();
if ( ! empty( $cdn_url ) ) {
$cdn_domain = parse_url( $cdn_url, PHP_URL_HOST );
} else {
$cdn_domain = '';
}
}
/**
* This method gets called a lot, often for identical urls it seems.
* `filter_optimize_css_images()` calls us, uses the resulting url and
* gives it to `can_optimize_image()`, and if that returns trueish
* then `build_imgopt_url()` is called (which, again, calls this method).
* Until we dig deeper into whether this all must really happen that
* way, having an internal cache here helps (to avoid doing repeated
* identical string operations).
*/
static $cache = null;
if ( null === $cache ) {
$cache = array();
}
// Do the work on cache miss only.
if ( ! isset( $cache[ $in ] ) ) {
// Default to (the trimmed version of) what was given to us.
$result = trim( $in );
// Some silly plugins wrap background images in html-encoded quotes, so remove those from the img url.
if ( strpos( $result, '"' ) !== false ) {
$result = str_replace( '"', '', $result );
}
if ( autoptimizeUtils::is_protocol_relative( $result ) ) {
$result = $parsed_site_url['scheme'] . ':' . $result;
} elseif ( 0 === strpos( $result, '/' ) ) {
// Root-relative...
$result = $parsed_site_url['scheme'] . '://' . $parsed_site_url['host'] . $result;
} elseif ( ! empty( $cdn_domain ) && strpos( $result, $cdn_domain ) !== 0 ) {
$result = str_replace( $cdn_domain, $parsed_site_url['host'], $result );
}
$result = apply_filters( 'autoptimize_filter_imgopt_normalized_url', $result );
// Store in cache.
$cache[ $in ] = $result;
}
return $cache[ $in ];
}
public function filter_optimize_css_images( $in )
{
$in = $this->normalize_img_url( $in );
if ( $this->can_optimize_image( $in ) ) {
return $this->build_imgopt_url( $in, '', '' );
} else {
return $in;
}
}
private function get_imgopt_base_url()
{
static $imgopt_base_url = null;
if ( null === $imgopt_base_url ) {
$imgopt_host = $this->get_imgopt_host();
$quality = $this->get_img_quality_string();
$ret_val = apply_filters( 'autoptimize_filter_imgopt_wait', 'ret_img' ); // values: ret_wait, ret_img, ret_json, ret_blank.
$imgopt_base_url = $imgopt_host . 'client/' . $quality . ',' . $ret_val;
$imgopt_base_url = apply_filters( 'autoptimize_filter_imgopt_base_url', $imgopt_base_url );
}
return $imgopt_base_url;
}
private function can_optimize_image( $url )
{
static $cdn_url = null;
static $nopti_images = null;
if ( null === $cdn_url ) {
$cdn_url = apply_filters(
'autoptimize_filter_base_cdnurl',
autoptimizeOptionWrapper::get_option( 'autoptimize_cdn_url', '' )
);
}
if ( null === $nopti_images ) {
$nopti_images = apply_filters( 'autoptimize_filter_imgopt_noptimize', '' );
}
$site_host = AUTOPTIMIZE_SITE_DOMAIN;
$url = $this->normalize_img_url( $url );
$url_parsed = parse_url( $url );
if ( array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host && empty( $cdn_url ) ) {
return false;
} elseif ( ! empty( $cdn_url ) && strpos( $url, $cdn_url ) === false && array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host ) {
return false;
} elseif ( strpos( $url, '.php' ) !== false ) {
return false;
} elseif ( str_ireplace( array( '.png', '.gif', '.jpg', '.jpeg', '.webp' ), '', $url_parsed['path'] ) === $url_parsed['path'] ) {
// fixme: better check against end of string.
return false;
} elseif ( ! empty( $nopti_images ) ) {
$nopti_images_array = array_filter( array_map( 'trim', explode( ',', $nopti_images ) ) );
foreach ( $nopti_images_array as $nopti_image ) {
if ( strpos( $url, $nopti_image ) !== false ) {
return false;
}
}
}
return true;
}
private function build_imgopt_url( $orig_url, $width = 0, $height = 0 )
{
// sanitize width and height.
if ( strpos( $width, '%' ) !== false ) {
$width = 0;
}
if ( strpos( $height, '%' ) !== false ) {
$height = 0;
}
$width = (int) $width;
$height = (int) $height;
$filtered_url = apply_filters(
'autoptimize_filter_imgopt_build_url',
$orig_url,
$width,
$height
);
// If filter modified the url, return that.
if ( $filtered_url !== $orig_url ) {
return $filtered_url;
}
$orig_url = $this->normalize_img_url( $orig_url );
$imgopt_base_url = $this->get_imgopt_base_url();
$imgopt_size = '';
if ( $width && 0 !== $width ) {
$imgopt_size = ',w_' . $width;
}
if ( $height && 0 !== $height ) {
$imgopt_size .= ',h_' . $height;
}
$url = $imgopt_base_url . $imgopt_size . '/' . $orig_url;
return $url;
}
public function replace_data_thumbs( $matches )
{
return $this->replace_img_callback( $matches, 150, 150 );
}
public function replace_img_callback( $matches, $width = 0, $height = 0 )
{
$_normalized_img_url = $this->normalize_img_url( $matches[1] );
if ( $this->can_optimize_image( $matches[1] ) ) {
return str_replace( $matches[1], $this->build_imgopt_url( $_normalized_img_url, $width, $height ), $matches[0] );
} else {
return $matches[0];
}
}
public function filter_optimize_images( $in )
{
/*
* potential future functional improvements:
*
* filter for critical CSS.
*/
$to_replace = array();
// hide noscript tags to avoid nesting noscript tags (as lazyloaded images add noscript).
if ( $this->should_lazyload() ) {
$in = autoptimizeBase::replace_contents_with_marker_if_exists(
'SCRIPT',
'<script',
'#<(?:no)?script.*?<\/(?:no)?script>#is',
$in
);
}
// extract img tags.
if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $in, $matches ) ) {
foreach ( $matches[0] as $tag ) {
$orig_tag = $tag;
$imgopt_w = '';
$imgopt_h = '';
// first do (data-)srcsets.
if ( preg_match_all( '#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER ) ) {
foreach ( $allsrcsets as $srcset ) {
$srcset = $srcset[2];
$srcsets = explode( ',', $srcset );
foreach ( $srcsets as $indiv_srcset ) {
$indiv_srcset_parts = explode( ' ', trim( $indiv_srcset ) );
if ( isset( $indiv_srcset_parts[1] ) && rtrim( $indiv_srcset_parts[1], 'w' ) !== $indiv_srcset_parts[1] ) {
$imgopt_w = rtrim( $indiv_srcset_parts[1], 'w' );
}
if ( $this->can_optimize_image( $indiv_srcset_parts[0] ) ) {
$imgopt_url = $this->build_imgopt_url( $indiv_srcset_parts[0], $imgopt_w, '' );
$tag = str_replace( $indiv_srcset_parts[0], $imgopt_url, $tag );
}
}
}
}
// proceed with img src.
// get width and height and add to $imgopt_size.
$_get_size = $this->get_size_from_tag( $tag );
$imgopt_w = $_get_size['width'];
$imgopt_h = $_get_size['height'];
// then start replacing images src.
if ( preg_match_all( '#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER ) ) {
foreach ( $urls as $url ) {
$full_src_orig = $url[0];
$url = $url[1];
if ( $this->can_optimize_image( $url ) ) {
$imgopt_url = $this->build_imgopt_url( $url, $imgopt_w, $imgopt_h );
$full_imgopt_src = str_replace( $url, $imgopt_url, $full_src_orig );
$tag = str_replace( $full_src_orig, $full_imgopt_src, $tag );
}
}
}
// do lazyload stuff.
if ( $this->should_lazyload( $in ) && ! empty( $url ) ) {
// first do lpiq placeholder logic.
if ( strpos( $url, $this->get_imgopt_host() ) === 0 ) {
// if all img src have been replaced during srcset, we have to extract the
// origin url from the imgopt one to be able to set a lqip placeholder.
$_url = substr( $url, strpos( $url, '/http' ) + 1 );
} else {
$_url = $url;
}
$_url = $this->normalize_img_url( $_url );
$placeholder = '';
if ( $this->can_optimize_image( $_url ) && apply_filters( 'autoptimize_filter_imgopt_lazyload_dolqip', true ) ) {
$lqip_w = '';
$lqip_h = '';
if ( isset( $imgopt_w ) && ! empty( $imgopt_w ) ) {
$lqip_w = ',w_' . $imgopt_w;
}
if ( isset( $imgopt_h ) && ! empty( $imgopt_h ) ) {
$lqip_h = ',h_' . $imgopt_h;
}
$placeholder = $this->get_imgopt_host() . 'client/q_lqip,ret_wait' . $lqip_w . $lqip_h . '/' . $_url;
}
// then call add_lazyload-function with lpiq placeholder if set.
$tag = $this->add_lazyload( $tag, $placeholder );
}
// and add tag to array for later replacement.
if ( $tag !== $orig_tag ) {
$to_replace[ $orig_tag ] = $tag;
}
}
}
// and replace all.
$out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $in );
// img thumbnails in e.g. woocommerce.
if ( strpos( $out, 'data-thumb' ) !== false && apply_filters( 'autoptimize_filter_imgopt_datathumbs', true ) ) {
$out = preg_replace_callback(
'/\<div(?:[^>]?)\sdata-thumb\=(?:\"|\')(.+?)(?:\"|\')(?:[^>]*)?\>/s',
array( $this, 'replace_data_thumbs' ),
$out
);
}
// background-image in inline style.
if ( strpos( $out, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_imgopt_backgroundimages', true ) ) {
$out = preg_replace_callback(
'/style=(?:"|\')[^<>]*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)/',
array( $this, 'replace_img_callback' ),
$out
);
}
// lazyload: restore noscript tags + lazyload picture source tags and bgimage.
if ( $this->should_lazyload() ) {
$out = autoptimizeBase::restore_marked_content(
'SCRIPT',
$out
);
$out = $this->process_picture_tag( $out, true, true );
$out = $this->process_bgimage( $out );
} else {
$out = $this->process_picture_tag( $out, true, false );
}
return $out;
}
public function get_size_from_tag( $tag ) {
// reusable function to extract widht and height from an image tag
// enforcing a filterable maximum width and height (default 4999X4999).
$width = '';
$height = '';
if ( preg_match( '#width=("|\')(.*)("|\')#Usmi', $tag, $_width ) ) {
if ( strpos( $_width[2], '%' ) === false ) {
$width = (int) $_width[2];
}
}
if ( preg_match( '#height=("|\')(.*)("|\')#Usmi', $tag, $_height ) ) {
if ( strpos( $_height[2], '%' ) === false ) {
$height = (int) $_height[2];
}
}
// check for and enforce (filterable) max sizes.
$_max_width = apply_filters( 'autoptimize_filter_imgopt_max_width', 4999 );
if ( $width > $_max_width ) {
$_width = $_max_width;
$height = $_width / $width * $height;
$width = $_width;
}
$_max_height = apply_filters( 'autoptimize_filter_imgopt_max_height', 4999 );
if ( $height > $_max_height ) {
$_height = $_max_height;
$width = $_height / $height * $width;
$height = $_height;
}
return array(
'width' => $width,
'height' => $height,
);
}
/**
* Lazyload functions
*/
public static function should_lazyload_wrapper() {
// needed in autoptimizeMain.php.
$self = new self();
return $self->should_lazyload();
}
public function should_lazyload( $context = '' ) {
if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) && false === $this->check_nolazy() ) {
$lazyload_return = true;
} else {
$lazyload_return = false;
}
$lazyload_return = apply_filters( 'autoptimize_filter_imgopt_should_lazyload', $lazyload_return, $context );
return $lazyload_return;
}
public function check_nolazy() {
if ( array_key_exists( 'ao_nolazy', $_GET ) && '1' === $_GET['ao_nolazy'] ) {
return true;
} else {
return false;
}
}
public function filter_lazyload_images( $in )
{
// only used is image optimization is NOT active but lazyload is.
$to_replace = array();
// hide (no)script tags to avoid nesting noscript tags (as lazyloaded images add noscript).
$out = autoptimizeBase::replace_contents_with_marker_if_exists(
'SCRIPT',
'<script',
'#<(?:no)?script.*?<\/(?:no)?script>#is',
$in
);
// extract img tags and add lazyload attribs.
if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $out, $matches ) ) {
foreach ( $matches[0] as $tag ) {
if ( $this->should_lazyload( $out ) ) {
$to_replace[ $tag ] = $this->add_lazyload( $tag );
}
}
$out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $out );
}
// and also lazyload picture tag.
$out = $this->process_picture_tag( $out, false, true );
// and inline style blocks with background-image.
$out = $this->process_bgimage( $out );
// restore noscript tags.
$out = autoptimizeBase::restore_marked_content(
'SCRIPT',
$out
);
return $out;
}
public function add_lazyload( $tag, $placeholder = '' ) {
// adds actual lazyload-attributes to an image node.
if ( str_ireplace( $this->get_lazyload_exclusions(), '', $tag ) === $tag ) {
$tag = $this->maybe_fix_missing_quotes( $tag );
// store original tag for use in noscript version.
$noscript_tag = '<noscript>' . autoptimizeUtils::remove_id_from_node( $tag ) . '</noscript>';
$lazyload_class = apply_filters( 'autoptimize_filter_imgopt_lazyload_class', 'lazyload' );
// insert lazyload class.
$tag = $this->inject_classes_in_tag( $tag, "$lazyload_class " );
if ( ! $placeholder || empty( $placeholder ) ) {
// get image width & heigth for placeholder fun (and to prevent content reflow).
$_get_size = $this->get_size_from_tag( $tag );
$width = $_get_size['width'];
$height = $_get_size['height'];
if ( false === $width || empty( $width ) ) {
$width = 210; // default width for SVG placeholder.
}
if ( false === $height || empty( $height ) ) {
$height = $width / 3 * 2; // if no height, base it on width using the 3/2 aspect ratio.
}
// insert the actual lazyload stuff.
// see https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/ for great read on why we're using empty svg's.
$placeholder = apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder( $width, $height ) );
}
$tag = preg_replace( '/(\s)src=/', ' src=\'' . $placeholder . '\' data-src=', $tag );
$tag = preg_replace( '/(\s)srcset=/', ' data-srcset=', $tag );
// move sizes to data-sizes unless filter says no.
if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_move_sizes', true ) ) {
$tag = str_replace( ' sizes=', ' data-sizes=', $tag );
}
// add the noscript-tag from earlier.
$tag = $noscript_tag . $tag;
$tag = apply_filters( 'autoptimize_filter_imgopt_lazyloaded_img', $tag );
}
return $tag;
}
public function add_lazyload_js_footer() {
if ( false === autoptimizeMain::should_buffer() ) {
return;
}
// The JS will by default be excluded form autoptimization but this can be changed with a filter.
$noptimize_flag = '';
if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_js_noptimize', true ) ) {
$noptimize_flag = ' data-noptimize="1"';
}
$lazysizes_js = plugins_url( 'external/js/lazysizes.min.js?ao_version=' . AUTOPTIMIZE_PLUGIN_VERSION, __FILE__ );
$cdn_url = $this->get_cdn_url();
if ( ! empty( $cdn_url ) ) {
$lazysizes_js = str_replace( AUTOPTIMIZE_WP_SITE_URL, $cdn_url, $lazysizes_js );
}
$type_js = '';
if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) {
$type_js = ' type="text/javascript"';
}
// Adds lazyload CSS & JS to footer, using echo because wp_enqueue_script seems not to support pushing attributes (async).
echo apply_filters( 'autoptimize_filter_imgopt_lazyload_cssoutput', '<noscript><style>.lazyload{display:none;}</style></noscript>' );
echo apply_filters( 'autoptimize_filter_imgopt_lazyload_jsconfig', '<script' . $type_js . $noptimize_flag . '>window.lazySizesConfig=window.lazySizesConfig||{};window.lazySizesConfig.loadMode=1;</script>' );
echo apply_filters( 'autoptimize_filter_imgopt_lazyload_js', '<script async' . $type_js . $noptimize_flag . ' src=\'' . $lazysizes_js . '\'></script>' );
// And add webp detection and loading JS.
if ( $this->should_webp() ) {
$_webp_detect = "function c_webp(A){var n=new Image;n.onload=function(){var e=0<n.width&&0<n.height;A(e)},n.onerror=function(){A(!1)},n.src='data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA=='}function s_webp(e){window.supportsWebP=e}c_webp(s_webp);";
$_webp_load = "document.addEventListener('lazybeforeunveil',function({target:b}){window.supportsWebP&&['data-src','data-srcset'].forEach(function(c){attr=b.getAttribute(c),null!==attr&&-1==attr.indexOf('/client/to_webp')&&b.setAttribute(c,attr.replace(/\/client\//,'/client/to_webp,'))})});";
echo apply_filters( 'autoptimize_filter_imgopt_webp_js', '<script' . $type_js . $noptimize_flag . '>' . $_webp_detect . $_webp_load . '</script>' );
}
}
public function get_cdn_url() {
// getting CDN url here to avoid having to make bigger changes to autoptimizeBase.
static $cdn_url = null;
if ( null === $cdn_url ) {
$cdn_url = autoptimizeOptionWrapper::get_option( 'autoptimize_cdn_url', '' );
$cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed( $cdn_url );
$cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $cdn_url );
}
return $cdn_url;
}
public function get_lazyload_exclusions() {
// returns array of strings that if found in an <img tag will stop the img from being lazy-loaded.
static $exclude_lazyload_array = null;
if ( null === $exclude_lazyload_array ) {
$options = $this->options;
// set default exclusions.
$exclude_lazyload_array = array( 'skip-lazy', 'data-no-lazy', 'notlazy', 'data-src', 'data-srcset', 'data:image/', 'data-lazyload', 'rev-slidebg', 'loading="eager"' );
// add from setting.
if ( array_key_exists( 'autoptimize_imgopt_text_field_5', $options ) ) {
$exclude_lazyload_option = $options['autoptimize_imgopt_text_field_5'];
if ( ! empty( $exclude_lazyload_option ) ) {
$exclude_lazyload_array = array_merge( $exclude_lazyload_array, array_filter( array_map( 'trim', explode( ',', $options['autoptimize_imgopt_text_field_5'] ) ) ) );
}
}
// and filter for developer-initiated changes.
$exclude_lazyload_array = apply_filters( 'autoptimize_filter_imgopt_lazyload_exclude_array', $exclude_lazyload_array );
}
return $exclude_lazyload_array;
}
public function inject_classes_in_tag( $tag, $target_class ) {
if ( strpos( $tag, 'class=' ) !== false ) {
$tag = preg_replace( '/(\sclass\s?=\s?("|\'))/', '$1' . $target_class, $tag );
} else {
$tag = preg_replace( '/(<img)\s/', '$1 class="' . trim( $target_class ) . '" ', $tag );
}
return $tag;
}
public function get_default_lazyload_placeholder( $imgopt_w, $imgopt_h ) {
return 'data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20' . $imgopt_w . '%20' . $imgopt_h . '%22%3E%3C/svg%3E';
}
public function should_webp() {
static $webp_return = null;
if ( is_null( $webp_return ) ) {
// webp only works if imgopt and lazyload are also active.
if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_4'] ) && ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) && $this->imgopt_active() ) {
$webp_return = true;
} else {
$webp_return = false;
}
}
return $webp_return;
}
public function process_picture_tag( $in, $imgopt = false, $lazy = false ) {
// check if "<picture" is present and if filter allows us to process <picture>.
if ( strpos( $in, '<picture' ) === false || apply_filters( 'autoptimize_filter_imgopt_dopicture', true ) === false ) {
return $in;
}
$_exclusions = $this->get_lazyload_exclusions();
$to_replace_pict = array();
// extract and process each picture-node.
preg_match_all( '#<picture.*</picture>#Usmi', $in, $_pictures, PREG_SET_ORDER );
foreach ( $_pictures as $_picture ) {
$_picture = $this->maybe_fix_missing_quotes( $_picture );
if ( strpos( $_picture[0], '<source ' ) !== false && preg_match_all( '#<source .*srcset=(?:"|\')(?!data)(.*)(?:"|\').*>#Usmi', $_picture[0], $_sources, PREG_SET_ORDER ) !== false ) {
foreach ( $_sources as $_source ) {
$_picture_replacement = $_source[0];
// should we optimize the image?
if ( $imgopt && $this->can_optimize_image( $_source[1] ) ) {
$_picture_replacement = str_replace( $_source[1], $this->build_imgopt_url( $_source[1] ), $_picture_replacement );
}
// should we lazy-load?
if ( $lazy && $this->should_lazyload() && str_ireplace( $_exclusions, '', $_picture_replacement ) === $_picture_replacement ) {
$_picture_replacement = str_replace( ' srcset=', ' data-srcset=', $_picture_replacement );
}
$to_replace_pict[ $_source[0] ] = $_picture_replacement;
}
}
}
// and return the fully procesed $in.
$out = str_replace( array_keys( $to_replace_pict ), array_values( $to_replace_pict ), $in );
return $out;
}
public function process_bgimage( $in ) {
if ( strpos( $in, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_imgopt_lazyload_backgroundimages', true ) ) {
$out = preg_replace_callback(
'/(<(?:article|aside|body|div|footer|header|p|section|table)[^>]*)\sstyle=(?:"|\')[^<>]*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)[^>]*/',
array( $this, 'lazyload_bgimg_callback' ),
$in
);
return $out;
}
return $in;
}
public function lazyload_bgimg_callback( $matches ) {
if ( str_ireplace( $this->get_lazyload_exclusions(), '', $matches[0] ) === $matches[0] ) {
// get placeholder & lazyload class strings.
$placeholder = apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder( 500, 300 ) );
$lazyload_class = apply_filters( 'autoptimize_filter_imgopt_lazyload_class', 'lazyload' );
// replace background-image URL with SVG placeholder.
$out = str_replace( $matches[2], $placeholder, $matches[0] );
// add data-bg attribute with real background-image URL for lazyload to pick up.
$out = str_replace( $matches[1], $matches[1] . ' data-bg="' . trim( str_replace( array( "\r\n", '"' ), '', $matches[2] ) ) . '"', $out );
// add lazyload class to tag.
$out = $this->inject_classes_in_tag( $out, "$lazyload_class " );
return $out;
}
return $matches[0];
}
public function maybe_fix_missing_quotes( $tag_in ) {
// W3TC's Minify_HTML class removes quotes around attribute value, this re-adds them for the class and width/height attributes so we can lazyload properly.
if ( file_exists( WP_PLUGIN_DIR . '/w3-total-cache/w3-total-cache.php' ) && class_exists( 'Minify_HTML' ) && apply_filters( 'autoptimize_filter_imgopt_fixquotes', true ) ) {
$tag_out = preg_replace( '/class\s?=([^("|\')]*)(\s|>)/U', 'class=\'$1\'$2', $tag_in );
$tag_out = preg_replace( '/\s(width|height)=(?:"|\')?([^\s"\'>]*)(?:"|\')?/', ' $1=\'$2\'', $tag_out );
return $tag_out;
} else {
return $tag_in;
}
}
/**
* Admin page logic and related functions below.
*/
public function imgopt_admin_menu()
{
// no acces if multisite and not network admin and no site config allowed.
if ( autoptimizeConfig::should_show_menu_tabs() ) {
add_submenu_page(
null,
'autoptimize_imgopt',
'autoptimize_imgopt',
'manage_options',
'autoptimize_imgopt',
array( $this, 'imgopt_options_page' )
);
}
register_setting( 'autoptimize_imgopt_settings', 'autoptimize_imgopt_settings' );
}
public function add_imgopt_tab( $in )
{
if ( autoptimizeConfig::should_show_menu_tabs() ) {
$in = array_merge( $in, array( 'autoptimize_imgopt' => __( 'Images', 'autoptimize' ) ) );
}
return $in;
}
public function imgopt_options_page()
{
// Check querystring for "refreshCacheChecker" and call cachechecker if so.
if ( array_key_exists( 'refreshImgProvStats', $_GET ) && 1 == $_GET['refreshImgProvStats'] ) {
$this->query_img_provider_stats();
}
$options = $this->fetch_options();
$sp_url_suffix = $this->get_service_url_suffix();
?>
<style>
#ao_settings_form {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;}
#ao_settings_form .form-table th {font-weight: normal;}
#autoptimize_imgopt_descr{font-size: 120%;}
</style>
<script>document.title = "Autoptimize: <?php _e( 'Images', 'autoptimize' ); ?> " + document.title;</script>
<div class="wrap">
<h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
<?php echo autoptimizeConfig::ao_admin_tabs(); ?>