Make WordPress Core


Ignore:
Timestamp:
02/02/2024 08:22:11 PM (6 months ago)
Author:
gziolo
Message:

Editor: Refactor the way block bindings sources are handled

It fixes the coding style issues reported. It goes further and improves the code quality it other places where the logic for block bindings was added.

Follow-up for [57514].
Props: gziolo, mukesh27, youknowriad, santosguillamot.
See #60282.

File:
1 edited

Legend:

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

    r57514 r57526  
    212212     *     "bindings": {
    213213     *       "title": {
    214      *         "source": "post_meta",
     214     *         "source": "core/post-meta",
    215215     *         "args": { "key": "text_custom_field" }
    216216     *       },
    217217     *       "url": {
    218      *         "source": "post_meta",
     218     *         "source": "core/post-meta",
    219219     *         "args": { "key": "url_custom_field" }
    220220     *       }
     
    227227     * block with the values of the `text_custom_field` and `url_custom_field` post meta.
    228228     *
    229      * @access private
    230229     * @since 6.5.0
    231230     *
    232      * @param string   $block_content Block content.
    233      * @param array    $block The full block, including name and attributes.
     231     * @param string $block_content Block content.
     232     * @param array  $block         The full block, including name and attributes.
     233     * @return string The modified block content.
    234234     */
    235235    private function process_block_bindings( $block_content ) {
    236         $block = $this->parsed_block;
     236        $parsed_block = $this->parsed_block;
    237237
    238238        // Allowed blocks that support block bindings.
     
    247247        // If the block doesn't have the bindings property, isn't one of the allowed
    248248        // block types, or the bindings property is not an array, return the block content.
    249         if ( ! isset( $block['attrs']['metadata']['bindings'] ) ||
    250                 ! is_array( $block['attrs']['metadata']['bindings'] ) ||
    251                 ! isset( $allowed_blocks[ $this->name ] )
    252                 ) {
     249        if (
     250            ! isset( $allowed_blocks[ $this->name ] ) ||
     251            empty( $parsed_block['attrs']['metadata']['bindings'] ) ||
     252            ! is_array( $parsed_block['attrs']['metadata']['bindings'] )
     253        ) {
    253254            return $block_content;
    254255        }
    255256
    256         $block_bindings_sources = get_all_registered_block_bindings_sources();
    257257        $modified_block_content = $block_content;
    258         foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) {
    259             // If the attribute is not in the list, process next attribute.
    260             if ( ! in_array( $binding_attribute, $allowed_blocks[ $this->name ], true ) ) {
     258        foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
     259            // If the attribute is not in the allowed list, process next attribute.
     260            if ( ! in_array( $attribute_name, $allowed_blocks[ $this->name ], true ) ) {
    261261                continue;
    262262            }
    263263            // If no source is provided, or that source is not registered, process next attribute.
    264             if ( ! isset( $binding_source['source'] ) || ! is_string( $binding_source['source'] ) || ! isset( $block_bindings_sources[ $binding_source['source'] ] ) ) {
     264            if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) {
    265265                continue;
    266266            }
    267267
    268             $source_callback = $block_bindings_sources[ $binding_source['source'] ]['get_value_callback'];
    269             // Get the value based on the source.
    270             if ( ! isset( $binding_source['args'] ) ) {
    271                 $source_args = array();
    272             } else {
    273                 $source_args = $binding_source['args'];
    274             }
    275             $source_value = call_user_func_array( $source_callback, array( $source_args, $this, $binding_attribute ) );
    276             // If the value is null, process next attribute.
    277             if ( is_null( $source_value ) ) {
     268            $block_binding_source = get_block_bindings_source( $block_binding['source'] );
     269            if ( null === $block_binding_source ) {
    278270                continue;
    279271            }
    280272
    281             // Process the HTML based on the block and the attribute.
    282             $modified_block_content = $this->replace_html( $modified_block_content, $this->name, $binding_attribute, $source_value );
    283         }
     273            $source_callback = $block_binding_source['get_value_callback'];
     274            $source_args     = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array();
     275            $source_value    = call_user_func_array( $source_callback, array( $source_args, $this, $attribute_name ) );
     276
     277            // If the value is not null, process the HTML based on the block and the attribute.
     278            if ( ! is_null( $source_value ) ) {
     279                $modified_block_content = $this->replace_html( $modified_block_content, $attribute_name, $source_value );
     280            }
     281        }
     282
    284283        return $modified_block_content;
    285284    }
    286285
    287286    /**
    288      * Depending on the block attributes, replace the HTML based on the value returned by the source.
     287     * Depending on the block attribute name, replace its value in the HTML based on the value provided.
    289288     *
    290289     * @since 6.5.0
    291290     *
    292      * @param string $block_content Block content.
    293      * @param string $block_name The name of the block to process.
    294      * @param string $block_attr The attribute of the block we want to process.
    295      * @param string $source_value The value used to replace the HTML.
    296      */
    297     private function replace_html( string $block_content, string $block_name, string $block_attr, string $source_value ) {
     291     * @param string $block_content  Block content.
     292     * @param string $attribute_name The attribute name to replace.
     293     * @param mixed  $source_value   The value used to replace in the HTML.
     294     * @return string The modified block content.
     295     */
     296    private function replace_html( string $block_content, string $attribute_name, $source_value ) {
    298297        $block_type = $this->block_type;
    299         if ( null === $block_type || ! isset( $block_type->attributes[ $block_attr ] ) ) {
     298        if ( ! isset( $block_type->attributes[ $attribute_name ] ) ) {
    300299            return $block_content;
    301300        }
    302301
    303302        // Depending on the attribute source, the processing will be different.
    304         switch ( $block_type->attributes[ $block_attr ]['source'] ) {
     303        switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
    305304            case 'html':
    306305            case 'rich-text':
     
    309308                // TODO: Support for CSS selectors whenever they are ready in the HTML API.
    310309                // In the meantime, support comma-separated selectors by exploding them into an array.
    311                 $selectors = explode( ',', $block_type->attributes[ $block_attr ]['selector'] );
     310                $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] );
    312311                // Add a bookmark to the first tag to be able to iterate over the selectors.
    313312                $block_reader->next_tag();
     
    317316                // Store the parent tag and its attributes to be able to restore them later in the button.
    318317                // The button block has a wrapper while the paragraph and heading blocks don't.
    319                 if ( 'core/button' === $block_name ) {
     318                if ( 'core/button' === $this->name ) {
    320319                    $button_wrapper                 = $block_reader->get_tag();
    321320                    $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' );
     
    349348                            $amended_content->set_attribute( $attribute_key, $attribute_value );
    350349                        }
    351                         if ( 'core/paragraph' === $block_name || 'core/heading' === $block_name ) {
     350                        if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) {
    352351                            return $amended_content->get_updated_html();
    353352                        }
    354                         if ( 'core/button' === $block_name ) {
     353                        if ( 'core/button' === $this->name ) {
    355354                            $button_markup  = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>";
    356355                            $amended_button = new WP_HTML_Tag_Processor( $button_markup );
     
    373372                    array(
    374373                        // TODO: build the query from CSS selector.
    375                         'tag_name' => $block_type->attributes[ $block_attr ]['selector'],
     374                        'tag_name' => $block_type->attributes[ $attribute_name ]['selector'],
    376375                    )
    377376                ) ) {
    378377                    return $block_content;
    379378                }
    380                 $amended_content->set_attribute( $block_type->attributes[ $block_attr ]['attribute'], esc_attr( $source_value ) );
     379                $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], esc_attr( $source_value ) );
    381380                return $amended_content->get_updated_html();
    382381                break;
Note: See TracChangeset for help on using the changeset viewer.