WP_Interactivity_API::data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array $tag_stack )
- Since
- 6.5.0, 6.9.0
- Source
wp-includes/interactivity-api/class-wp-interactivity-api.php:1537
data-wp-each directive.Description
This directive gets an array passed as reference and iterates over it generating new content for each item based on the inner markup of the template tag.
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
$pWP_Interactivity_API_Directives_Processor- The directives processor instance.
$modestring- Whether the processing is entering or exiting the tag.
$tag_stackarray- The reference to the tag stack.
Performance profile
How much work a call to WP_Interactivity_API::data_wp_each_processor() 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
- 6–111
- Plugin surface
- None
- Called by
- 0
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 153.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Interactivity_API::data_wp_each_processor() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$mode !== "enter" | 6 | none |
$mode === "enter" | 10 | ->get_tag() |
$mode === "enter" | 18–24 | ->get_tag(), ->get_directive_entries() |
$mode === "enter" && !empty($entries) && !isset($entry) | 55–59 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) | 61–65 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_pop() |
$mode === "enter" && !empty($entries) && !isset($entry) && !empty($result) && is_array($result) | 63–66 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) && !empty($result) && is_array($result) | 69–72 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), array_pop() |
$mode === "enter" && !empty($entries) && !isset($entry) && !empty($result) && is_array($result) && array_is_list() && !str_ends_with() | 72 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), array_pop() |
$mode === "enter" && !empty($entries) && !isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() | 78–79 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), ->append_content_after_template_tag_closer(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) && !empty($result) && is_array($result) && array_is_list() && !str_ends_with() | 78 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() | 84–85 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), ->append_content_after_template_tag_closer(), array_pop() |
$mode === "enter" && !empty($entries) && !isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null | 100 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->_process_directives(), array_pop() |
3 further outcomes, up to 111 instructions
$mode === "enter" && !empty($entries) && !isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null | 105 | ->get_tag(), ->get_directive_entries(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), end(), ->_process_directives(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null | 106 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->_process_directives(), array_pop() |
$mode === "enter" && !empty($entries) && isset($entry) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null | 111 | ->get_tag(), ->get_directive_entries(), ->kebab_to_camel_case(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), end(), ->_process_directives(), array_pop() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 151 | 6–109 | 17 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 153 | 6–111 | 17 | |
| 8.4 | 153 | 6–111 | 17 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 160 | 6–118 | 17 | |
| 8.2 | 160 | 6–118 | 17 | |
| 8.1 | 160 | 6–118 | 17 | |
| 7.4 | 160 | 6–118 | 17 |
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 · 8
- array_is_list()Polyfill for `array_is_list()` function added in PHP 8.1.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- WP_Interactivity_API::get_directive_entries()Parse the HTML element and get all the valid directives with the given prefix.
- WP_Interactivity_API::kebab_to_camel_case()Transforms a kebab-case string to camelCase.
- WP_Interactivity_API::evaluate()Evaluates the reference path passed to a directive based on the current store namespace, state and context.
- WP_Interactivity_API::_process_directives()Processes the interactivity directives contained within the HTML content and updates the markup accordingly.
- WP_Interactivity_API_Directives_Processor::__construct()
Source code
private function data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$tag_stack ) { if ( 'enter' === $mode && 'TEMPLATE' === $p->get_tag() ) { $entries = $this->get_directive_entries( $p, 'each' ); if ( count( $entries ) > 1 || empty( $entries ) ) { // There should be only one `data-wp-each` directive per template tag. return; } $entry = $entries[0]; if ( null !== $entry['unique_id'] ) { return; } $item_name = isset( $entry['suffix'] ) ? $this->kebab_to_camel_case( $entry['suffix'] ) : 'item'; $result = $this->evaluate( $entry ); // Gets the content between the template tags and leaves the cursor in the closer tag. $inner_content = $p->get_content_between_balanced_template_tags(); // Checks if there is a manual server-side directive processing. $template_end = 'data-wp-each: template end'; $p->set_bookmark( $template_end ); $p->next_tag(); $manual_sdp = $p->get_attribute( 'data-wp-each-child' ); $p->seek( $template_end ); // Rewinds to the template closer tag. $p->release_bookmark( $template_end ); /* * It doesn't process in these situations: * - Manual server-side directive processing. * - Empty or non-array values. * - Associative arrays because those are deserialized as objects in JS. * - Templates that contain top-level texts because those texts can't be * identified and removed in the client. */ if ( $manual_sdp || empty( $result ) || ! is_array( $result ) || ! array_is_list( $result ) || ! str_starts_with( trim( $inner_content ), '<' ) || ! str_ends_with( trim( $inner_content ), '>' ) ) { array_pop( $tag_stack ); return; } // Processes the inner content for each item of the array. $processed_content = ''; foreach ( $result as $item ) { // Creates a new context that includes the current item of the array. $this->context_stack[] = array_replace_recursive( end( $this->context_stack ) !== false ? end( $this->context_stack ) : array(), array( $entry['namespace'] => array( $item_name => $item ) ) ); // Processes the inner content with the new context. $processed_item = $this->_process_directives( $inner_content ); if ( null === $processed_item ) { // If the HTML is unbalanced, stop processing it. array_pop( $this->context_stack ); return; } /* * Adds the `data-wp-each-child` directive to each top-level tag * rendered by this `data-wp-each` directive. The value is the * `data-wp-each` directive's namespace and path. * * Nested `data-wp-each` directives could render * `data-wp-each-child` elements at the top level as well, and * they should be overwritten. * * @since 6.9.0 */ $i = new WP_Interactivity_API_Directives_Processor( $processed_item ); while ( $i->next_tag() ) { $i->set_attribute( 'data-wp-each-child', $entry['namespace'] . '::' . $entry['value'] ); $i->next_balanced_tag_closer_tag(); } $processed_content .= $i->get_updated_html();Changelog
Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
data-wp-each-child directives.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.