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/tests/phpunit/tests/block-bindings/render.php

    r57574 r57641  
    116116
    117117    /**
     118     * Tests passing `uses_context` as argument to the source.
     119     *
     120     * @ticket 60525
     121     *
     122     * @covers ::register_block_bindings_source
     123     */
     124    public function test_passing_uses_context_to_source() {
     125        $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) {
     126            $value = $block_instance->context['sourceContext'];
     127            return "Value: $value";
     128        };
     129
     130        register_block_bindings_source(
     131            self::SOURCE_NAME,
     132            array(
     133                'label'              => self::SOURCE_LABEL,
     134                'get_value_callback' => $get_value_callback,
     135                'uses_context'       => array( 'sourceContext' ),
     136            )
     137        );
     138
     139        $block_content = <<<HTML
     140<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "test"}}}}} -->
     141<p>This should not appear</p>
     142<!-- /wp:paragraph -->
     143HTML;
     144        $parsed_blocks = parse_blocks( $block_content );
     145        $block         = new WP_Block( $parsed_blocks[0], array( 'sourceContext' => 'source context value' ) );
     146        $result        = $block->render();
     147
     148        $this->assertSame(
     149            'Value: source context value',
     150            $block->attributes['content'],
     151            "The 'content' should be updated with the value of the source context."
     152        );
     153        $this->assertSame(
     154            '<p>Value: source context value</p>',
     155            trim( $result ),
     156            'The block content should be updated with the value of the source context.'
     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.
Note: See TracChangeset for help on using the changeset viewer.