wppaste
WordPress

WP_Interactivity_API::data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array $tag_stack )

Since
6.5.0
Source
wp-includes/interactivity-api/class-wp-interactivity-api.php:1180
Processes the 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.5.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), 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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
6–126

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 20 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.

WhenInstructionsCalls it makes
$mode !== "enter"6none
$mode === "enter"10->get_tag()
$mode === "enter" && !isset($extracted_suffix)53–57->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_pop()
$mode === "enter" && isset($extracted_suffix)59–63->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_pop()
$mode === "enter" && !isset($extracted_suffix) && !empty($result) && is_array($result)61–64->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result)67–70->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), array_pop()
$mode === "enter" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && !str_ends_with()70->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->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" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && !str_ends_with()76->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->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" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with()89–92->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->append_content_after_template_tag_closer(), array_pop()
$mode === "enter" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value)94–95->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), ->append_content_after_template_tag_closer(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with()95–98->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->append_content_after_template_tag_closer(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value)100–101->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), ->append_content_after_template_tag_closer(), array_pop()
8 further outcomes, up to 126 instructions
$mode === "enter" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null110–112->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->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" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null115–117->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), end(), end(), ->_process_directives(), array_pop()
$mode === "enter" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value) && !$result && $processed_item === null115->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), end(), ->_process_directives(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null116–118->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->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" && !isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value) && !$result && $processed_item === null120->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), end(), end(), ->_process_directives(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && !$result && $processed_item === null121–123->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), end(), end(), ->_process_directives(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value) && !$result && $processed_item === null121->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), end(), ->_process_directives(), array_pop()
$mode === "enter" && isset($extracted_suffix) && !empty($result) && is_array($result) && array_is_list() && str_ends_with() && is_string($attribute_value) && !empty($attribute_value) && !$result && $processed_item === null126->get_tag(), ->get_attribute_names_with_prefix(), ->extract_prefix_and_suffix(), ->kebab_to_camel_case(), ->get_attribute(), ->evaluate(), ->get_content_between_balanced_template_tags(), ->set_bookmark(), ->next_tag(), ->get_attribute(), ->seek(), ->release_bookmark(), array_is_list(), str_ends_with(), end(), ->extract_directive_value(), end(), end(), ->_process_directives(), array_pop()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1626–123163 fewer instructions than PHP 8.5
8.51656–12616
8.41656–126167 fewer instructions than PHP 8.3
8.31726–13316
8.21726–13316
8.11726–13316
7.41726–13316

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 · 9

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() ) {			$attribute_name   = $p->get_attribute_names_with_prefix( 'data-wp-each' )[0];			$extracted_suffix = $this->extract_prefix_and_suffix( $attribute_name );			$item_name        = isset( $extracted_suffix[1] ) ? $this->kebab_to_camel_case( $extracted_suffix[1] ) : 'item';			$attribute_value  = $p->get_attribute( $attribute_name );			$result           = $this->evaluate( $attribute_value ); 			// 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;			} 			// Extracts the namespace from the directive attribute value.			$namespace_value         = end( $this->namespace_stack );			list( $namespace_value ) = is_string( $attribute_value ) && ! empty( $attribute_value )				? $this->extract_directive_value( $attribute_value, $namespace_value )				: array( $namespace_value, null ); 			// 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( $namespace_value => 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` to each top-level tag.				$i = new WP_Interactivity_API_Directives_Processor( $processed_item );				while ( $i->next_tag() ) {					$i->set_attribute( 'data-wp-each-child', true );					$i->next_balanced_tag_closer_tag();				}				$processed_content .= $i->get_updated_html(); 				// Removes the current context from the stack.				array_pop( $this->context_stack );			} 			// Appends the processed content after the tag closer of the template.			$p->append_content_after_template_tag_closer( $processed_content ); 			// Pops the last tag because it skipped the closing tag of the template tag.			array_pop( $tag_stack );

Changelog

Introduced in 6.5.0. Unchanged from 6.7.10 through 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

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

About this page

Parsed data
Generated from the wordpress-develop 6.8.11 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.