-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Committed with https://core.trac.wordpress.org/changeset/57373. |
* @since 6.5.0 | ||
* | ||
* @var array |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo ☝️
There was a problem hiding this comment.
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-]+$/'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
wordpress-develop/src/wp-includes/class-wp-block-type-registry.php
Lines 64 to 81 in cabfa6f
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in f2a611b
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.