WP_Interactivity_API::evaluate( string|true $directive_value, string $default_namespace, array|false $context = false ): mixed|null

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Evaluates the reference path passed to a directive based on the current store namespace, state and context.

Parameters

$directive_valuestring|truerequired
The directive attribute value string or true when it’s a boolean attribute.
$default_namespacestringrequired
The default namespace to use if none is explicitly defined in the directive value.
$contextarray|falseoptional
The current context for evaluating the directive or false if there is no context.

Default:false

Return

mixed|null The result of the evaluation. Null if the reference path doesn’t exist.

Source

private function evaluate( $directive_value, string $default_namespace, $context = false ) {
	list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace );
	if ( empty( $path ) ) {
		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 ( isset( $current[ $path_segment ] ) ) {
			$current = $current[ $path_segment ];
		} else {
			return null;
		}
	}

	// Returns the opposite if it contains a negation operator (!).
	return $should_negate_value ? ! $current : $current;
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.