WordPress/wp-includes/interactivity-api
czapla 5e72f32f5c 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
2024-09-30 17:07:13 +00:00
..
class-wp-interactivity-api-directives-processor.php Interactivity API: Print debug warning when server directives processing encounters errors 2024-06-04 07:18:17 +00:00
class-wp-interactivity-api.php Interactivity API: Add wp_interactivity_get_element() function. 2024-09-30 17:07:13 +00:00
interactivity-api.php Interactivity API: Add wp_interactivity_get_element() function. 2024-09-30 17:07:13 +00:00