WP_Interactivity_API::get_directive_entriesprivateWP 6.9.0

Parse the HTML element and get all the valid directives with the given prefix.

Method of the class: WP_Interactivity_API{}

No Hooks.

Returns

Array. An array of entries containing the directive namespace, value, suffix, and unique ID.

Usage

// private - for code of main (parent) class only
$result = $this->get_directive_entries( $p, $prefix );
$p(WP_Interactivity_API_Directives_Processor) (required)
The directives processor instance.
$prefix(string) (required)
The directive prefix to filter by.

Changelog

Since 6.9.0 Introduced.

WP_Interactivity_API::get_directive_entries() code WP 6.9.1

private function get_directive_entries( WP_Interactivity_API_Directives_Processor $p, string $prefix ) {
	$directive_attributes = $p->get_attribute_names_with_prefix( 'data-wp-' . $prefix );
	$entries              = array();
	foreach ( $directive_attributes as $attribute_name ) {
		[ 'prefix' => $attr_prefix, 'suffix' => $suffix, 'unique_id' => $unique_id] = $this->parse_directive_name( $attribute_name );
		// Ensure it is the desired directive.
		if ( $prefix !== $attr_prefix ) {
			continue;
		}
		list( $namespace, $value ) = $this->extract_directive_value( $p->get_attribute( $attribute_name ), end( $this->namespace_stack ) );
		$entries[]                 = array(
			'namespace' => $namespace,
			'value'     => $value,
			'suffix'    => $suffix,
			'unique_id' => $unique_id,
		);
	}
	// Sort directive entries to ensure stable ordering with the client.
	// Put nulls first, then sort by suffix and finally by uniqueIds.
	usort(
		$entries,
		function ( $a, $b ) {
			$a_suffix = $a['suffix'] ?? '';
			$b_suffix = $b['suffix'] ?? '';
			if ( $a_suffix !== $b_suffix ) {
				return $a_suffix < $b_suffix ? -1 : 1;
			}
			$a_id = $a['unique_id'] ?? '';
			$b_id = $b['unique_id'] ?? '';
			if ( $a_id === $b_id ) {
				return 0;
			}
			return $a_id > $b_id ? 1 : -1;
		}
	);
	return $entries;
}