Automattic\WooCommerce\Blocks\Utils
BlockTemplateUtils::flatten_blocks
Returns an array containing the references of the passed blocks and their inner blocks.
Method of the class: BlockTemplateUtils{}
No Hooks.
Returns
array. block references to the passed blocks and their inner blocks.
Usage
$result = BlockTemplateUtils::flatten_blocks( $blocks );
- $blocks(array) (required) (passed by reference — &)
- array of blocks.
BlockTemplateUtils::flatten_blocks() BlockTemplateUtils::flatten blocks code WC 11.2.2
public static function flatten_blocks( &$blocks ) {
$all_blocks = array();
$queue = array();
foreach ( $blocks as &$block ) {
$queue[] = &$block;
}
$queue_count = count( $queue );
while ( $queue_count > 0 ) {
$block = &$queue[0];
array_shift( $queue );
$all_blocks[] = &$block;
if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
}
}
$queue_count = count( $queue );
}
return $all_blocks;
}