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/class-wp-interactivity-api.php

    r59130 r59131  
    9797
    9898    /**
     99     * Representation in array format of the element currently being processed.
     100     *
     101     * This is only available during directive processing, otherwise it is `null`.
     102     *
     103     * @since 6.7.0
     104     * @var array<mixed>|null
     105     */
     106    private $current_element = null;
     107    /**
    99108     * Gets and/or sets the initial state of an Interactivity API store for a
    100109     * given namespace.
     
    297306            ? $context[ $store_namespace ]
    298307            : array();
     308    }
     309    /**
     310     * Returns an array representation of the current element being processed.
     311     *
     312     * The returned array contains a copy of the element attributes.
     313     *
     314     * @since 6.7.0
     315     *
     316     * @return array|null Current element.
     317     */
     318    public function get_element(): ?array {
     319        if ( null === $this->current_element ) {
     320            _doing_it_wrong(
     321                __METHOD__,
     322                __( 'The element can only be read during directive processing.' ),
     323                '6.7.0'
     324            );
     325        }
     326
     327        return $this->current_element;
    299328    }
    300329
     
    448477                'enter' => ! $p->is_tag_closer(),
    449478                'exit'  => $p->is_tag_closer() || ! $p->has_and_visits_its_closer_tag(),
     479            );
     480
     481            // Get the element attributes to include them in the element representation.
     482            $element_attrs = array();
     483            $attr_names    = $p->get_attribute_names_with_prefix( '' ) ?? array();
     484
     485            foreach ( $attr_names as $name ) {
     486                $element_attrs[ $name ] = $p->get_attribute( $name );
     487            }
     488
     489            // Assign the current element right before running its directive processors.
     490            $this->current_element = array(
     491                'attributes' => $element_attrs,
    450492            );
    451493
     
    471513                }
    472514            }
     515
     516            // Clear the current element.
     517            $this->current_element = null;
    473518        }
    474519
Note: See TracChangeset for help on using the changeset viewer.