Source
private static function get_block_bindings_processor( string $block_content ) {
$internal_processor_class = new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE) extends WP_HTML_Processor {
/**
* Replace the rich text content between a tag opener and matching closer.
*
* 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).
*
* @param string $rich_text The rich text to replace the original content with.
* @param int[] $inner_block_offsets Optional. Byte offsets in the source HTML where
* inner blocks' rendered output begins. Default empty array.
* @return bool True on success.
*/
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;
}
};
return $internal_processor_class::create_fragment( $block_content );
}
View all references View on Trac View on GitHub
User Contributed Notes
You must log in before being able to contribute a note or feedback.