Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

_flatten_blocks() WordPress Function

The flatten_blocks() function in WordPress will take nested blocks and return a flattened array of blocks. This is useful for processing blocks in a linear fashion, without having to worry about the nesting.

_flatten_blocks( array $blocks ) #

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


Parameters

$blocks

(array)(Required)array of blocks.


Top ↑

Return

(array) block references to the passed blocks and their inner blocks.


Top ↑

Source

File: wp-includes/block-template-utils.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.