mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 21:08:44 +01:00
Media: Do not lazy load hidden images or embeds.
Improve the check for sourceless or dimensionless media when determining if the lazy loading attribute should be added to iframes and images. Never include the lazy loading attribute on embeds of WordPress posts as the iframe is initially hidden. Including `loading="lazy"` on initially hidden iframes and images can prevent the media from loading in some browsers. Props adamsilverstein, fabianpimminger, flixos90, johnbillion, jonkastonka, joyously, peterwilsoncc, SergeyBiryukov, SirStuey, swissspidy. Merges [50682], [50683] to the 5.7 branch. Fixes #52768. git-svn-id: https://develop.svn.wordpress.org/branches/5.7@50684 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8b25cd6995
commit
8bf7643c4b
@ -1869,6 +1869,11 @@ function wp_filter_content_tags( $content, $context = null ) {
|
||||
* @return string Converted `img` tag with `loading` attribute added.
|
||||
*/
|
||||
function wp_img_tag_add_loading_attr( $image, $context ) {
|
||||
// Images should have source and dimension attributes for the `loading` attribute to be added.
|
||||
if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the `loading` attribute value to add to an image. Default `lazy`.
|
||||
*
|
||||
@ -1889,11 +1894,6 @@ function wp_img_tag_add_loading_attr( $image, $context ) {
|
||||
$value = 'lazy';
|
||||
}
|
||||
|
||||
// Images should have source and dimension attributes for the `loading` attribute to be added.
|
||||
if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
|
||||
}
|
||||
|
||||
@ -1989,6 +1989,17 @@ function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id
|
||||
* @return string Converted `iframe` tag with `loading` attribute added.
|
||||
*/
|
||||
function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
|
||||
// Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are
|
||||
// visually hidden initially.
|
||||
if ( false !== strpos( $iframe, ' data-secret="' ) ) {
|
||||
return $iframe;
|
||||
}
|
||||
|
||||
// Iframes should have source and dimension attributes for the `loading` attribute to be added.
|
||||
if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
|
||||
return $iframe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the `loading` attribute value to add to an iframe. Default `lazy`.
|
||||
*
|
||||
@ -2009,11 +2020,6 @@ function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
|
||||
$value = 'lazy';
|
||||
}
|
||||
|
||||
// Iframes should have source and dimension attributes for the `loading` attribute to be added.
|
||||
if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
|
||||
return $iframe;
|
||||
}
|
||||
|
||||
return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe );
|
||||
}
|
||||
|
||||
|
@ -2935,6 +2935,19 @@ EOF;
|
||||
function test_wp_iframe_tag_add_loading_attr_opt_out() {
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
add_filter( 'wp_iframe_tag_add_loading_attr', '__return_false' );
|
||||
$iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$this->assertNotContains( ' loading=', $iframe );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52768
|
||||
*/
|
||||
function test_wp_iframe_tag_add_loading_attr_skip_wp_embed() {
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
$fallback = '<blockquote>Fallback content.</blockquote>';
|
||||
$iframe = wp_filter_oembed_result( $fallback . $iframe, (object) array( 'type' => 'rich' ), 'https://www.example.com' );
|
||||
$iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$this->assertNotContains( ' loading=', $iframe );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user