Automattic\WooCommerce\Blocks\Templates
SingleProductTemplateCompatibility::group_blocks
Group blocks in this way: B1 + TP1 + B2 + B3 + B4 + TP2 + B5 (B = Block, TP = Template Part) becomes: [[B1], [TP1], [B2, B3, B4], [TP2], [B5]]
Method of the class: SingleProductTemplateCompatibility{}
No Hooks.
Returns
Array. Array of blocks grouped by template part.
Usage
$result = SingleProductTemplateCompatibility::group_blocks( $parsed_blocks );
- $parsed_blocks(array) (required)
- Array of parsed block objects.
SingleProductTemplateCompatibility::group_blocks() SingleProductTemplateCompatibility::group blocks code WC 10.9.1
private static function group_blocks( $parsed_blocks ) {
return array_reduce(
$parsed_blocks,
function ( array $carry, array $block ) {
if ( 'core/template-part' === $block['blockName'] ) {
$carry[] = array( $block );
return $carry;
}
$last_element_index = count( $carry ) - 1;
if ( isset( $carry[ $last_element_index ][0]['blockName'] ) && 'core/template-part' !== $carry[ $last_element_index ][0]['blockName'] ) {
$carry[ $last_element_index ][] = $block;
return $carry;
}
$carry[] = array( $block );
return $carry;
},
array()
);
}