Apply the pre_render_block, render_block_data, and render_block_context filters when rendering inner/nested blocks. Introdices another param to these filters: $parent_block that is the "parent" WP_Block instance for nested blocks and null for top level blocks. Adds unit tests for the filters.

Props noisysocks, gaambo, azaozz.
Fixes #51612.

git-svn-id: https://develop.svn.wordpress.org/trunk@51894 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz
2021-10-06 18:47:09 +00:00
parent 411eaea04f
commit ed9f437fc0
3 changed files with 90 additions and 12 deletions

View File

@@ -628,4 +628,44 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
$gradient_support = block_has_support( $block_type, array( 'color', 'gradient' ), true );
$this->assertFalse( $gradient_support );
}
/**
* @ticket 51612
*/
public function test_block_filters_for_inner_blocks() {
$pre_render_callback = new MockAction();
$render_block_data_callback = new MockAction();
$render_block_context_callback = new MockAction();
$this->registry->register(
'core/outer',
array(
'render_callback' => function( $block_attributes, $content ) {
return $content;
},
)
);
$this->registry->register(
'core/inner',
array(
'render_callback' => function() {
return 'b';
},
)
);
$parsed_blocks = parse_blocks( '<!-- wp:outer -->a<!-- wp:inner /-->c<!-- /wp:outer -->' );
$parsed_block = $parsed_blocks[0];
add_filter( 'pre_render_block', array( $pre_render_callback, 'filter' ) );
add_filter( 'render_block_data', array( $render_block_data_callback, 'filter' ) );
add_filter( 'render_block_context', array( $render_block_context_callback, 'filter' ) );
render_block( $parsed_block );
$this->assertSame( 2, $pre_render_callback->get_call_count() );
$this->assertSame( 2, $render_block_data_callback->get_call_count() );
$this->assertSame( 2, $render_block_context_callback->get_call_count() );
}
}