wppaste
WordPress

class@anonymous::replace_rich_text( string $rich_text ): bool

Source
wp-includes/class-wp-block.php:439
Replace the rich text content between a tag opener and matching closer.

Description

When stopped on a tag opener, replace the content enclosed by it and its matching closer with the provided rich text.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in 3 of the 5 tracked releases, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$rich_textstring
The rich text to replace the original content with.

Return value

bool
True on success.

Performance profile

How much work a call to class@anonymous::replace_rich_text() 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
5–51

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

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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
->is_tag_closer()5->is_tag_closer()
!->is_tag_closer() && !->expects_closer()8->is_tag_closer(), ->expects_closer()
!->is_tag_closer() && ->expects_closer() && !->next_token()28->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->is_tag_closer()
->expects_closer() && !->next_token() && $tag_name !== false32->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->is_tag_closer(), ->get_tag()
!->is_tag_closer() && ->expects_closer() && ->next_token() && !$depth32->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->get_current_depth(), ->is_tag_closer()
->expects_closer() && ->next_token() && !$depth && $tag_name !== false36->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->get_current_depth(), ->is_tag_closer(), ->get_tag()
->expects_closer() && !->next_token() && $tag_name === false47->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->is_tag_closer(), ->get_tag(), ->set_bookmark()
->expects_closer() && ->next_token() && !$depth && $tag_name === false51->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->get_current_depth(), ->is_tag_closer(), ->get_tag(), ->set_bookmark()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 53 instructions, 5–51 executed per call, 6 branches. The work does not change between versions.

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

  • \\class@anonymous::is_tag_closer()
  • \\class@anonymous::expects_closer()
  • \\class@anonymous::get_current_depth()
  • \\class@anonymous::get_tag()
  • \\class@anonymous::set_bookmark()
  • \\class@anonymous::next_token()
  • WP_HTML_Text_Replacement::__construct()Constructor.

Source code

			public function replace_rich_text( $rich_text ) {				if ( $this->is_tag_closer() || ! $this->expects_closer() ) {					return false;				} 				$depth    = $this->get_current_depth();				$tag_name = $this->get_tag(); 				$this->set_bookmark( '_wp_block_bindings' );				// The bookmark names are prefixed with `_` so the key below has an extra `_`.				$tag_opener = $this->bookmarks['__wp_block_bindings'];				$start      = $tag_opener->start + $tag_opener->length; 				// Find matching tag closer.				while ( $this->next_token() && $this->get_current_depth() >= $depth ) {				} 				if ( ! $this->is_tag_closer() || $tag_name !== $this->get_tag() ) {					return false;				} 				$this->set_bookmark( '_wp_block_bindings' );				$tag_closer = $this->bookmarks['__wp_block_bindings'];				$end        = $tag_closer->start; 				$this->lexical_updates[] = new WP_HTML_Text_Replacement(					$start,					$end - $start,					$rich_text				); 				return true;			}

Changelog

One change between 6.9.10 and 7.1.3.

  1. 6.9.10
  2. 7.0.7
  3. 7.1.3

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

7.1.3
Parameter $inner_block_offsets added.verified against source

About this page

Parsed data
Generated from the wordpress-develop 7.0.7 tag, from src/wp-includes/class-wp-block.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.