Media: Improve support for opting out of lazy-loading for template images.

With this changeset, in addition to the already present `wp_lazy_loading_enabled` filter, developers can now opt out of lazy-loading template images via `wp_get_attachment_image()` by passing a `loading` attribute with boolean value `false`. This can be used e.g. by theme developers on images which are very likely to be in the initial viewport.

This changeset also improves related test coverage.

Props adamsilverstein, azaozz, joemcgill, johnbillion.
See #50425, #44427.


git-svn-id: https://develop.svn.wordpress.org/trunk@48272 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2020-07-02 02:01:28 +00:00
parent 46f51c4bc1
commit 284b92b122
2 changed files with 126 additions and 4 deletions

View File

@ -1048,6 +1048,12 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
$attr = wp_parse_args( $attr, $default_attr );
// If `loading` attribute default of `lazy` is overridden for this
// image to omit the attribute, ensure it is not included.
if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
unset( $attr['loading'] );
}
// Generate 'srcset' and 'sizes' if not already present.
if ( empty( $attr['srcset'] ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
@ -1725,9 +1731,10 @@ function wp_img_tag_add_loading_attr( $image, $context ) {
*
* @since 5.5.0
*
* @param string $value The `loading` attribute value, defaults to `lazy`.
* @param string $image The HTML `img` tag to be filtered.
* @param string $context Additional context about how the function was called or where the img tag is.
* @param string|bool $value The `loading` attribute value. Returning a false-y value will result in the
* attribute being omitted for the image. Default is `lazy`.
* @param string $image The HTML `img` tag to be filtered.
* @param string $context Additional context about how the function was called or where the img tag is.
*/
$value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );

View File

@ -2675,24 +2675,139 @@ EOF;
}
/**
* @ticket 44427
* @ticket 50367
*/
function test_wp_img_tag_add_loading_attr() {
$img = '<img src="example.png" alt=" width="300" height="225" />';
$img = wp_img_tag_add_loading_attr( $img, 'test' );
$this->assertContains( ' loading="lazy"', $img );
}
/**
* @ticket 44427
* @ticket 50367
*/
function test_wp_img_tag_add_loading_attr_without_src() {
$img = '<img alt=" width="300" height="225" />';
$img = wp_img_tag_add_loading_attr( $img, 'test' );
$this->assertNotContains( ' loading="lazy"', $img );
$this->assertNotContains( ' loading=', $img );
}
/**
* @ticket 44427
* @ticket 50367
*/
function test_wp_img_tag_add_loading_attr_with_single_quotes() {
$img = "<img src='example.png' alt=' width='300' height='225' />";
$img = wp_img_tag_add_loading_attr( $img, 'test' );
$this->assertNotContains( ' loading=', $img );
// Test specifically that the attribute is not there with double-quotes,
// to avoid regressions.
$this->assertNotContains( ' loading="lazy"', $img );
}
/**
* @ticket 44427
* @ticket 50425
*/
function test_wp_img_tag_add_loading_attr_opt_out() {
$img = '<img src="example.png" alt=" width="300" height="225" />';
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
$this->assertNotContains( ' loading=', $img );
}
/**
* @ticket 44427
* @ticket 50425
*/
function test_wp_get_attachment_image_loading() {
$img = wp_get_attachment_image( self::$large_id );
$this->assertContains( ' loading="lazy"', $img );
}
/**
* @ticket 44427
* @ticket 50425
*/
function test_wp_get_attachment_image_loading_opt_out() {
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
$img = wp_get_attachment_image( self::$large_id );
// There should not be any loading attribute in this case.
$this->assertNotContains( ' loading=', $img );
}
/**
* @ticket 44427
* @ticket 50425
*/
function test_wp_get_attachment_image_loading_opt_out_individual() {
// The default is already tested above, the filter below ensures that
// lazy-loading is definitely enabled globally for images.
add_filter( 'wp_lazy_loading_enabled', '__return_true' );
$img = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'loading' => false ) );
// There should not be any loading attribute in this case.
$this->assertNotContains( ' loading=', $img );
}
/**
* @ticket 44427
* @ticket 50425
* @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults
*
* @param string $tag_name Function context.
* @param bool $expected Expected return value.
*/
function test_wp_lazy_loading_enabled_tag_name_defaults( $tag_name, $expected ) {
if ( $expected ) {
$this->assertTrue( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
} else {
$this->assertFalse( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
}
}
function data_wp_lazy_loading_enabled_tag_name_defaults() {
return array(
'img => true' => array( 'img', true ),
'iframe => false' => array( 'iframe', false ),
'arbitrary tag => false' => array( 'blink', false ),
);
}
/**
* @ticket 50425
* @dataProvider data_wp_lazy_loading_enabled_context_defaults
*
* @param string $context Function context.
* @param bool $expected Expected return value.
*/
function test_wp_lazy_loading_enabled_context_defaults( $context, $expected ) {
if ( $expected ) {
$this->assertTrue( wp_lazy_loading_enabled( 'img', $context ) );
} else {
$this->assertFalse( wp_lazy_loading_enabled( 'img', $context ) );
}
}
function data_wp_lazy_loading_enabled_context_defaults() {
return array(
'wp_get_attachment_image => true' => array( 'wp_get_attachment_image', true ),
'the_content => true' => array( 'the_content', true ),
'the_excerpt => true' => array( 'the_excerpt', true ),
'widget_text_content => true' => array( 'widget_text_content', true ),
'get_avatar => true' => array( 'get_avatar', true ),
'arbitrary context => true' => array( 'something_completely_arbitrary', true ),
);
}
}
/**