Skip to content

Commit

Permalink
Media: Ensure images within shortcodes are correctly considered for l…
Browse files Browse the repository at this point in the history
…oading 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.


git-svn-id: https://develop.svn.wordpress.org/trunk@56693 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Sep 26, 2023
1 parent 0444cf5 commit 6560f46
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 63 deletions.
12 changes: 5 additions & 7 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,17 @@
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' );
Expand All @@ -230,13 +231,13 @@
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' );

Expand Down Expand Up @@ -625,9 +626,6 @@
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' );
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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. */
Expand Down
12 changes: 8 additions & 4 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

/*
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/formatting/wpTrimExcerpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function test_wp_trim_excerpt_should_not_permanently_unhook_wp_filter_con

wp_trim_excerpt( '', $post );

$this->assertSame( 10, has_filter( 'the_content', 'wp_filter_content_tags' ), 'wp_filter_content_tags() was not restored in wp_trim_excerpt()' );
$this->assertSame( 12, has_filter( 'the_content', 'wp_filter_content_tags' ), 'wp_filter_content_tags() was not restored in wp_trim_excerpt()' );
}

/**
Expand All @@ -141,7 +141,7 @@ public function test_wp_trim_excerpt_does_not_restore_wp_filter_content_tags_if_
$post = self::factory()->post->create();

// Remove wp_filter_content_tags() from 'the_content' filter generally.
remove_filter( 'the_content', 'wp_filter_content_tags' );
remove_filter( 'the_content', 'wp_filter_content_tags', 12 );

wp_trim_excerpt( '', $post );

Expand Down
Loading

0 comments on commit 6560f46

Please sign in to comment.