PluginProbe
Performance Lab / 1.8.0
Performance Lab v1.8.0
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / modules / images / fetchpriority / load.php

load.php in Performance Lab 1.8.0, at modules/images/fetchpriority/load.php

55 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: Fetchpriority
4 * Description: Adds a fetchpriority hint for the primary content image on the page to load faster.
5 * Experimental: Yes
6 *
7 * @since 1.8.0
8 * @package performance-lab
9 */
10
11 /**
12 * Filters an image tag in content to add the fetchpriority attribute if it is not lazy-loaded.
13 *
14 * @since 1.8.0
15 *
16 * @param string $filtered_image The image tag to filter.
17 * @param string $context The context of the image.
18 * @return string The filtered image tag.
19 */
20 function fetchpriority_img_tag_add_attr( $filtered_image, $context ) {
21
22 if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) {
23 return $filtered_image;
24 }
25
26 // Fetchpriority relies on lazy loading logic.
27 if ( ! wp_lazy_loading_enabled( 'img', $context ) ) {
28 return $filtered_image;
29 }
30
31 if ( ! empty( $filtered_image ) && strpos( $filtered_image, 'loading="lazy"' ) === false && strpos( $filtered_image, 'fetchpriority=' ) === false ) {
32 $filtered_image = str_replace( '<img ', '<img fetchpriority="high" ', $filtered_image );
33 remove_filter( 'wp_content_img_tag', 'fetchpriority_img_tag_add_attr' );
34 remove_filter( 'post_thumbnail_html', 'fetchpriority_filter_post_thumbnail_html' );
35 }
36
37 return $filtered_image;
38 }
39 add_filter( 'wp_content_img_tag', 'fetchpriority_img_tag_add_attr', 10, 2 );
40
41 /**
42 * Filters the post thumbnail HTML to conditionally add the fetchpriority attribute.
43 *
44 * @since 1.8.0
45 *
46 * @param string $html The post thumbnail HTML to filter.
47 * @return string The filtered post thumbnail HTML.
48 */
49 function fetchpriority_filter_post_thumbnail_html( $html ) {
50 $html = fetchpriority_img_tag_add_attr( $html, 'the_post_thumbnail' );
51
52 return $html;
53 }
54 add_filter( 'post_thumbnail_html', 'fetchpriority_filter_post_thumbnail_html' );
55