Skip to content

Commit def41d5

Browse files
committed
Pass content to dynamic block render functions
1 parent 08bcffa commit def41d5

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/blocks.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ function do_blocks( $content ) {
128128
$block_attributes_string = $matches['attributes'][ $index ][0];
129129
$block_attributes = parse_block_attributes( $block_attributes_string );
130130

131+
$content = null;
132+
if ( isset( $matches['content'][ $index ][0] ) ) {
133+
$content = $matches['content'][ $index ][0];
134+
}
135+
131136
// Call the block's render function to generate the dynamic output.
132-
$output = call_user_func( $wp_registered_blocks[ $block_name ]['render'], $block_attributes );
137+
$output = call_user_func( $wp_registered_blocks[ $block_name ]['render'], $block_attributes, $content );
133138
}
134139

135140
// Replace the matched block with the static or dynamic output.

phpunit/class-dynamic-blocks-render-test.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
2121
* Dummy block rendering function.
2222
*
2323
* @param array $attributes Block attributes.
24+
* @param array $content Content.
2425
*
2526
* @return string Block output.
2627
*/
27-
function render_dummy_block( $attributes ) {
28+
function render_dummy_block( $attributes, $content ) {
2829
$this->dummy_block_instance_number += 1;
29-
return $this->dummy_block_instance_number . ':' . $attributes['value'];
30+
return $this->dummy_block_instance_number . ':' . $attributes['value'] . ":$content";
3031
}
3132

3233
/**
@@ -64,11 +65,11 @@ function test_dynamic_block_rendering() {
6465
$updated_post_content = do_blocks( $post_content );
6566
$this->assertEquals( $updated_post_content,
6667
'before' .
67-
'1:b1' .
68-
'2:b1' .
68+
'1:b1:' .
69+
'2:b1:' .
6970
'between' .
70-
'3:b2' .
71-
'4:b2' .
71+
'3:b2:' .
72+
'4:b2:' .
7273
'after'
7374
);
7475
}
@@ -88,17 +89,17 @@ function test_dynamic_block_rendering_with_content() {
8889
register_block_type( 'core/dummy', $settings );
8990
$post_content =
9091
'before' .
91-
'<!-- wp:core/dummy value="b1" -->this should be ignored<!-- /wp:core/dummy -->' .
92+
'<!-- wp:core/dummy value="b1" -->content1<!-- /wp:core/dummy -->' .
9293
'between' .
93-
'<!-- wp:core/dummy value="b2" -->this should also be ignored<!-- /wp:core/dummy -->' .
94+
'<!-- wp:core/dummy value="b2" -->content2<!-- /wp:core/dummy -->' .
9495
'after';
9596

9697
$updated_post_content = do_blocks( $post_content );
9798
$this->assertEquals( $updated_post_content,
9899
'before' .
99-
'1:b1' .
100+
'1:b1:content1' .
100101
'between' .
101-
'2:b2' .
102+
'2:b2:content2' .
102103
'after'
103104
);
104105
}

0 commit comments

Comments
 (0)