WP_Interactivity_API::get_directive_entries
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 ): array;
- $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() WP Interactivity API::get directive entries code WP 7.1
private function get_directive_entries( WP_Interactivity_API_Directives_Processor $p, string $prefix ): array {
$directive_attributes = $p->get_attribute_names_with_prefix( 'data-wp-' . $prefix );
if ( null === $directive_attributes ) {
return array();
}
$entries = array();
foreach ( $directive_attributes as $attribute_name ) {
$parsed_directive = $this->parse_directive_name( $attribute_name );
if ( null === $parsed_directive ) {
continue;
}
[ 'prefix' => $attr_prefix, 'suffix' => $suffix, 'unique_id' => $unique_id ] = $parsed_directive;
// Ensure it is the desired directive.
if ( $prefix !== $attr_prefix ) {
continue;
}
$attribute_value = $p->get_attribute( $attribute_name );
if ( null === $attribute_value ) {
continue;
}
/*
* The namespace stack can hold false, which data_wp_interactive_processor() pushes for a
* `data-wp-interactive` whose namespace is invalid and which has no enclosing one to inherit. Only a
* string names a store, so anything else counts as no default namespace at all.
*/
$default_namespace = array_last( $this->namespace_stack ?? array() );
if ( ! is_string( $default_namespace ) ) {
$default_namespace = null;
}
list( $namespace, $value ) = $this->extract_directive_value( $attribute_value, $default_namespace );
$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;
}
$a_id = $a['unique_id'] ?? '';
$b_id = $b['unique_id'] ?? '';
return $a_id <=> $b_id;
}
);
return $entries;
}