-
Notifications
You must be signed in to change notification settings - Fork 3.4k
get_block_wrapper_attributes: Ensures that user-provided attributes override the attributes generated by block supports #10922
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
Closed
t-hamano
wants to merge
33
commits into
WordPress:trunk
from
t-hamano:64564-get-block-wrapper-attributes
Closed
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
57b548b
get_block_wrapper_attributes: Ensures that user-provided attributes o…
t-hamano dd6a4a5
Remove double blank lines
t-hamano 68fab57
Update PHPDoc
t-hamano bd3be96
Update PHPDoc
t-hamano d5dcbb8
Remove space
t-hamano 5cbc4b5
Simplify logic
t-hamano ba375fe
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano 46877b9
Add more safeguards
t-hamano 517f69c
Fix PHPCS error
t-hamano acf25f6
Fix PHPCS error
t-hamano 11e1150
unnecessary
t-hamano 9445bd3
Remove unnecessary test
t-hamano beb3a42
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano 40caa8e
Add static modifier for style callback function
t-hamano 0ea5ebf
Add static modifier for class callback function
t-hamano d68e980
Add static modifier for id callback function
t-hamano 810d5ac
Add static modifier for aria-label callback function
t-hamano eff534e
Use null coalescing operator
t-hamano 2030226
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano 66a9169
Don't split on commas
t-hamano 1deffb5
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano d7878da
Add type for data_get_block_wrapper_attributes_merge_or_override
t-hamano 4c9fe19
Remove unnecessary indent
t-hamano 06bae0c
Improve type for data_get_block_wrapper_attributes_merge_or_override
t-hamano a5955f0
Use PHPStan array shape syntax
t-hamano 54740cf
Priorize extra attributes
t-hamano 8bc645f
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano fd1f9b3
use PHPStan array shape syntax
t-hamano 2aeb26d
Explicitly convert to array
t-hamano 21f404f
Remove extra array wrapper from data provider
t-hamano 0e8d485
Remove extra indentation after 21f404f
westonruter 1ea701e
Use assertSame to catch additional expected attributes
westonruter da5ac94
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano 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 hidden or 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 |
|---|---|---|
|
|
@@ -179,30 +179,50 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { | |
| return ''; | ||
| } | ||
|
|
||
| // This is hardcoded on purpose. | ||
| // We only support a fixed list of attributes. | ||
| $attributes_to_merge = array( 'style', 'class', 'id', 'aria-label' ); | ||
| $attributes = array(); | ||
| foreach ( $attributes_to_merge as $attribute_name ) { | ||
| if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( empty( $new_attributes[ $attribute_name ] ) ) { | ||
| $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ]; | ||
| continue; | ||
| } | ||
|
|
||
| if ( empty( $extra_attributes[ $attribute_name ] ) ) { | ||
| $attributes[ $attribute_name ] = $new_attributes[ $attribute_name ]; | ||
| // Attribute values are concatenated or overridden depending on the attribute type. | ||
| // This is hardcoded on purpose, as we only support a fixed list of attributes. | ||
| $attribute_merge_callbacks = array( | ||
| 'style' => function ( $new_attribute, $extra_attribute ) { | ||
| $styles = array_filter( | ||
| array( | ||
| rtrim( trim( $extra_attribute ), ';' ), | ||
| rtrim( trim( $new_attribute ), ';' ), | ||
| ) | ||
| ); | ||
| return safecss_filter_attr( implode( ';', array_filter( $styles ) ) ); | ||
| }, | ||
|
t-hamano marked this conversation as resolved.
|
||
| 'class' => function ( $new_attribute, $extra_attribute ) { | ||
|
t-hamano marked this conversation as resolved.
Outdated
|
||
| $classes = array_merge( | ||
| wp_parse_list( $extra_attribute ), | ||
| wp_parse_list( $new_attribute ) | ||
|
t-hamano marked this conversation as resolved.
Outdated
|
||
| ); | ||
| $classes = array_unique( array_filter( array_map( 'sanitize_html_class', $classes ) ) ); | ||
| return implode( ' ', $classes ); | ||
| }, | ||
| 'id' => function ( $new_attribute, $extra_attribute ) { | ||
|
t-hamano marked this conversation as resolved.
Outdated
|
||
| return '' !== $extra_attribute ? $extra_attribute : $new_attribute; | ||
| }, | ||
| 'aria-label' => function ( $new_attribute, $extra_attribute ) { | ||
|
t-hamano marked this conversation as resolved.
Outdated
|
||
| return '' !== $extra_attribute ? $extra_attribute : $new_attribute; | ||
| }, | ||
| ); | ||
|
|
||
| $attributes = array(); | ||
|
t-hamano marked this conversation as resolved.
Outdated
t-hamano marked this conversation as resolved.
Outdated
|
||
| foreach ( $attribute_merge_callbacks as $attribute_name => $merge_callback ) { | ||
| $new_attribute = isset( $new_attributes[ $attribute_name ] ) ? $new_attributes[ $attribute_name ] : ''; | ||
| $extra_attribute = isset( $extra_attributes[ $attribute_name ] ) ? $extra_attributes[ $attribute_name ] : ''; | ||
|
t-hamano marked this conversation as resolved.
Outdated
|
||
| $new_attribute = is_string( $new_attribute ) ? $new_attribute : ''; | ||
| $extra_attribute = is_string( $extra_attribute ) ? $extra_attribute : ''; | ||
|
Comment on lines
+214
to
+215
Contributor
Author
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. Sanitize non-string attribute values here. |
||
|
|
||
| if ( '' === $new_attribute && '' === $extra_attribute ) { | ||
| continue; | ||
| } | ||
|
|
||
| $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ]; | ||
| $attributes[ $attribute_name ] = $merge_callback( $new_attribute, $extra_attribute ); | ||
| } | ||
|
|
||
| foreach ( $extra_attributes as $attribute_name => $value ) { | ||
| if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) { | ||
| if ( ! isset( $attribute_merge_callbacks[ $attribute_name ] ) ) { | ||
| $attributes[ $attribute_name ] = $value; | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.