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
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
- Scaling
- Scales with input
- Instructions
- 30–67
- Plugin surface
- None
- Called by
- 5
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- serializeserialisation
json_encode()called directly - hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 30–31 | end(), __(), json_encode(), sprintf(), _doing_it_wrong() |
| always | 39–63 | end(), explode() |
!$path_segments && $path_segment === "length" && is_array($current) | 52–67 | end(), explode(), array_is_list() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 157 | 30–67 | 20 | |
| 8.5 | 157 | 30–67 | 20 | |
| 8.4 | 157 | 30–67 | 20 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 167 | 30–71 | 20 | |
| 8.2 | 167 | 30–71 | 20 | |
| 8.1 | 167 | 30–71 | 20 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 168 | 30–72 | 20 |
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
- __()Retrieves the translation of $text.
- _doing_it_wrong()Marks something as being incorrectly called.
- array_is_list()Polyfill for `array_is_list()` function added in PHP 8.1.
Used by · 5
- WP_Interactivity_API::data_wp_bind_processor()Processes the `data-wp-bind` directive.
- WP_Interactivity_API::data_wp_class_processor()Processes the `data-wp-class` directive.
- WP_Interactivity_API::data_wp_each_processor()Processes the `data-wp-each` directive.
- WP_Interactivity_API::data_wp_style_processor()Processes the `data-wp-style` directive.
- WP_Interactivity_API::data_wp_text_processor()Processes the `data-wp-text` directive.
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.
Signature, return type and hooks compared across 5 parsed releases.
$entry added.verified against source$directive_value removed.verified against sourcedefault_namespace and context arguments.from the docblockAbout 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.