Make WordPress Core


Ignore:
Timestamp:
10/15/2024 08:50:08 AM (7 weeks 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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(
Note: See TracChangeset for help on using the changeset viewer.