Make WordPress Core

Changeset 59080


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.

Location:
trunk
Files:
4 edited

Legend:

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

    r58395 r59080  
    190190                $this->sources[ $source_name ] = $source;
    191191
    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 
    206192                return $source;
    207193        }
  • trunk/src/wp-includes/class-wp-block.php

    r59009 r59080  
    300300                        if ( null === $block_binding_source ) {
    301301                                continue;
     302                        }
     303
     304                        // Adds the necessary context defined by the source.
     305                        if ( ! empty( $block_binding_source->uses_context ) ) {
     306                                foreach ( $block_binding_source->uses_context as $context_name ) {
     307                                        if ( array_key_exists( $context_name, $this->available_context ) ) {
     308                                                $this->context[ $context_name ] = $this->available_context[ $context_name ];
     309                                        }
     310                                }
    302311                        }
    303312
  • 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.
  • trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php

    r58799 r59080  
    345345                $this->assertTrue( $result );
    346346        }
    347 
    348         /**
    349          * Tests merging `uses_context` from multiple sources.
    350          *
    351          * @ticket 60525
    352          *
    353          * @covers ::register_block_bindings_source
    354          * @covers WP_Block_Type::get_uses_context
    355          */
    356         public function test_merging_uses_context_from_multiple_sources() {
    357                 $get_value_callback = function () {
    358                         return 'Anything';
    359                 };
    360 
    361                 $block_registry        = WP_Block_Type_Registry::get_instance();
    362                 $original_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
    363 
    364                 register_block_bindings_source(
    365                         'test/source-one',
    366                         array(
    367                                 'label'              => 'Test Source One',
    368                                 'get_value_callback' => $get_value_callback,
    369                                 'uses_context'       => array( 'commonContext', 'sourceOneContext' ),
    370                         )
    371                 );
    372 
    373                 register_block_bindings_source(
    374                         'test/source-two',
    375                         array(
    376                                 'label'              => 'Test Source Two',
    377                                 'get_value_callback' => $get_value_callback,
    378                                 'uses_context'       => array( 'commonContext', 'sourceTwoContext' ),
    379                         )
    380                 );
    381 
    382                 $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
    383                 unregister_block_bindings_source( 'test/source-one' );
    384                 unregister_block_bindings_source( 'test/source-two' );
    385                 // Checks that the resulting `uses_context` contains the values from both sources.
    386                 $this->assertContains( 'commonContext', $new_uses_context );
    387                 $this->assertContains( 'sourceOneContext', $new_uses_context );
    388                 $this->assertContains( 'sourceTwoContext', $new_uses_context );
    389                 // Checks that the resulting `uses_context` added 3 unique items.
    390                 $this->assertSame( count( $original_uses_context ) + 3, count( $new_uses_context ) );
    391                 // Checks that the array isn't sparse to prevent issues in the editor.
    392                 $this->assertSame( array_key_last( $new_uses_context ), count( $new_uses_context ) - 1 );
    393         }
    394347}
Note: See TracChangeset for help on using the changeset viewer.