diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index dea530e9fb9f2..c4727ea3ba8a0 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -106,6 +106,24 @@ static function ( $classes ) { '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/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index 7e2e763dee32d..a678fc1e44ab3 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -135,6 +135,24 @@ static function ( $classes ) { '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/src/wp-admin/widgets-form-blocks.php b/src/wp-admin/widgets-form-blocks.php index db13d2e8b988b..0d161cf526d50 100644 --- a/src/wp-admin/widgets-form-blocks.php +++ b/src/wp-admin/widgets-form-blocks.php @@ -51,6 +51,24 @@ '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/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 71d95aff5ca8a..be8a8f901fe5c 100644 --- a/src/wp-includes/block-editor.php +++ b/src/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/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index 7ddc8deedc7b3..98c6944478804 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -866,6 +866,24 @@ public function enqueue_scripts() { '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/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index b10dbb93dae01..0682839605da9 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -720,38 +720,4 @@ public function data_block_editor_rest_api_preload_adds_missing_leading_slash() ), ); } - - /** - * @ticket 61641 - */ - public function test_get_block_editor_settings_block_bindings_sources() { - $block_editor_context = new WP_Block_Editor_Context(); - register_block_bindings_source( - 'test/source-one', - array( - 'label' => 'Source One', - 'get_value_callback' => function () {}, - 'uses_context' => array( 'postId' ), - ) - ); - register_block_bindings_source( - 'test/source-two', - array( - 'label' => 'Source Two', - 'get_value_callback' => function () {}, - ) - ); - $settings = get_block_editor_settings( array(), $block_editor_context ); - $exposed_sources = $settings['blockBindingsSources']; - unregister_block_bindings_source( 'test/source-one' ); - unregister_block_bindings_source( 'test/source-two' ); - // It is expected to have 4 sources: the 2 registered sources in the test, and the 2 core sources. - $this->assertCount( 4, $exposed_sources ); - $source_one = $exposed_sources['test/source-one']; - $this->assertSame( 'Source One', $source_one['label'] ); - $this->assertSameSets( array( 'postId' ), $source_one['usesContext'] ); - $source_two = $exposed_sources['test/source-two']; - $this->assertSame( 'Source Two', $source_two['label'] ); - $this->assertArrayNotHasKey( 'usesContext', $source_two ); - } }