From 5e72f32f5c4d320a535c7d659c28a0c12c08d8d0 Mon Sep 17 00:00:00 2001 From: czapla Date: Mon, 30 Sep 2024 17:07:13 +0000 Subject: [PATCH] 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 --- .../class-wp-interactivity-api.php | 45 +++++++++++++++++++ .../interactivity-api/interactivity-api.php | 13 ++++++ wp-includes/version.php | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/wp-includes/interactivity-api/class-wp-interactivity-api.php b/wp-includes/interactivity-api/class-wp-interactivity-api.php index 022bc08254..297e7ec5ff 100644 --- a/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -95,6 +95,15 @@ final class WP_Interactivity_API { */ 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|null + */ + private $current_element = null; /** * Gets and/or sets the initial state of an Interactivity API store for a * given namespace. @@ -297,6 +306,26 @@ final class WP_Interactivity_API { ? $context[ $store_namespace ] : 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. @@ -449,6 +478,19 @@ final class WP_Interactivity_API { '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 ) { if ( ! $should_run ) { continue; @@ -470,6 +512,9 @@ final class WP_Interactivity_API { call_user_func_array( $func, array( $p, $mode, &$tag_stack ) ); } } + + // Clear the current element. + $this->current_element = null; } if ( $unbalanced ) { diff --git a/wp-includes/interactivity-api/interactivity-api.php b/wp-includes/interactivity-api/interactivity-api.php index e007d512f9..71102ae04b 100644 --- a/wp-includes/interactivity-api/interactivity-api.php +++ b/wp-includes/interactivity-api/interactivity-api.php @@ -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 { 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(); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 0fb6c6144e..8064505eda 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.