Commit 312da2b826 for wordpress.org

commit 312da2b8266d4c0717de72c0180478c0f4f012db
Author: Peter Wilson <wilson@peterwilson.cc>
Date:   Wed Apr 7 01:01:05 2021 +0000

    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.
    Fixes #52768.


    Built from https://develop.svn.wordpress.org/trunk@50682


    git-svn-id: http://core.svn.wordpress.org/trunk@50291 1a063a9b-81f0-0310-95a4-ce76da25c4cd

diff --git a/wp-includes/media.php b/wp-includes/media.php
index 89b972603b..f5b6a39968 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -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 );
 	}

diff --git a/wp-includes/version.php b/wp-includes/version.php
index 303bb006f7..5196613a2f 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -13,7 +13,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '5.8-alpha-50677';
+$wp_version = '5.8-alpha-50682';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.