Processes the data-wp-context
directive.
Description
It adds the context defined in the directive value to the stack so that it’s available for the nested interactivity elements.
Parameters
$p
WP_Interactivity_API_Directives_Processorrequired- The directives processor instance.
$mode
stringrequired- Whether the processing is entering or exiting the tag.
$context_stack
arrayrequired- The reference to the context stack.
$namespace_stack
arrayrequired- The reference to the store namespace stack.
Source
* @since 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty.
* @since 6.6.0 Removed `default_namespace` and `context` arguments.
* @since 6.6.0 Add support for derived state.
*
* @param string|true $directive_value The directive attribute value string or `true` when it's a boolean attribute.
* @return mixed|null The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.
*/
private function evaluate( $directive_value ) {
$default_namespace = end( $this->namespace_stack );
$context = end( $this->context_stack );
list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace );
if ( ! $ns || ! $path ) {
/* translators: %s: The directive value referenced. */
$message = sprintf( __( 'Namespace or reference path cannot be empty. Directive value referenced: %s' ), $directive_value );
_doing_it_wrong( __METHOD__, $message, '6.6.0' );
return null;
}
$store = array(
'state' => $this->state_data[ $ns ] ?? array(),
'context' => $context[ $ns ] ?? array(),
);
// Checks if the reference path is preceded by a negation operator (!).
$should_negate_value = '!' === $path[0];
$path = $should_negate_value ? substr( $path, 1 ) : $path;
// Extracts the value from the store using the reference path.
$path_segments = explode( '.', $path );
$current = $store;
foreach ( $path_segments as $path_segment ) {
if ( ( is_array( $current ) || $current instanceof ArrayAccess ) && isset( $current[ $path_segment ] ) ) {
Changelog
Version | Description |
---|---|
6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.