From c069da7f79d396c3dbe3936bf97409c24e408f50 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Thu, 11 Jul 2024 20:01:17 +0200 Subject: [PATCH 1/7] Add sources to editor settings --- src/wp-includes/block-editor.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 00633d4d7edd9..149c23808256e 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['blockBindings'] = array(); + foreach ( $registered_block_bindings_sources as $source_name => $source_properties ) { + // Add source with the label to editor settings. + $editor_settings['blockBindings'][ $source_name ] = array( + 'label' => $source_properties->label, + ); + // Add `usesContext` property if exists. + if ( ! empty( $source_properties->uses_context ) ) { + $editor_settings['blockBindings'][ $source_name ]['usesContext'] = $source_properties->uses_context; + } + } + } + /** * Filters the settings to pass to the block editor for all editor type. * From 876155ffda8835b1ce24ce98b13ff1854c08c34c Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Sat, 13 Jul 2024 12:03:20 +0200 Subject: [PATCH 2/7] Use blockBindingsSource name --- src/wp-includes/block-editor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 149c23808256e..e8900d1ccdc1a 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -652,15 +652,15 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $registered_block_bindings_sources = get_all_registered_block_bindings_sources(); if ( ! empty( $registered_block_bindings_sources ) ) { // Initialize array. - $editor_settings['blockBindings'] = 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['blockBindings'][ $source_name ] = array( + $editor_settings['blockBindingsSources'][ $source_name ] = array( 'label' => $source_properties->label, ); // Add `usesContext` property if exists. if ( ! empty( $source_properties->uses_context ) ) { - $editor_settings['blockBindings'][ $source_name ]['usesContext'] = $source_properties->uses_context; + $editor_settings['blockBindingsSources'][ $source_name ]['usesContext'] = $source_properties->uses_context; } } } From 745509ab27e82497c757b8341b671cc326084ef3 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 22 Jul 2024 14:16:23 +0200 Subject: [PATCH 3/7] Add unit test to check editor settings --- tests/phpunit/tests/blocks/editor.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 0682839605da9..358bb544d7bfd 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -720,4 +720,21 @@ 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(); + $settings = get_block_editor_settings( array(), $block_editor_context ); + $registered_block_bindings_sources = get_all_registered_block_bindings_sources(); + + foreach ( $registered_block_bindings_sources as $name => $properties ) { + $registered_properties = get_object_vars( $properties ); + // Remove name property as it is not part of the settings. + unset( $registered_properties['name'] ); + $settings_properties = $settings['blockBindingsSources'][ $name ]; + $this->assertSameSets( $registered_properties, $settings_properties ); + } + } } From ac767be71bd9be25dd95a767767eb9721eb3750b Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 22 Jul 2024 15:01:36 +0200 Subject: [PATCH 4/7] Adapt test to check only expected properties --- tests/phpunit/tests/blocks/editor.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 358bb544d7bfd..a73743462315d 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -730,11 +730,22 @@ public function test_get_block_editor_settings_block_bindings_sources() { $registered_block_bindings_sources = get_all_registered_block_bindings_sources(); foreach ( $registered_block_bindings_sources as $name => $properties ) { - $registered_properties = get_object_vars( $properties ); - // Remove name property as it is not part of the settings. - unset( $registered_properties['name'] ); - $settings_properties = $settings['blockBindingsSources'][ $name ]; - $this->assertSameSets( $registered_properties, $settings_properties ); + // Check all the registered sources are exposed. + $this->assertArrayHasKey( $name, $settings['blockBindingsSources'] ); + + // Check only the expected properties are included, and they have the proper value. + $expected_properties = array( + 'label' => $properties->label, + ); + // Add optional properties if they are defined. + if ( ! empty( $properties->uses_context ) ) { + $expected_properties['usesContext'] = $properties->uses_context; + } + + $this->assertSameSets( + $expected_properties, + $settings['blockBindingsSources'][ $name ] + ); } } } From 2b2bed58356bdfa66e3e7e8682c28d1caa2fcb1e Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 22 Jul 2024 16:02:15 +0200 Subject: [PATCH 5/7] Register sources in unit test --- tests/phpunit/tests/blocks/editor.php | 48 +++++++++++++++------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index a73743462315d..fd4c502b3728c 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -725,27 +725,31 @@ 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(); - $settings = get_block_editor_settings( array(), $block_editor_context ); - $registered_block_bindings_sources = get_all_registered_block_bindings_sources(); - - foreach ( $registered_block_bindings_sources as $name => $properties ) { - // Check all the registered sources are exposed. - $this->assertArrayHasKey( $name, $settings['blockBindingsSources'] ); - - // Check only the expected properties are included, and they have the proper value. - $expected_properties = array( - 'label' => $properties->label, - ); - // Add optional properties if they are defined. - if ( ! empty( $properties->uses_context ) ) { - $expected_properties['usesContext'] = $properties->uses_context; - } - - $this->assertSameSets( - $expected_properties, - $settings['blockBindingsSources'][ $name ] - ); - } + $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']; + // 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 ); } } From a18de617e53c0843cb3874cc4539b52296a9659b Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 22 Jul 2024 16:37:48 +0200 Subject: [PATCH 6/7] Unregister sources in tests --- tests/phpunit/tests/block-bindings/register.php | 3 +++ .../tests/block-bindings/wpBlockBindingsRegistry.php | 8 ++++++++ tests/phpunit/tests/blocks/editor.php | 2 ++ 3 files changed, 13 insertions(+) diff --git a/tests/phpunit/tests/block-bindings/register.php b/tests/phpunit/tests/block-bindings/register.php index 49dd203d30ca6..df5112cca67d9 100644 --- a/tests/phpunit/tests/block-bindings/register.php +++ b/tests/phpunit/tests/block-bindings/register.php @@ -77,6 +77,9 @@ public function test_get_all_registered() { ); $registered = get_all_registered_block_bindings_sources(); + unregister_block_bindings_source( 'test/source-one' ); + unregister_block_bindings_source( 'test/source-two' ); + unregister_block_bindings_source( 'test/source-three' ); $this->assertEquals( $expected, $registered ); } diff --git a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php index fc5b91a9d702a..ef095f26d836d 100644 --- a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php +++ b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php @@ -269,6 +269,9 @@ public function test_get_all_registered() { ); $registered = $this->registry->get_all_registered(); + $this->registry->unregister( 'test/source-one' ); + $this->registry->unregister( 'test/source-two' ); + $this->registry->unregister( 'test/source-three' ); $this->assertEquals( $expected, $registered ); } @@ -311,6 +314,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 +386,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 fd4c502b3728c..b10dbb93dae01 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -743,6 +743,8 @@ public function test_get_block_editor_settings_block_bindings_sources() { ); $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']; From f258a462f0969ca3a762cec096a7df96fe9d7ec1 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Tue, 23 Jul 2024 14:27:48 +0200 Subject: [PATCH 7/7] Remove unnecessary `unregister` --- tests/phpunit/tests/block-bindings/register.php | 3 --- tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php | 3 --- 2 files changed, 6 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/register.php b/tests/phpunit/tests/block-bindings/register.php index df5112cca67d9..49dd203d30ca6 100644 --- a/tests/phpunit/tests/block-bindings/register.php +++ b/tests/phpunit/tests/block-bindings/register.php @@ -77,9 +77,6 @@ public function test_get_all_registered() { ); $registered = get_all_registered_block_bindings_sources(); - unregister_block_bindings_source( 'test/source-one' ); - unregister_block_bindings_source( 'test/source-two' ); - unregister_block_bindings_source( 'test/source-three' ); $this->assertEquals( $expected, $registered ); } diff --git a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php index ef095f26d836d..1d5c4b1947467 100644 --- a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php +++ b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php @@ -269,9 +269,6 @@ public function test_get_all_registered() { ); $registered = $this->registry->get_all_registered(); - $this->registry->unregister( 'test/source-one' ); - $this->registry->unregister( 'test/source-two' ); - $this->registry->unregister( 'test/source-three' ); $this->assertEquals( $expected, $registered ); }