Make WordPress Core

Changeset 58798


Ignore:
Timestamp:
07/24/2024 10:57:27 AM (7 weeks ago)
Author:
cbravobernal
Message:

Block Bindings: Adds sources in the editor settings to consume them in the client

Adds a new property blockBindingsSources to the editor settings to expose the block bindings sources registered in the server.

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-editor.php

    r58703 r58798  
    647647    if ( isset( $post_content_block_attributes ) ) {
    648648        $editor_settings['postContentAttributes'] = $post_content_block_attributes;
     649    }
     650
     651    // Expose block bindings sources in the editor settings.
     652    $registered_block_bindings_sources = get_all_registered_block_bindings_sources();
     653    if ( ! empty( $registered_block_bindings_sources ) ) {
     654        // Initialize array.
     655        $editor_settings['blockBindingsSources'] = array();
     656        foreach ( $registered_block_bindings_sources as $source_name => $source_properties ) {
     657            // Add source with the label to editor settings.
     658            $editor_settings['blockBindingsSources'][ $source_name ] = array(
     659                'label' => $source_properties->label,
     660            );
     661            // Add `usesContext` property if exists.
     662            if ( ! empty( $source_properties->uses_context ) ) {
     663                $editor_settings['blockBindingsSources'][ $source_name ]['usesContext'] = $source_properties->uses_context;
     664            }
     665        }
    649666    }
    650667
  • trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php

    r58176 r58798  
    312312        $expected = new WP_Block_Bindings_Source( $source_two_name, $source_two_properties );
    313313        $result   = $this->registry->get_registered( 'test/source-two' );
     314        $this->registry->unregister( 'test/source-one' );
     315        $this->registry->unregister( 'test/source-two' );
     316        $this->registry->unregister( 'test/source-three' );
    314317
    315318        $this->assertEquals(
     
    381384
    382385        $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
     386        unregister_block_bindings_source( 'test/source-one' );
     387        unregister_block_bindings_source( 'test/source-two' );
    383388        // Checks that the resulting `uses_context` contains the values from both sources.
    384389        $this->assertContains( 'commonContext', $new_uses_context );
  • trunk/tests/phpunit/tests/blocks/editor.php

    r56682 r58798  
    721721        );
    722722    }
     723
     724    /**
     725     * @ticket 61641
     726     */
     727    public function test_get_block_editor_settings_block_bindings_sources() {
     728        $block_editor_context = new WP_Block_Editor_Context();
     729        register_block_bindings_source(
     730            'test/source-one',
     731            array(
     732                'label'              => 'Source One',
     733                'get_value_callback' => function () {},
     734                'uses_context'       => array( 'postId' ),
     735            )
     736        );
     737        register_block_bindings_source(
     738            'test/source-two',
     739            array(
     740                'label'              => 'Source Two',
     741                'get_value_callback' => function () {},
     742            )
     743        );
     744        $settings        = get_block_editor_settings( array(), $block_editor_context );
     745        $exposed_sources = $settings['blockBindingsSources'];
     746        unregister_block_bindings_source( 'test/source-one' );
     747        unregister_block_bindings_source( 'test/source-two' );
     748        // It is expected to have 4 sources: the 2 registered sources in the test, and the 2 core sources.
     749        $this->assertCount( 4, $exposed_sources );
     750        $source_one = $exposed_sources['test/source-one'];
     751        $this->assertSame( 'Source One', $source_one['label'] );
     752        $this->assertSameSets( array( 'postId' ), $source_one['usesContext'] );
     753        $source_two = $exposed_sources['test/source-two'];
     754        $this->assertSame( 'Source Two', $source_two['label'] );
     755        $this->assertArrayNotHasKey( 'usesContext', $source_two );
     756    }
    723757}
Note: See TracChangeset for help on using the changeset viewer.