Blocks: Allow for nested the_content calls within do_blocks().

When `do_blocks()` is run, it sets up some special handling of the `wpautop` filter, as we don't want `wpautop` to run on block content, but we do want it to be available for subsequent runs of `the_content`, which may be happening on non-block content.

As we set this up before rendering dynamic blocks, however, a dynamic block choosing to run `the_content` will cause unintentially structural deficiences in this particular recursive block tower.

Moving this handling to after dynamic blocks are rendered makes our tower lean a little less.

Props aldavigdis, pento.
Fixes #45495.



git-svn-id: https://develop.svn.wordpress.org/trunk@45139 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2019-04-08 06:53:14 +00:00
parent c9bb62507f
commit edd6a1f129
2 changed files with 33 additions and 7 deletions

View File

@ -262,13 +262,6 @@ function parse_blocks( $content ) {
* @return string Updated post content.
*/
function do_blocks( $content ) {
// If there are blocks in this content, we shouldn't run wpautop() on it later.
$priority = has_filter( 'the_content', 'wpautop' );
if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
remove_filter( 'the_content', 'wpautop', $priority );
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
}
$blocks = parse_blocks( $content );
$output = '';
@ -276,6 +269,13 @@ function do_blocks( $content ) {
$output .= render_block( $block );
}
// If there are blocks in this content, we shouldn't run wpautop() on it later.
$priority = has_filter( 'the_content', 'wpautop' );
if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
remove_filter( 'the_content', 'wpautop', $priority );
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
}
return $output;
}

View File

@ -87,6 +87,32 @@ class WP_Test_Block_Render extends WP_UnitTestCase {
return $content;
}
/**
* @ticket 45495
*/
function test_nested_calls_to_the_content() {
register_block_type(
'core/test',
array(
'render_callback' => array(
$this,
'dynamic_the_content_call',
),
)
);
$content = "foo\n\nbar";
$the_content = apply_filters( 'the_content', '<!-- wp:core/test -->' . $content . '<!-- /wp:core/test -->' );
$this->assertSame( $content, $the_content );
}
function dynamic_the_content_call( $attrs, $content ) {
apply_filters( 'the_content', '' );
return $content;
}
public function test_can_nest_at_least_so_deep() {
$minimum_depth = 99;