Make WordPress Core

Changeset 57641

Timestamp:
02/16/2024 12:53:16 PM (5 months 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.

Location:
trunk
Files:
9 edited

Legend:

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

    r57562 r57641  
    7676 *     The array of arguments that are used to register a source.
    7777 *
    78  *     @type string   $label              The label of the source.
    79  *     @type callback $get_value_callback A callback executed when the source is processed during block rendering.
    80  *                                        The callback should have the following signature:
     78 *     @type string   $label              The label of the source.
     79 *     @type callback $get_value_callback A callback executed when the source is processed during block rendering.
     80 *                                        The callback should have the following signature:
    8181 *
    82  *                                        `function ($source_args, $block_instance,$attribute_name): mixed`
    83  *                                            - @param array    $source_args    Array containing source arguments
    84  *                                                                              used to look up the override value,
    85  *                                                                              i.e. {"key": "foo"}.
    86  *                                            - @param WP_Block $block_instance The block instance.
    87  *                                            - @param string   $attribute_name The name of an attribute .
    88  *                                        The callback has a mixed return type; it may return a string to override
    89  *                                        the block's original value, null, false to remove an attribute, etc.
     82 *                                             `function ($source_args, $block_instance,$attribute_name): mixed`
     83 *                                                 - @param array    $source_args    Array containing source arguments
     84 *                                                                                   used to look up the override value,
     85 *                                                                                   i.e. {"key": "foo"}.
     86 *                                                 - @param WP_Block $block_instance The block instance.
     87 *                                                 - @param string   $attribute_name The name of an attribute .
     88 *                                             The callback has a mixed return type; it may return a string to override
     89 *                                             the block's original value, null, false to remove an attribute, etc.
     90 *     @type array    $uses_context (optional) Array of values to add to block `uses_context` needed by the source.
    9091 * }
    9192 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure.
  • trunk/src/wp-includes/block-bindings/pattern-overrides.php

    r57585 r57641  
    4040            'label'              => _x( 'Pattern Overrides', 'block bindings source' ),
    4141            'get_value_callback' => '_block_bindings_pattern_overrides_get_value',
     42
    4243        )
    4344    );
  • trunk/src/wp-includes/block-bindings/post-meta.php

    r57526 r57641  
    1414 * @access private
    1515 *
    16  * @param array $source_args Array containing source arguments used to look up the override value.
    17  *                           Example: array( "key" => "foo" ).
     16 * @param array    $source_args    Array containing source arguments used to look up the override value.
     17 *                                 Example: array( "key" => "foo" ).
     18 * @param WP_Block $block_instance The block instance.
    1819 * @return mixed The value computed for the source.
    1920 */
    20 function _block_bindings_post_meta_get_value( array $source_args ) {
    21     if ( ! isset( $source_args['key'] ) ) {
     21function _block_bindings_post_meta_get_value( array $source_args ) {
     22    if ( ( $source_args['key'] ) ) {
    2223        return null;
    2324    }
    2425
    25     // Use the postId attribute if available.
    26     if ( isset( $source_args['postId'] ) ) {
    27         $post_id = $source_args['postId'];
    28     } else {
    29         // $block_instance->context['postId'] is not available in the Image block.
    30         $post_id = get_the_ID();
     26    if ( empty( $block_instance->context['postId'] ) ) {
     27        return null;
    3128    }
     29
    3230
    3331    // If a post isn't public, we need to prevent unauthorized users from accessing the post meta.
     
    5250            'label'              => _x( 'Post Meta', 'block bindings source' ),
    5351            'get_value_callback' => '_block_bindings_post_meta_get_value',
     52
    5453        )
    5554    );
  • trunk/src/wp-includes/class-wp-block-bindings-registry.php

    r57575 r57641  
    3434
    3535    /**
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     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        /
    111137        if ( ! isset( $source_properties['label'] ) ) {
    112138            _doing_it_wrong(
     
    118144        }
    119145
    120         /* Validate that the source properties contain the get_value_callback */
     146        /
    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        /
    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
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
    135180                '6.5.0'
    136181            );
     
    145190        $this->sources[ $source_name ] = $source;
    146191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
    147206        return $source;
    148207    }
  • trunk/src/wp-includes/class-wp-block-bindings-source.php

    r57562 r57641  
    4747
    4848    /**
     49
     50
     51
     52
     53
     54
     55
     56
    4957     * Constructor.
    5058     *
     
    5866     */
    5967    public function __construct( string $name, array $source_properties ) {
    60         $this->name               = $name;
    61         $this->label              = $source_properties['label'];
    62         $this->get_value_callback = $source_properties['get_value_callback'];
     68        $this->name = $name;
     69        foreach ( $source_properties as $property_name => $property_value ) {
     70            $this->$property_name = $property_value;
     71        }
    6372    }
    6473
  • trunk/src/wp-includes/class-wp-block-type.php

    r57565 r57641  
    181181     * @var string[]
    182182     */
    183     public $uses_context = array();
     183    p $uses_context = array();
    184184
    185185    /**
     
    367367        }
    368368
     369
     370
     371
     372
    369373        if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
    370374            return;
     
    395399     */
    396400    public function __isset( $name ) {
    397         if ( 'variations' === $name ) {
     401        if ( ) {
    398402            return true;
    399403        }
     
    418422     */
    419423    public function __set( $name, $value ) {
    420         if ( 'variations' === $name ) {
    421             $this->variations = $value;
    422             return;
    423         }
    424 
    425424        if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
    426425            $this->{$name} = $value;
     
    617616        return apply_filters( 'get_block_type_variations', $this->variations, $this );
    618617    }
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
    619637}
  • trunk/src/wp-includes/class-wp-block.php

    r57576 r57641  
    232232     */
    233233    private function process_block_bindings() {
    234         $parsed_block = $this->parsed_block;
    235 
    236         $computed_attributes = array();
    237 
    238         // Allowed blocks that support block bindings.
    239         // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes?
    240         $allowed_blocks = array(
     234        $parsed_block               = $this->parsed_block;
     235        $computed_attributes        = array();
     236        $supported_block_attributes = array(
    241237            'core/paragraph' => array( 'content' ),
    242238            'core/heading'   => array( 'content' ),
     
    245241        );
    246242
    247         // If the block doesn't have the bindings property, isn't one of the allowed
     243        // If the block doesn't have the bindings property, isn't one of the ed
    248244        // block types, or the bindings property is not an array, return the block content.
    249245        if (
    250             ! isset( $allowed_blocks[ $this->name ] ) ||
     246            ! isset( $s[ $this->name ] ) ||
    251247            empty( $parsed_block['attrs']['metadata']['bindings'] ) ||
    252248            ! is_array( $parsed_block['attrs']['metadata']['bindings'] )
     
    256252
    257253        foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
    258             // If the attribute is not in the allowed list, process next attribute.
    259             if ( ! in_array( $attribute_name, $allowed_blocks[ $this->name ], true ) ) {
     254            // If the attribute is not in the ed list, process next attribute.
     255            if ( ! in_array( $attribute_name, $s[ $this->name ], true ) ) {
    260256                continue;
    261257            }
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r57574 r57641  
    116116
    117117    /**
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
    118161     * Tests if the block content is updated with the value returned by the source
    119162     * for the Image block in the placeholder state.
  • trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php

    r57562 r57641  
    4040                return 'test-value';
    4141            },
     42
    4243        );
    4344    }
     
    158159
    159160        self::$test_source_properties['get_value_callback'] = 'not-a-callback';
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
    160178
    161179        $result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
     
    180198            $result
    181199        );
     200
     201
     202
     203
     204
     205
     206
    182207    }
    183208
     
    322347        $this->assertTrue( $result );
    323348    }
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
    324394}
Note: See TracChangeset for help on using the changeset viewer.