Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the block name as a parameter to the dynamic block render callback #4673

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function render( $attributes = array(), $content = null ) {

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

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

/**
Expand Down
35 changes: 23 additions & 12 deletions phpunit/class-block-type-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ function test_render() {
$block_type = new WP_Block_Type( 'core/dummy', array(
'render_callback' => array( $this, 'render_dummy_block' ),
) );
$output = $block_type->render( $attributes );
$this->assertEquals( $attributes, json_decode( $output, true ) );
$output = json_decode( $block_type->render( $attributes ), true );

$this->assertSame( $attributes, $output['attributes'] );
$this->assertNull( $output['content'] );
$this->assertSame( 'core/dummy', $output['name'] );
}

function test_render_with_content() {
Expand All @@ -43,12 +46,14 @@ function test_render_with_content() {
);
$content = '<p>Test content.</p>';

$block_type = new WP_Block_Type( 'core/dummy', array(
$block_type = new WP_Block_Type( 'core/dummy', array(
'render_callback' => array( $this, 'render_dummy_block_with_content' ),
) );
$output = $block_type->render( $attributes, $content );
$attributes['_content'] = $content;
$this->assertSame( $attributes, json_decode( $output, true ) );
$output = json_decode( $block_type->render( $attributes, $content ), true );

$this->assertSame( $attributes, $output['attributes'] );
$this->assertSame( $content, $output['content'] );
$this->assertSame( 'core/dummy', $output['name'] );
}

function test_render_without_callback() {
Expand Down Expand Up @@ -101,13 +106,19 @@ function test_prepare_attributes() {
), $prepared_attributes );
}

function render_dummy_block( $attributes ) {
return json_encode( $attributes );
function render_dummy_block( $attributes, $content, $name ) {
return json_encode( array(
'attributes' => $attributes,
'content' => $content,
'name' => $name,
) );
}

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

return json_encode( $attributes );
function render_dummy_block_with_content( $attributes, $content, $name ) {
return json_encode( array(
'attributes' => $attributes,
'content' => $content,
'name' => $name,
) );
}
}