Make WordPress Core


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

    r57562 r57641  
    4040                return 'test-value';
    4141            },
     42            'uses_context'       => array( 'sourceContext' ),
    4243        );
    4344    }
     
    158159
    159160        self::$test_source_properties['get_value_callback'] = 'not-a-callback';
     161
     162        $result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
     163        $this->assertFalse( $result );
     164    }
     165
     166    /**
     167     * Should reject block bindings registration if `uses_context` is not an array.
     168     *
     169     * @ticket 60525
     170     *
     171     * @covers WP_Block_Bindings_Registry::register
     172     *
     173     * @expectedIncorrectUsage WP_Block_Bindings_Registry::register
     174     */
     175    public function test_register_invalid_string_uses_context() {
     176
     177        self::$test_source_properties['uses_context'] = 'not-an-array';
    160178
    161179        $result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
     
    180198            $result
    181199        );
     200        $this->assertSame( 'test/source', $result->name );
     201        $this->assertSame( 'Test source', $result->label );
     202        $this->assertSame(
     203            'test-value',
     204            $result->get_value( array(), null, '' )
     205        );
     206        $this->assertEquals( array( 'sourceContext' ), $result->uses_context );
    182207    }
    183208
     
    322347        $this->assertTrue( $result );
    323348    }
     349
     350    /**
     351     * Tests merging `uses_context` from multiple sources.
     352     *
     353     * @ticket 60525
     354     *
     355     * @covers ::register_block_bindings_source
     356     * @covers WP_Block_Type::get_uses_context
     357     */
     358    public function test_merging_uses_context_from_multiple_sources() {
     359        $get_value_callback = function () {
     360            return 'Anything';
     361        };
     362
     363        $block_registry        = WP_Block_Type_Registry::get_instance();
     364        $original_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
     365
     366        register_block_bindings_source(
     367            'test/source-one',
     368            array(
     369                'label'              => 'Test Source One',
     370                'get_value_callback' => $get_value_callback,
     371                'uses_context'       => array( 'commonContext', 'sourceOneContext' ),
     372            )
     373        );
     374
     375        register_block_bindings_source(
     376            'test/source-two',
     377            array(
     378                'label'              => 'Test Source Two',
     379                'get_value_callback' => $get_value_callback,
     380                'uses_context'       => array( 'commonContext', 'sourceTwoContext' ),
     381            )
     382        );
     383
     384        $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
     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    }
    324394}
Note: See TracChangeset for help on using the changeset viewer.