Make WordPress Core

Opened 8 months ago

Closed 8 months ago

Last modified 7 months ago

#62136 closed feature request (fixed)

Interactivity API: Allow reading element attributes inside derived state props

Reported by: darerodz's profile darerodz Owned by: czapla's profile czapla
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.7
Component: Interactivity API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Developers can define derived state props with wp_interactivity_state since WordPress 6.6 (see Updates to the Interactivity API in 6.6). However, they lack a function to obtain the current element's attributes, which is possible in JavaScript with the getElement() function exposed from @wordpress/interactivity.

A PHP function equivalent to getElement() should be implemented to allow developers to write derived state props that depend on element attributes.

Change History (4)

This ticket was mentioned in PR #7460 on WordPress/wordpress-develop by @darerodz.


8 months ago
#1

  • Keywords has-patch has-unit-tests added

This PR adds a PHP function named wp_interactivity_get_element, analogous to the getElement() function exposed in the @wordpress/interactivity JavaScript module (see docs).

The function is intended to be used in derived state props inside wp_interactivity_state, same as wp_interactivity_get_context.

For now, the returned value is an array that contains only the attributes property, which lists the originally defined attributes present in the element. That means attributes added or modified by directive processing don't appear.

Trac ticket: https://core.trac.wordpress.org/ticket/62136

#2 @czapla
8 months ago

  • Owner set to czapla
  • Resolution set to fixed
  • Status changed from new to closed

In 59131:

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.

#4 @desrosj
7 months ago

  • Milestone changed from Awaiting Review to 6.7
Note: See TracTickets for help on using tickets.