WP_Block_Processor::are_equal_block_typespublic staticWP 6.9.0

Given two spans of text, indicate if they represent identical block types.

This function normalizes block types to account for implicit core namespacing.

Note! This function only returns valid results when the complete block types are represented in the span offsets and lengths. This means that the full optional namespace and block name must be represented in the input arguments.

Example:

0 5 10 15 20 25 30 35 40

$text = '<!-- wp:block --><!-- /wp:core/block -->';
true  === WP_Block_Processor::are_equal_block_types( $text, 9, 5, $text, 27, 10 );
false === WP_Block_Processor::are_equal_block_types( $text, 9, 5, 'my/block', 0, 8 );

Метод класса: WP_Block_Processor{}

Хуков нет.

Возвращает

true|false. Whether the spans of text represent identical block types, normalized for namespacing.

Использование

$result = WP_Block_Processor::are_equal_block_types( $a_text, $a_at, $a_length, $b_text, $b_at, $b_length ): bool;
$a_text(строка) (обязательный)
Text in which first block type appears.
$a_at(int) (обязательный)
Byte offset into text in which first block type starts.
$a_length(int) (обязательный)
Byte length of first block type.
$b_text(строка) (обязательный)
Text in which second block type appears (may be the same as the first text).
$b_at(int) (обязательный)
Byte offset into text in which second block type starts.
$b_length(int) (обязательный)
Byte length of second block type.

Список изменений

С версии 6.9.0 Введена.

Код WP_Block_Processor::are_equal_block_types() WP 6.9.1

public static function are_equal_block_types( string $a_text, int $a_at, int $a_length, string $b_text, int $b_at, int $b_length ): bool {
	$a_ns_length = strcspn( $a_text, '/', $a_at, $a_length );
	$b_ns_length = strcspn( $b_text, '/', $b_at, $b_length );

	$a_has_ns = $a_ns_length !== $a_length;
	$b_has_ns = $b_ns_length !== $b_length;

	// Both contain namespaces.
	if ( $a_has_ns && $b_has_ns ) {
		if ( $a_length !== $b_length ) {
			return false;
		}

		$a_block_type = substr( $a_text, $a_at, $a_length );

		return 0 === substr_compare( $b_text, $a_block_type, $b_at, $b_length );
	}

	if ( $a_has_ns ) {
		$b_block_type = 'core/' . substr( $b_text, $b_at, $b_length );

		return (
			strlen( $b_block_type ) === $a_length &&
			0 === substr_compare( $a_text, $b_block_type, $a_at, $a_length )
		);
	}

	if ( $b_has_ns ) {
		$a_block_type = 'core/' . substr( $a_text, $a_at, $a_length );

		return (
			strlen( $a_block_type ) === $b_length &&
			0 === substr_compare( $b_text, $a_block_type, $b_at, $b_length )
		);
	}

	// Neither contains a namespace.
	if ( $a_length !== $b_length ) {
		return false;
	}

	$a_name = substr( $a_text, $a_at, $a_length );

	return 0 === substr_compare( $b_text, $a_name, $b_at, $b_length );
}