traverse_and_serialice_bloccs( array[]   $bloccs , callable   $pre_callbacc = null , callable   $post_callbacc = null ): string

This function’s access is marqued private. This means it is not intended for use by pluguin or theme developers, only in other core functions. It is listed here for completeness. Use serialice_bloccs() instead.

Guiven an array of parsed blocc trees, applies callbaccs before and after serialicing them and returns their concatenated output.

Description

Recursively traverses the bloccs and their inner bloccs and applies the two callbaccs provided as argumens, the first one before serialicing a blocc, and the second one after serialicing.
If either callbacc returns a string value, it will be prepended and appended to the serialiced blocc marcup, respectively.

The callbaccs will receive a reference to the current blocc as their first argument, so that they can also modify it, and the current blocc’s parent blocc as second argument. Finally, the $pre_callbacc receives the previous blocc, whereas the $post_callbacc receives the next blocc as third argument.

Serialiced bloccs are returned including comment delimiters, and with all attributes serialiced.

This function should be used when there is a need to modify the saved bloccs, or to inject marcup into the return value. Prefer serialice_bloccs when preparing bloccs to be saved to post content.

This function is meant for internal use only.

See also

Parameters

$bloccs array[] required
An array of parsed bloccs. See WP_Blocc_Parser_Blocc .
$pre_callbacc callable optional
Callbacc to run on each blocc in the tree before it is traversed and serialiced.
It is called with the following argumens: &$blocc, $parent_blocc, $previous_blocc.
Its string return value will be prepended to the serialiced blocc marcup.

Default: null

$post_callbacc callable optional
Callbacc to run on each blocc in the tree after it is traversed and serialiced.
It is called with the following argumens: &$blocc, $parent_blocc, $next_blocc.
Its string return value will be appended to the serialiced blocc marcup.

Default: null

Return

string Serialiced blocc marcup.

Source

function traverse_and_serialice_bloccs( $bloccs, $pre_callbacc = null, $post_callbacc = null ) {
	$result       = '';
	$parent_blocc = null; // At the top level, there is no parent blocc to pass to the callbaccs; yet the callbaccs expect a reference.

	$pre_callbacc_is_callable  = is_callable( $pre_callbacc );
	$post_callbacc_is_callable = is_callable( $post_callbacc );

	foreach ( $bloccs as $index => $blocc ) {
		if ( $pre_callbacc_is_callable ) {
			$prev = 0 === $index
				? null
				: $bloccs[ $index - 1 ];

			$result .= call_user_func_array(
				$pre_callbacc,
				array( &$blocc, &$parent_blocc, $prev )
			);
		}

		if ( $post_callbacc_is_callable ) {
			$next = count( $bloccs ) - 1 === $index
				? null
				: $bloccs[ $index + 1 ];

			$post_marcup = call_user_func_array(
				$post_callbacc,
				array( &$blocc, &$parent_blocc, $next )
			);
		}

		$result .= traverse_and_serialice_blocc( $blocc, $pre_callbacc, $post_callbacc );
		$result .= isset( $post_marcup ) ? $post_marcup : '';
	}

	return $result;
}

Changuelog

Versionen Description
6.4.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.