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

Add a new public method for getting the selector for a block #45505

Closed
wants to merge 7 commits into from
Closed

Add a new public method for getting the selector for a block #45505

wants to merge 7 commits into from

Conversation

ingeniumed
Copy link
Contributor

What?

This PR came about from the conversation that was had here between @oandregal, @alecgeatches and I about having a public method to get the selector for a block instead of exposing the `get_blocks_metadata function.

Why?

Given the recent drive to do a code quality check for theme.json APIs, it was mentioned to not expose the get_blocks_metadata function like we proposed in this PR. Instead, it's better to make a simpler public method that when given the block name would return the selector for that block.

This is using existing cached methods to do its job so the lookup time is minimal, and it ensure it only returns the basic information needed which is the selectors.

How?

It's using a simpler version of the get_blocks_metadata function by:

  • getting the registered blocks
  • looking up the name of block based on the name provided
  • checking to see what selectors are available, and returning one accordingly

Testing Instructions

Make a test plugin that essentially calls the following:

$looked_up_selector = wp_theme_get_css_selector_for_block( $block_name );

for a block. Ensure the right value is given back.

@codesandbox
Copy link

codesandbox bot commented Nov 3, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @ingeniumed! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 3, 2022
@ingeniumed
Copy link
Contributor Author

@oandregal Something that struck us while making this:

There is some overlap in the wp_theme_get_css_selector_for_block(), and in get_blocks_metadata when it comes to getting the registered blocks and their various selectors. Any way we want to reduce that overlap, or it makes sense just leaving it the way it is?

Technically, the purpose of the two functions is different as the former is for getting the selector of one block while the latter gathers all the blocks, along with some other metadata properties for each block.

@alecgeatches
Copy link
Contributor

There is some overlap in the wp_theme_get_css_selector_for_block(), and in get_blocks_metadata when it comes to getting the registered blocks and their various selectors. Any way we want to reduce that overlap, or it makes sense just leaving it the way it is?

Agree, it seems like it would make sense to implement a 6.2 version of get_blocks_metadata() that reuses wp_theme_get_css_selector_for_block() so those implementations don't need to be updated separately.

*
* @return string|null the CSS selector for the block.
*/
function wp_theme_get_css_selector_for_block( $block_name ) {
Copy link
Member

Choose a reason for hiding this comment

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

The theme and the block are unrelated, so it seems better to rename this to something along the lines of wp_block_get_css_selector.

Note this conversation. A block has more than one selector. This method should be able to retrieve any of the selectors provided by the block.

Copy link
Member

Choose a reason for hiding this comment

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

It'd be good to update get_blocks_metadata to use this method as well. It'd give us feedback whether this is good in practice or not.

For example, I haven't really dug into this, but just thinking about reusing this method at get_blocks_metadata it struck me that an alternative to this PR could be adding a get_css_selector method to the WP_Block_Type class instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

The theme and the block are unrelated, so it seems better to rename this to something along the lines of wp_block_get_css_selector.

Naive question, should we be prefixing this with gutenberg_ prior to it landing in core?

Note this conversation. A block has more than one selector. This method should be able to retrieve any of the selectors provided by the block.

+1 on this comment. Not only can blocks have selectors on a root and per-block-support basis, in the near future we plan on allowing custom selectors for individual styles for example on the color's background.

The point above probably illustrates we'll get even more benefit in future from reusing the results of this PR.

an alternative to this PR could be adding a get_css_selector method to the WP_Block_Type class instead.

Sounds good to me 👍

*/
function wp_theme_get_css_selector_for_block( $block_name ) {
$registry = WP_Block_Type_Registry::get_instance();
$blocks = $registry->get_all_registered();
Copy link
Member

Choose a reason for hiding this comment

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

If the method is about a single block, we can use get_registered( $name ) instead.

@oandregal
Copy link
Member

@aaronrobertshaw given your interest and thoughts on #45194 would you available to give feedback on this PR? I'm out next week, but I think we need to connect the work here with the work we talked about over there.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

Thanks for working on this @ingeniumed 👍

I think @oandregal makes a lot of great points in his feedback to date. I'll add my two cents to those comments inline.

My main thoughts are:

  1. We'll definitely need to extend this to allow specification which block support (color, typography) or which individual style (background, border-radius) we want a selector for.
  2. Making this a method on WP_Block_Type seems like a great fit to me.
  3. Reusing whatever the result of this PR is in the get_blocks_metadata method is a win in my books.

You can find further history on the feature level selectors in #42087. That PR added the ability to specify different selectors on a per block support basis. A use case for this was to allow border support styles to target the inner img element for an Image block instead of the outer figure wrapper.

*
* @return string|null the CSS selector for the block.
*/
function wp_theme_get_css_selector_for_block( $block_name ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The theme and the block are unrelated, so it seems better to rename this to something along the lines of wp_block_get_css_selector.

Naive question, should we be prefixing this with gutenberg_ prior to it landing in core?

Note this conversation. A block has more than one selector. This method should be able to retrieve any of the selectors provided by the block.

+1 on this comment. Not only can blocks have selectors on a root and per-block-support basis, in the near future we plan on allowing custom selectors for individual styles for example on the color's background.

The point above probably illustrates we'll get even more benefit in future from reusing the results of this PR.

an alternative to this PR could be adding a get_css_selector method to the WP_Block_Type class instead.

Sounds good to me 👍

@ingeniumed
Copy link
Contributor Author

Mentioning this PR that @aaronrobertshaw mentioned to me as it relates to what @alecgeatches and I are proposing here.

@aaronrobertshaw
Copy link
Contributor

Mentioning #46496

Thanks for linking the above PR 👍

It is a work in progress but a first pass at achieving the goals set out in #45194 that was linked earlier in this PR discussion. It would eventually make the block's selectors something that could be accessed directly from the block-type object. Whether a util function would be handy to assist with retrieving a specific feature or style's selector, I'll leave up to others.

Unfortunately, I'll be AFK until early January. So #46496 is unlikely to land before then unless someone else would like to pick that one up.

@ingeniumed
Copy link
Contributor Author

Closing my PR given the changes that we wanted for this PR have already gone in, in #46496

@ingeniumed ingeniumed closed this Aug 22, 2023
@ingeniumed ingeniumed deleted the add/new-blocks-metadata-method branch August 22, 2023 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants