diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 00633d4d7edd9..e8900d1ccdc1a 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -648,6 +648,23 @@ 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; + } + } + } + /** * Filters the settings to pass to the block editor for all editor type. * diff --git a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php index fc5b91a9d702a..1d5c4b1947467 100644 --- a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php +++ b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php @@ -311,6 +311,9 @@ public function test_get_registered() { $expected = new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ); $result = $this->registry->get_registered( 'test/source-two' ); + $this->registry->unregister( 'test/source-one' ); + $this->registry->unregister( 'test/source-two' ); + $this->registry->unregister( 'test/source-three' ); $this->assertEquals( $expected, @@ -380,6 +383,8 @@ public function test_merging_uses_context_from_multiple_sources() { ); $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context; + unregister_block_bindings_source( 'test/source-one' ); + unregister_block_bindings_source( 'test/source-two' ); // Checks that the resulting `uses_context` contains the values from both sources. $this->assertContains( 'commonContext', $new_uses_context ); $this->assertContains( 'sourceOneContext', $new_uses_context ); diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 0682839605da9..b10dbb93dae01 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -720,4 +720,38 @@ 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 ); + } }