mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-24 08:21:38 +01:00
Interactivity API: Add wp_interactivity_get_element()
function.
Introduces the `wp_interactivity_get_element()` function to the Interactivity API, analogous to the `getElement()` function in the `@wordpress/interactivity` JavaScript module. This function allows access to the current element being processed during directive processing. The function returns an array containing the `attributes` property, which includes only the originally defined attributes present on the element. Attributes added or modified by directive processing are not included. This is intended for use in derived state properties inside `wp_interactivity_state()`, similar to how `wp_interactivity_get_context()` is used. Example usage: ```php wp_interactivity_state( 'myPlugin', array( 'buttonText' => function() { $context = wp_interactivity_get_context(); $element = wp_interactivity_get_element(); return isset( $context['buttonText'] ) ? $context['buttonText'] : $element['attributes']['data-default-button-text']; }, ) ); ``` Includes unit tests to cover the new functionality. Props darerodz, swissspidy, cbravobernal, czapla. Fixes #62136. Built from https://develop.svn.wordpress.org/trunk@59131 git-svn-id: http://core.svn.wordpress.org/trunk@58527 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
245519d952
commit
5e72f32f5c
@ -95,6 +95,15 @@ final class WP_Interactivity_API {
|
|||||||
*/
|
*/
|
||||||
private $context_stack = null;
|
private $context_stack = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Representation in array format of the element currently being processed.
|
||||||
|
*
|
||||||
|
* This is only available during directive processing, otherwise it is `null`.
|
||||||
|
*
|
||||||
|
* @since 6.7.0
|
||||||
|
* @var array<mixed>|null
|
||||||
|
*/
|
||||||
|
private $current_element = null;
|
||||||
/**
|
/**
|
||||||
* Gets and/or sets the initial state of an Interactivity API store for a
|
* Gets and/or sets the initial state of an Interactivity API store for a
|
||||||
* given namespace.
|
* given namespace.
|
||||||
@ -297,6 +306,26 @@ final class WP_Interactivity_API {
|
|||||||
? $context[ $store_namespace ]
|
? $context[ $store_namespace ]
|
||||||
: array();
|
: array();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the current element being processed.
|
||||||
|
*
|
||||||
|
* The returned array contains a copy of the element attributes.
|
||||||
|
*
|
||||||
|
* @since 6.7.0
|
||||||
|
*
|
||||||
|
* @return array|null Current element.
|
||||||
|
*/
|
||||||
|
public function get_element(): ?array {
|
||||||
|
if ( null === $this->current_element ) {
|
||||||
|
_doing_it_wrong(
|
||||||
|
__METHOD__,
|
||||||
|
__( 'The element can only be read during directive processing.' ),
|
||||||
|
'6.7.0'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->current_element;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the `@wordpress/interactivity` script modules.
|
* Registers the `@wordpress/interactivity` script modules.
|
||||||
@ -449,6 +478,19 @@ final class WP_Interactivity_API {
|
|||||||
'exit' => $p->is_tag_closer() || ! $p->has_and_visits_its_closer_tag(),
|
'exit' => $p->is_tag_closer() || ! $p->has_and_visits_its_closer_tag(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Get the element attributes to include them in the element representation.
|
||||||
|
$element_attrs = array();
|
||||||
|
$attr_names = $p->get_attribute_names_with_prefix( '' ) ?? array();
|
||||||
|
|
||||||
|
foreach ( $attr_names as $name ) {
|
||||||
|
$element_attrs[ $name ] = $p->get_attribute( $name );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the current element right before running its directive processors.
|
||||||
|
$this->current_element = array(
|
||||||
|
'attributes' => $element_attrs,
|
||||||
|
);
|
||||||
|
|
||||||
foreach ( $modes as $mode => $should_run ) {
|
foreach ( $modes as $mode => $should_run ) {
|
||||||
if ( ! $should_run ) {
|
if ( ! $should_run ) {
|
||||||
continue;
|
continue;
|
||||||
@ -470,6 +512,9 @@ final class WP_Interactivity_API {
|
|||||||
call_user_func_array( $func, array( $p, $mode, &$tag_stack ) );
|
call_user_func_array( $func, array( $p, $mode, &$tag_stack ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear the current element.
|
||||||
|
$this->current_element = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $unbalanced ) {
|
if ( $unbalanced ) {
|
||||||
|
@ -125,3 +125,16 @@ function wp_interactivity_data_wp_context( array $context, string $store_namespa
|
|||||||
function wp_interactivity_get_context( ?string $store_namespace = null ): array {
|
function wp_interactivity_get_context( ?string $store_namespace = null ): array {
|
||||||
return wp_interactivity()->get_context( $store_namespace );
|
return wp_interactivity()->get_context( $store_namespace );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array representation of the current element being processed.
|
||||||
|
*
|
||||||
|
* The function should be used only during directive processing.
|
||||||
|
*
|
||||||
|
* @since 6.7.0
|
||||||
|
*
|
||||||
|
* @return array|null Current element.
|
||||||
|
*/
|
||||||
|
function wp_interactivity_get_element(): ?array {
|
||||||
|
return wp_interactivity()->get_element();
|
||||||
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.7-alpha-59130';
|
$wp_version = '6.7-alpha-59131';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user