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.