class@anonymous::replace_rich_text( string $rich_text, int[] $inner_block_offsets = array() ): bool
- Source
wp-includes/class-wp-block.php:456
Description
When stopped on a tag opener, replace the content enclosed by it and its matching closer with the provided rich text.
If byte offsets of inner blocks' rendered output are provided, the replacement stops at the first inner block found inside the element, preserving any markup produced by nested inner blocks (e.g. a List block nested inside a List Item).
Compatibility
- WordPress
- core
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
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.
$inner_block_offsetsint[]optional- Byte offsets in the source HTML where inner blocks' rendered output begins. Default empty array.Default:
array()
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
- Scaling
- Scales with input
- Instructions
- 6–60
- 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 62.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
->is_tag_closer() | 6 | ->is_tag_closer() |
!->is_tag_closer() && !->expects_closer() | 9 | ->is_tag_closer(), ->expects_closer() |
!->is_tag_closer() && ->expects_closer() && !->next_token() | 29 | ->is_tag_closer(), ->expects_closer(), ->get_current_depth(), ->get_tag(), ->set_bookmark(), ->next_token(), ->is_tag_closer() |
->expects_closer() && !->next_token() && $tag_name !== false | 33 | ->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() && !$depth | 33 | ->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 !== false | 37 | ->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 === false | 50–56 | ->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 === false | 54–60 | ->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: 62 instructions, 6–60 executed per call, 10 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, $inner_block_offsets = array() ) { 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; /* * Stop at the first inner block that renders inside this element so * its markup is preserved. The block's own rich text always precedes * its inner blocks, so replacing up to the first inner block offset * replaces only that rich text. Offsets are recorded during render in * the same byte coordinates as this fragment, and are in ascending * order, so the first match is the earliest inner block. * * The lower bound is inclusive of `$start`: when an inner block * begins immediately, with no leading rich text, the (empty) rich * text is still replaced instead of the inner block markup. */ foreach ( $inner_block_offsets as $inner_block_offset ) { if ( $inner_block_offset >= $start && $inner_block_offset < $end ) { $end = $inner_block_offset; break; } } $this->lexical_updates[] = new WP_HTML_Text_Replacement( $start, $end - $start, $rich_text ); return true; }Changelog
One change between 6.9.7 and 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
$inner_block_offsets added.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.