Make WordPress Core


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

File:
1 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.
Note: See TracChangeset for help on using the changeset viewer.