Skip to content

Commit

Permalink
Pass block name to render callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheu committed Apr 18, 2018
1 parent 264bbc7 commit 1fb516a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function gutenberg_render_block( $block ) {
if ( $block_name ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if ( null !== $block_type && $block_type->is_dynamic() ) {
return $block_type->render( $attributes, $raw_content );
return $block_type->render( $attributes, $raw_content, $block_name );
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ function do_blocks( $content ) {
}

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

if ( ! $is_self_closing ) {
$content = substr( $content, $end_offset + strlen( $end_tag ) );
Expand Down
7 changes: 4 additions & 3 deletions lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ public function __construct( $block_type, $args = array() ) {
* @since 0.6.0
*
* @param array $attributes Optional. Block attributes. Default empty array.
* @param string $content Optional. Block content. Default empty string.
* @param string $content Optional. Block content. Default empty string.
* @param string $block_name Optional. Block Name. The name of the block.
* @return string Rendered block type output.
*/
public function render( $attributes = array(), $content = '' ) {
public function render( $attributes = array(), $content = '', $block_name = null ) {
if ( ! $this->is_dynamic() ) {
return '';
}

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

return (string) call_user_func( $this->render_callback, $attributes, $content );
return (string) call_user_func( $this->render_callback, $attributes, $content, $block_name );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions phpunit/class-block-type-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ function test_dynamic_block_with_content() {
$block_type = new WP_Block_Type( 'core/dummy', array(
'render_callback' => array( $this, 'render_dummy_block_with_content' ),
) );
$output = json_decode( $block_type->render( array(), 'hello world' ), true );
$output = json_decode( $block_type->render( array(), 'hello world', 'core/dummy' ), true );
$this->assertEquals( 'hello world', $output['_content'] );
$this->assertEquals( 'core/dummy', $output['_block_name'] );
}

function test_prepare_attributes() {
Expand Down Expand Up @@ -107,8 +108,9 @@ function render_dummy_block( $attributes ) {
return json_encode( $attributes );
}

function render_dummy_block_with_content( $attributes, $content ) {
$attributes['_content'] = $content;
function render_dummy_block_with_content( $attributes, $content, $block_name ) {
$attributes['_content'] = $content;
$attributes['_block_name'] = $block_name;

return json_encode( $attributes );
}
Expand Down

0 comments on commit 1fb516a

Please sign in to comment.