Make WordPress Core


Ignore:
Timestamp:
09/30/2024 05:05:24 PM (9 months ago)
Author:
czapla
Message:

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( $contextbuttonText? )

? $contextbuttonText?
: $elementattributes?data-default-button-text?;

},

) );
`

Includes unit tests to cover the new functionality.

Props darerodz, swissspidy, cbravobernal, czapla.
Fixes #62136.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/interactivity-api/interactivity-api.php

    r58327 r59131  
    126126    return wp_interactivity()->get_context( $store_namespace );
    127127}
     128
     129/**
     130 * Returns an array representation of the current element being processed.
     131 *
     132 * The function should be used only during directive processing.
     133 *
     134 * @since 6.7.0
     135 *
     136 * @return array|null Current element.
     137 */
     138function wp_interactivity_get_element(): ?array {
     139    return wp_interactivity()->get_element();
     140}
Note: See TracChangeset for help on using the changeset viewer.