From 264bbc7054fe4e300cec7fdfcb80d5dd6e574d30 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Tue, 17 Apr 2018 23:13:13 +0100 Subject: [PATCH 1/6] Pass content to block render callback --- lib/blocks.php | 13 ++++++++---- lib/class-wp-block-type.php | 7 ++++--- phpunit/class-block-type-test.php | 8 ++++++++ phpunit/class-dynamic-blocks-render-test.php | 21 ++++++++++---------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index e28971bad032c2..efcc33bd7fbf4b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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 ); } } @@ -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; @@ -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 = '//'; if ( ! preg_match( $end_tag_pattern, $content, $block_match_end, PREG_OFFSET_CAPTURE ) ) { @@ -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 ) ); } } diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 39f08e54fddc28..6ea26822ce10dc 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -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 ''; } $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 ); } /** diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index 8a08cb112dcf28..90b576c664755f 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -57,6 +57,14 @@ 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'] ); + } + function test_prepare_attributes() { $attributes = array( 'correct' => 'include', diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index a642184fc99a5b..559d91e14793a4 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -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; } /** @@ -51,7 +51,7 @@ function tearDown() { } /** - * Test dynamic blocks that lack content, including void blocks. + * Test dynamic blocks, including void blocks. * * @covers ::do_blocks */ @@ -68,21 +68,22 @@ function test_dynamic_block_rendering() { $post_content = 'before' . '' . - '' . + 'hello world' . 'between' . '' . '' . '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 ); } From 1fb516acdafd7d1e8a2fc128810fa1111af6429b Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 18 Apr 2018 15:37:41 +0100 Subject: [PATCH 2/6] Pass block name to render callback --- lib/blocks.php | 4 ++-- lib/class-wp-block-type.php | 7 ++++--- phpunit/class-block-type-test.php | 8 +++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index efcc33bd7fbf4b..16a8d94dd0baa8 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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 ); } } @@ -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 ) ); diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 6ea26822ce10dc..884aefe3f249fe 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -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 ); } /** diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index 90b576c664755f..04347f31721240 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -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() { @@ -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 ); } From dddc95b57c086202bf67f3a2c86ee596c15024b6 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 18 Apr 2018 15:43:02 +0100 Subject: [PATCH 3/6] Revert "Pass block name to render callback" This reverts commit 1fb516acdafd7d1e8a2fc128810fa1111af6429b. --- lib/blocks.php | 4 ++-- lib/class-wp-block-type.php | 7 +++---- phpunit/class-block-type-test.php | 8 +++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 16a8d94dd0baa8..efcc33bd7fbf4b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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, $block_name ); + return $block_type->render( $attributes, $raw_content ); } } @@ -194,7 +194,7 @@ function do_blocks( $content ) { } // Replace dynamic block with server-rendered output. - $rendered_content .= $block_type->render( $attributes, $block_content, $block_name ); + $rendered_content .= $block_type->render( $attributes, $block_content ); if ( ! $is_self_closing ) { $content = substr( $content, $end_offset + strlen( $end_tag ) ); diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 884aefe3f249fe..6ea26822ce10dc 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -95,18 +95,17 @@ 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 $block_name Optional. Block Name. The name of the block. + * @param string $content Optional. Block content. Default empty string. * @return string Rendered block type output. */ - public function render( $attributes = array(), $content = '', $block_name = null ) { + public function render( $attributes = array(), $content = '' ) { if ( ! $this->is_dynamic() ) { return ''; } $attributes = $this->prepare_attributes_for_render( $attributes ); - return (string) call_user_func( $this->render_callback, $attributes, $content, $block_name ); + return (string) call_user_func( $this->render_callback, $attributes, $content ); } /** diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index 04347f31721240..90b576c664755f 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -61,9 +61,8 @@ 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', 'core/dummy' ), true ); + $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() { @@ -108,9 +107,8 @@ function render_dummy_block( $attributes ) { return json_encode( $attributes ); } - function render_dummy_block_with_content( $attributes, $content, $block_name ) { - $attributes['_content'] = $content; - $attributes['_block_name'] = $block_name; + function render_dummy_block_with_content( $attributes, $content ) { + $attributes['_content'] = $content; return json_encode( $attributes ); } From 3e1301c8862ca5a6c20ae1ad4317a4f1ec0ff06b Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 18 Apr 2018 15:46:05 +0100 Subject: [PATCH 4/6] Simpler approach to passing block name --- lib/class-wp-block-type.php | 4 ++-- phpunit/class-block-type-test.php | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 6ea26822ce10dc..9e46e8bc805abf 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -95,7 +95,7 @@ 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. * @return string Rendered block type output. */ public function render( $attributes = array(), $content = '' ) { @@ -105,7 +105,7 @@ public function render( $attributes = array(), $content = '' ) { $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, $this->name ); } /** diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index 90b576c664755f..f5b07c9fd93589 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -63,6 +63,7 @@ function test_dynamic_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() { @@ -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 ); } From 5ba233ada1c4e4da888e0b29be2be3478f312bac Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 18 Apr 2018 16:33:39 +0100 Subject: [PATCH 5/6] render should return content if not dynamic --- lib/class-wp-block-type.php | 2 +- phpunit/class-block-type-test.php | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 9e46e8bc805abf..08a10aab105190 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -100,7 +100,7 @@ public function __construct( $block_type, $args = array() ) { */ public function render( $attributes = array(), $content = '' ) { if ( ! $this->is_dynamic() ) { - return ''; + return $content; } $attributes = $this->prepare_attributes_for_render( $attributes ); diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index f5b07c9fd93589..81b237ae5864fd 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -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() ); From a565be739646893be669870631e784fb32b9fb8f Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Wed, 18 Apr 2018 16:33:59 +0100 Subject: [PATCH 6/6] render callback, pass instance as 3rd param not name --- lib/class-wp-block-type.php | 2 +- phpunit/class-block-type-test.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 08a10aab105190..22f68a2deeadd2 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -105,7 +105,7 @@ public function render( $attributes = array(), $content = '' ) { $attributes = $this->prepare_attributes_for_render( $attributes ); - return (string) call_user_func( $this->render_callback, $attributes, $content, $this->name ); + return (string) call_user_func( $this->render_callback, $attributes, $content, $this ); } /** diff --git a/phpunit/class-block-type-test.php b/phpunit/class-block-type-test.php index 81b237ae5864fd..62cc4b2d8356ec 100644 --- a/phpunit/class-block-type-test.php +++ b/phpunit/class-block-type-test.php @@ -115,9 +115,9 @@ function render_dummy_block( $attributes ) { return json_encode( $attributes ); } - function render_dummy_block_with_content( $attributes, $content, $block_name ) { + function render_dummy_block_with_content( $attributes, $content, $instance ) { $attributes['_content'] = $content; - $attributes['_block_name'] = $block_name; + $attributes['_block_name'] = $instance->name; return json_encode( $attributes ); }