mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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:
parent
84bb54d618
commit
65caf62944
@ -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 );
|
||||
|
||||
@ -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' ),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user