From 70e0a22e7ddd2a68ec9bb6b2f81c3e39b7d5f7e8 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 26 Sep 2023 00:13:12 +0000 Subject: [PATCH] Media: Ensure images within shortcodes are correctly considered for loading optimization attributes. Prior to this change, images added in shortcodes would be considered separately from all other images within post content, which led to incorrect application of the loading optimization attributes `loading="lazy"` and `fetchpriority="high"`. This changeset changes the filter priority of `wp_filter_content_tags()` from the default `10` to `12` on the various content filters it is hooked in, in order to run that function after parsing shortcodes. While this may technically be considered a backward compatibility break, substantial research and lack of any relevant usage led to the assessment that the change is acceptable given its benefits. An additional related fix included is that now the duplicate processing of images is prevented not only for post content blobs (`the_content` filter), but also for widget content blobs (`widget_text_content` and `widget_block_content` filters). Props joemcgill, mukesh27, costdev, spacedmonkey, flixos90. Fixes #58853. Built from https://develop.svn.wordpress.org/trunk@56693 git-svn-id: http://core.svn.wordpress.org/trunk@56205 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/default-filters.php | 12 +++++------- wp-includes/formatting.php | 4 ++-- wp-includes/media.php | 12 ++++++++---- wp-includes/version.php | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 1a20c919d2..adba75fcd7 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -195,16 +195,17 @@ add_filter( 'the_content', 'convert_smilies', 20 ); add_filter( 'the_content', 'wpautop' ); add_filter( 'the_content', 'shortcode_unautop' ); add_filter( 'the_content', 'prepend_attachment' ); -add_filter( 'the_content', 'wp_filter_content_tags' ); add_filter( 'the_content', 'wp_replace_insecure_home_url' ); +add_filter( 'the_content', 'do_shortcode', 11 ); // AFTER wpautop(). +add_filter( 'the_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode(). add_filter( 'the_excerpt', 'wptexturize' ); add_filter( 'the_excerpt', 'convert_smilies' ); add_filter( 'the_excerpt', 'convert_chars' ); add_filter( 'the_excerpt', 'wpautop' ); add_filter( 'the_excerpt', 'shortcode_unautop' ); -add_filter( 'the_excerpt', 'wp_filter_content_tags' ); add_filter( 'the_excerpt', 'wp_replace_insecure_home_url' ); +add_filter( 'the_excerpt', 'wp_filter_content_tags', 12 ); add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 ); add_filter( 'the_post_thumbnail_caption', 'wptexturize' ); @@ -230,13 +231,13 @@ add_filter( 'widget_text_content', 'wptexturize' ); add_filter( 'widget_text_content', 'convert_smilies', 20 ); add_filter( 'widget_text_content', 'wpautop' ); add_filter( 'widget_text_content', 'shortcode_unautop' ); -add_filter( 'widget_text_content', 'wp_filter_content_tags' ); add_filter( 'widget_text_content', 'wp_replace_insecure_home_url' ); add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run. +add_filter( 'widget_text_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode(). add_filter( 'widget_block_content', 'do_blocks', 9 ); -add_filter( 'widget_block_content', 'wp_filter_content_tags' ); add_filter( 'widget_block_content', 'do_shortcode', 11 ); +add_filter( 'widget_block_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode(). add_filter( 'block_type_metadata', 'wp_migrate_old_typography_shape' ); @@ -625,9 +626,6 @@ add_action( 'change_locale', 'create_initial_taxonomies' ); add_action( 'template_redirect', 'redirect_canonical' ); add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 ); -// Shortcodes. -add_filter( 'the_content', 'do_shortcode', 11 ); // AFTER wpautop(). - // Media. add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' ); diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 76f77c2a90..f94d3170a5 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -3980,7 +3980,7 @@ function wp_trim_excerpt( $text = '', $post = null ) { * within the excerpt are stripped out. Modifying the tags here * is wasteful and can lead to bugs in the image counting logic. */ - $filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags' ); + $filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 ); /* * Temporarily unhook do_blocks() since excerpt_remove_blocks( $text ) @@ -4003,7 +4003,7 @@ function wp_trim_excerpt( $text = '', $post = null ) { * which is generally used for the filter callback in WordPress core. */ if ( $filter_image_removed ) { - add_filter( 'the_content', 'wp_filter_content_tags' ); + add_filter( 'the_content', 'wp_filter_content_tags', 12 ); } /* translators: Maximum number of words used in a post excerpt. */ diff --git a/wp-includes/media.php b/wp-includes/media.php index 13fa8e9930..988086e009 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -5649,16 +5649,20 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { } /* - * Skip programmatically created images within post content as they need to be handled together with the other - * images within the post content. + * Skip programmatically created images within content blobs as they need to be handled together with the other + * images within the post content or widget content. * Without this clause, they would already be considered within their own context which skews the image count and * can result in the first post content image being lazy-loaded or an image further down the page being marked as a * high priority. */ - // TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853). - if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) { + if ( + 'the_content' !== $context && doing_filter( 'the_content' ) || + 'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) || + 'widget_block_content' !== $context && doing_filter( 'widget_block_content' ) + ) { /** This filter is documented in wp-includes/media.php */ return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); + } /* diff --git a/wp-includes/version.php b/wp-includes/version.php index 9cf213f3c6..f461397d37 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56692'; +$wp_version = '6.4-alpha-56693'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.