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
|
<?php namespace Imagify\Webp\Picture;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Display webp images on the site with <picture> tags. * * @since 1.9 * @author Grégory Viguier */ class Display { use \Imagify\Traits\InstanceGetterTrait;
/** * Option value. * * @var string * @since 1.9 * @author Grégory Viguier */ const OPTION_VALUE = 'picture';
/** * Filesystem object. * * @var \Imagify_Filesystem * @since 1.9 * @access protected * @author Grégory Viguier */ protected $filesystem;
/** * Constructor. * * @since 1.9 * @access public * @author Grégory Viguier */ public function __construct() { $this->filesystem = \Imagify_Filesystem::get_instance(); }
/** * Init. * * @since 1.9 * @access public * @author Grégory Viguier */ public function init() { add_action( 'template_redirect', [ $this, 'start_content_process' ], -1000 ); }
/** ----------------------------------------------------------------------------------------- */ /** ADD <PICTURE> TAGS TO THE PAGE ========================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Start buffering the page content. * * @since 1.9 * @access public * @author Grégory Viguier */ public function start_content_process() { if ( ! get_imagify_option( 'display_webp' ) ) { return; }
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) { return; }
/** * Prevent the replacement of <img> tags into <picture> tags. * * @since 1.9 * @author Grégory Viguier * * @param bool $allow True to allow the use of <picture> tags (default). False to prevent their use. */ $allow = apply_filters( 'imagify_allow_picture_tags_for_webp', true );
if ( ! $allow ) { return; }
ob_start( [ $this, 'maybe_process_buffer' ] ); }
/** * Maybe process the page content. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $buffer The buffer content. * @return string */ public function maybe_process_buffer( $buffer ) { if ( ! $this->is_html( $buffer ) ) { return $buffer; }
if ( strlen( $buffer ) <= 255 ) { // Buffer length must be > 255 (IE does not read pages under 255 c). return $buffer; }
$buffer = $this->process_content( $buffer );
/** * Filter the page content after Imagify. * * @since 1.9 * @author Grégory Viguier * * @param string $buffer The page content. */ $buffer = (string) apply_filters( 'imagify_buffer', $buffer );
return $buffer; }
/** * Process the content. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $content The content. * @return string */ public function process_content( $content ) { $images = $this->get_images( $content );
if ( ! $images ) { return $content; }
foreach ( $images as $image ) { $tag = $this->build_picture_tag( $image ); $content = str_replace( $image['tag'], $tag, $content ); }
return $content; }
/** ----------------------------------------------------------------------------------------- */ /** BUILD HTML TAGS AND ATTRIBUTES ========================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Build the <picture> tag to insert. * * @since 1.9 * @see $this->process_image() * @access protected * @author Grégory Viguier * * @param array $image An array of data. * @return string A <picture> tag. */ protected function build_picture_tag( $image ) { $to_remove = [ 'alt' => '', 'height' => '', 'width' => '', 'data-lazy-src' => '', 'data-src' => '', 'src' => '', 'data-lazy-srcset' => '', 'data-srcset' => '', 'srcset' => '', 'data-lazy-sizes' => '', 'data-sizes' => '', 'sizes' => '', ];
$attributes = array_diff_key( $image['attributes'], $to_remove );
/** * Filter the attributes to be added to the <picture> tag. * * @since 1.9 * @author Grégory Viguier * * @param array $attributes A list of attributes to be added to the <picture> tag. * @param array $data Data built from the originale <img> tag. See $this->process_image(). */ $attributes = apply_filters( 'imagify_picture_attributes', $attributes, $image );
$output = '<picture' . $this->build_attributes( $attributes ) . ">\n"; /** * Allow to add more <source> tags to the <picture> tag. * * @since 1.9 * @author Grégory Viguier * * @param string $more_source_tags Additional <source> tags. * @param array $data Data built from the originale <img> tag. See $this->process_image(). */ $output .= apply_filters( 'imagify_additional_source_tags', '', $image ); $output .= $this->build_source_tag( $image ); $output .= $this->build_img_tag( $image ); $output .= "</picture>\n";
return $output; }
/** * Build the <source> tag to insert in the <picture>. * * @since 1.9 * @see $this->process_image() * @access protected * @author Grégory Viguier * * @param array $image An array of data. * @return string A <source> tag. */ protected function build_source_tag( $image ) { $srcset_source = ! empty( $image['srcset_attribute'] ) ? $image['srcset_attribute'] : $image['src_attribute'] . 'set'; $attributes = [ 'type' => 'image/webp', $srcset_source => [], ];
if ( ! empty( $image['srcset'] ) ) { foreach ( $image['srcset'] as $srcset ) { if ( empty( $srcset['webp_url'] ) ) { continue; }
$attributes[ $srcset_source ][] = $srcset['webp_url'] . ' ' . $srcset['descriptor']; } }
if ( empty( $attributes[ $srcset_source ] ) ) { $attributes[ $srcset_source ][] = $image['src']['webp_url']; }
$attributes[ $srcset_source ] = implode( ', ', $attributes[ $srcset_source ] );
foreach ( [ 'data-lazy-srcset', 'data-srcset', 'srcset' ] as $srcset_attr ) { if ( ! empty( $image['attributes'][ $srcset_attr ] ) && $srcset_attr !== $srcset_source ) { $attributes[ $srcset_attr ] = $image['attributes'][ $srcset_attr ]; } }
if ( 'srcset' !== $srcset_source && empty( $attributes['srcset'] ) && ! empty( $image['attributes']['src'] ) ) { // Lazyload: the "src" attr should contain a placeholder (a data image or a blank.gif ). $attributes['srcset'] = $image['attributes']['src']; }
foreach ( [ 'data-lazy-sizes', 'data-sizes', 'sizes' ] as $sizes_attr ) { if ( ! empty( $image['attributes'][ $sizes_attr ] ) ) { $attributes[ $sizes_attr ] = $image['attributes'][ $sizes_attr ]; } }
/** * Filter the attributes to be added to the <source> tag. * * @since 1.9 * @author Grégory Viguier * * @param array $attributes A list of attributes to be added to the <source> tag. * @param array $data Data built from the original <img> tag. See $this->process_image(). */ $attributes = apply_filters( 'imagify_picture_source_attributes', $attributes, $image );
return '<source' . $this->build_attributes( $attributes ) . "/>\n"; }
/** * Build the <img> tag to insert in the <picture>. * * @since 1.9 * @see $this->process_image() * @access protected * @author Grégory Viguier * * @param array $image An array of data. * @return string A <img> tag. */ protected function build_img_tag( $image ) { $to_remove = [ 'class' => '', 'id' => '', 'style' => '', 'title' => '', ];
$attributes = array_diff_key( $image['attributes'], $to_remove );
/** * Filter the attributes to be added to the <img> tag. * * @since 1.9 * @author Grégory Viguier * * @param array $attributes A list of attributes to be added to the <img> tag. * @param array $data Data built from the originale <img> tag. See $this->process_image(). */ $attributes = apply_filters( 'imagify_picture_img_attributes', $attributes, $image );
return '<img' . $this->build_attributes( $attributes ) . "/>\n"; }
/** * Create HTML attributes from an array. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param array $attributes A list of attribute pairs. * @return string HTML attributes. */ protected function build_attributes( $attributes ) { if ( ! $attributes || ! is_array( $attributes ) ) { return ''; }
$out = '';
foreach ( $attributes as $attribute => $value ) { $out .= ' ' . $attribute . '="' . esc_attr( $value ) . '"'; }
return $out; }
/** ----------------------------------------------------------------------------------------- */ /** VARIOUS TOOLS =========================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Get a list of images in a content. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $content The content. * @return array */ protected function get_images( $content ) { // Remove comments. $content = preg_replace( '/<!--(.*)-->/Uis', '', $content );
if ( ! preg_match_all( '/<img\s.*>/isU', $content, $matches ) ) { return []; }
$images = array_map( [ $this, 'process_image' ], $matches[0] ); $images = array_filter( $images );
/** * Filter the images to display with a <picture> tag. * * @since 1.9 * @see $this->process_image() * @author Grégory Viguier * * @param array $images A list of arrays. * @param string $content The page content. */ $images = apply_filters( 'imagify_webp_picture_images_to_display', $images, $content );
if ( ! $images || ! is_array( $images ) ) { return []; }
foreach ( $images as $i => $image ) { if ( empty( $image['src']['webp_exists'] ) || empty( $image['src']['webp_url'] ) ) { unset( $images[ $i ] ); continue; }
unset( $images[ $i ]['src']['webp_path'], $images[ $i ]['src']['webp_exists'] );
if ( empty( $image['srcset'] ) || ! is_array( $image['srcset'] ) ) { unset( $images[ $i ]['srcset'] ); continue; }
foreach ( $image['srcset'] as $j => $srcset ) { if ( ! is_array( $srcset ) ) { continue; }
if ( empty( $srcset['webp_exists'] ) || empty( $srcset['webp_url'] ) ) { unset( $images[ $i ]['srcset'][ $j ]['webp_url'] ); }
unset( $images[ $i ]['srcset'][ $j ]['webp_path'], $images[ $i ]['srcset'][ $j ]['webp_exists'] ); } }
return $images; }
/** * Process an image tag and get an array containing some data. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $image An image html tag. * @return array|false { * An array of data if the image has a webp version. False otherwise. * * @type string $tag The image tag. * @type array $attributes The image attributes (minus src and srcset). * @type array $src { * @type string $url URL to the original image. * @type string $webp_url URL to the webp version. * } * @type array $srcset { * An array or arrays. Not set if not applicable. * * @type string $url URL to the original image. * @type string $webp_url URL to the webp version. Not set if not applicable. * @type string $descriptor A src descriptor. * } * } */ protected function process_image( $image ) { static $extensions;
$atts_pattern = '/(?<name>[^\s"\']+)\s*=\s*(["\'])\s*(?<value>.*?)\s*\2/s';
if ( ! preg_match_all( $atts_pattern, $image, $tmp_attributes, PREG_SET_ORDER ) ) { // No attributes? return false; }
$attributes = [];
foreach ( $tmp_attributes as $attribute ) { $attributes[ $attribute['name'] ] = $attribute['value']; }
if ( ! empty( $attributes['class'] ) && strpos( $attributes['class'], 'imagify-no-webp' ) !== false ) { // Has the 'imagify-no-webp' class. return false; }
// Deal with the src attribute. $src_source = false;
foreach ( [ 'data-lazy-src', 'data-src', 'src' ] as $src_attr ) { if ( ! empty( $attributes[ $src_attr ] ) ) { $src_source = $src_attr; break; } }
if ( ! $src_source ) { // No src attribute. return false; }
if ( ! isset( $extensions ) ) { $extensions = imagify_get_mime_types( 'image' ); $extensions = array_keys( $extensions ); $extensions = implode( '|', $extensions ); }
if ( ! preg_match( '@^(?<src>(?:(?:https?:)?//|/).+\.(?<extension>' . $extensions . '))(?<query>\?.*)?$@i', $attributes[ $src_source ], $src ) ) { // Not a supported image format. return false; }
$webp_url = imagify_path_to_webp( $src['src'] ); $webp_path = $this->url_to_path( $webp_url ); $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
$data = [ 'tag' => $image, 'attributes' => $attributes, 'src_attribute' => $src_source, 'src' => [ 'url' => $attributes[ $src_source ], 'webp_url' => $webp_url, 'webp_path' => $webp_path, 'webp_exists' => $webp_path && $this->filesystem->exists( $webp_path ), ], 'srcset_attribute' => false, 'srcset' => [], ];
// Deal with the srcset attribute. $srcset_source = false;
foreach ( [ 'data-lazy-srcset', 'data-srcset', 'srcset' ] as $srcset_attr ) { if ( ! empty( $attributes[ $srcset_attr ] ) ) { $srcset_source = $srcset_attr; break; } }
if ( $srcset_source ) { $data['srcset_attribute'] = $srcset_source;
$srcset = explode( ',', $attributes[ $srcset_source ] );
foreach ( $srcset as $srcs ) { $srcs = preg_split( '/\s+/', trim( $srcs ) );
if ( count( $srcs ) > 2 ) { // Not a good idea to have space characters in file name. $descriptor = array_pop( $srcs ); $srcs = [ implode( ' ', $srcs ), $descriptor ]; }
if ( empty( $srcs[1] ) ) { $srcs[1] = '1x'; }
if ( ! preg_match( '@^(?<src>(?:https?:)?//.+\.(?<extension>' . $extensions . '))(?<query>\?.*)?$@i', $srcs[0], $src ) ) { // Not a supported image format. $data['srcset'][] = [ 'url' => $srcs[0], 'descriptor' => $srcs[1], ]; continue; }
$webp_url = imagify_path_to_webp( $src['src'] ); $webp_path = $this->url_to_path( $webp_url ); $webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
$data['srcset'][] = [ 'url' => $srcs[0], 'descriptor' => $srcs[1], 'webp_url' => $webp_url, 'webp_path' => $webp_path, 'webp_exists' => $webp_path && $this->filesystem->exists( $webp_path ), ]; } }
/** * Filter a processed image tag. * * @since 1.9 * @author Grégory Viguier * * @param array $data An array of data for this image. * @param string $image An image html tag. */ $data = apply_filters( 'imagify_webp_picture_process_image', $data, $image );
if ( ! $data || ! is_array( $data ) ) { return false; }
if ( ! isset( $data['tag'], $data['attributes'], $data['src_attribute'], $data['src'], $data['srcset_attribute'], $data['srcset'] ) ) { return false; }
return $data; }
/** * Tell if a content is HTML. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $content The content. * @return bool */ protected function is_html( $content ) { return preg_match( '/<\/html>/i', $content ); }
/** * Convert a file URL to an absolute path. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $url A file URL. * @return string|bool The file path. False on failure. */ protected function url_to_path( $url ) { static $uploads_url; static $uploads_dir; static $root_url; static $root_dir; static $cdn_url; static $domain_url;
/** * $url, $uploads_url, $root_url, and $cdn_url are passed through `set_url_scheme()` only to make sure `stripos()` doesn't fail over a stupid http/https difference. */ if ( ! isset( $uploads_url ) ) { $uploads_url = set_url_scheme( $this->filesystem->get_upload_baseurl() ); $uploads_dir = $this->filesystem->get_upload_basedir( true ); $root_url = set_url_scheme( $this->filesystem->get_site_root_url() ); $root_dir = $this->filesystem->get_site_root(); $cdn_url = $this->get_cdn_source(); $cdn_url = $cdn_url['url'] ? set_url_scheme( $cdn_url['url'] ) : false; $domain_url = wp_parse_url( $root_url );
if ( ! empty( $domain_url['scheme'] ) && ! empty( $domain_url['host'] ) ) { $domain_url = $domain_url['scheme'] . '://' . $domain_url['host'] . '/'; } else { $domain_url = false; } }
// Get the right URL format. if ( $domain_url && strpos( $url, '/' ) === 0 ) { // URL like `/path/to/image.jpg.webp`. $url = $domain_url . ltrim( $url, '/' ); }
$url = set_url_scheme( $url );
if ( $cdn_url && $domain_url && stripos( $url, $cdn_url ) === 0 ) { // CDN. $url = str_ireplace( $cdn_url, $domain_url, $url ); }
// Return the path. if ( stripos( $url, $uploads_url ) === 0 ) { return str_ireplace( $uploads_url, $uploads_dir, $url ); }
if ( stripos( $url, $root_url ) === 0 ) { return str_ireplace( $root_url, $root_dir, $url ); }
return false; }
/** * Get the CDN "source". * * @since 1.9.3 * @access public * @author Grégory Viguier * * @param string $option_url An URL to use instead of the one stored in the option. It is used only if no constant/filter. * @return array { * @type string $source Where does it come from? Possible values are 'constant', 'filter', or 'option'. * @type string $name Who? Can be a constant name, a plugin name, or an empty string. * @type string $url The CDN URL, with a trailing slash. An empty string if no URL is set. * } */ public function get_cdn_source( $option_url = '' ) { if ( defined( 'IMAGIFY_CDN_URL' ) && IMAGIFY_CDN_URL && is_string( IMAGIFY_CDN_URL ) ) { // Use a constant. $source = [ 'source' => 'constant', 'name' => 'IMAGIFY_CDN_URL', 'url' => IMAGIFY_CDN_URL, ]; } else { // Maybe use a filter. $filter_source = [ 'name' => null, 'url' => null, ];
/** * Provide a custom CDN source. * * @since 1.9.3 * @author Grégory Viguier * * @param array $filter_source { * @type $name string The name of which provides the URL (plugin name, etc). * @type $url string The CDN URL. * } */ $filter_source = apply_filters( 'imagify_cdn_source', $filter_source );
if ( ! empty( $filter_source['url'] ) ) { $source = [ 'source' => 'filter', 'name' => ! empty( $filter_source['name'] ) ? $filter_source['name'] : '', 'url' => $filter_source['url'], ]; } }
if ( empty( $source['url'] ) ) { // No constant, no filter: use the option. $source = [ 'source' => 'option', 'name' => '', 'url' => $option_url && is_string( $option_url ) ? $option_url : get_imagify_option( 'cdn_url' ), ]; }
if ( empty( $source['url'] ) ) { // Nothing set. return [ 'source' => 'option', 'name' => '', 'url' => '', ]; }
$source['url'] = $this->sanitize_cdn_url( $source['url'] );
if ( empty( $source['url'] ) ) { // Not an URL. return [ 'source' => 'option', 'name' => '', 'url' => '', ]; }
return $source; }
/** * Sanitize the CDN URL value. * * @since 1.9.3 * @access public * @author Grégory Viguier * * @param string $url The URL to sanitize. * @return string */ public function sanitize_cdn_url( $url ) { $url = sanitize_text_field( $url );
if ( ! $url || ! preg_match( '@^https?://.+\.[^.]+@i', $url ) ) { // Not an URL. return ''; }
return trailingslashit( $url ); } }
|