-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Include namespace in layout classname for non-core blocks #53404
Conversation
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/block-supports/layout.php |
Size Change: +18 B (0%) Total Size: 1.44 MB
ℹ️ View Unchanged
|
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.
Oh good catch! Just left a couple of comments about whether we can avoid accessing the second element of an array in case anyone winds up registering a block with name core
with no namespace. Probably very edge-casey, though!
lib/block-supports/layout.php
Outdated
$block_name = explode( '/', $block['blockName'] ); | ||
$class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname; | ||
$split_block_name = explode( '/', $block['blockName'] ); | ||
$full_block_name = 'core' === $split_block_name[0] ? $split_block_name[1] : implode( '-', $split_block_name ); |
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.
Tiny nit: $split_block_name[1]
assumes the array will have at least two elements. Should we re-use end()
to account for a custom block registered with the whole name core
? I think that might wind up being something like:
$full_block_name = 'core' === $split_block_name[0] ? $split_block_name[1] : implode( '-', $split_block_name ); | |
$full_block_name = 'core' === $split_block_name[0] ? end( split_block_name ) : implode( '-', $split_block_name ); |
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.
Sure we can do that!
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'm not sure it's possible to have more than one /
in a block name anyway? It'll throw a Notice: Function WP_Block_Type_Registry::register was called incorrectly.
warning.
Only mentioning because accessing via index might be a tick faster? Probably by 0.00000001
of a nanosecond 😄
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.
Hrm, good point! I'd assumed you could register a block without a namespace (so, no /
character), but it looks like you can't: https://github.com/WordPress/wordpress-develop/blob/5ebe28966e5decbe1862c147bf98db0d8094038e/src/wp-includes/class-wp-block-type-registry.php#L73
I was quite possibly overthinking this! I'm usually quite scared of PHP notices surrounding accessing indexes that aren't available, largely due to my own past mistakes 😅
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.
Me too! I think it's fine to leave all the same. end()
does change the index pointer, but I don't think that's a problem for the rest of the code in this instance.
I was also curious to see whether I could register a custom block with the name core/something
but it turns out I can't. 🤷🏻
const splitBlockName = blockName.split( '/' ); | ||
const fullBlockName = | ||
splitBlockName[ 0 ] === 'core' | ||
? splitBlockName[ 1 ] |
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.
Similar to the other comment, to account for a potential custom block called core
(with no namespace), should we retain the use of .pop()
in this side of the ternary?
Thanks for reviewing @andrewserong ! Feedback has been addressed. |
Testing after feedback: Before
After
👍🏻 |
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.
What?
Fixes #53295.
The compound [block name]-[layout name] classname generated for layout blocks should take into account the block namespace if it's not a core block.
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast