From 2b28fea7c9c444025874182e9851a94be3f5a77f Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 18 May 2023 19:55:23 +0000 Subject: [PATCH] Media: Prevent special images within post content to skew image counts and cause lazy-loading bugs. In order to skip lazy-loading the first few images on a page, as of WordPress 5.9 there has been logic to count images that are eligible based on certain criteria. One of those groups are images that appear within the content of a post. This changeset fixes a bug where images created via `get_the_post_thumbnail()` or `wp_get_attachment_image()` that are injected into the post content would skew the count and therefore result in all images to be lazy-loaded, potentially hurting load time performance. This is relevant for example when those functions are called in server-side rendered blocks, or any other filter callbacks hooked into `the_content`. Props flixos90, antpb, joedolson, spacedmonkey, mukesh27, thekt12, costdev, jrf. Fixes #58089. See #53675. Built from https://develop.svn.wordpress.org/trunk@55825 git-svn-id: http://core.svn.wordpress.org/trunk@55337 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 10 ++++++++++ wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 111a99d02f..27dd4dfb3a 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -5506,6 +5506,16 @@ function wp_get_loading_attr_default( $context ) { return false; } + /* + * Skip programmatically created images within post content as they need to be handled together with the other + * images within the post content. + * Without this clause, they would already be counted below which skews the number and can result in the first + * post content image being lazy-loaded only because there are images elsewhere in the post content. + */ + if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) && doing_filter( 'the_content' ) ) { + return false; + } + /* * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded, * as they are likely above the fold. diff --git a/wp-includes/version.php b/wp-includes/version.php index b5886d8ab3..3d5ddff634 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55824'; +$wp_version = '6.3-alpha-55825'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.