From ca087ea9bc67f8a064f76d0493db812f14aacd67 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 11:14:44 -0500 Subject: [PATCH 01/22] rebase and squash branch --- lib/blocks.php | 119 +++++++++++++------------------------------------ 1 file changed, 31 insertions(+), 88 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 6ef34c4176eefb..c8e035c42e8a3b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -148,27 +148,39 @@ function get_dynamic_blocks_regex() { * Renders a single block into a HTML string. * * @since 1.9.0 + * @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string + * @global WP_Post $post The post to edit. * * @param array $block A single parsed block object. * @return string String of rendered HTML. */ function gutenberg_render_block( $block ) { - $block_name = isset( $block['blockName'] ) ? $block['blockName'] : null; - $attributes = is_array( $block['attrs'] ) ? $block['attrs'] : array(); - $raw_content = isset( $block['innerHTML'] ) ? $block['innerHTML'] : null; + global $post; - 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 ); - } + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); + $has_blocks = ! empty( $block['innerBlocks'] ); + + if ( $is_dynamic ) { + $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); + $global_post = $post; + $output = $block_type->render( $attributes, $block['innerHTML'] ); + $post = $global_post; + + return $output; } - if ( $raw_content ) { - return $raw_content; + if ( ! $has_blocks ) { + return $block['innerHTML']; } - return ''; + $output = ''; + $index = 0; + foreach ( $block['innerContent'] as $chunk ) { + $output .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); + } + + return $output; } if ( ! function_exists( 'do_blocks' ) ) { @@ -176,91 +188,22 @@ function gutenberg_render_block( $block ) { * Parses dynamic blocks out of `post_content` and re-renders them. * * @since 0.1.0 - * @global WP_Post $post The post to edit. + * @since 4.4.0 performs full parse on input post content * * @param string $content Post content. * @return string Updated post content. */ function do_blocks( $content ) { - global $post; - - $rendered_content = ''; - $dynamic_block_pattern = get_dynamic_blocks_regex(); - - /* - * Back up global post, to restore after render callback. - * Allows callbacks to run new WP_Query instances without breaking the global post. - */ - $global_post = $post; - - while ( preg_match( $dynamic_block_pattern, $content, $block_match, PREG_OFFSET_CAPTURE ) ) { - $opening_tag = $block_match[0][0]; - $offset = $block_match[0][1]; - $block_name = $block_match[1][0]; - $is_self_closing = isset( $block_match[4] ); - - // Reset attributes JSON to prevent scope bleed from last iteration. - $block_attributes_json = null; - if ( isset( $block_match[3] ) ) { - $block_attributes_json = $block_match[3][0]; - } + $without_trailing_newlines = preg_replace( '/()\r?\n?/m', '$1', $content ); + $without_final_newline = preg_replace( '/[\n]$/', '', $without_trailing_newlines ); + $blocks = gutenberg_parse_blocks( $without_final_newline ); + $output = ''; - // Since content is a working copy since the last match, append to - // rendered content up to the matched offset... - $rendered_content .= substr( $content, 0, $offset ); - - // ...then update the working copy of content. - $content = substr( $content, $offset + strlen( $opening_tag ) ); - - // Make implicit core namespace explicit. - $is_implicit_core_namespace = ( false === strpos( $block_name, '/' ) ); - $normalized_block_name = $is_implicit_core_namespace ? 'core/' . $block_name : $block_name; - - // Find registered block type. We can assume it exists since we use the - // `get_dynamic_block_names` function as a source for pattern matching. - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $normalized_block_name ); - - // Attempt to parse attributes JSON, if available. - $attributes = array(); - if ( ! empty( $block_attributes_json ) ) { - $decoded_attributes = json_decode( $block_attributes_json, true ); - if ( ! is_null( $decoded_attributes ) ) { - $attributes = $decoded_attributes; - } - } - - $inner_content = ''; - - if ( ! $is_self_closing ) { - $end_tag_pattern = '//'; - if ( ! preg_match( $end_tag_pattern, $content, $block_match_end, PREG_OFFSET_CAPTURE ) ) { - // If no closing tag is found, abort all matching, and continue - // to append remainder of content to rendered output. - break; - } - - // Update content to omit text up to and including closing tag. - $end_tag = $block_match_end[0][0]; - $end_offset = $block_match_end[0][1]; - - $inner_content = substr( $content, 0, $end_offset ); - $content = substr( $content, $end_offset + strlen( $end_tag ) ); - } - - // Replace dynamic block with server-rendered output. - $rendered_content .= $block_type->render( $attributes, $inner_content ); - - // Restore global $post. - $post = $global_post; + foreach ( $blocks as $block ) { + $output .= gutenberg_render_block( $block ); } - // Append remaining unmatched content. - $rendered_content .= $content; - - // Strip remaining block comment demarcations. - $rendered_content = preg_replace( '/\r?\n?/m', '', $rendered_content ); - - return $rendered_content; + return $output; } add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed. From 62ca3ca4690577d77ae59589791e5eae27de1dd7 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 11:21:02 -0500 Subject: [PATCH 02/22] stick with only removing trailing newline --- lib/blocks.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index c8e035c42e8a3b..364de8ced88d11 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -195,8 +195,7 @@ function gutenberg_render_block( $block ) { */ function do_blocks( $content ) { $without_trailing_newlines = preg_replace( '/()\r?\n?/m', '$1', $content ); - $without_final_newline = preg_replace( '/[\n]$/', '', $without_trailing_newlines ); - $blocks = gutenberg_parse_blocks( $without_final_newline ); + $blocks = gutenberg_parse_blocks( $without_trailing_newlines ); $output = ''; foreach ( $blocks as $block ) { From b8ae16f0b200dfbfc0331fa9ec5f8cd543e8650b Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 12:06:10 -0500 Subject: [PATCH 03/22] remove newline from test --- phpunit/fixtures/do-blocks-expected.html | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit/fixtures/do-blocks-expected.html b/phpunit/fixtures/do-blocks-expected.html index 4a3dc379ef48f9..57fab9ee030c23 100644 --- a/phpunit/fixtures/do-blocks-expected.html +++ b/phpunit/fixtures/do-blocks-expected.html @@ -14,4 +14,3 @@

[someshortcode]

And some content?!

[/someshortcode]

- From c448038c9e07784c06f79aec7346692dbb2635be Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 12:09:48 -0500 Subject: [PATCH 04/22] stop stripping newlines --- lib/blocks.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 364de8ced88d11..c0b9527fc62076 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -194,9 +194,8 @@ function gutenberg_render_block( $block ) { * @return string Updated post content. */ function do_blocks( $content ) { - $without_trailing_newlines = preg_replace( '/()\r?\n?/m', '$1', $content ); - $blocks = gutenberg_parse_blocks( $without_trailing_newlines ); - $output = ''; + $blocks = gutenberg_parse_blocks( $content ); + $output = ''; foreach ( $blocks as $block ) { $output .= gutenberg_render_block( $block ); From cf3d7c3989577d7f8a0c99789ac2ea25bd1ad48c Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 12:44:19 -0500 Subject: [PATCH 05/22] retrigger tests --- phpunit/fixtures/do-blocks-expected.html | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit/fixtures/do-blocks-expected.html b/phpunit/fixtures/do-blocks-expected.html index 57fab9ee030c23..99fa29b6f9019d 100644 --- a/phpunit/fixtures/do-blocks-expected.html +++ b/phpunit/fixtures/do-blocks-expected.html @@ -2,6 +2,7 @@ +

First Gutenberg Paragraph

Second Auto Paragraph

From d8f203cfef7be101033df41ad05519ca8ee9a5ba Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 12:55:17 -0500 Subject: [PATCH 06/22] update tests --- phpunit/class-do-blocks-test.php | 2 +- phpunit/fixtures/do-blocks-expected.html | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/phpunit/class-do-blocks-test.php b/phpunit/class-do-blocks-test.php index 9da7ddc0fcb27d..784164b1092cf9 100644 --- a/phpunit/class-do-blocks-test.php +++ b/phpunit/class-do-blocks-test.php @@ -30,7 +30,7 @@ function test_the_content() { add_shortcode( 'someshortcode', array( $this, 'handle_shortcode' ) ); $classic_content = "Foo\n\n[someshortcode]\n\nBar\n\n[/someshortcode]\n\nBaz"; - $block_content = "\n

Foo

\n\n\n[someshortcode]\n\nBar\n\n[/someshortcode]\n\n\n

Baz

\n"; + $block_content = "

Foo

\n\n\n[someshortcode]\n\nBar\n\n[/someshortcode]\n\n\n

Baz

\n"; $classic_filtered_content = apply_filters( 'the_content', $classic_content ); $block_filtered_content = apply_filters( 'the_content', $block_content ); diff --git a/phpunit/fixtures/do-blocks-expected.html b/phpunit/fixtures/do-blocks-expected.html index 99fa29b6f9019d..f4131820513347 100644 --- a/phpunit/fixtures/do-blocks-expected.html +++ b/phpunit/fixtures/do-blocks-expected.html @@ -5,13 +5,18 @@

First Gutenberg Paragraph

+

Second Auto Paragraph

+ +

Third Gutenberg Paragraph

+

Third Auto Paragraph

[someshortcode]

And some content?!

[/someshortcode]

+ From f74a80615afbc67c96d3a4f003f28b21fc1a34e7 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 14:29:54 -0500 Subject: [PATCH 07/22] prerender inner blocks before rendering outer dynamic blocks --- lib/blocks.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index c0b9527fc62076..949801c46dfd46 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -162,9 +162,20 @@ function gutenberg_render_block( $block ) { $has_blocks = ! empty( $block['innerBlocks'] ); if ( $is_dynamic ) { - $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); + $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); + $inner_content = ''; + + if ( $has_blocks ) { + $index = 0; + foreach ( $block['innerContent'] as $chunk ) { + $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); + } + } else { + $inner_content = $block['innerHTML']; + } + $global_post = $post; - $output = $block_type->render( $attributes, $block['innerHTML'] ); + $output = $block_type->render( $attributes, $inner_content ); $post = $global_post; return $output; From 447d5c9ee31c8da400cf4f68e2771f61fb8764bb Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 14:46:31 -0500 Subject: [PATCH 08/22] formating --- lib/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 949801c46dfd46..0a23c045bfabc9 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -166,14 +166,14 @@ function gutenberg_render_block( $block ) { $inner_content = ''; if ( $has_blocks ) { - $index = 0; + $index = 0; foreach ( $block['innerContent'] as $chunk ) { $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); } } else { $inner_content = $block['innerHTML']; } - + $global_post = $post; $output = $block_type->render( $attributes, $inner_content ); $post = $global_post; From 981e5ef25e5c4cbfc124e50c2d0469b7de56f8d0 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 15:26:55 -0500 Subject: [PATCH 09/22] add test for dynamic block behavior --- phpunit/class-dynamic-blocks-render-test.php | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index 812d6aa8d21e1f..2ec3142ab68234 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -38,6 +38,10 @@ function render_dummy_block_numeric() { return 10; } + function render_serialize_dynamic_block( $attributes, $content ) { + return base64_encode( serialize( array( $attributes, $content ) ) ); + } + /** * Dummy block rendering function, creating a new WP_Query instance. * @@ -164,4 +168,45 @@ function test_dynamic_block_renders_string() { $this->assertSame( '10', $rendered ); $this->assertInternalType( 'string', $rendered ); } + + function test_dynamic_block_gets_inner_html() { + register_block_type( 'core/dynamic', array( + 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), + ) ); + + $output = do_blocks( 'inner' ); + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + + $this->assertEqual( $content, 'inner' ); + } + + function test_dynamic_block_gets_rendered_inner_blocks() { + register_block_type( 'core/dummy', array( + 'render_callback' => array( $this, 'render_dummy_block_numeric' ), + ) ); + register_block_type( 'core/dynamic', array( + 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), + ) ); + + $output = do_blocks( 'beforeafter' ); + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + + $this->assertEqual( $content, 'before10after' ); + } + + function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { + register_block_type( 'core/dynamic', array( + 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), + ) ); + + $output = do_blocks( 'beforedeep innerafter' ); + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + + $this->assertStringStartsWith( 'before', $content ); + $this->assertStringEndWith( 'after', $content ); + + list( /* attrs */, $content ) = unserialize( base64_decode( $content ) ); + + $this->assertEqual( $content, 'deep inner' ); + } } From 3d5af34f27de2f9ddf543e7ee4ede29f331ca292 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 15:30:49 -0500 Subject: [PATCH 10/22] fix broken test --- phpunit/class-dynamic-blocks-render-test.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index 2ec3142ab68234..efb7d54e20934c 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -202,11 +202,8 @@ function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { $output = do_blocks( 'beforedeep innerafter' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $this->assertStringStartsWith( 'before', $content ); - $this->assertStringEndWith( 'after', $content ); + $inner = $this->render_serialize_dynamic_block( [], 'deep inner' ); - list( /* attrs */, $content ) = unserialize( base64_decode( $content ) ); - - $this->assertEqual( $content, 'deep inner' ); + $this->assertEqual( $content, 'before' . $inner . 'after' ); } } From 1d01cb64992501ad0fc20119d55252fe83153eb2 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 15:37:37 -0500 Subject: [PATCH 11/22] tests fail --- phpunit/class-dynamic-blocks-render-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index efb7d54e20934c..06259c5b142de9 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -177,7 +177,7 @@ function test_dynamic_block_gets_inner_html() { $output = do_blocks( 'inner' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $this->assertEqual( $content, 'inner' ); + $this->assertEqual( 'inner', $content ); } function test_dynamic_block_gets_rendered_inner_blocks() { @@ -191,7 +191,7 @@ function test_dynamic_block_gets_rendered_inner_blocks() { $output = do_blocks( 'beforeafter' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $this->assertEqual( $content, 'before10after' ); + $this->assertEqual( 'before10after', $content ); } function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { From c285144cbdd3eafe45ebd18523a9ec2eb5f5e4e1 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 15:37:57 -0500 Subject: [PATCH 12/22] tests fail --- phpunit/class-dynamic-blocks-render-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index 06259c5b142de9..c25efea47415ec 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -177,7 +177,7 @@ function test_dynamic_block_gets_inner_html() { $output = do_blocks( 'inner' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $this->assertEqual( 'inner', $content ); + $this->assertEquals( 'inner', $content ); } function test_dynamic_block_gets_rendered_inner_blocks() { @@ -191,7 +191,7 @@ function test_dynamic_block_gets_rendered_inner_blocks() { $output = do_blocks( 'beforeafter' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $this->assertEqual( 'before10after', $content ); + $this->assertEquals( 'before10after', $content ); } function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { @@ -204,6 +204,6 @@ function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { $inner = $this->render_serialize_dynamic_block( [], 'deep inner' ); - $this->assertEqual( $content, 'before' . $inner . 'after' ); + $this->assertEquals( $content, 'before' . $inner . 'after' ); } } From eb2bae352545f7b296bfaf695eaaac6e4270cdf3 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 16:26:56 -0500 Subject: [PATCH 13/22] linting fix --- phpunit/class-dynamic-blocks-render-test.php | 49 ++++++++++++++------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index c25efea47415ec..7dda23fc93263a 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -170,9 +170,14 @@ function test_dynamic_block_renders_string() { } function test_dynamic_block_gets_inner_html() { - register_block_type( 'core/dynamic', array( - 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), - ) ); + register_block_type( + 'core/dynamic', array( + 'render_callback' => array( + $this, + 'render_serialize_dynamic_block' + ), + ) + ); $output = do_blocks( 'inner' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); @@ -181,12 +186,24 @@ function test_dynamic_block_gets_inner_html() { } function test_dynamic_block_gets_rendered_inner_blocks() { - register_block_type( 'core/dummy', array( - 'render_callback' => array( $this, 'render_dummy_block_numeric' ), - ) ); - register_block_type( 'core/dynamic', array( - 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), - ) ); + register_block_type( + 'core/dummy', + array( + 'render_callback' => array( + $this, + 'render_dummy_block_numeric' + ), + ) + ); + register_block_type( + 'core/dynamic', + array( + 'render_callback' => array( + $this, + 'render_serialize_dynamic_block' + ), + ) + ); $output = do_blocks( 'beforeafter' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); @@ -195,14 +212,20 @@ function test_dynamic_block_gets_rendered_inner_blocks() { } function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { - register_block_type( 'core/dynamic', array( - 'render_callback' => array( $this, 'render_serialize_dynamic_block' ), - ) ); + register_block_type( + 'core/dynamic', + array( + 'render_callback' => array( + $this, + 'render_serialize_dynamic_block' + ), + ) + ); $output = do_blocks( 'beforedeep innerafter' ); list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); - $inner = $this->render_serialize_dynamic_block( [], 'deep inner' ); + $inner = $this->render_serialize_dynamic_block( array(), 'deep inner' ); $this->assertEquals( $content, 'before' . $inner . 'after' ); } From b186f142cd2ef3abb52cab09c70becdd24110185 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 16:44:35 -0500 Subject: [PATCH 14/22] simplify render function --- lib/blocks.php | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 0a23c045bfabc9..1c0bcf9079e41d 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -157,22 +157,17 @@ function get_dynamic_blocks_regex() { function gutenberg_render_block( $block ) { global $post; - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); - $has_blocks = ! empty( $block['innerBlocks'] ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); + $inner_content = ''; + $index = 0; + + foreach ( $block['innerContent'] as $chunk ) { + $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); + } if ( $is_dynamic ) { $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); - $inner_content = ''; - - if ( $has_blocks ) { - $index = 0; - foreach ( $block['innerContent'] as $chunk ) { - $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); - } - } else { - $inner_content = $block['innerHTML']; - } $global_post = $post; $output = $block_type->render( $attributes, $inner_content ); @@ -181,17 +176,7 @@ function gutenberg_render_block( $block ) { return $output; } - if ( ! $has_blocks ) { - return $block['innerHTML']; - } - - $output = ''; - $index = 0; - foreach ( $block['innerContent'] as $chunk ) { - $output .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); - } - - return $output; + return $inner_content; } if ( ! function_exists( 'do_blocks' ) ) { From ed8f372624de77e2b60109987677ca592eb3b271 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 16:56:29 -0500 Subject: [PATCH 15/22] linting fix --- lib/blocks.php | 3 +-- phpunit/class-dynamic-blocks-render-test.php | 15 ++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 1c0bcf9079e41d..5ae0349f87ba95 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -167,8 +167,7 @@ function gutenberg_render_block( $block ) { } if ( $is_dynamic ) { - $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); - + $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); $global_post = $post; $output = $block_type->render( $attributes, $inner_content ); $post = $global_post; diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index 7dda23fc93263a..a7f1d29a18f8ae 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -171,7 +171,8 @@ function test_dynamic_block_renders_string() { function test_dynamic_block_gets_inner_html() { register_block_type( - 'core/dynamic', array( + 'core/dynamic', + array( 'render_callback' => array( $this, 'render_serialize_dynamic_block' @@ -179,8 +180,8 @@ function test_dynamic_block_gets_inner_html() { ) ); - $output = do_blocks( 'inner' ); - list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + $output = do_blocks( 'inner' ); + $content = unserialize( base64_decode( $output ) )[1]; $this->assertEquals( 'inner', $content ); } @@ -205,8 +206,8 @@ function test_dynamic_block_gets_rendered_inner_blocks() { ) ); - $output = do_blocks( 'beforeafter' ); - list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + $output = do_blocks( 'beforeafter' ); + $content = unserialize( base64_decode( $output ) )[ 1 ]; $this->assertEquals( 'before10after', $content ); } @@ -222,8 +223,8 @@ function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { ) ); - $output = do_blocks( 'beforedeep innerafter' ); - list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); + $output = do_blocks( 'beforedeep innerafter' ); + $content = unserialize( base64_decode( $output ) )[ 1 ]; $inner = $this->render_serialize_dynamic_block( array(), 'deep inner' ); From db516f4a09d7e63f3c079e049e3329c98b870604 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 17:06:28 -0500 Subject: [PATCH 16/22] have it your way, phpcs --- phpunit/class-dynamic-blocks-render-test.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index a7f1d29a18f8ae..f5d6d127a59587 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -180,8 +180,9 @@ function test_dynamic_block_gets_inner_html() { ) ); - $output = do_blocks( 'inner' ); - $content = unserialize( base64_decode( $output ) )[1]; + $output = do_blocks( 'inner' ); + + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); $this->assertEquals( 'inner', $content ); } @@ -206,8 +207,9 @@ function test_dynamic_block_gets_rendered_inner_blocks() { ) ); - $output = do_blocks( 'beforeafter' ); - $content = unserialize( base64_decode( $output ) )[ 1 ]; + $output = do_blocks( 'beforeafter' ); + + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); $this->assertEquals( 'before10after', $content ); } @@ -223,8 +225,9 @@ function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { ) ); - $output = do_blocks( 'beforedeep innerafter' ); - $content = unserialize( base64_decode( $output ) )[ 1 ]; + $output = do_blocks( 'beforedeep innerafter' ); + + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); $inner = $this->render_serialize_dynamic_block( array(), 'deep inner' ); From 12cbd125fb524714b9c681f22153f5ac0eec1d47 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 20:11:05 -0500 Subject: [PATCH 17/22] linting fix --- phpunit/class-dynamic-blocks-render-test.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index f5d6d127a59587..ab52c448feb577 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -175,7 +175,7 @@ function test_dynamic_block_gets_inner_html() { array( 'render_callback' => array( $this, - 'render_serialize_dynamic_block' + 'render_serialize_dynamic_block', ), ) ); @@ -193,7 +193,7 @@ function test_dynamic_block_gets_rendered_inner_blocks() { array( 'render_callback' => array( $this, - 'render_dummy_block_numeric' + 'render_dummy_block_numeric', ), ) ); @@ -202,7 +202,7 @@ function test_dynamic_block_gets_rendered_inner_blocks() { array( 'render_callback' => array( $this, - 'render_serialize_dynamic_block' + 'render_serialize_dynamic_block', ), ) ); @@ -220,13 +220,13 @@ function test_dynamic_block_gets_rendered_inner_dynamic_blocks() { array( 'render_callback' => array( $this, - 'render_serialize_dynamic_block' + 'render_serialize_dynamic_block', ), ) ); $output = do_blocks( 'beforedeep innerafter' ); - + list( /* attrs */, $content ) = unserialize( base64_decode( $output ) ); $inner = $this->render_serialize_dynamic_block( array(), 'deep inner' ); From 890d53798f73959143152567d82bfe65f05ca191 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 20:20:01 -0500 Subject: [PATCH 18/22] also unregister that one --- phpunit/class-dynamic-blocks-render-test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index ab52c448feb577..09509100cdaf1d 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -79,6 +79,7 @@ function tearDown() { $registry = WP_Block_Type_Registry::get_instance(); $registry->unregister( 'core/dummy' ); + $registry->unregister( 'core/dynamic' ); } /** From 58f33daeabf01e9d730fcf96dc6b7e9ae502f294 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 20:31:06 -0500 Subject: [PATCH 19/22] conditionally unregister --- phpunit/class-dynamic-blocks-render-test.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/phpunit/class-dynamic-blocks-render-test.php b/phpunit/class-dynamic-blocks-render-test.php index 09509100cdaf1d..35799bc9bf73db 100644 --- a/phpunit/class-dynamic-blocks-render-test.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -78,8 +78,14 @@ function tearDown() { $this->dummy_block_instance_number = 0; $registry = WP_Block_Type_Registry::get_instance(); - $registry->unregister( 'core/dummy' ); - $registry->unregister( 'core/dynamic' ); + + if ( $registry->is_registered( 'core/dummy' ) ) { + $registry->unregister( 'core/dummy' ); + } + + if ( $registry->is_registered( 'core/dynamic' ) ) { + $registry->unregister( 'core/dynamic' ); + } } /** From 7309d7f3dbabcc29833b6f2d891ae2617ebcbf3e Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 20:48:40 -0500 Subject: [PATCH 20/22] remove unjustified optimization --- lib/blocks.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 5ae0349f87ba95..a8083466a04053 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -58,21 +58,6 @@ function unregister_block_type( $name ) { * @return array Array of parsed block objects. */ function gutenberg_parse_blocks( $content ) { - /* - * If there are no blocks in the content, return a single block, rather - * than wasting time trying to parse the string. - */ - if ( ! has_blocks( $content ) ) { - return array( - array( - 'blockName' => null, - 'attrs' => array(), - 'innerBlocks' => array(), - 'innerHTML' => $content, - ), - ); - } - /** * Filter to allow plugins to replace the server-side block parser * From 9186d8b238d4e02cb1d1dfa5f7a1ffce9f441d05 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 21:40:09 -0500 Subject: [PATCH 21/22] toss in some new tests --- phpunit/class-do-blocks-test.php | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/phpunit/class-do-blocks-test.php b/phpunit/class-do-blocks-test.php index 784164b1092cf9..5d44da9e86ae94 100644 --- a/phpunit/class-do-blocks-test.php +++ b/phpunit/class-do-blocks-test.php @@ -9,6 +9,19 @@ * Test do_blocks */ class Do_Blocks_Test extends WP_UnitTestCase { + /** + * Tear down. + */ + function tearDown() { + parent::tearDown(); + + $registry = WP_Block_Type_Registry::get_instance(); + + if ( $registry->is_registered( 'core/dummy' ) ) { + $registry->unregister( 'core/dummy' ); + } + } + /** * Test do_blocks removes comment demarcations. * @@ -41,7 +54,43 @@ function test_the_content() { $this->assertEquals( $classic_filtered_content, $block_filtered_content ); } + function test_can_nest_at_least_so_deep() { + $minimum_depth = 99; + + $content = 'deep inside'; + for ( $i = 0; $i < $minimum_depth; $i++ ) { + $content = '' . $content . ''; + } + + $this->assertEquals( 'deep inside', do_blocks( $content ) ); + } + + function test_can_nest_at_least_so_deep_with_dynamic_blocks() { + $minimum_depth = 99; + + $content = '0'; + for ( $i = 0; $i < $minimum_depth; $i++ ) { + $content = '' . $content . ''; + } + + register_block_type( + 'core/dummy', + array( + 'render_callback' => array( + $this, + 'render_dynamic_incrementer', + ) + ) + ); + + $this->assertEquals( $minimum_depth, (int) do_blocks( $content ) ); + } + function handle_shortcode( $atts, $content ) { return $content; } + + function render_dynamic_incrementer( $attrs, $content ) { + return (string) ( 1 + (int) $content ); + } } From fff5c1c54e605c0da291c8f960ff63fb8a8d727d Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 9 Nov 2018 21:49:12 -0500 Subject: [PATCH 22/22] add the blimey comma --- phpunit/class-do-blocks-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-do-blocks-test.php b/phpunit/class-do-blocks-test.php index 5d44da9e86ae94..147545e65ca0a2 100644 --- a/phpunit/class-do-blocks-test.php +++ b/phpunit/class-do-blocks-test.php @@ -79,7 +79,7 @@ function test_can_nest_at_least_so_deep_with_dynamic_blocks() { 'render_callback' => array( $this, 'render_dynamic_incrementer', - ) + ), ) );