Make WordPress Core


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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, $block_instance ) {
     22    if ( empty( $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    $post_id = $block_instance->context['postId'];
    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            'uses_context'       => array( 'postId', 'postType' ),
    5453        )
    5554    );
Note: See TracChangeset for help on using the changeset viewer.