Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block bindings: Expose sources in the editor settings to consume them in the client #7020

Conversation

SantosGuillamot
Copy link

@SantosGuillamot SantosGuillamot commented Jul 12, 2024

Originally explored in this other pull request: #6456

What?

It adds a new property to the editor settings to expose the block bindings sources registered in the server. This allows to reuse some of its properties like label and uses_context without having to replicate that in JavaScript.

There is a related pull request in Gutenberg, where these settings are being consumed.

Why?

It makes some block bindings editor functionalities work by default, improving the user experience, and it simplifies how sources are registered in the client.

How?

Add a new property including all the registered sources while the editor settings are being initialized.

Trac ticket: https://core.trac.wordpress.org/ticket/61641


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Trac Ticket Missing

This pull request is missing a link to a Trac ticket. For a contribution to be considered, there must be a corresponding ticket in Trac.

To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. More information about contributing to WordPress on GitHub can be found in the Core Handbook.

Copy link

github-actions bot commented Jul 12, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props santosguillamot, cbravobernal, gziolo, artemiosans.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@gziolo
Copy link
Member

gziolo commented Jul 22, 2024

I’m wondering if we can update an existing unit test to validate the setting is correctly propagated to the editor

Maybe something similar to

public function test_get_block_editor_settings_theme_json_settings() {
switch_theme( 'block-theme' );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$settings = get_block_editor_settings( array(), $post_editor_context );
, but a simpler one as no theme switching is necessary.

@SantosGuillamot
Copy link
Author

I’m wondering if we can update an existing unit test to validate the setting is correctly propagated to the editor

I have just added this new unit test to check that the registered sources are properly added to the editor settings: link.

Copy link

@artemiomorales artemiomorales left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very excited about the work that exposes the sources from the server, as we will have a better information to share with users about sources, and an improved handling for the necessary block context.

@@ -77,6 +77,9 @@ public function test_get_all_registered() {
);

$registered = get_all_registered_block_bindings_sources();
unregister_block_bindings_source( 'test/source-one' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are redundant as there is automated cleanup included in the tear_down lifecycle method:

public function tear_down() {
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
if ( str_starts_with( $source_name, 'test/' ) ) {
unregister_block_bindings_source( $source_name );
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense 🙂 And it explains why they weren't failing before. I made this change as part of this commit.

@@ -269,6 +269,9 @@ public function test_get_all_registered() {
);

$registered = $this->registry->get_all_registered();
$this->registry->unregister( 'test/source-one' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is more nuanced, but also redundant. There is a new instance of the registry created for every test using the lifecycle method set_up:

$this->registry = new WP_Block_Bindings_Registry();

So, the instance with registered custom sources doesn't persist between tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense as well 🙂 I made this change as part of this commit.

tests/phpunit/tests/blocks/editor.php Show resolved Hide resolved
@cbravobernal
Copy link
Contributor

Completed with my first commit to Core in https://core.trac.wordpress.org/changeset/58798

@noisysocks
Copy link
Member

Thanks for committing this early in the 6.7 cycle ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants