Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Block Hooks (a.k.a. Auto-inserting Blocks) #5158

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,26 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
return $template_content;
}

/**
* Injects the active theme's stylesheet as a `theme` attribute
* into a given template part block.
*
* @since 6.4.0
* @access private
*
* @param array $block a parsed block.
* @return array Updated block.
*/
function _inject_theme_attribute_in_template_part_block( $block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = get_stylesheet();
}
return $block;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to change this to a more general function (e.g. called _visit_block) that is called for each block during serialization (see below).

We can then extend that function to also take care of automatic insertion:

diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php
index 460372b8cc..97126341d5 100644
--- a/src/wp-includes/block-template-utils.php
+++ b/src/wp-includes/block-template-utils.php
@@ -523,13 +523,15 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
  * @param array $block a parsed block.
  * @return array Updated block.
  */
-function _inject_theme_attribute_in_template_part_block( $block ) {
+function _visit_parsed_block( $block ) {
        if (
                'core/template-part' === $block['blockName'] &&
                ! isset( $block['attrs']['theme'] )
        ) {
                $block['attrs']['theme'] = get_stylesheet();
        }
+
+       $block = insert_hooked_blocks( $block );
        return $block;
 }

Pending implementation insert_hooked_blocks (and adapting the WP_Block_Type class and friends to include a new $block_hooks field etc), this should give us parity with what we're currently doing in Gutenberg through some filter magic.


We can then introduce a way to limit automatic insertion to certain template types; off-the-cuff, I'd say this should be possible via binding inside of _build_block_template_result_from_file (but maybe we can make it more elegant).

diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php
index 460372b8cc..88fedf939a 100644
--- a/src/wp-includes/block-template-utils.php
+++ b/src/wp-includes/block-template-utils.php
@@ -514,23 +514,31 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
 }
 
 /**
- * Injects the active theme's stylesheet as a `theme` attribute
- * into a given template part block.
+ * Returns a function that...
  *
  * @since 6.4.0
  * @access private
  *
- * @param array $block a parsed block.
- * @return array Updated block.
+ * @param WP_Block_Template $block_template a block template.
+ * @return callable A function that returns a block.
  */
-function _inject_theme_attribute_in_template_part_block( $block ) {
-	if (
-		'core/template-part' === $block['blockName'] &&
-		! isset( $block['attrs']['theme'] )
-	) {
-		$block['attrs']['theme'] = get_stylesheet();
+function _parsed_block_visitor( $block_template ) {
+	return function( $block ) use ( $block_template ) {
+		if (
+			'core/template-part' === $block['blockName'] &&
+			! isset( $block['attrs']['theme'] )
+		) {
+			$block['attrs']['theme'] = get_stylesheet();
+		}
+
+		$original_block = $block;
+		$block = insert_hooked_blocks( $block );
+		/**
+		 * This filter allows modifiying the auto-inserting behavior...
+		 */
+		$block = apply_filters( 'auto_insert_blocks', $block, $original_block, $block_template );
+		return $block;
 	}
-	return $block;
 }
 
 /**
@@ -609,7 +617,8 @@ function _build_block_template_result_from_file( $template_file, $template_type
 	}
 
 	$blocks            = parse_blocks( $template_content );
-	$template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );
+	$visitor           = _parsed_block_visitor( $template );
+	$template->content = serialize_blocks( $blocks, $visitor );
 
 	return $template;
 }

This should allow us to preserve separation of concerns (e.g. serialize_blocks doesn't know about auto-inserted blocks or templates; insert_block_hooks doesn't know about templates).

/**
* Parses a block template and removes the theme attribute from each template part.
*
Expand Down Expand Up @@ -565,7 +585,6 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = _inject_theme_attribute_in_block_template_content( $template_content );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
Expand All @@ -589,6 +608,9 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template->area = $template_file['area'];
}

$blocks = parse_blocks( $template_content );
$template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );

return $template;
}

Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,22 @@ function get_comment_delimited_block_content( $block_name, $block_attributes, $b
* instead preserve the markup as parsed.
*
* @since 5.3.1
* @since 6.4.0 The `$callback` parameter was added.
*
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
* @param string $callback Optional. Callback to run on the block before serialization.
ockham marked this conversation as resolved.
Show resolved Hide resolved
* @return string String of rendered HTML.
*/
function serialize_block( $block ) {
function serialize_block( $block, $callback = null ) {
if ( is_callable( $callback ) ) {
$block = call_user_func( $callback, $block );
}

$block_content = '';

$index = 0;
foreach ( $block['innerContent'] as $chunk ) {
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ], $callback );
}

if ( ! is_array( $block['attrs'] ) ) {
Expand Down