Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::flatten_blocks()public staticWC 1.0

Returns an array containing the references of the passed blocks and their inner blocks.

Method of the class: BlockTemplateUtils{}

No Hooks.

Return

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() code WC 9.4.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;
}