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

Blocks: Add block bindings registry class #5965

Closed
wants to merge 3 commits into from

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Jan 29, 2024

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

Extracted from #5888. Props to @michalczaplinski and @artemiomorales for most of the work.


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

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 Author

gziolo commented Jan 29, 2024

Committed with https://core.trac.wordpress.org/changeset/57373.

@gziolo gziolo closed this Jan 29, 2024
@gziolo gziolo deleted the add/wp-block-binding-registry branch January 29, 2024 11:15
Comment on lines +22 to +24
* @since 6.5.0
*
* @var array
Copy link
Member

Choose a reason for hiding this comment

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

The line break is not needed, similar to the property below.

Copy link
Member

Choose a reason for hiding this comment

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

@gziolo ☝️

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll include that change in the next batch of changes planned: #5966.

return false;
}

$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';

Choose a reason for hiding this comment

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

This regex is probably a bit too eager. Only alphanumeric characters and hyphens might be too arbitrarily limited. Underscores, dots or colons are all common in technical names. It also doesn't take into account any non-english characters.

I think we should only check whether there is a / present to indicate a namespace and accept any string before- and after.

On another note, the _doing_it_wrong() warnings do not show up in PHP unit tests. I don't know if this is by design or there is another method that could be used here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I copied the same logic that the block types registry uses:

if ( preg_match( '/[A-Z]+/', $name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Block type names must not contain uppercase characters.' ),
'5.0.0'
);
return false;
}
$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
if ( ! preg_match( $name_matcher, $name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' ),
'5.0.0'
);
return false;
}

I thought it would be good to follow the same pattern for consistency.

On another note, the _doing_it_wrong() warnings do not show up in PHP unit tests. I don't know if this is by design or there is another method that could be used here?

_doing_it_wrong() call should fail the test if the following PHPDoc annotation isn't provided:

* @expectedIncorrectUsage WP_Block_Bindings_Registry::register

Choose a reason for hiding this comment

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

_doing_it_wrong() call should fail the test if the following PHPDoc annotation isn't provided:

I see, thanks!

I copied the same logic that the block types registry uses:

OK, I think that's fine in that case! 👍 Let's not forget to mention it in the documentation. I'll update the docstring for register_block_bindings_source() & registry->register() in #5888 as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. That would be important to have covered 👍

Choose a reason for hiding this comment

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

done in f2a611b

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