Make WordPress Core

Changeset 59238


Ignore:
Timestamp:
10/15/2024 08:50:08 AM (3 months ago)
Author:
gziolo
Message:

Editor: Bootstrap block binding sources with inline script from server

Bootstrap block bindings sources earlier in the process through an inline script to ensure they are available when developers want to extend them in the client. Following the same pattern other APIs like registering block types are doing.

Props santosguillamot, cbravobernal, gziolo.
Fixes #6225.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-blocks.php

    r59092 r59238  
    106106    'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
    107107);
     108
     109// Preload server-registered block bindings sources.
     110$registered_sources = get_all_registered_block_bindings_sources();
     111if ( ! empty( $registered_sources ) ) {
     112    $filtered_sources = array();
     113    foreach ( $registered_sources as $source ) {
     114        $filtered_sources[] = array(
     115            'name'        => $source->name,
     116            'label'       => $source->label,
     117            'usesContext' => $source->uses_context,
     118        );
     119    }
     120    $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
     121    wp_add_inline_script(
     122        'wp-blocks',
     123        $script
     124    );
     125}
    108126
    109127// Get admin url for handling meta boxes.
  • trunk/src/wp-admin/site-editor.php

    r58984 r59238  
    136136);
    137137
     138// Preload server-registered block bindings sources.
     139$registered_sources = get_all_registered_block_bindings_sources();
     140if ( ! empty( $registered_sources ) ) {
     141    $filtered_sources = array();
     142    foreach ( $registered_sources as $source ) {
     143        $filtered_sources[] = array(
     144            'name'        => $source->name,
     145            'label'       => $source->label,
     146            'usesContext' => $source->uses_context,
     147        );
     148    }
     149    $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
     150    wp_add_inline_script(
     151        'wp-blocks',
     152        $script
     153    );
     154}
     155
    138156wp_add_inline_script(
    139157    'wp-blocks',
  • trunk/src/wp-admin/widgets-form-blocks.php

    r56671 r59238  
    5151    'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
    5252);
     53
     54// Preload server-registered block bindings sources.
     55$registered_sources = get_all_registered_block_bindings_sources();
     56if ( ! empty( $registered_sources ) ) {
     57    $filtered_sources = array();
     58    foreach ( $registered_sources as $source ) {
     59        $filtered_sources[] = array(
     60            'name'        => $source->name,
     61            'label'       => $source->label,
     62            'usesContext' => $source->uses_context,
     63        );
     64    }
     65    $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
     66    wp_add_inline_script(
     67        'wp-blocks',
     68        $script
     69    );
     70}
    5371
    5472wp_add_inline_script(
  • trunk/src/wp-includes/block-editor.php

    r59122 r59238  
    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         }
    666649    }
    667650
  • trunk/src/wp-includes/class-wp-customize-widgets.php

    r58146 r59238  
    866866                'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
    867867            );
     868
     869            // Preload server-registered block bindings sources.
     870            $registered_sources = get_all_registered_block_bindings_sources();
     871            if ( ! empty( $registered_sources ) ) {
     872                $filtered_sources = array();
     873                foreach ( $registered_sources as $source ) {
     874                    $filtered_sources[] = array(
     875                        'name'        => $source->name,
     876                        'label'       => $source->label,
     877                        'usesContext' => $source->uses_context,
     878                    );
     879                }
     880                $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
     881                wp_add_inline_script(
     882                    'wp-blocks',
     883                    $script
     884                );
     885            }
    868886
    869887            wp_add_inline_script(
  • trunk/tests/phpunit/tests/blocks/editor.php

    r58798 r59238  
    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     }
    757723}
Note: See TracChangeset for help on using the changeset viewer.