Media: Allow for customization of lazy-loading featured images.

When lazy-loading images was introduced, in [52065] the check for `wp_lazy_loading_enabled()` was omitted by accident in the logic to set the attribute with its default value on `img` tags from `get_the_post_thumbnail()`. Without this check, it is impossible for third-party developers to modify the behavior for featured images.

This changeset fixes the problem by introducing the check.

Props flixos90, joemcgill, mukesh27.
Fixes #57490.


git-svn-id: https://develop.svn.wordpress.org/trunk@55093 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2023-01-19 07:32:28 +00:00
parent 84bb54d618
commit 65caf62944
2 changed files with 48 additions and 10 deletions

View File

@ -186,17 +186,20 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr =
update_post_thumbnail_cache();
}
// Get the 'loading' attribute value to use as default, taking precedence over the default from
// `wp_get_attachment_image()`.
$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
// Add `loading` attribute.
if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
// Get the 'loading' attribute value to use as default, taking precedence over the default from
// `wp_get_attachment_image()`.
$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
// Add the default to the given attributes unless they already include a 'loading' directive.
if ( empty( $attr ) ) {
$attr = array( 'loading' => $loading );
} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
$attr['loading'] = $loading;
} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
$attr .= '&loading=' . $loading;
// Add the default to the given attributes unless they already include a 'loading' directive.
if ( empty( $attr ) ) {
$attr = array( 'loading' => $loading );
} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
$attr['loading'] = $loading;
} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
$attr .= '&loading=' . $loading;
}
}
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

View File

@ -408,6 +408,41 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
$this->assertSame( $expected, $result );
}
/**
* @ticket 57490
*/
public function test_get_the_post_thumbnail_includes_loading_lazy() {
set_post_thumbnail( self::$post, self::$attachment_id );
$html = get_the_post_thumbnail( self::$post );
$this->assertStringContainsString( ' loading="lazy"', $html );
}
/**
* @ticket 57490
*/
public function test_get_the_post_thumbnail_respects_passed_loading_attr() {
set_post_thumbnail( self::$post, self::$attachment_id );
$html = get_the_post_thumbnail( self::$post, 'post-thumbnail', array( 'loading' => 'eager' ) );
$this->assertStringContainsString( ' loading="eager"', $html, 'loading=eager was not present in img tag because attributes array with loading=eager was overwritten.' );
$html = get_the_post_thumbnail( self::$post, 'post-thumbnail', 'loading=eager' );
$this->assertStringContainsString( ' loading="eager"', $html, 'loading=eager was not present in img tag because attributes string with loading=eager was overwritten.' );
}
/**
* @ticket 57490
*/
public function test_get_the_post_thumbnail_respects_wp_lazy_loading_enabled_filter() {
set_post_thumbnail( self::$post, self::$attachment_id );
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
$html = get_the_post_thumbnail( self::$post );
$this->assertStringNotContainsString( ' loading="lazy"', $html );
}
public function data_post_thumbnail_size_filter_complex() {
return array(
array( 0, 'medium' ),