Make WordPress Core


Ignore:
Timestamp:
02/16/2024 12:53:16 PM (3 years ago)
Author:
gziolo
Message:

Editor: Merge uses_context defined by block bindings sources with block types

Adds logic that fixes the limitation for souces by allowing merging the uses_context defined by block bindings sources into supported block types. Each source defines the context it needs and it is added to the block types that are using the block bindings API.

Fixes #60525.
Props santosguillamot, gziolo, czapla, thekt12.

File:
1 edited

Legend:

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

    r57575 r57641  
    3434
    3535        /**
     36         * Supported source properties that can be passed to the registered source.
     37         *
     38         * @since 6.5.0
     39         * @var array
     40         */
     41        private $allowed_source_properties = array(
     42                'label',
     43                'get_value_callback',
     44                'uses_context',
     45        );
     46
     47        /**
     48         * Supported blocks that can use the block bindings API.
     49         *
     50         * @since 6.5.0
     51         * @var array
     52         */
     53        private $supported_blocks = array(
     54                'core/paragraph',
     55                'core/heading',
     56                'core/image',
     57                'core/button',
     58        );
     59
     60        /**
    3661         * Registers a new block bindings source.
    3762         *
    … …  
    5479         *     The array of arguments that are used to register a source.
    5580         *
    56          *     @type string   $label              The label of the source.
    57          *     @type callback $get_value_callback A callback executed when the source is processed during block rendering.
    58          *                                        The callback should have the following signature:
    59          *
    60          *                                        `function ($source_args, $block_instance,$attribute_name): mixed`
    61          *                                            - @param array    $source_args    Array containing source arguments
    62          *                                                                              used to look up the override value,
    63          *                                                                              i.e. {"key": "foo"}.
    64          *                                            - @param WP_Block $block_instance The block instance.
    65          *                                            - @param string   $attribute_name The name of the target attribute.
    66          *                                        The callback has a mixed return type; it may return a string to override
    67          *                                        the block's original value, null, false to remove an attribute, etc.
     81         *     @type string   $label                   The label of the source.
     82         *     @type callback $get_value_callback      A callback executed when the source is processed during block rendering.
     83         *                                             The callback should have the following signature:
     84         *
     85         *                                             `function ($source_args, $block_instance,$attribute_name): mixed`
     86         *                                                 - @param array    $source_args    Array containing source arguments
     87         *                                                                                   used to look up the override value,
     88         *                                                                                   i.e. {"key": "foo"}.
     89         *                                                 - @param WP_Block $block_instance The block instance.
     90         *                                                 - @param string   $attribute_name The name of the target attribute.
     91         *                                             The callback has a mixed return type; it may return a string to override
     92         *                                             the block's original value, null, false to remove an attribute, etc.
     93         *     @type array    $uses_context (optional) Array of values to add to block `uses_context` needed by the source.
    6894         * }
    6995         * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure.
    … …  
    108134                }
    109135
    110                 /* Validate that the source properties contain the label */
     136                // Validates that the source properties contain the label.
    111137                if ( ! isset( $source_properties['label'] ) ) {
    112138                        _doing_it_wrong(
    … …  
    118144                }
    119145
    120                 /* Validate that the source properties contain the get_value_callback */
     146                // Validates that the source properties contain the get_value_callback.
    121147                if ( ! isset( $source_properties['get_value_callback'] ) ) {
    122148                        _doing_it_wrong(
    … …  
    128154                }
    129155
    130                 /* Validate that the get_value_callback is a valid callback */
     156                // Validates that the get_value_callback is a valid callback.
    131157                if ( ! is_callable( $source_properties['get_value_callback'] ) ) {
    132158                        _doing_it_wrong(
    133159                                __METHOD__,
    134160                                __( 'The "get_value_callback" parameter must be a valid callback.' ),
     161                                '6.5.0'
     162                        );
     163                        return false;
     164                }
     165
     166                // Validates that the uses_context parameter is an array.
     167                if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) {
     168                        _doing_it_wrong(
     169                                __METHOD__,
     170                                __( 'The "uses_context" parameter must be an array.' ),
     171                                '6.5.0'
     172                        );
     173                        return false;
     174                }
     175
     176                if ( ! empty( array_diff( array_keys( $source_properties ), $this->allowed_source_properties ) ) ) {
     177                        _doing_it_wrong(
     178                                __METHOD__,
     179                                __( 'The $source_properties array contains invalid properties.' ),
    135180                                '6.5.0'
    136181                        );
    … …  
    145190                $this->sources[ $source_name ] = $source;
    146191
     192                // Adds `uses_context` defined by block bindings sources.
     193                add_filter(
     194                        'get_block_type_uses_context',
     195                        function ( $uses_context, $block_type ) use ( $source ) {
     196                                if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) {
     197                                        return $uses_context;
     198                                }
     199                                // Use array_values to reset the array keys.
     200                                return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) );
     201                        },
     202                        10,
     203                        2
     204                );
     205
    147206                return $source;
    148207        }
Note: See TracChangeset for help on using the changeset viewer.