Skip to content
This repository was archived by the owner on Dec 23, 2020. It is now read-only.

Remove support for iframes #11

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function test_wp_lazy_load_content_media() {
<p>Image, with pre-existing "loading" attribute.</p>
%5$s

<p>Iframe, standard. Should not be modified by default.</p>
<p>Iframe, standard. Should not be modified.</p>
%4$s';

$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager );
Expand All @@ -59,21 +59,16 @@ function test_wp_lazy_load_content_media() {
* @ticket 44427
*/
function test_wp_lazy_load_content_media_opted_in() {
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
$iframe = '<iframe src="https://www.example.com"></iframe>';
$img = get_image_tag( self::$large_id, '', '', '', 'medium' );

$lazy_img = str_replace( '<img ', '<img loading="lazy" ', $img );
$lazy_iframe = str_replace( '<iframe ', '<iframe loading="lazy" ', $iframe );
$lazy_img = str_replace( '<img ', '<img loading="lazy" ', $img );

$content = '
<p>Image, standard.</p>
%1$s

<p>Iframe, standard.</p>
%2$s';
%1$s';

$content_unfiltered = sprintf( $content, $img, $iframe );
$content_filtered = sprintf( $content, $lazy_img, $lazy_iframe );
$content_unfiltered = sprintf( $content, $img );
$content_filtered = sprintf( $content, $lazy_img );

// Enable globally for all tags.
add_filter( 'wp_lazy_loading_enabled', '__return_true' );
Expand Down
27 changes: 5 additions & 22 deletions wp-lazy-loading.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,48 +119,31 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
}


// TODO: update docs.
/**
* Add `loading="lazy"` to `img` and/or `iframe` HTML tags.
* Add `loading="lazy"` to `img` HTML tags.
*
* Currently the "loading" attribute is only supported for `img`, and is enabled by default.
* Support for `iframe` is for testing purposes only.
*
* @since (TBD)
*
* @param string $content The raw post content to be filtered.
* @param string $content The HTML content to be filtered.
* @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set.
* @return string Converted content with 'loading' attributes added to images.
*/
function wp_add_lazy_load_attributes( $content, $context = null ) {
$tags = array();

if ( null === $context ) {
$context = current_filter();
}

if ( wp_lazy_loading_enabled( 'img', $context ) ) {
$tags[] = 'img';
}

// Experimental. Will be removed when merging unless the HTML specs are updated by that time.
if ( wp_lazy_loading_enabled( 'iframe', $context ) ) {
$tags[] = 'iframe';
}

if ( empty( $tags ) ) {
if ( ! wp_lazy_loading_enabled( 'img', $context ) ) {
return $content;
}

return preg_replace_callback(
'/<(' . implode( '|', $tags ) . ')(\s)[^>]+>/',
'/<img\s[^>]+>/',
function( array $matches ) {
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
Copy link
Contributor

@azaozz azaozz Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex here should look for \b not \s at the beginning. Same considerations as above, if the img tag was formatted with each attribute starting on a new line, there's a chance that loading won't have a white-space char before it.

Actually, nevermind :) Both \b and \s should work, "word boundary" perhaps being a bit more intuitive.

$tag = $matches[1];
$space = $matches[2];

return str_replace( '<' . $tag . $space, '<' . $tag . $space . 'loading="lazy" ', $matches[0] );
return str_replace( '<img', '<img loading="lazy"', $matches[0] );
}

return $matches[0];
Expand Down