Skip to content

Commit 1fb516a

Browse files
committed
Pass block name to render callback
1 parent 264bbc7 commit 1fb516a

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/blocks.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function gutenberg_render_block( $block ) {
104104
if ( $block_name ) {
105105
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
106106
if ( null !== $block_type && $block_type->is_dynamic() ) {
107-
return $block_type->render( $attributes, $raw_content );
107+
return $block_type->render( $attributes, $raw_content, $block_name );
108108
}
109109
}
110110

@@ -194,7 +194,7 @@ function do_blocks( $content ) {
194194
}
195195

196196
// Replace dynamic block with server-rendered output.
197-
$rendered_content .= $block_type->render( $attributes, $block_content );
197+
$rendered_content .= $block_type->render( $attributes, $block_content, $block_name );
198198

199199
if ( ! $is_self_closing ) {
200200
$content = substr( $content, $end_offset + strlen( $end_tag ) );

lib/class-wp-block-type.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,18 @@ public function __construct( $block_type, $args = array() ) {
9595
* @since 0.6.0
9696
*
9797
* @param array $attributes Optional. Block attributes. Default empty array.
98-
* @param string $content Optional. Block content. Default empty string.
98+
* @param string $content Optional. Block content. Default empty string.
99+
* @param string $block_name Optional. Block Name. The name of the block.
99100
* @return string Rendered block type output.
100101
*/
101-
public function render( $attributes = array(), $content = '' ) {
102+
public function render( $attributes = array(), $content = '', $block_name = null ) {
102103
if ( ! $this->is_dynamic() ) {
103104
return '';
104105
}
105106

106107
$attributes = $this->prepare_attributes_for_render( $attributes );
107108

108-
return (string) call_user_func( $this->render_callback, $attributes, $content );
109+
return (string) call_user_func( $this->render_callback, $attributes, $content, $block_name );
109110
}
110111

111112
/**

phpunit/class-block-type-test.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ function test_dynamic_block_with_content() {
6161
$block_type = new WP_Block_Type( 'core/dummy', array(
6262
'render_callback' => array( $this, 'render_dummy_block_with_content' ),
6363
) );
64-
$output = json_decode( $block_type->render( array(), 'hello world' ), true );
64+
$output = json_decode( $block_type->render( array(), 'hello world', 'core/dummy' ), true );
6565
$this->assertEquals( 'hello world', $output['_content'] );
66+
$this->assertEquals( 'core/dummy', $output['_block_name'] );
6667
}
6768

6869
function test_prepare_attributes() {
@@ -107,8 +108,9 @@ function render_dummy_block( $attributes ) {
107108
return json_encode( $attributes );
108109
}
109110

110-
function render_dummy_block_with_content( $attributes, $content ) {
111-
$attributes['_content'] = $content;
111+
function render_dummy_block_with_content( $attributes, $content, $block_name ) {
112+
$attributes['_content'] = $content;
113+
$attributes['_block_name'] = $block_name;
112114

113115
return json_encode( $attributes );
114116
}

0 commit comments

Comments
 (0)