Make WordPress Core


Ignore:
Timestamp:
08/27/2025 03:41:36 PM (10 months ago)
Author:
Bernhard Reiter
Message:

Block Bindings: Allow generically setting rich-text block attributes.

Replace the existing block-specific, hard-coded, logic in the WP_Block class with more generic code that is able to locate and replace a rich-text sourced attribute based on the selector definition in its block.json.

This should make it easier to add block bindings support for more block attributes.

Props bernhard-reiter, jonsurrell, gziolo, cbravobernal, dmsnell.
Fixes #63840.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block.php

    r60663 r60684  
    417417            case 'html':
    418418            case 'rich-text':
    419                 $block_reader = new WP_HTML_Tag_Processor( $block_content );
     419                $block_reader = self::get_block_bindings_processor( $block_content );
    420420
    421421                // TODO: Support for CSS selectors whenever they are ready in the HTML API.
     
    425425                $block_reader->next_tag();
    426426                $block_reader->set_bookmark( 'iterate-selectors' );
    427 
    428                 // TODO: This shouldn't be needed when the `set_inner_html` function is ready.
    429                 // Store the parent tag and its attributes to be able to restore them later in the button.
    430                 // The button block has a wrapper while the paragraph and heading blocks don't.
    431                 if ( 'core/button' === $this->name ) {
    432                     $button_wrapper                 = $block_reader->get_tag();
    433                     $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
    434                     $button_wrapper_attrs           = array();
    435                     foreach ( $button_wrapper_attribute_names as $name ) {
    436                         $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name );
    437                     }
    438                 }
    439427
    440428                foreach ( $selectors as $selector ) {
     
    445433                        )
    446434                    ) ) {
     435                        // TODO: Use `WP_HTML_Processor::set_inner_html` method once it's available.
    447436                        $block_reader->release_bookmark( 'iterate-selectors' );
    448 
    449                         // TODO: Use `set_inner_html` method whenever it's ready in the HTML API.
    450                         // Until then, it is hardcoded for the paragraph, heading, and button blocks.
    451                         // Store the tag and its attributes to be able to restore them later.
    452                         $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
    453                         $selector_attrs           = array();
    454                         foreach ( $selector_attribute_names as $name ) {
    455                             $selector_attrs[ $name ] = $block_reader->get_attribute( $name );
    456                         }
    457                         $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>";
    458                         $amended_content = new WP_HTML_Tag_Processor( $selector_markup );
    459                         $amended_content->next_tag();
    460                         foreach ( $selector_attrs as $attribute_key => $attribute_value ) {
    461                             $amended_content->set_attribute( $attribute_key, $attribute_value );
    462                         }
    463                         if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) {
    464                             return $amended_content->get_updated_html();
    465                         }
    466                         if ( 'core/button' === $this->name ) {
    467                             $button_markup  = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>";
    468                             $amended_button = new WP_HTML_Tag_Processor( $button_markup );
    469                             $amended_button->next_tag();
    470                             foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) {
    471                                 $amended_button->set_attribute( $attribute_key, $attribute_value );
    472                             }
    473                             return $amended_button->get_updated_html();
    474                         }
     437                        $block_reader->replace_rich_text( wp_kses_post( $source_value ) );
     438                        return $block_reader->get_updated_html();
    475439                    } else {
    476440                        $block_reader->seek( 'iterate-selectors' );
     
    498462    }
    499463
     464    private static function get_block_bindings_processor( string $block_content ) {
     465        $internal_processor_class = new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE) extends WP_HTML_Processor {
     466            /**
     467             * Replace the rich text content between a tag opener and matching closer.
     468             *
     469             * When stopped on a tag opener, replace the content enclosed by it and its
     470             * matching closer with the provided rich text.
     471             *
     472             * @param string $rich_text The rich text to replace the original content with.
     473             * @return bool True on success.
     474             */
     475            public function replace_rich_text( $rich_text ) {
     476                if ( $this->is_tag_closer() || ! $this->expects_closer() ) {
     477                    return false;
     478                }
     479
     480                $depth = $this->get_current_depth();
     481
     482                $this->set_bookmark( '_wp_block_bindings_tag_opener' );
     483                // The bookmark names are prefixed with `_` so the key below has an extra `_`.
     484                $tag_opener = $this->bookmarks['__wp_block_bindings_tag_opener'];
     485                $start      = $tag_opener->start + $tag_opener->length;
     486                $this->release_bookmark( '_wp_block_bindings_tag_opener' );
     487
     488                // Find matching tag closer.
     489                while ( $this->next_token() && $this->get_current_depth() >= $depth ) {
     490                }
     491
     492                $this->set_bookmark( '_wp_block_bindings_tag_closer' );
     493                $tag_closer  = $this->bookmarks['__wp_block_bindings_tag_closer'];
     494                $end         = $tag_closer->start;
     495                $this->release_bookmark( '_wp_block_bindings_tag_closer' );
     496
     497                $this->lexical_updates[] = new WP_HTML_Text_Replacement(
     498                    $start,
     499                    $end - $start,
     500                    $rich_text
     501                );
     502
     503                return true;
     504            }
     505        };
     506
     507        return $internal_processor_class::create_fragment( $block_content );
     508    }
    500509
    501510    /**
Note: See TracChangeset for help on using the changeset viewer.