diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 487c2765ac249..a4ce0112663fc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2648,6 +2648,9 @@ function _restore_wpautop_hook( $content ) { * @return string The processed content. */ function _wp_apply_block_content_filters( $content, $context = '', &$seen_ids = null, $id = null ) { + global $wp_embed; + $content = $wp_embed->autoembed( $content ); + $content = shortcode_unautop( $content ); $content = do_shortcode( $content ); @@ -2667,9 +2670,6 @@ function _wp_apply_block_content_filters( $content, $context = '', &$seen_ids = $content = convert_smilies( $content ); $content = wp_filter_content_tags( $content, $context ); - global $wp_embed; - $content = $wp_embed->autoembed( $content ); - return $content; } diff --git a/tests/phpunit/tests/blocks/wpApplyBlockContentFilters.php b/tests/phpunit/tests/blocks/wpApplyBlockContentFilters.php index 56ee579dab8ff..344be4436bdb4 100644 --- a/tests/phpunit/tests/blocks/wpApplyBlockContentFilters.php +++ b/tests/phpunit/tests/blocks/wpApplyBlockContentFilters.php @@ -184,6 +184,46 @@ static function () { ); } + /** + * Tests that autoembed runs before do_blocks() so that URLs which only appear + * in a block's rendered output are not turned into embeds. + * + * @ticket 66077 + */ + public function test_autoembed_runs_before_do_blocks() { + wp_embed_register_handler( + self::TEST_EMBED_HANDLER, + '#https?://example\.com/apply-block-content-filters#i', + array( $this, 'render_test_embed' ) + ); + + unregister_block_type( self::TEST_BLOCK_NAME ); + register_block_type( + self::TEST_BLOCK_NAME, + array( + 'render_callback' => static function () { + return "\n\nhttps://example.com/apply-block-content-filters\n\n"; + }, + ) + ); + + $output = _wp_apply_block_content_filters( + '', + 'test-context' + ); + + $this->assertStringNotContainsString( + '
Embedded content
', + $output, + 'WP_Embed::autoembed() should not process URLs that only appear in a block\'s rendered output.' + ); + $this->assertStringContainsString( + 'https://example.com/apply-block-content-filters', + $output, + 'A URL in a block\'s rendered output should be left as-is.' + ); + } + /** * Tests that seen IDs are set during block rendering and cleared afterward. *