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
29 changes: 29 additions & 0 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ function wp_theme_has_theme_json() {
}
}

if ( ! function_exists( 'wp_theme_get_css_selector_for_block' ) ) {
/**
* Lookup a CSS selector for the block provided, and return it if it exists.
*
* @param string $block_name The name of the block to lookup the CSS selector for.
*
* @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 👍

$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.


if ( isset( $blocks[ $block_name ] ) ) {
$block = $blocks[ $block_name ];
if (
isset( $block->supports['__experimentalSelector'] ) &&
is_string( $block->supports['__experimentalSelector'] )
) {
return $block->supports['__experimentalSelector'];
} else {
return '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) );
}
}

// Selector for the block was not found.
return null;
}
}

if ( ! function_exists( 'wp_theme_has_theme_json_clean_cache' ) ) {
/**
* Function to clean the cache used by wp_theme_has_theme_json method.
Expand Down