diff --git a/src/wp-includes/post-thumbnail-template.php b/src/wp-includes/post-thumbnail-template.php index cee8fa480b..b349cc66e5 100644 --- a/src/wp-includes/post-thumbnail-template.php +++ b/src/wp-includes/post-thumbnail-template.php @@ -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 ); diff --git a/tests/phpunit/tests/post/thumbnails.php b/tests/phpunit/tests/post/thumbnails.php index d170e1bfa5..73199e4b4e 100644 --- a/tests/phpunit/tests/post/thumbnails.php +++ b/tests/phpunit/tests/post/thumbnails.php @@ -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' ),