Skip to content

Commit e390302

Browse files
committed
Block Bindings: Simplify replace_html() method
1 parent 94cab03 commit e390302

File tree

1 file changed

+6
-38
lines changed

1 file changed

+6
-38
lines changed

src/wp-includes/class-wp-block.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private function replace_html( string $block_content, string $attribute_name, $s
417417
switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
418418
case 'html':
419419
case 'rich-text':
420-
$block_reader = new WP_HTML_Tag_Processor( $block_content );
420+
$block_reader = WP_HTML_Processor::create_fragment( $block_content );
421421

422422
// TODO: Support for CSS selectors whenever they are ready in the HTML API.
423423
// In the meantime, support comma-separated selectors by exploding them into an array.
@@ -426,18 +426,6 @@ private function replace_html( string $block_content, string $attribute_name, $s
426426
$block_reader->next_tag();
427427
$block_reader->set_bookmark( 'iterate-selectors' );
428428

429-
// TODO: This shouldn't be needed when the `set_inner_html` function is ready.
430-
// Store the parent tag and its attributes to be able to restore them later in the button.
431-
// The button block has a wrapper while the paragraph and heading blocks don't.
432-
if ( 'core/button' === $this->name ) {
433-
$button_wrapper = $block_reader->get_tag();
434-
$button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
435-
$button_wrapper_attrs = array();
436-
foreach ( $button_wrapper_attribute_names as $name ) {
437-
$button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name );
438-
}
439-
}
440-
441429
foreach ( $selectors as $selector ) {
442430
// If the parent tag, or any of its children, matches the selector, replace the HTML.
443431
if ( strcasecmp( $block_reader->get_tag(), $selector ) === 0 || $block_reader->next_tag(
@@ -447,32 +435,12 @@ private function replace_html( string $block_content, string $attribute_name, $s
447435
) ) {
448436
$block_reader->release_bookmark( 'iterate-selectors' );
449437

450-
// TODO: Use `set_inner_html` method whenever it's ready in the HTML API.
451-
// Until then, it is hardcoded for the paragraph, heading, and button blocks.
452-
// Store the tag and its attributes to be able to restore them later.
453-
$selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
454-
$selector_attrs = array();
455-
foreach ( $selector_attribute_names as $name ) {
456-
$selector_attrs[ $name ] = $block_reader->get_attribute( $name );
457-
}
458-
$selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>";
459-
$amended_content = new WP_HTML_Tag_Processor( $selector_markup );
460-
$amended_content->next_tag();
461-
foreach ( $selector_attrs as $attribute_key => $attribute_value ) {
462-
$amended_content->set_attribute( $attribute_key, $attribute_value );
463-
}
464-
if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) {
465-
return $amended_content->get_updated_html();
466-
}
467-
if ( 'core/button' === $this->name ) {
468-
$button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>";
469-
$amended_button = new WP_HTML_Tag_Processor( $button_markup );
470-
$amended_button->next_tag();
471-
foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) {
472-
$amended_button->set_attribute( $attribute_key, $attribute_value );
473-
}
474-
return $amended_button->get_updated_html();
438+
// TODO: Use `set_inner_html` method whenever it's ready in the HTML API.)
439+
$block_reader->next_token();
440+
if ( '#text' === $block_reader->get_token_type() ) {
441+
$block_reader->set_modifiable_text( $source_value );
475442
}
443+
return $block_reader->get_updated_html();
476444
} else {
477445
$block_reader->seek( 'iterate-selectors' );
478446
}

0 commit comments

Comments
 (0)