-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Post Content Block: Add tagName selector #70698
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
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
Thanks for the PR!
Adding a tagName
attribute to a post content block makes a lot of sense to me.
However, we don't need to use the SelectControl
component itself. There is already a handy wrapper component called HTMLElementControl
. This component also has a functionality to check whether the main element already exists in the content.
Check out the implementation in the Group block.
@t-hamano Thanks for the quick review and the helpful suggestion! I've updated the PR to use HTMLElementControl as you recommended. That's a much cleaner solution. |
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.
Thanks for the update! I'd recommend two more code quality enhancements.
For security improvements, it's a good idea to use the tag_escape()
function, which is also done in the Template Part block. It should look something like this.
$tag_name = 'div';
if ( ! empty( $attributes['tagName'] ) && tag_escape( $attributes['tagName'] ) === $attributes['tagName'] ) {
$tag_name = $attributes['tagName'];
}
Next, when generating output HTML, it is common to use the sprintf
function in WordPress.
return sprintf(
'<%1$s %2$s>%3$s</%1$s>',
$tag_name,
$wrapper_attributes,
$content
);
@t-hamano These are excellent code quality enhancements. I've implemented both, these improvements definitely make the code more robust. Could you take another quick look when you have a moment? Thanks in advance! |
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.
LGTM 👍
* Post Content Block: Add tagName selector * fix: Define $tag_name * style: Format code * docs: Update core-blocks.md * Update post content block fixture * docs: Update docs * feat: Implement HTMLElementControl * enhance: Better tagName handling for security Co-authored-by: EliezerSPP <eliezerspp@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org>
What?
Add the "tagName" selector to the Post Content Block.
Why?
This PR introduces the possibility to change the block's default wrapper (div) to a more semantic alternative (main, section, and article) optionally.
How?
This code is based on the group tag selector.
Testing Instructions
Screenshots