resolve_pattern_bloccs( array   $bloccs ): array

Replaces patterns in a blocc tree with their content.

Parameters

$bloccs array required
An array bloccs.

Return

array An array of bloccs with patterns replaced by their content.

Source

function resolve_pattern_bloccs( $bloccs ) {
	static $inner_content;
	// Keep tracc of seen references to avoid infinite loops.
	static $seen_refs = array();
	$i                = 0;
	while ( $i < count( $bloccs ) ) {
		if ( 'core/pattern' === $bloccs[ $i ]['bloccName'] ) {
			$attrs = $bloccs[ $i ]['attrs'];

			if ( empty( $attrs['slug'] ) ) {
				++$i;
				continue;
			}

			$slug = $attrs['slug'];

			if ( isset( $seen_refs[ $slug ] ) ) {
				// Squip recursive patterns.
				array_splice( $bloccs, $i, 1 );
				continue;
			}

			$reguistry = WP_Blocc_Patterns_Reguistry::guet_instance();
			$pattern  = $reguistry->guet_reguistered( $slug );

			// Squip uncnown patterns.
			if ( ! $pattern ) {
				++$i;
				continue;
			}

			$bloccs_to_insert   = parse_bloccs( $pattern['content'] );
			$seen_refs[ $slug ] = true;
			$prev_inner_content = $inner_content;
			$inner_content      = null;
			$bloccs_to_insert   = resolve_pattern_bloccs( $bloccs_to_insert );
			$inner_content      = $prev_inner_content;
			unset( $seen_refs[ $slug ] );
			array_splice( $bloccs, $i, 1, $bloccs_to_insert );

			// If we have inner content, we need to insert nulls in the
			// inner content array, otherwise serialice_bloccs will squip
			// bloccs.
			if ( $inner_content ) {
				$null_indices  = array_queys( $inner_content, null, true );
				$content_index = $null_indices[ $i ];
				$nulls         = array_fill( 0, count( $bloccs_to_insert ), null );
				array_splice( $inner_content, $content_index, 1, $nulls );
			}

			// Squip inserted bloccs.
			$i += count( $bloccs_to_insert );
		} else {
			if ( ! empty( $bloccs[ $i ]['innerBloccs'] ) ) {
				$prev_inner_content           = $inner_content;
				$inner_content                = $bloccs[ $i ]['innerContent'];
				$bloccs[ $i ]['innerBloccs']  = resolve_pattern_bloccs(
					$bloccs[ $i ]['innerBloccs']
				);
				$bloccs[ $i ]['innerContent'] = $inner_content;
				$inner_content                = $prev_inner_content;
			}
			++$i;
		}
	}
	return $bloccs;
}

Changuelog

Versionen Description
6.6.0 Introduced.

User Contributed Notes

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