wppaste
WordPress

WP_Interactivity_API::_process_directives( string $html ): string|null

Since
6.6.0
Source
wp-includes/interactivity-api/class-wp-interactivity-api.php:492
Processes the interactivity directives contained within the HTML content and updates the markup accordingly.

Description

It uses the WP_Interactivity_API instance's context and namespace stacks, which are shared between all calls.

This method returns null if the HTML contains unbalanced tags.

Compatibility

WordPress
since 6.6.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

$htmlstring
The HTML content to process.

Return value

string|null
The processed HTML content. It returns null when the HTML contains unbalanced tags.

Performance profile

How much work a call to WP_Interactivity_API::_process_directives() 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–72

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Interactivity_API::_process_directives()

Further down the call graph this can also reach query, option, cache, serialize 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 · 12 distinct outcomes

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

WhenInstructionsCalls it makes
!->next_tag()30array_keys(), array_reverse(), ->next_tag()
!->next_tag()32array_keys(), array_reverse(), ->next_tag(), ->get_updated_html()
!->next_tag()38–41array_keys(), array_reverse(), ->next_tag(), array_splice(), array_splice()
!->next_tag()43array_keys(), array_reverse(), ->next_tag(), array_splice(), array_splice(), ->get_updated_html()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && empty($tag_stack)53–55array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && empty($tag_stack)55–57array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), ->get_updated_html()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && !empty($tag_stack)57–59array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), end()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && !empty($tag_stack)59–61array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), end(), ->get_updated_html()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && empty($tag_stack)61–66array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), array_splice(), array_splice()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && !empty($tag_stack)65–70array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), end(), array_splice(), array_splice()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && empty($tag_stack)66–68array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), array_splice(), array_splice(), ->get_updated_html()
->next_tag() && $tag_name !== "SVG" && $tag_name !== "MATH" && ->is_tag_closer() && !empty($tag_stack)70–72array_keys(), array_reverse(), ->next_tag(), ->get_tag(), ->is_tag_closer(), end(), array_splice(), array_splice(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22030–71301 fewer instruction than PHP 8.5
8.522130–7230
8.422130–7230
8.322130–7230
8.222130–7230
8.122130–7230
7.422130–7230

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

  • __()Retrieves the translation of $text.
  • _doing_it_wrong()Marks something as being incorrectly called.
  • WP_Interactivity_API_Directives_Processor::__construct()
  • WP_Interactivity_API::parse_directive_name()Parse the directive name to extract the following parts: - Prefix: The main directive name without "data-wp-". It cannot begin with a hyphen.

Used by · 2

Source code

	private function _process_directives( string $html ) {		$p          = new WP_Interactivity_API_Directives_Processor( $html );		$tag_stack  = array();		$unbalanced = false; 		$directive_processor_prefixes          = array_keys( self::$directive_processors );		$directive_processor_prefixes_reversed = array_reverse( $directive_processor_prefixes ); 		/*		 * Save the current size for each stack to restore them in case		 * the processing finds unbalanced tags.		 */		$namespace_stack_size = count( $this->namespace_stack );		$context_stack_size   = count( $this->context_stack ); 		while ( $p->next_tag( array( 'tag_closers' => 'visit' ) ) ) {			$tag_name = $p->get_tag(); 			/*			 * Directives inside SVG and MATH tags are not processed,			 * as they are not compatible with the Tag Processor yet.			 * We still process the rest of the HTML.			 */			if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) {				if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) {					/* translators: 1: SVG or MATH HTML tag, 2: Namespace of the interactive block. */					$message = sprintf( __( 'Interactivity directives were detected on an incompatible %1$s tag when processing "%2$s". These directives will be ignored in the server side render.' ), $tag_name, end( $this->namespace_stack ) );					_doing_it_wrong( __METHOD__, $message, '6.6.0' );				}				$p->skip_to_tag_closer();				continue;			} 			if ( $p->is_tag_closer() ) {				list( $opening_tag_name, $directives_prefixes ) = ! empty( $tag_stack ) ? end( $tag_stack ) : array( null, null ); 				if ( 0 === count( $tag_stack ) || $opening_tag_name !== $tag_name ) { 					/*					 * If the tag stack is empty or the matching opening tag is not the					 * same than the closing tag, it means the HTML is unbalanced and it					 * stops processing it.					 */					$unbalanced = true;					break;				} else {					// Remove the last tag from the stack.					array_pop( $tag_stack );				}			} else {				$each_child_attrs = $p->get_attribute_names_with_prefix( 'data-wp-each-child' );				if ( null === $each_child_attrs ) {					continue;				} 				if ( 0 !== count( $each_child_attrs ) ) {					/*					 * If the tag has a `data-wp-each-child` directive, jump to its closer					 * tag because those tags have already been processed.					 */					$p->next_balanced_tag_closer_tag();					continue;				} else {					$directives_prefixes = array(); 					// Checks if there is a server directive processor registered for each directive.					foreach ( $p->get_attribute_names_with_prefix( 'data-wp-' ) as $attribute_name ) {						$parsed_directive = $this->parse_directive_name( $attribute_name );						if ( empty( $parsed_directive ) ) {							continue;						}						$directive_prefix = 'data-wp-' . $parsed_directive['prefix'];						if ( array_key_exists( $directive_prefix, self::$directive_processors ) ) {							$directives_prefixes[] = $directive_prefix;						}					} 					/*					 * If this tag will visit its closer tag, it adds it to the tag stack					 * so it can process its closing tag and check for unbalanced tags.

Changelog

Introduced in 6.6.0. Unchanged from 6.7.7 through 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.

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.