wppaste
WordPress

_wptexturize_pushpop_element( string $text, string[] $stack, string[] $disabled_elements )

Since
2.9.0
Source
wp-includes/formatting.php:391
Searches for disabled element tags. Pushes element to stack on tag open and pops on tag close.

Description

Assumes first char of $text is tag opening and last char is tag closing.
Assumes second char of $text is optionally / to indicate closing as in </html>.

Compatibility

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

$textstring
Text to check. Must be a tag like <html> or [shortcode].
$stackstring[]
Array of open tag elements.
$disabled_elementsstring[]
Array of tag names to match against. Spaces are not allowed in tag names.

Performance profile

How much work a call to _wptexturize_pushpop_element() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–33

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
always9–24none
$tag25–29array_push()
$tag && $tag !== false26–30end()
$tag && $tag === false29–33end(), array_pop()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev439–337
8.5439–337
8.4439–33710 fewer instructions than PHP 8.3
8.3539–437
8.2539–437
8.1539–437
7.4539–437

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.

Used by · 1

  • wptexturize()Replaces common plain text characters with formatted entities.

Source code

function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {	// Is it an opening tag or closing tag?	if ( isset( $text[1] ) && '/' !== $text[1] ) {		$opening_tag = true;		$name_offset = 1;	} elseif ( 0 === count( $stack ) ) {		// Stack is empty. Just stop.		return;	} else {		$opening_tag = false;		$name_offset = 2;	} 	// Parse out the tag name.	$space = strpos( $text, ' ' );	if ( false === $space ) {		$space = -1;	} else {		$space -= $name_offset;	}	$tag = substr( $text, $name_offset, $space ); 	// Handle disabled tags.	if ( in_array( $tag, $disabled_elements, true ) ) {		if ( $opening_tag ) {			/*			 * This disables texturize until we find a closing tag of our type			 * (e.g. <pre>) even if there was invalid nesting before that.			 *			 * Example: in the case <pre>sadsadasd</code>"baba"</pre>			 *          "baba" won't be texturized.			 */ 			array_push( $stack, $tag );		} elseif ( end( $stack ) === $tag ) {			array_pop( $stack );		}	}}

Changelog

Introduced in 2.9.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/formatting.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.