From bf0f36b03a79ccdd34cdee68037d12dbba125122 Mon Sep 17 00:00:00 2001
From: Felix Arntz
Date: Thu, 23 Jan 2020 11:14:34 +0100
Subject: [PATCH 1/2] Remove support for iframes.
---
tests/phpunit/tests/media.php | 17 ++++++-----------
wp-lazy-loading.php | 27 +++++----------------------
2 files changed, 11 insertions(+), 33 deletions(-)
diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
index 85e9760..90b7003 100644
--- a/tests/phpunit/tests/media.php
+++ b/tests/phpunit/tests/media.php
@@ -46,7 +46,7 @@ function test_wp_lazy_load_content_media() {
Image, with pre-existing "loading" attribute.
%5$s
- Iframe, standard. Should not be modified by default.
+ Iframe, standard. Should not be modified.
%4$s';
$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager );
@@ -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 = '';
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $lazy_img = str_replace( '
Image, standard.
- %1$s
-
- Iframe, standard.
- %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' );
diff --git a/wp-lazy-loading.php b/wp-lazy-loading.php
index 57ab518..2287a98 100644
--- a/wp-lazy-loading.php
+++ b/wp-lazy-loading.php
@@ -122,10 +122,9 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
// TODO: update docs.
/**
- * Add `loading="lazy"` to `img` and/or `iframe` HTML tags.
+ * Add `loading="lazy"` to `img` HTML tags if enabled.
*
* Currently the "loading" attribute is only supported for `img`, and is enabled by default.
- * Support for `iframe` is for testing purposes only.
*
* @since (TBD)
*
@@ -134,33 +133,17 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
* @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)[^>]+>/',
+ '/
]+>/',
function( array $matches ) {
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
- $tag = $matches[1];
- $space = $matches[2];
+ $space = $matches[1];
- return str_replace( '<' . $tag . $space, '<' . $tag . $space . 'loading="lazy" ', $matches[0] );
+ return str_replace( '
Date: Thu, 23 Jan 2020 13:18:15 -0800
Subject: [PATCH 2/2] Update wp-lazy-loading.php
Add the suggested changes so this branch can be used as base for https://github.com/WordPress/wp-lazy-loading/pull/10.
---
wp-lazy-loading.php | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/wp-lazy-loading.php b/wp-lazy-loading.php
index 2287a98..6bfbca2 100644
--- a/wp-lazy-loading.php
+++ b/wp-lazy-loading.php
@@ -119,31 +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` HTML tags if enabled.
+ * Add `loading="lazy"` to `img` HTML tags.
*
* Currently the "loading" attribute is only supported for `img`, and is enabled by default.
*
* @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 ) {
+ if ( null === $context ) {
+ $context = current_filter();
+ }
+
if ( ! wp_lazy_loading_enabled( 'img', $context ) ) {
return $content;
}
return preg_replace_callback(
- '/
]+>/',
+ '/
]+>/',
function( array $matches ) {
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
- $space = $matches[1];
-
- return str_replace( '![]()