From a9b1993d23c78be4d6d851e17fcfa1fb5d0648ec Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 26 Apr 2023 15:19:17 +0200 Subject: [PATCH 01/39] Insert a string after each comment-content block --- lib/experimental/blocks.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index a33d2f6b980123..ebe38b93a257ef 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -100,3 +100,31 @@ function gutenberg_register_metadata_attribute( $args ) { return $args; } add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); + +/** + * Auto-insert blocks relative to a given block. + * + * @param string $block_content The block content. + * @param array $block The full block, including name and attributes. + * @param WP_Block $instance The block instance. + */ +function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { + $block_name = 'core/comment-content'; + $block_position = 'after'; // Child blocks could be a bit trickier. + + // TODO: Parse actually inserted block. + $inserted_content = 'LIKE'; + + // Can we void infinite loops? + + if ( $block['blockName'] === $block_name ) { + if ( 'before' === $block_position ) { + $block_content = $inserted_content . $block_content; + } elseif ( 'after' === $block_position ) { + $block_content = $block_content . $inserted_content; + } + } + + return $block_content; +} +add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 3 ); From 6ac30c1fd65e265685f4474dcfc9585661cf6ea5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 26 Apr 2023 15:49:56 +0200 Subject: [PATCH 02/39] Use an actual block --- lib/experimental/blocks.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index ebe38b93a257ef..b7788c3a154e37 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -112,12 +112,15 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { $block_name = 'core/comment-content'; $block_position = 'after'; // Child blocks could be a bit trickier. - // TODO: Parse actually inserted block. - $inserted_content = 'LIKE'; - // Can we void infinite loops? if ( $block['blockName'] === $block_name ) { + $inserted_block_markup = ' + + '; + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); + if ( 'before' === $block_position ) { $block_content = $inserted_content . $block_content; } elseif ( 'after' === $block_position ) { From 03ebf7e3d06f923039706c5a3f097909408751cc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 26 Apr 2023 17:03:38 +0200 Subject: [PATCH 03/39] Start implementing basic last-child mechanism --- lib/experimental/blocks.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index b7788c3a154e37..cb77c906594245 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -101,6 +101,23 @@ function gutenberg_register_metadata_attribute( $args ) { } add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); +/** + * Filters the block being rendered in render_block(), before it's processed. + * + * @param array $parsed_block The block being rendered. + * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. + */ +function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $parent_block ) { + // first or last child + // of comment-template + if ( isset( $parent_block ) ) { + $parsed_block['parentBlock'] = $parent_block->name; + } + return $parsed_block; +} +add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 3 ); + /** * Auto-insert blocks relative to a given block. * @@ -109,11 +126,21 @@ function gutenberg_register_metadata_attribute( $args ) { * @param WP_Block $instance The block instance. */ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { - $block_name = 'core/comment-content'; + $block_name = 'core/post-content'; $block_position = 'after'; // Child blocks could be a bit trickier. // Can we void infinite loops? + if ( 'core/comment-template' === $block['parentBlock'] ) { + $inserted_block_markup = ' + + '; + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); + + $block_content = $block_content . $inserted_content; // after! + } + if ( $block['blockName'] === $block_name ) { $inserted_block_markup = ' From 9382109e72c886ce1cac6c2c2bd4cbec1ac591de Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 26 Apr 2023 17:29:53 +0200 Subject: [PATCH 04/39] Bit of polish --- lib/experimental/blocks.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index cb77c906594245..108486613f6bde 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -131,10 +131,12 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { // Can we void infinite loops? - if ( 'core/comment-template' === $block['parentBlock'] ) { - $inserted_block_markup = ' - - '; + if ( isset( $block['parentBlock'] ) && 'core/comment-template' === $block['parentBlock'] ) { + $inserted_block_markup = << + + +END; $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); @@ -142,9 +144,11 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { } if ( $block['blockName'] === $block_name ) { - $inserted_block_markup = ' - - '; + $inserted_block_markup = << + + +END; $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); From 01de9dc17bb95fc16f616c77890af5b7aad0a92e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 26 Apr 2023 17:34:42 +0200 Subject: [PATCH 05/39] More polish --- lib/experimental/blocks.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 108486613f6bde..a6f43ee685155d 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -109,8 +109,6 @@ function gutenberg_register_metadata_attribute( $args ) { * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $parent_block ) { - // first or last child - // of comment-template if ( isset( $parent_block ) ) { $parsed_block['parentBlock'] = $parent_block->name; } @@ -131,25 +129,21 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { // Can we void infinite loops? - if ( isset( $block['parentBlock'] ) && 'core/comment-template' === $block['parentBlock'] ) { - $inserted_block_markup = << END; - $inserted_blocks = parse_blocks( $inserted_block_markup ); + + if ( isset( $block['parentBlock'] ) && 'core/comment-template' === $block['parentBlock'] ) { + $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); $block_content = $block_content . $inserted_content; // after! } if ( $block['blockName'] === $block_name ) { - $inserted_block_markup = << - - -END; - $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); if ( 'before' === $block_position ) { From df6cf73e65a4e92bebdb44358e8d1455eb55372d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 27 Apr 2023 14:26:41 +0200 Subject: [PATCH 06/39] More polish --- lib/experimental/blocks.php | 40 +++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index a6f43ee685155d..60796e0425a03c 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -124,28 +124,46 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren * @param WP_Block $instance The block instance. */ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { - $block_name = 'core/post-content'; - $block_position = 'after'; // Child blocks could be a bit trickier. + // $block_name = 'core/post-content'; + // $block_position = 'after'; // Child blocks could be a bit trickier. + + $block_name = 'core/comment-template'; + $block_position = 'last-child'; // Can we void infinite loops? + if ( + ! ( + $block_name === $block['blockName'] && + ( 'before' === $block_position || 'after' === $block_position ) + ) && ! ( + isset( $block['parentBlock'] ) && + $block_name === $block['parentBlock'] && + ( 'first-child' === $block_position || 'last-child' === $block_position ) + ) + ) { + return $block_content; + } + + $inserted_block_markup = << END; - - if ( isset( $block['parentBlock'] ) && 'core/comment-template' === $block['parentBlock'] ) { - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); - - $block_content = $block_content . $inserted_content; // after! + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); + + if ( isset( $block['parentBlock'] ) && $block_name === $block['parentBlock'] ) { + if ( 'last-child' === $block_position ) { + // FIXME: This is currently apppending the auto-inserted block + // after each child of the parent block, rather than only after + // the last one. + $block_content = $block_content . $inserted_content; + } } if ( $block['blockName'] === $block_name ) { - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); - if ( 'before' === $block_position ) { $block_content = $inserted_content . $block_content; } elseif ( 'after' === $block_position ) { From 9db02c386bb4ed62d797a89d9be57e532c58b723 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 27 Apr 2023 14:28:24 +0200 Subject: [PATCH 07/39] Consistency --- lib/experimental/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 60796e0425a03c..3d7b8f3290d6c6 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -163,7 +163,7 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { } } - if ( $block['blockName'] === $block_name ) { + if ( $block_name === $block['blockName'] ) { if ( 'before' === $block_position ) { $block_content = $inserted_content . $block_content; } elseif ( 'after' === $block_position ) { From 9a46647d176dd01209d392c83debcef3888289e2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 2 May 2023 14:14:56 +0600 Subject: [PATCH 08/39] Formatting --- lib/experimental/blocks.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 3d7b8f3290d6c6..30ebd28e87bca7 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -121,13 +121,12 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren * * @param string $block_content The block content. * @param array $block The full block, including name and attributes. - * @param WP_Block $instance The block instance. */ -function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { - // $block_name = 'core/post-content'; +function gutenberg_auto_insert_blocks( $block_content, $block ) { + // $block_name = 'core/post-content'; // $block_position = 'after'; // Child blocks could be a bit trickier. - $block_name = 'core/comment-template'; + $block_name = 'core/comment-template'; $block_position = 'last-child'; // Can we void infinite loops? @@ -145,12 +144,12 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { return $block_content; } - $inserted_block_markup = << END; + $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); @@ -173,4 +172,4 @@ function gutenberg_auto_insert_blocks( $block_content, $block, $instance ) { return $block_content; } -add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 3 ); +add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 2 ); From d1b0225684ff17239a8c3957fa96f61726ed7b3d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 2 May 2023 18:57:01 +0600 Subject: [PATCH 09/39] Auto-insert Avatar block under comments --- lib/experimental/blocks.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 30ebd28e87bca7..018ae7d2cc1497 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -144,11 +144,7 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { return $block_content; } - $inserted_block_markup = << - - -END; + $inserted_block_markup = ''; $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); From 6f36442a5a230031a5a5296786c1f8d745dba16a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:33:51 +0200 Subject: [PATCH 10/39] Use render_block_data filter to insert child block --- lib/experimental/blocks.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 018ae7d2cc1497..ec737b3ce4a0a2 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -109,8 +109,18 @@ function gutenberg_register_metadata_attribute( $args ) { * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $parent_block ) { - if ( isset( $parent_block ) ) { - $parsed_block['parentBlock'] = $parent_block->name; + if ( 'core/comment-template' === $parsed_block['blockName'] ) { + $inserted_block_markup = << + +' +END; + $inserted_blocks = parse_blocks( $inserted_block_markup ); + + $parsed_block['innerBlocks'][] = $inserted_blocks[0]; + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to append the new block to that array. + $parsed_block['innerContent'][] = $inserted_blocks[0]; } return $parsed_block; } From 02883ef3469aee0b70486cd797a9e093a13890fe Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:48:14 +0200 Subject: [PATCH 11/39] Make child block insertion more flexible --- lib/experimental/blocks.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index ec737b3ce4a0a2..753589fd3ceddb 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -102,14 +102,18 @@ function gutenberg_register_metadata_attribute( $args ) { add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); /** - * Filters the block being rendered in render_block(), before it's processed. + * Auto-insert a block as another block's first or last inner block. * * @param array $parsed_block The block being rendered. * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $parent_block ) { - if ( 'core/comment-template' === $parsed_block['blockName'] ) { + // TODO: Implement an API for users to set the following two parameters. + $block_name = 'core/comment-template'; + $block_position = 'last-child'; + + if ( $block_name === $parsed_block['blockName'] ) { $inserted_block_markup = << @@ -117,10 +121,17 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren END; $inserted_blocks = parse_blocks( $inserted_block_markup ); - $parsed_block['innerBlocks'][] = $inserted_blocks[0]; - // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to append the new block to that array. - $parsed_block['innerContent'][] = $inserted_blocks[0]; + if ( 'first-child' === $block_position ) { + array_unshift( $parsed_block['innerBlocks'], $inserted_blocks[0] ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to prepend the new block to that array. + array_unshift( $parsed_block['innerContent'], $inserted_blocks[0] ); + } elseif ( 'last-child' === $block_position ) { + array_push( $parsed_block['innerBlocks'], $inserted_blocks[0] ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to append the new block to that array. + array_push( $parsed_block['innerContent'], $inserted_blocks[0] ); + } } return $parsed_block; } From 8bd8c6dcdbe88ef25b9bd229e1559efc267c3310 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:48:30 +0200 Subject: [PATCH 12/39] Remove child block insertion from render_block hook --- lib/experimental/blocks.php | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 753589fd3ceddb..19c11e9a02f79e 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -148,19 +148,13 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { // $block_position = 'after'; // Child blocks could be a bit trickier. $block_name = 'core/comment-template'; - $block_position = 'last-child'; + $block_position = 'after'; // Can we void infinite loops? if ( - ! ( - $block_name === $block['blockName'] && - ( 'before' === $block_position || 'after' === $block_position ) - ) && ! ( - isset( $block['parentBlock'] ) && - $block_name === $block['parentBlock'] && - ( 'first-child' === $block_position || 'last-child' === $block_position ) - ) + $block_name !== $block['blockName'] || + ! in_array( $block_position, array( 'before', 'after' ), true ) ) { return $block_content; } @@ -170,15 +164,6 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); - if ( isset( $block['parentBlock'] ) && $block_name === $block['parentBlock'] ) { - if ( 'last-child' === $block_position ) { - // FIXME: This is currently apppending the auto-inserted block - // after each child of the parent block, rather than only after - // the last one. - $block_content = $block_content . $inserted_content; - } - } - if ( $block_name === $block['blockName'] ) { if ( 'before' === $block_position ) { $block_content = $inserted_content . $block_content; From e1fde473188c264d6019b4135110eae4e3a55f03 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:49:12 +0200 Subject: [PATCH 13/39] Simplify render_block logic --- lib/experimental/blocks.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 19c11e9a02f79e..14e7f25060032c 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -152,19 +152,12 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { // Can we void infinite loops? - if ( - $block_name !== $block['blockName'] || - ! in_array( $block_position, array( 'before', 'after' ), true ) - ) { - return $block_content; - } - - $inserted_block_markup = ''; + if ( $block_name === $block['blockName'] ) { + $inserted_block_markup = ''; - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); - if ( $block_name === $block['blockName'] ) { if ( 'before' === $block_position ) { $block_content = $inserted_content . $block_content; } elseif ( 'after' === $block_position ) { From 9e36ff103787f0f9bf308e32ef8bcfef94854d59 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:49:49 +0200 Subject: [PATCH 14/39] Comment typo --- lib/experimental/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 14e7f25060032c..459fc6342d8ba1 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -150,7 +150,7 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { $block_name = 'core/comment-template'; $block_position = 'after'; - // Can we void infinite loops? + // Can we avoid infinite loops? if ( $block_name === $block['blockName'] ) { $inserted_block_markup = ''; From 118819264919867e1c05b52ca81750de057ff193 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:50:42 +0200 Subject: [PATCH 15/39] Switch places --- lib/experimental/blocks.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 459fc6342d8ba1..63e105a42e9070 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -114,11 +114,7 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren $block_position = 'last-child'; if ( $block_name === $parsed_block['blockName'] ) { - $inserted_block_markup = << - -' -END; + $inserted_block_markup = ''; $inserted_blocks = parse_blocks( $inserted_block_markup ); if ( 'first-child' === $block_position ) { @@ -153,7 +149,11 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { // Can we avoid infinite loops? if ( $block_name === $block['blockName'] ) { - $inserted_block_markup = ''; + $inserted_block_markup = << + +' +END; $inserted_blocks = parse_blocks( $inserted_block_markup ); $inserted_content = render_block( $inserted_blocks[0] ); From e8161e82dd3bd7a9b19a491206047d70fb394313 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:53:02 +0200 Subject: [PATCH 16/39] Insert Social Icon after Post Content --- lib/experimental/blocks.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 63e105a42e9070..bb9f3523932c8d 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -140,10 +140,7 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren * @param array $block The full block, including name and attributes. */ function gutenberg_auto_insert_blocks( $block_content, $block ) { - // $block_name = 'core/post-content'; - // $block_position = 'after'; // Child blocks could be a bit trickier. - - $block_name = 'core/comment-template'; + $block_name = 'core/post-content'; $block_position = 'after'; // Can we avoid infinite loops? From 05f3bd4196c7881b48aae5b60dea65ade1afba34 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:53:33 +0200 Subject: [PATCH 17/39] Add comment to render_block hook --- lib/experimental/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index bb9f3523932c8d..f93f082f4c31c8 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -140,6 +140,7 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren * @param array $block The full block, including name and attributes. */ function gutenberg_auto_insert_blocks( $block_content, $block ) { + // TODO: Implement an API for users to set the following two parameters. $block_name = 'core/post-content'; $block_position = 'after'; From db0434d4b7818f4110bf512b1dc798498e602801 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:55:39 +0200 Subject: [PATCH 18/39] Remove now-obsolete filter args --- lib/experimental/blocks.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index f93f082f4c31c8..ce2d1896410299 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -105,10 +105,8 @@ function gutenberg_register_metadata_attribute( $args ) { * Auto-insert a block as another block's first or last inner block. * * @param array $parsed_block The block being rendered. - * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. - * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. */ -function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $parent_block ) { +function gutenberg_auto_insert_child_block( $parsed_block ) { // TODO: Implement an API for users to set the following two parameters. $block_name = 'core/comment-template'; $block_position = 'last-child'; @@ -131,7 +129,7 @@ function gutenberg_auto_insert_child_block( $parsed_block, $source_block, $paren } return $parsed_block; } -add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 3 ); +add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); /** * Auto-insert blocks relative to a given block. From 829806e70bc2e67349df1a764a029027a28ab92a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 15:57:26 +0200 Subject: [PATCH 19/39] A bit nicer still --- lib/experimental/blocks.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index ce2d1896410299..2d90d6a94bcdc1 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -113,18 +113,19 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { if ( $block_name === $parsed_block['blockName'] ) { $inserted_block_markup = ''; - $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_block = $inserted_blocks[0]; if ( 'first-child' === $block_position ) { - array_unshift( $parsed_block['innerBlocks'], $inserted_blocks[0] ); + array_unshift( $parsed_block['innerBlocks'], $inserted_block ); // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) // when rendering blocks, we also need to prepend the new block to that array. - array_unshift( $parsed_block['innerContent'], $inserted_blocks[0] ); + array_unshift( $parsed_block['innerContent'], $inserted_block ); } elseif ( 'last-child' === $block_position ) { - array_push( $parsed_block['innerBlocks'], $inserted_blocks[0] ); + array_push( $parsed_block['innerBlocks'], $inserted_block ); // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) // when rendering blocks, we also need to append the new block to that array. - array_push( $parsed_block['innerContent'], $inserted_blocks[0] ); + array_push( $parsed_block['innerContent'], $inserted_block ); } } return $parsed_block; From da7290d31b49be96f38a797064339875ddb7ec57 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 16:00:44 +0200 Subject: [PATCH 20/39] Manually create inserted block --- lib/experimental/blocks.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 2d90d6a94bcdc1..6103913b08fb72 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -112,9 +112,18 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { $block_position = 'last-child'; if ( $block_name === $parsed_block['blockName'] ) { - $inserted_block_markup = ''; - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_block = $inserted_blocks[0]; + // parse_blocks( '' )[0] + $inserted_block = array( + 'blockName' => 'core/avatar', + 'attrs' => array( + 'size' => 40, + 'style' => array( + 'border' => array( 'radius' => '10px' ), + ), + ), + 'innerHTML' => '', + 'innerContent' => array(), + ); if ( 'first-child' === $block_position ) { array_unshift( $parsed_block['innerBlocks'], $inserted_block ); From 4076c4ee5cff2d82f66ce7b0aedc92f4ad0114ef Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 16:29:37 +0200 Subject: [PATCH 21/39] Formatting --- lib/experimental/blocks.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 6103913b08fb72..0711d12dde1fe7 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -104,7 +104,7 @@ function gutenberg_register_metadata_attribute( $args ) { /** * Auto-insert a block as another block's first or last inner block. * - * @param array $parsed_block The block being rendered. + * @param array $parsed_block The block being rendered. */ function gutenberg_auto_insert_child_block( $parsed_block ) { // TODO: Implement an API for users to set the following two parameters. @@ -112,11 +112,11 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { $block_position = 'last-child'; if ( $block_name === $parsed_block['blockName'] ) { - // parse_blocks( '' )[0] + // parse_blocks( '' )[0]. $inserted_block = array( 'blockName' => 'core/avatar', 'attrs' => array( - 'size' => 40, + 'size' => 40, 'style' => array( 'border' => array( 'radius' => '10px' ), ), @@ -144,8 +144,8 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { /** * Auto-insert blocks relative to a given block. * - * @param string $block_content The block content. - * @param array $block The full block, including name and attributes. + * @param string $block_content The block content. + * @param array $block The full block, including name and attributes. */ function gutenberg_auto_insert_blocks( $block_content, $block ) { // TODO: Implement an API for users to set the following two parameters. From 97fbbaedb168632fb89dfc98d8dfa45c833bb2aa Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 May 2023 17:14:31 +0200 Subject: [PATCH 22/39] Fix unit test --- phpunit/blocks/render-comment-template-test.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpunit/blocks/render-comment-template-test.php b/phpunit/blocks/render-comment-template-test.php index c297d1729d0dc5..19f453fc185940 100644 --- a/phpunit/blocks/render-comment-template-test.php +++ b/phpunit/blocks/render-comment-template-test.php @@ -142,6 +142,9 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() { return $parsed_block; }; + // Remove auto-insertion filter so it won't collide. + remove_filter( 'render_block_data', 'gutenberg_auto_insert_child_block' ); + add_filter( 'render_block_data', $render_block_data_callback, 10, 1 ); $parsed_blocks = parse_blocks( '' @@ -154,6 +157,8 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() { ); $block->render(); remove_filter( 'render_block_data', $render_block_data_callback ); + // Add back auto-insertion filter. + add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); $this->assertSame( 5, $render_block_callback->get_call_count() ); From 8b78c3411d32d3fd63cc0bc3891af2dafbd416c6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 1 Jun 2023 12:54:05 +0200 Subject: [PATCH 23/39] A bit easier on the eye --- lib/experimental/blocks.php | 51 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 0711d12dde1fe7..0504a37c9eedc0 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -111,32 +111,35 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { $block_name = 'core/comment-template'; $block_position = 'last-child'; - if ( $block_name === $parsed_block['blockName'] ) { - // parse_blocks( '' )[0]. - $inserted_block = array( - 'blockName' => 'core/avatar', - 'attrs' => array( - 'size' => 40, - 'style' => array( - 'border' => array( 'radius' => '10px' ), - ), - ), - 'innerHTML' => '', - 'innerContent' => array(), - ); + if ( $block_name !== $parsed_block['blockName'] ) { + return $parsed_block; + } - if ( 'first-child' === $block_position ) { - array_unshift( $parsed_block['innerBlocks'], $inserted_block ); - // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to prepend the new block to that array. - array_unshift( $parsed_block['innerContent'], $inserted_block ); - } elseif ( 'last-child' === $block_position ) { - array_push( $parsed_block['innerBlocks'], $inserted_block ); - // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to append the new block to that array. - array_push( $parsed_block['innerContent'], $inserted_block ); - } + // parse_blocks( '' )[0]. + $inserted_block = array( + 'blockName' => 'core/avatar', + 'attrs' => array( + 'size' => 40, + 'style' => array( + 'border' => array( 'radius' => '10px' ), + ), + ), + 'innerHTML' => '', + 'innerContent' => array(), + ); + + if ( 'first-child' === $block_position ) { + array_unshift( $parsed_block['innerBlocks'], $inserted_block ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to prepend the new block to that array. + array_unshift( $parsed_block['innerContent'], $inserted_block ); + } elseif ( 'last-child' === $block_position ) { + array_push( $parsed_block['innerBlocks'], $inserted_block ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to append the new block to that array. + array_push( $parsed_block['innerContent'], $inserted_block ); } + return $parsed_block; } add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); From 8e9c832125ab7f25577be64519861232c7d496c4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 1 Jun 2023 18:22:19 +0200 Subject: [PATCH 24/39] Use null rather than block instance in innerContent --- lib/experimental/blocks.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 0504a37c9eedc0..6616e2750821cc 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -131,13 +131,15 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { if ( 'first-child' === $block_position ) { array_unshift( $parsed_block['innerBlocks'], $inserted_block ); // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to prepend the new block to that array. - array_unshift( $parsed_block['innerContent'], $inserted_block ); + // when rendering blocks, we also need to prepend a value (`null`, to mark a block + // location) to that array. + array_unshift( $parsed_block['innerContent'], null ); } elseif ( 'last-child' === $block_position ) { array_push( $parsed_block['innerBlocks'], $inserted_block ); // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to append the new block to that array. - array_push( $parsed_block['innerContent'], $inserted_block ); + // when rendering blocks, we also need to prepend a value (`null`, to mark a block + // location) to that array. + array_push( $parsed_block['innerContent'], null ); } return $parsed_block; From 36665093505e183aefd88832321c7b14dc02e4e6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 5 Jun 2023 17:26:57 +0200 Subject: [PATCH 25/39] A bit nicer to read yet --- lib/experimental/blocks.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 6616e2750821cc..72d9743824416b 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -159,21 +159,23 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { // Can we avoid infinite loops? - if ( $block_name === $block['blockName'] ) { - $inserted_block_markup = << ' END; - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); + $inserted_blocks = parse_blocks( $inserted_block_markup ); + $inserted_content = render_block( $inserted_blocks[0] ); - if ( 'before' === $block_position ) { - $block_content = $inserted_content . $block_content; - } elseif ( 'after' === $block_position ) { - $block_content = $block_content . $inserted_content; - } + if ( 'before' === $block_position ) { + $block_content = $inserted_content . $block_content; + } elseif ( 'after' === $block_position ) { + $block_content = $block_content . $inserted_content; } return $block_content; From 2f5f3852ad08de27000d67fd650501cdb8120913 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 15:28:51 +0200 Subject: [PATCH 26/39] Add autoInsert field to block.json schema --- schemas/json/block.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/schemas/json/block.json b/schemas/json/block.json index 5b92a654fbc4a5..82a52abfc95e81 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -737,6 +737,19 @@ "render": { "type": "string", "description": "Template file loaded on the server when rendering a block." + }, + "autoInsert": { + "type": "object", + "description": "Positions and blocks to auto-insert this block next to.", + "patternProperties": { + "[a-zA-Z]": { + "type": "array", + "description": "Array of the names of blocks to auto-insert this block next to.", + "items": { + "type": "string" + } + } + } } }, "required": [ "name", "title" ], From 1f7efb4c3f5496f097455cb83bba4de00754e0e7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 15:31:21 +0200 Subject: [PATCH 27/39] More precise schema --- schemas/json/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemas/json/block.json b/schemas/json/block.json index 82a52abfc95e81..6e300fce9195ee 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -742,7 +742,7 @@ "type": "object", "description": "Positions and blocks to auto-insert this block next to.", "patternProperties": { - "[a-zA-Z]": { + "^(nextSibling|previousSibling|firstChild|lastChild)$": { "type": "array", "description": "Array of the names of blocks to auto-insert this block next to.", "items": { From d5ab6d12a45f9556f54a7455beaefe5d2fa11afe Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 15:35:09 +0200 Subject: [PATCH 28/39] Just before/after --- schemas/json/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemas/json/block.json b/schemas/json/block.json index 6e300fce9195ee..c4f5d683c36508 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -742,7 +742,7 @@ "type": "object", "description": "Positions and blocks to auto-insert this block next to.", "patternProperties": { - "^(nextSibling|previousSibling|firstChild|lastChild)$": { + "^(after|before|firstChild|lastChild)$": { "type": "array", "description": "Array of the names of blocks to auto-insert this block next to.", "items": { From 9ee00d2b71653d654d3859dcac80b7b813ce6006 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 15:39:16 +0200 Subject: [PATCH 29/39] Extract auto-insert settings from metadata --- lib/experimental/blocks.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 72d9743824416b..81733a66fe8d5d 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -181,3 +181,26 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { return $block_content; } add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 2 ); + +function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { + if ( ! isset( $metadata['autoInsert'] ) ) { + return $settings; + } + + $property_mappings = array( + 'before' => 'before', + 'after' => 'after', + 'firstChild' => 'first_child', + 'lastChild' => 'last_child', + ); + + $auto_insert = $metadata['autoInsert']; + foreach ( $property_mappings as $key => $mapped_key ) { + if ( isset( $auto_insert[ $key ] ) ) { + $settings['auto_insert'][ $mapped_key ] = $auto_insert[ $key ]; + } + } + + return $settings; +} +add_filter( 'block_type_metadata_settings', 'gutenberg_register_auto_inserted_blocks', 10, 2 ); From 44953040d2627affd4549983c3f5fa059e1f8100 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 15:49:24 +0200 Subject: [PATCH 30/39] Nicer syntax --- lib/experimental/blocks.php | 8 +++++--- schemas/json/block.json | 12 +++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 81733a66fe8d5d..8a0811a391b61f 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -195,10 +195,12 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { ); $auto_insert = $metadata['autoInsert']; - foreach ( $property_mappings as $key => $mapped_key ) { - if ( isset( $auto_insert[ $key ] ) ) { - $settings['auto_insert'][ $mapped_key ] = $auto_insert[ $key ]; + foreach ( $auto_insert as $block => $position ) { + if ( ! isset( $property_mappings[ $position ] ) ) { + continue; } + + $settings['auto_insert'][ $block ] = $property_mappings[ $position ]; } return $settings; diff --git a/schemas/json/block.json b/schemas/json/block.json index c4f5d683c36508..81996b7a17974c 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -740,14 +740,12 @@ }, "autoInsert": { "type": "object", - "description": "Positions and blocks to auto-insert this block next to.", + "description": "Blocks to auto-insert this block next to.", "patternProperties": { - "^(after|before|firstChild|lastChild)$": { - "type": "array", - "description": "Array of the names of blocks to auto-insert this block next to.", - "items": { - "type": "string" - } + "[a-zA-Z]": { + "type": "string", + "description": "Position relative to the block to auto-insert this block next to.", + "pattern": "^(nextSibling|previousSibling|firstChild|lastChild)$" } } } From 42811527298546a3fabeeded49eb2bda7f940bfe Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 16:21:56 +0200 Subject: [PATCH 31/39] Leverage block-specific render_block hook --- lib/experimental/blocks.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 8a0811a391b61f..442440e0f298d6 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -150,19 +150,13 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { * Auto-insert blocks relative to a given block. * * @param string $block_content The block content. - * @param array $block The full block, including name and attributes. */ -function gutenberg_auto_insert_blocks( $block_content, $block ) { +function gutenberg_auto_insert_blocks( $block_content ) { // TODO: Implement an API for users to set the following two parameters. - $block_name = 'core/post-content'; $block_position = 'after'; // Can we avoid infinite loops? - if ( $block_name !== $block['blockName'] ) { - return $block_content; - } - $inserted_block_markup = << @@ -180,7 +174,7 @@ function gutenberg_auto_insert_blocks( $block_content, $block ) { return $block_content; } -add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 2 ); +add_filter( 'render_block_core/post-content', 'gutenberg_auto_insert_blocks', 10, 1 ); function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { if ( ! isset( $metadata['autoInsert'] ) ) { From 50a63e4c471879ce0729235450f69f90ec7d3b02 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 17:06:31 +0200 Subject: [PATCH 32/39] Insert block dynamically --- lib/experimental/blocks.php | 43 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 442440e0f298d6..7add308b20b6c0 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -147,34 +147,26 @@ function gutenberg_auto_insert_child_block( $parsed_block ) { add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); /** - * Auto-insert blocks relative to a given block. + * Return a function that auto-inserts blocks relative to a given block. * - * @param string $block_content The block content. + * @param string $relative_position The position relative to the given block. + * @param array $inserted_block The block to insert. + * @return callable A function that accepts a block's content and returns the content with the inserted block. */ -function gutenberg_auto_insert_blocks( $block_content ) { - // TODO: Implement an API for users to set the following two parameters. - $block_position = 'after'; - +function gutenberg_auto_insert_blocks( $relative_position, $inserted_block ) { // Can we avoid infinite loops? - $inserted_block_markup = << - -' -END; + return function( $block_content ) use ( $relative_position, $inserted_block ) { + $inserted_content = render_block( $inserted_block ); - $inserted_blocks = parse_blocks( $inserted_block_markup ); - $inserted_content = render_block( $inserted_blocks[0] ); - - if ( 'before' === $block_position ) { - $block_content = $inserted_content . $block_content; - } elseif ( 'after' === $block_position ) { - $block_content = $block_content . $inserted_content; - } - - return $block_content; + if ( 'before' === $relative_position ) { + $block_content = $inserted_content . $block_content; + } elseif ( 'after' === $relative_position ) { + $block_content = $block_content . $inserted_content; + } + return $block_content; + }; } -add_filter( 'render_block_core/post-content', 'gutenberg_auto_insert_blocks', 10, 1 ); function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { if ( ! isset( $metadata['autoInsert'] ) ) { @@ -194,7 +186,12 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { continue; } - $settings['auto_insert'][ $block ] = $property_mappings[ $position ]; + $mapped_position = $property_mappings[ $position ]; + if ( 'before' === $mapped_position || 'after' === $mapped_position ) { + $inserter = gutenberg_auto_insert_blocks( $mapped_position, array( 'blockName' => $metadata['name'] ) ); + add_filter( "render_block_$block", $inserter, 10, 2 ); + } + $settings['auto_insert'][ $block ] = $mapped_position; } return $settings; From 236dc32dc5de3827fe7f5345b96a5d524022719e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 17:06:54 +0200 Subject: [PATCH 33/39] Insert block after Post Content --- packages/block-library/src/avatar/block.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json index 3fbb6dd9221aec..4f9f06a9db5385 100644 --- a/packages/block-library/src/avatar/block.json +++ b/packages/block-library/src/avatar/block.json @@ -50,5 +50,8 @@ } }, "editorStyle": "wp-block-avatar", - "style": "wp-block-avatar" + "style": "wp-block-avatar", + "autoInsert": { + "core/post-content": "after" + } } From 2ae6616a0199824842f081692adf2654f4fe1588 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 6 Jun 2023 17:16:03 +0200 Subject: [PATCH 34/39] Change syntax again --- lib/experimental/blocks.php | 2 +- packages/block-library/src/avatar/block.json | 2 +- schemas/json/block.json | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 7add308b20b6c0..67d306a9e5300d 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -181,7 +181,7 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { ); $auto_insert = $metadata['autoInsert']; - foreach ( $auto_insert as $block => $position ) { + foreach ( $auto_insert as $position => $block ) { if ( ! isset( $property_mappings[ $position ] ) ) { continue; } diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json index 4f9f06a9db5385..b510234c84f9e8 100644 --- a/packages/block-library/src/avatar/block.json +++ b/packages/block-library/src/avatar/block.json @@ -52,6 +52,6 @@ "editorStyle": "wp-block-avatar", "style": "wp-block-avatar", "autoInsert": { - "core/post-content": "after" + "after": "core/post-content" } } diff --git a/schemas/json/block.json b/schemas/json/block.json index 81996b7a17974c..1ce941288ab6f4 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -740,12 +740,12 @@ }, "autoInsert": { "type": "object", - "description": "Blocks to auto-insert this block next to.", + "description": "Position and block that specify where to auto-insert this block.", "patternProperties": { - "[a-zA-Z]": { + "^(nextSibling|previousSibling|firstChild|lastChild)$": { "type": "string", - "description": "Position relative to the block to auto-insert this block next to.", - "pattern": "^(nextSibling|previousSibling|firstChild|lastChild)$" + "description": "Block to insert this block next to.", + "pattern": "[a-zA-Z]" } } } From b3ad57b96761f2fecc26d136d5b2d1f65f5ea273 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 7 Jun 2023 09:58:21 +0200 Subject: [PATCH 35/39] Revert "Change syntax again" This reverts commit 2ae6616a0199824842f081692adf2654f4fe1588. --- lib/experimental/blocks.php | 2 +- packages/block-library/src/avatar/block.json | 2 +- schemas/json/block.json | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 67d306a9e5300d..7add308b20b6c0 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -181,7 +181,7 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { ); $auto_insert = $metadata['autoInsert']; - foreach ( $auto_insert as $position => $block ) { + foreach ( $auto_insert as $block => $position ) { if ( ! isset( $property_mappings[ $position ] ) ) { continue; } diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json index b510234c84f9e8..4f9f06a9db5385 100644 --- a/packages/block-library/src/avatar/block.json +++ b/packages/block-library/src/avatar/block.json @@ -52,6 +52,6 @@ "editorStyle": "wp-block-avatar", "style": "wp-block-avatar", "autoInsert": { - "after": "core/post-content" + "core/post-content": "after" } } diff --git a/schemas/json/block.json b/schemas/json/block.json index 1ce941288ab6f4..81996b7a17974c 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -740,12 +740,12 @@ }, "autoInsert": { "type": "object", - "description": "Position and block that specify where to auto-insert this block.", + "description": "Blocks to auto-insert this block next to.", "patternProperties": { - "^(nextSibling|previousSibling|firstChild|lastChild)$": { + "[a-zA-Z]": { "type": "string", - "description": "Block to insert this block next to.", - "pattern": "[a-zA-Z]" + "description": "Position relative to the block to auto-insert this block next to.", + "pattern": "^(nextSibling|previousSibling|firstChild|lastChild)$" } } } From 411f160edf82b54df7756dcd20109a00b87cc550 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 7 Jun 2023 10:05:31 +0200 Subject: [PATCH 36/39] Add comment about auto-inserted blocks registry --- lib/experimental/blocks.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 7add308b20b6c0..f8233dc8adc4dd 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -187,6 +187,8 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { } $mapped_position = $property_mappings[ $position ]; + + // TODO: In the long run, we'd likely want some sort of registry for auto-inserted blocks. if ( 'before' === $mapped_position || 'after' === $mapped_position ) { $inserter = gutenberg_auto_insert_blocks( $mapped_position, array( 'blockName' => $metadata['name'] ) ); add_filter( "render_block_$block", $inserter, 10, 2 ); From 8bd4f28ea95d332a911789f8f28fe1109fd645d1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 7 Jun 2023 10:30:06 +0200 Subject: [PATCH 37/39] Use analog mechanism for child block insertion --- lib/experimental/blocks.php | 79 ++++++++++---------- packages/block-library/src/avatar/block.json | 3 +- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index f8233dc8adc4dd..b1147832df862e 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -102,49 +102,30 @@ function gutenberg_register_metadata_attribute( $args ) { add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); /** - * Auto-insert a block as another block's first or last inner block. + * Return a function that auto-inserts as the first or last inner block of a given block. * - * @param array $parsed_block The block being rendered. + * @param string $relative_position The position relative to the given block ("first_child" or "last_child"). + * @param array $inserted_block The block to insert. + * @return callable A function that accepts a block's content and returns the content with the inserted block. */ -function gutenberg_auto_insert_child_block( $parsed_block ) { - // TODO: Implement an API for users to set the following two parameters. - $block_name = 'core/comment-template'; - $block_position = 'last-child'; - - if ( $block_name !== $parsed_block['blockName'] ) { +function gutenberg_auto_insert_child_block( $relative_position, $inserted_block ) { + return function( $parsed_block ) use ( $relative_position, $inserted_block ) { + if ( 'first_child' === $relative_position ) { + array_unshift( $parsed_block['innerBlocks'], $inserted_block ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to prepend a value (`null`, to mark a block + // location) to that array. + array_unshift( $parsed_block['innerContent'], null ); + } elseif ( 'last_child' === $relative_position ) { + array_push( $parsed_block['innerBlocks'], $inserted_block ); + // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) + // when rendering blocks, we also need to prepend a value (`null`, to mark a block + // location) to that array. + array_push( $parsed_block['innerContent'], null ); + } return $parsed_block; - } - - // parse_blocks( '' )[0]. - $inserted_block = array( - 'blockName' => 'core/avatar', - 'attrs' => array( - 'size' => 40, - 'style' => array( - 'border' => array( 'radius' => '10px' ), - ), - ), - 'innerHTML' => '', - 'innerContent' => array(), - ); - - if ( 'first-child' === $block_position ) { - array_unshift( $parsed_block['innerBlocks'], $inserted_block ); - // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to prepend a value (`null`, to mark a block - // location) to that array. - array_unshift( $parsed_block['innerContent'], null ); - } elseif ( 'last-child' === $block_position ) { - array_push( $parsed_block['innerBlocks'], $inserted_block ); - // Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`) - // when rendering blocks, we also need to prepend a value (`null`, to mark a block - // location) to that array. - array_push( $parsed_block['innerContent'], null ); - } - - return $parsed_block; + }; } -add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 ); /** * Return a function that auto-inserts blocks relative to a given block. @@ -192,6 +173,9 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { if ( 'before' === $mapped_position || 'after' === $mapped_position ) { $inserter = gutenberg_auto_insert_blocks( $mapped_position, array( 'blockName' => $metadata['name'] ) ); add_filter( "render_block_$block", $inserter, 10, 2 ); + } elseif ( 'first_child' === $mapped_position || 'last_child' === $mapped_position ) { + $inserter = gutenberg_auto_insert_child_block( $mapped_position, array( 'blockName' => $metadata['name'] ) ); + add_filter( "render_block_data_$block", $inserter, 10, 2 ); } $settings['auto_insert'][ $block ] = $mapped_position; } @@ -199,3 +183,20 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { return $settings; } add_filter( 'block_type_metadata_settings', 'gutenberg_register_auto_inserted_blocks', 10, 2 ); + +function gutenberg_apply_render_block_data_block_type_filter( $parsed_block, $source_block, $parent_block ) { + $block_name = $parsed_block['blockName']; + /** + * Filters the block being rendered in render_block(), before it's processed. + * + * The dynamic portion of the hook name, `$name`, refers to + * the block name, e.g. "core/paragraph". + * + * @param array $parsed_block The block being rendered. + * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. + * @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. + */ + $parsed_block = apply_filters( "render_block_data_$block_name", $parsed_block, $source_block, $parent_block ); + return $parsed_block; +} +add_filter( 'render_block_data', 'gutenberg_apply_render_block_data_block_type_filter', 15, 3 ); diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json index 4f9f06a9db5385..5f3bc5abea28e9 100644 --- a/packages/block-library/src/avatar/block.json +++ b/packages/block-library/src/avatar/block.json @@ -52,6 +52,7 @@ "editorStyle": "wp-block-avatar", "style": "wp-block-avatar", "autoInsert": { - "core/post-content": "after" + "core/post-content": "after", + "core/comment-template": "lastChild" } } From 46e30e5e540567c23467ec1d8ff357b18ad8683e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 7 Jun 2023 15:32:12 +0200 Subject: [PATCH 38/39] Support block attributes --- lib/experimental/blocks.php | 17 +++++++++++------ packages/block-library/src/avatar/block.json | 13 +++++++++++-- .../block-library/src/social-link/block.json | 11 ++++++++++- schemas/json/block.json | 19 +++++++++++++++++-- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index b1147832df862e..fa490c81a8b426 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -162,22 +162,27 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { ); $auto_insert = $metadata['autoInsert']; - foreach ( $auto_insert as $block => $position ) { + foreach ( $auto_insert as $block_name => $block_data ) { + $position = $block_data['position']; if ( ! isset( $property_mappings[ $position ] ) ) { continue; } $mapped_position = $property_mappings[ $position ]; + $inserted_block = array( + 'blockName' => $metadata['name'], + 'attrs' => $block_data['attrs'], + ); // TODO: In the long run, we'd likely want some sort of registry for auto-inserted blocks. if ( 'before' === $mapped_position || 'after' === $mapped_position ) { - $inserter = gutenberg_auto_insert_blocks( $mapped_position, array( 'blockName' => $metadata['name'] ) ); - add_filter( "render_block_$block", $inserter, 10, 2 ); + $inserter = gutenberg_auto_insert_blocks( $mapped_position, $inserted_block ); + add_filter( "render_block_$block_name", $inserter, 10, 2 ); } elseif ( 'first_child' === $mapped_position || 'last_child' === $mapped_position ) { - $inserter = gutenberg_auto_insert_child_block( $mapped_position, array( 'blockName' => $metadata['name'] ) ); - add_filter( "render_block_data_$block", $inserter, 10, 2 ); + $inserter = gutenberg_auto_insert_child_block( $mapped_position, $inserted_block ); + add_filter( "render_block_data_$block_name", $inserter, 10, 2 ); } - $settings['auto_insert'][ $block ] = $mapped_position; + $settings['auto_insert'][ $block_name ] = $mapped_position; } return $settings; diff --git a/packages/block-library/src/avatar/block.json b/packages/block-library/src/avatar/block.json index 5f3bc5abea28e9..902c3300bfec5b 100644 --- a/packages/block-library/src/avatar/block.json +++ b/packages/block-library/src/avatar/block.json @@ -52,7 +52,16 @@ "editorStyle": "wp-block-avatar", "style": "wp-block-avatar", "autoInsert": { - "core/post-content": "after", - "core/comment-template": "lastChild" + "core/comment-template": { + "position": "lastChild", + "attrs": { + "size": 40, + "style": { + "border": { + "radius": "10px" + } + } + } + } } } diff --git a/packages/block-library/src/social-link/block.json b/packages/block-library/src/social-link/block.json index 140cc123ec484c..3ae1fe41819bae 100644 --- a/packages/block-library/src/social-link/block.json +++ b/packages/block-library/src/social-link/block.json @@ -34,5 +34,14 @@ "reusable": false, "html": false }, - "editorStyle": "wp-block-social-link-editor" + "editorStyle": "wp-block-social-link-editor", + "autoInsert": { + "core/post-content": { + "position": "after", + "attrs": { + "service": "wordpress", + "url": "https://wordpress.org/" + } + } + } } diff --git a/schemas/json/block.json b/schemas/json/block.json index 81996b7a17974c..14a560b4a00701 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -743,9 +743,24 @@ "description": "Blocks to auto-insert this block next to.", "patternProperties": { "[a-zA-Z]": { - "type": "string", + "type": "object", "description": "Position relative to the block to auto-insert this block next to.", - "pattern": "^(nextSibling|previousSibling|firstChild|lastChild)$" + "properties": { + "position": { + "type": "string", + "description": "Position relative to the block to auto-insert this block next to.", + "enum": [ + "before", + "after", + "firstChild", + "lastChild" + ] + }, + "attrs": { + "type": "object", + "description": "Attributes for the auto-inserted block." + } + } } } } From 023d652cca7cd9d9718714b5fab87906cf4ab3ce Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 13 Jun 2023 14:07:35 +0200 Subject: [PATCH 39/39] Rename gutenberg_auto_insert_blocks to gutenberg_auto_insert_block --- lib/experimental/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index fa490c81a8b426..dad4ef971d43f7 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -134,7 +134,7 @@ function gutenberg_auto_insert_child_block( $relative_position, $inserted_block * @param array $inserted_block The block to insert. * @return callable A function that accepts a block's content and returns the content with the inserted block. */ -function gutenberg_auto_insert_blocks( $relative_position, $inserted_block ) { +function gutenberg_auto_insert_block( $relative_position, $inserted_block ) { // Can we avoid infinite loops? return function( $block_content ) use ( $relative_position, $inserted_block ) { @@ -176,7 +176,7 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) { ); // TODO: In the long run, we'd likely want some sort of registry for auto-inserted blocks. if ( 'before' === $mapped_position || 'after' === $mapped_position ) { - $inserter = gutenberg_auto_insert_blocks( $mapped_position, $inserted_block ); + $inserter = gutenberg_auto_insert_block( $mapped_position, $inserted_block ); add_filter( "render_block_$block_name", $inserter, 10, 2 ); } elseif ( 'first_child' === $mapped_position || 'last_child' === $mapped_position ) { $inserter = gutenberg_auto_insert_child_block( $mapped_position, $inserted_block );