WP_Interactivity_API::evaluate()privateWP 6.5.0

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

Метод класса: WP_Interactivity_API{}

Хуков нет.

Возвращает

Разное|null. The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->evaluate( $directive_value );
$directive_value(строка|true) (обязательный)
The directive attribute value string or true when it's a boolean attribute.

Список изменений

С версии 6.5.0 Введена.
С версии 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty.
С версии 6.6.0 Removed default_namespace and context arguments.
С версии 6.6.0 Add support for derived state.

Код WP_Interactivity_API::evaluate() WP 6.7.1

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 ] ) ) {
			$current = $current[ $path_segment ];
		} elseif ( is_object( $current ) && isset( $current->$path_segment ) ) {
			$current = $current->$path_segment;
		} else {
			$current = null;
			break;
		}

		if ( $current instanceof Closure ) {
			/*
			 * This state getter's namespace is added to the stack so that
			 * `state()` or `get_config()` read that namespace when called
			 * without specifying one.
			 */
			array_push( $this->namespace_stack, $ns );
			try {
				$current = $current();
			} catch ( Throwable $e ) {
				_doing_it_wrong(
					__METHOD__,
					sprintf(
						/* translators: 1: Path pointing to an Interactivity API state property, 2: Namespace for an Interactivity API store. */
						__( 'Uncaught error executing a derived state callback with path "%1$s" and namespace "%2$s".' ),
						$path,
						$ns
					),
					'6.6.0'
				);
				return null;
			} finally {
				// Remove the property's namespace from the stack.
				array_pop( $this->namespace_stack );
			}
		}
	}

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