Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Insert hooked blocks during serialization
  • Loading branch information
ockham committed Sep 7, 2023
commit 501354789fb49ded8ed65e30c80a2698955ac000
45 changes: 32 additions & 13 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,23 +514,41 @@ 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();
}
return $block;
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();
}
Comment on lines +527 to +532
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = get_stylesheet();
}
$block = _inject_theme_attribute_in_template_part_block( $block );


$hooked_blocks = get_hooked_blocks( $block['blockName'] );
foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) {
$hooked_block = array(
'blockName' => $hooked_block_type,
'attrs' => array(),
'innerHTML' => '',
'innerContent' => array(),
'innerBlocks' => array(),
);
// Need to pass full current block, (potentially its parent -- for sibiling insertion), relative position, and hooked_block.
$block = insert_hooked_block( $hooked_block, $relative_position, $block );
/**
* This filter allows modifiying the auto-inserting behavior...
*/
$block = apply_filters( 'insert_hooked_block', $block, $hooked_blocks[ $hooked_block_type ], $block_template );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might use separate filters for template and pattern insertion

Suggested change
$block = apply_filters( 'insert_hooked_block', $block, $hooked_blocks[ $hooked_block_type ], $block_template );
$block = apply_filters( 'insert_hooked_block_into_template', $block, $hooked_blocks[ $hooked_block_type ], $block_template );

}
return $block;
};
}

/**
Expand Down Expand Up @@ -609,7 +627,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;
}
Expand Down