mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Media: Enable lazy-loading of iframes by adding the loading="lazy" attribute to iframe tags on the front-end.
* Expands the capabilities of `wp_filter_content_tags()` to add the attribute to iframe tags if enabled. * Modifies the default behavior of `wp_lazy_loading_enabled()` so that it returns `true` for `iframe` tags. * Introduces a `wp_iframe_tag_add_loading_attr()` function. * Introduces a `wp_iframe_tag_add_loading_attr` filter. Like for images, the attribute is only added to iframes which have both `width` and `height` specified (see related #50367). Props azaozz, flixos90, westonruter. Fixes #50756. git-svn-id: https://develop.svn.wordpress.org/trunk@49808 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2736,25 +2736,29 @@ EOF;
|
||||
/**
|
||||
* @ticket 44427
|
||||
* @ticket 50367
|
||||
* @ticket 50756
|
||||
* @requires function imagejpeg
|
||||
*/
|
||||
function test_wp_filter_content_tags_loading_lazy() {
|
||||
$image_meta = wp_get_attachment_metadata( self::$large_id );
|
||||
$size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
|
||||
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$img_xhtml = str_replace( ' />', '/>', $img );
|
||||
$img_html5 = str_replace( ' />', '>', $img );
|
||||
$img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
|
||||
$img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
|
||||
$iframe = '<iframe src="https://www.example.com"></iframe>';
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$img_xhtml = str_replace( ' />', '/>', $img );
|
||||
$img_html5 = str_replace( ' />', '>', $img );
|
||||
$img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
|
||||
$img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
$iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>';
|
||||
|
||||
$lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
|
||||
$lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' );
|
||||
$lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' );
|
||||
$lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
// The following should not be modified because there already is a 'loading' attribute.
|
||||
$img_eager = str_replace( ' />', ' loading="eager" />', $img );
|
||||
$img_eager = str_replace( ' />', ' loading="eager" />', $img );
|
||||
$iframe_eager = str_replace( '">', '" loading="eager">', $iframe );
|
||||
|
||||
$content = '
|
||||
<p>Image, standard.</p>
|
||||
@@ -2767,11 +2771,15 @@ EOF;
|
||||
%4$s
|
||||
<p>Image, without dimension attributes. Should not be modified.</p>
|
||||
%5$s
|
||||
<p>Iframe, standard. Should not be modified.</p>
|
||||
%6$s';
|
||||
<p>Iframe, standard.</p>
|
||||
%6$s
|
||||
<p>Iframe, with pre-existing "loading" attribute. Should not be modified.</p>
|
||||
%7$s
|
||||
<p>Iframe, without dimension attributes. Should not be modified.</p>
|
||||
%8$s';
|
||||
|
||||
$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe );
|
||||
$content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe );
|
||||
$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
|
||||
$content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height );
|
||||
|
||||
// Do not add width, height, srcset, and sizes.
|
||||
add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
|
||||
@@ -2785,17 +2793,22 @@ EOF;
|
||||
|
||||
/**
|
||||
* @ticket 44427
|
||||
* @ticket 50756
|
||||
*/
|
||||
function test_wp_filter_content_tags_loading_lazy_opted_in() {
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
$lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$content = '
|
||||
<p>Image, standard.</p>
|
||||
%1$s';
|
||||
%1$s
|
||||
<p>Iframe, standard.</p>
|
||||
%2$s';
|
||||
|
||||
$content_unfiltered = sprintf( $content, $img );
|
||||
$content_filtered = sprintf( $content, $lazy_img );
|
||||
$content_unfiltered = sprintf( $content, $img, $iframe );
|
||||
$content_filtered = sprintf( $content, $lazy_img, $lazy_iframe );
|
||||
|
||||
// Do not add srcset and sizes while testing.
|
||||
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
|
||||
@@ -2810,14 +2823,18 @@ EOF;
|
||||
|
||||
/**
|
||||
* @ticket 44427
|
||||
* @ticket 50756
|
||||
*/
|
||||
function test_wp_filter_content_tags_loading_lazy_opted_out() {
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
|
||||
$content = '
|
||||
<p>Image, standard.</p>
|
||||
%1$s';
|
||||
$content = sprintf( $content, $img );
|
||||
%1$s
|
||||
<p>Iframe, standard.</p>
|
||||
%2$s';
|
||||
$content = sprintf( $content, $img, $iframe );
|
||||
|
||||
// Do not add srcset and sizes while testing.
|
||||
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
|
||||
@@ -2878,6 +2895,50 @@ EOF;
|
||||
$this->assertNotContains( ' loading=', $img );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50756
|
||||
*/
|
||||
function test_wp_iframe_tag_add_loading_attr() {
|
||||
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
|
||||
$iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$this->assertContains( ' loading="lazy"', $iframe );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50756
|
||||
*/
|
||||
function test_wp_iframe_tag_add_loading_attr_without_src() {
|
||||
$iframe = '<iframe width="640" height="360"></iframe>';
|
||||
$iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$this->assertNotContains( ' loading=', $iframe );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50756
|
||||
*/
|
||||
function test_wp_iframe_tag_add_loading_attr_with_single_quotes() {
|
||||
$iframe = "<iframe src='https://www.example.com' width='640' height='360'></iframe>";
|
||||
$iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
|
||||
|
||||
$this->assertNotContains( ' loading=', $iframe );
|
||||
|
||||
// Test specifically that the attribute is not there with double-quotes,
|
||||
// to avoid regressions.
|
||||
$this->assertNotContains( ' loading="lazy"', $iframe );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50756
|
||||
*/
|
||||
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' );
|
||||
|
||||
$this->assertNotContains( ' loading=', $iframe );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 44427
|
||||
* @ticket 50425
|
||||
@@ -2918,6 +2979,7 @@ EOF;
|
||||
/**
|
||||
* @ticket 44427
|
||||
* @ticket 50425
|
||||
* @ticket 50756
|
||||
* @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults
|
||||
*
|
||||
* @param string $tag_name Tag name.
|
||||
@@ -2934,7 +2996,7 @@ EOF;
|
||||
function data_wp_lazy_loading_enabled_tag_name_defaults() {
|
||||
return array(
|
||||
'img => true' => array( 'img', true ),
|
||||
'iframe => false' => array( 'iframe', false ),
|
||||
'iframe => true' => array( 'iframe', true ),
|
||||
'arbitrary tag => false' => array( 'blink', false ),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user