Make WordPress Core


Ignore:
Timestamp:
09/23/2024 12:33:14 PM (2 years ago)
Author:
gziolo
Message:

Block Bindings: Adds context needed by sources during its processing

Extends block context during block bindings processing. This implies that the context is extended ONLY for the blocks where bindings are defined and only when rendered on the page.

Props santosguillamot, gziolo, artemiosans, cbravobernal.
Fixes #61642.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r58972 r59080  
    159159
    160160        /**
     161         * Tests that blocks can only access the context from the specific source.
     162         *
     163         * @ticket 61642
     164         *
     165         * @covers ::register_block_bindings_source
     166         */
     167        public function test_blocks_can_just_access_the_specific_uses_context() {
     168                register_block_bindings_source(
     169                        'test/source-one',
     170                        array(
     171                                'label'              => 'Test Source One',
     172                                'get_value_callback' => function () {
     173                                        return;
     174                                },
     175                                'uses_context'       => array( 'contextOne' ),
     176                        )
     177                );
     178
     179                register_block_bindings_source(
     180                        'test/source-two',
     181                        array(
     182                                'label'              => 'Test Source Two',
     183                                'get_value_callback' => function ( $source_args, $block_instance, $attribute_name ) {
     184                                        $value = $block_instance->context['contextTwo'];
     185                                        // Try to use the context from source one, which shouldn't be available.
     186                                        if ( ! empty( $block_instance->context['contextOne'] ) ) {
     187                                                $value = $block_instance->context['contextOne'];
     188                                        }
     189                                        return "Value: $value";
     190                                },
     191                                'uses_context'       => array( 'contextTwo' ),
     192                        )
     193                );
     194
     195                $block_content = <<<HTML
     196<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source-two", "args": {"key": "test"}}}}} -->
     197<p>Default content</p>
     198<!-- /wp:paragraph -->
     199HTML;
     200                $parsed_blocks = parse_blocks( $block_content );
     201                $block         = new WP_Block(
     202                        $parsed_blocks[0],
     203                        array(
     204                                'contextOne' => 'source one context value',
     205                                'contextTwo' => 'source two context value',
     206                        )
     207                );
     208                $result        = $block->render();
     209
     210                $this->assertSame(
     211                        'Value: source two context value',
     212                        $block->attributes['content'],
     213                        "The 'content' should be updated with the value of the second source context value."
     214                );
     215                $this->assertSame(
     216                        '<p>Value: source two context value</p>',
     217                        trim( $result ),
     218                        'The block content should be updated with the value of the source context.'
     219                );
     220        }
     221
     222        /**
    161223         * Tests if the block content is updated with the value returned by the source
    162224         * for the Image block in the placeholder state.
Note: See TracChangeset for help on using the changeset viewer.