From 4988a706588042e22e24400ed361ab12d9688de0 Mon Sep 17 00:00:00 2001 From: gziolo Date: Tue, 15 Oct 2024 08:52:17 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@59238 git-svn-id: http://core.svn.wordpress.org/trunk@58630 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/edit-form-blocks.php | 18 ++++++++++++++++++ wp-admin/site-editor.php | 18 ++++++++++++++++++ wp-admin/widgets-form-blocks.php | 18 ++++++++++++++++++ wp-includes/block-editor.php | 17 ----------------- wp-includes/class-wp-customize-widgets.php | 18 ++++++++++++++++++ wp-includes/version.php | 2 +- 6 files changed, 73 insertions(+), 18 deletions(-) diff --git a/wp-admin/edit-form-blocks.php b/wp-admin/edit-form-blocks.php index dea530e9fb..c4727ea3ba 100644 --- a/wp-admin/edit-form-blocks.php +++ b/wp-admin/edit-form-blocks.php @@ -106,6 +106,24 @@ wp_add_inline_script( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' ); +// Preload server-registered block bindings sources. +$registered_sources = get_all_registered_block_bindings_sources(); +if ( ! empty( $registered_sources ) ) { + $filtered_sources = array(); + foreach ( $registered_sources as $source ) { + $filtered_sources[] = array( + 'name' => $source->name, + 'label' => $source->label, + 'usesContext' => $source->uses_context, + ); + } + $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); + wp_add_inline_script( + 'wp-blocks', + $script + ); +} + // Get admin url for handling meta boxes. $meta_box_url = admin_url( 'post.php' ); $meta_box_url = add_query_arg( diff --git a/wp-admin/site-editor.php b/wp-admin/site-editor.php index 7e2e763dee..a678fc1e44 100644 --- a/wp-admin/site-editor.php +++ b/wp-admin/site-editor.php @@ -135,6 +135,24 @@ wp_add_inline_script( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' ); +// Preload server-registered block bindings sources. +$registered_sources = get_all_registered_block_bindings_sources(); +if ( ! empty( $registered_sources ) ) { + $filtered_sources = array(); + foreach ( $registered_sources as $source ) { + $filtered_sources[] = array( + 'name' => $source->name, + 'label' => $source->label, + 'usesContext' => $source->uses_context, + ); + } + $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); + wp_add_inline_script( + 'wp-blocks', + $script + ); +} + wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array() ) ), diff --git a/wp-admin/widgets-form-blocks.php b/wp-admin/widgets-form-blocks.php index db13d2e8b9..0d161cf526 100644 --- a/wp-admin/widgets-form-blocks.php +++ b/wp-admin/widgets-form-blocks.php @@ -51,6 +51,24 @@ wp_add_inline_script( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' ); +// Preload server-registered block bindings sources. +$registered_sources = get_all_registered_block_bindings_sources(); +if ( ! empty( $registered_sources ) ) { + $filtered_sources = array(); + foreach ( $registered_sources as $source ) { + $filtered_sources[] = array( + 'name' => $source->name, + 'label' => $source->label, + 'usesContext' => $source->uses_context, + ); + } + $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); + wp_add_inline_script( + 'wp-blocks', + $script + ); +} + wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), diff --git a/wp-includes/block-editor.php b/wp-includes/block-editor.php index 71d95aff5c..be8a8f901f 100644 --- a/wp-includes/block-editor.php +++ b/wp-includes/block-editor.php @@ -648,23 +648,6 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $editor_settings['postContentAttributes'] = $post_content_block_attributes; } - // Expose block bindings sources in the editor settings. - $registered_block_bindings_sources = get_all_registered_block_bindings_sources(); - if ( ! empty( $registered_block_bindings_sources ) ) { - // Initialize array. - $editor_settings['blockBindingsSources'] = array(); - foreach ( $registered_block_bindings_sources as $source_name => $source_properties ) { - // Add source with the label to editor settings. - $editor_settings['blockBindingsSources'][ $source_name ] = array( - 'label' => $source_properties->label, - ); - // Add `usesContext` property if exists. - if ( ! empty( $source_properties->uses_context ) ) { - $editor_settings['blockBindingsSources'][ $source_name ]['usesContext'] = $source_properties->uses_context; - } - } - } - $editor_settings['canUpdateBlockBindings'] = current_user_can( 'edit_block_binding', $block_editor_context ); /** diff --git a/wp-includes/class-wp-customize-widgets.php b/wp-includes/class-wp-customize-widgets.php index 7ddc8deedc..98c6944478 100644 --- a/wp-includes/class-wp-customize-widgets.php +++ b/wp-includes/class-wp-customize-widgets.php @@ -866,6 +866,24 @@ final class WP_Customize_Widgets { 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' ); + // Preload server-registered block bindings sources. + $registered_sources = get_all_registered_block_bindings_sources(); + if ( ! empty( $registered_sources ) ) { + $filtered_sources = array(); + foreach ( $registered_sources as $source ) { + $filtered_sources[] = array( + 'name' => $source->name, + 'label' => $source->label, + 'usesContext' => $source->uses_context, + ); + } + $script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); + wp_add_inline_script( + 'wp-blocks', + $script + ); + } + wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), diff --git a/wp-includes/version.php b/wp-includes/version.php index 6713809522..984bd55268 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-beta3-59237'; +$wp_version = '6.7-beta3-59238'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.