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 content to block render callback #6239

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
13 changes: 9 additions & 4 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 );
return $block_type->render( $attributes, $raw_content );
}
}

Expand Down Expand Up @@ -146,6 +146,7 @@ function do_blocks( $content ) {
$offset = $block_match[0][1];
$block_name = $block_match[1][0];
$is_self_closing = isset( $block_match[4] );
$block_content = '';

// Reset attributes JSON to prevent scope bleed from last iteration.
$block_attributes_json = null;
Expand Down Expand Up @@ -177,9 +178,6 @@ function do_blocks( $content ) {
}
}

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

if ( ! $is_self_closing ) {
$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', preg_quote( $block_name ) ) . '\s+-->/';
if ( ! preg_match( $end_tag_pattern, $content, $block_match_end, PREG_OFFSET_CAPTURE ) ) {
Expand All @@ -192,6 +190,13 @@ function do_blocks( $content ) {
$end_tag = $block_match_end[0][0];
$end_offset = $block_match_end[0][1];

$block_content = substr( $content, 0, $end_offset );
}

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

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

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

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

/**
Expand Down
23 changes: 20 additions & 3 deletions phpunit/class-block-type-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ function test_render() {
$this->assertEquals( $attributes, json_decode( $output, true ) );
}

function test_render_for_static_block() {
function test_render_for_static_block_default() {
$block_type = new WP_Block_Type( 'core/dummy', array() );
$output = $block_type->render();

$this->assertEquals( '', $output );
}

function test_render_for_static_block_with_content() {
$block_type = new WP_Block_Type( 'core/dummy', array() );
$output = $block_type->render( array(), 'some dummy content' );

$this->assertEquals( 'some dummy content', $output );
}

function test_is_dynamic_for_static_block() {
$block_type = new WP_Block_Type( 'core/dummy', array() );

Expand All @@ -57,6 +64,15 @@ function test_is_dynamic_for_dynamic_block() {
$this->assertTrue( $block_type->is_dynamic() );
}

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 );
$this->assertEquals( 'hello world', $output['_content'] );
$this->assertEquals( 'core/dummy', $output['_block_name'] );
}

function test_prepare_attributes() {
$attributes = array(
'correct' => 'include',
Expand Down Expand Up @@ -99,8 +115,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, $instance ) {
$attributes['_content'] = $content;
$attributes['_block_name'] = $instance->name;

return json_encode( $attributes );
}
Expand Down
21 changes: 11 additions & 10 deletions phpunit/class-dynamic-blocks-render-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Dynamic_Blocks_Render_Test extends WP_UnitTestCase {
*
* @return string Block output.
*/
function render_dummy_block( $attributes ) {
function render_dummy_block( $attributes, $content = '' ) {
$this->dummy_block_instance_number += 1;
return $this->dummy_block_instance_number . ':' . $attributes['value'];
return $this->dummy_block_instance_number . ':' . $attributes['value'] . ':' . $content;
}

/**
Expand All @@ -51,7 +51,7 @@ function tearDown() {
}

/**
* Test dynamic blocks that lack content, including void blocks.
* Test dynamic blocks, including void blocks.
*
* @covers ::do_blocks
*/
Expand All @@ -68,21 +68,22 @@ function test_dynamic_block_rendering() {
$post_content =
'before' .
'<!-- wp:core/dummy {"value":"b1"} --><!-- /wp:core/dummy -->' .
'<!-- wp:core/dummy {"value":"b1"} --><!-- /wp:core/dummy -->' .
'<!-- wp:core/dummy {"value":"b1"} -->hello world<!-- /wp:core/dummy -->' .
'between' .
'<!-- wp:core/dummy {"value":"b2"} /-->' .
'<!-- wp:core/dummy {"value":"b2"} /-->' .
'after';

$updated_post_content = do_blocks( $post_content );
$this->assertEquals( $updated_post_content,
$this->assertEquals(
'before' .
'1:b1' .
'2:b1' .
'1:b1:' .
'2:b1:hello world' .
'between' .
'3:b2' .
'4:b2' .
'after'
'3:b2:' .
'4:b2:' .
'after',
$updated_post_content
);
}

Expand Down