-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ingeniumed
wants to merge
7
commits into
WordPress:trunk
from
ingeniumed:add/new-blocks-metadata-method
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ded201
Add new public method for block to selector lookup
ingeniumed 643bd56
Re-write the public method so it doesn't depend on an internal method
ingeniumed 0778151
Add some public documentation and clean it up a bit
ingeniumed 5650f54
Cleanup based on internal comments the implementation so it's cleaner
ingeniumed fa97276
Fix the linting errors that were raised
ingeniumed 2436e41
Merging with trunk and fixing conflicts
ingeniumed 8560934
Merge branch 'WordPress:trunk' into add/new-blocks-metadata-method
ingeniumed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) { | ||
$registry = WP_Block_Type_Registry::get_instance(); | ||
$blocks = $registry->get_all_registered(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the method is about a single block, we can use |
||
|
||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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.
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.
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 aget_css_selector
method to theWP_Block_Type
class instead.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.
Naive question, should we be prefixing this with
gutenberg_
prior to it landing in core?+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.
Sounds good to me 👍