_flatten_blocks()WP 5.9.0

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

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

Array. block references to the passed blocks and their inner blocks.

Usage

_flatten_blocks( $blocks );
$blocks(array) (required) (passed by reference — &)
array of blocks.

Changelog

Since 5.9.0 Introduced.

_flatten_blocks() code WP 6.5.2

function _flatten_blocks( &$blocks ) {
	$all_blocks = array();
	$queue      = array();
	foreach ( $blocks as &$block ) {
		$queue[] = &$block;
	}

	while ( count( $queue ) > 0 ) {
		$block = &$queue[0];
		array_shift( $queue );
		$all_blocks[] = &$block;

		if ( ! empty( $block['innerBlocks'] ) ) {
			foreach ( $block['innerBlocks'] as &$inner_block ) {
				$queue[] = &$inner_block;
			}
		}
	}

	return $all_blocks;
}