wppaste
WordPress

WP_Interactivity_API::evaluate( array $entry ): mixed|null

Since
6.5.0, 6.6.0, 6.6.0, 6.6.0, 6.9.0
Source
wp-includes/interactivity-api/class-wp-interactivity-api.php:663
Evaluates the reference path passed to a directive based on the current store namespace, state and context.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$entryarray
An array containing a whole directive entry with its namespace, value, suffix, or unique ID.

Return value

mixed|null
The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.

Performance profile

How much work a call to WP_Interactivity_API::evaluate() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
30–67

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • serializeserialisationjson_encode()called directly
  • hookthird-party callbacksdo_action()one call below WP_Interactivity_API::evaluate()

Further down the call graph this can also reach query, option, cache and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Interactivity_API::evaluate() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always30–31end(), __(), json_encode(), sprintf(), _doing_it_wrong()
always39–63end(), explode()
!$path_segments && $path_segment === "length" && is_array($current)52–67end(), explode(), array_is_list()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15730–6720
8.515730–6720
8.415730–672010 fewer instructions than PHP 8.3
8.316730–7120
8.216730–7120
8.116730–71201 fewer instruction than PHP 7.4
7.416830–7220

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

Used by · 5

Source code

	private function evaluate( $entry ) {		$context                               = end( $this->context_stack );		['namespace' => $ns, 'value' => $path] = $entry; 		if ( ! $ns || ! $path ) {			/* translators: %s: The directive value referenced. */			$message = sprintf( __( 'Namespace or reference path cannot be empty. Directive value referenced: %s' ), json_encode( $entry ) );			_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 $index => $path_segment ) {			/*			 * Special case for numeric arrays and strings. Add length			 * property mimicking JavaScript behavior.			 *			 * @since 6.8.0			 */			if ( 'length' === $path_segment ) {				if ( is_array( $current ) && array_is_list( $current ) ) {					$current = count( $current );					break;				} 				if ( is_string( $current ) ) {					/*					 * Differences in encoding between PHP strings and					 * JavaScript mean that it's complicated to calculate					 * the string length JavaScript would see from PHP.					 * `strlen` is a reasonable approximation.					 *					 * Users that desire a more precise length likely have					 * more precise needs than "bytelength" and should					 * implement their own length calculation in derived					 * state taking into account encoding and their desired					 * output (codepoints, graphemes, bytes, etc.).					 */					$current = strlen( $current );					break;				}			} 			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(); 					/*					 * Tracks derived state properties that are accessed during					 * rendering.					 *					 * @since 6.9.0					 */					$this->derived_state_closures[ $ns ] = $this->derived_state_closures[ $ns ] ?? array();

Changelog

Introduced in 6.5.0. 2 changes between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $entry added.verified against source
6.9.7
Parameter $directive_value removed.verified against source
6.9.0
Receive $entry as an argument instead of the directive value string.from the docblock
6.6.0
Add support for derived state.from the docblock
6.6.0
Removed default_namespace and context arguments.from the docblock
6.6.0
The function now adds a warning when the namespace is null, falsy, or the directive value is empty.from the docblock
6.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/interactivity-api/class-wp-interactivity-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.