Repository navigation
Block Supports: Add a helper that returns every support's classes and styles #15485
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
Open
aaronrobertshaw
wants to merge
15
commits into
WordPress:trunk
Choose a base branch
from
aaronrobertshaw:backport/block-supports-classes-and-styles-helper
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,152
−148
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
76ef139
Block Supports: Add PHP helpers for block support classes and styles.
aaronrobertshaw c9a8b1c
Block Supports: Fall back to the custom color when a preset attribute…
aaronrobertshaw 06e463e
Tests: Expect the custom color for a null preset, and drop the style …
aaronrobertshaw 86e0dbd
Block Supports: Match JS truthiness for color preset attributes.
aaronrobertshaw 07a6415
Block Supports: Mark the classes and styles helpers as added in 7.3.0.
aaronrobertshaw 7bd0b73
Block Supports: Only treat a non-empty string as a color preset slug.
aaronrobertshaw aebff9e
Block Supports: Return early for non-array attributes in the backgrou…
aaronrobertshaw 4020b55
Block Supports: Return early for non-array attributes in the shadow a…
aaronrobertshaw ca3113e
Block Supports: Rename the background helper's return array to $attri…
aaronrobertshaw 7272ad7
Merge branch 'trunk' into backport/php-support-style-helpers
westonruter fd9ccd3
Block Supports: Match the Gutenberg comments in the border and backgr…
aaronrobertshaw 99a9486
Tests: Match the trimmed Gutenberg helper tests and make their data p…
aaronrobertshaw 2bb0144
Block Supports: Add a helper that returns every support's classes and…
aaronrobertshaw 896ba07
Tests: Add the Trac ticket number to the block supports helper test.
aaronrobertshaw a2621d6
Block Supports: Narrow the docblock types of the block supports helper.
aaronrobertshaw 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
Some comments aren't visible on the classic Files Changed page.
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
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 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -52,76 +52,111 @@ function wp_apply_border_support( $block_type, $block_attributes ) { | |||||||||||||||||||||||||||
| return array(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $border_block_styles = array(); | ||||||||||||||||||||||||||||
| $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' ); | ||||||||||||||||||||||||||||
| $has_border_width_support = wp_has_border_feature_support( $block_type, 'width' ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Border radius. | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| wp_has_border_feature_support( $block_type, 'radius' ) && | ||||||||||||||||||||||||||||
| isset( $block_attributes['style']['border']['radius'] ) && | ||||||||||||||||||||||||||||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| $border_radius = $block_attributes['style']['border']['radius']; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( is_numeric( $border_radius ) ) { | ||||||||||||||||||||||||||||
| $border_radius .= 'px'; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' ); | ||||||||||||||||||||||||||||
| $has_border_radius_support = wp_has_border_feature_support( $block_type, 'radius' ); | ||||||||||||||||||||||||||||
| $has_border_style_support = wp_has_border_feature_support( $block_type, 'style' ); | ||||||||||||||||||||||||||||
| $has_border_width_support = wp_has_border_feature_support( $block_type, 'width' ); | ||||||||||||||||||||||||||||
| $skip_color = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ); | ||||||||||||||||||||||||||||
| $skip_radius = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ); | ||||||||||||||||||||||||||||
| $skip_style = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ); | ||||||||||||||||||||||||||||
| $skip_width = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // The unset() calls below fail on a non-array style or border. | ||||||||||||||||||||||||||||
| if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( ! $has_border_radius_support || $skip_radius ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border']['radius'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $border_block_styles['radius'] = $border_radius; | ||||||||||||||||||||||||||||
| if ( ! $has_border_style_support || $skip_style ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border']['style'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Border style. | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| wp_has_border_feature_support( $block_type, 'style' ) && | ||||||||||||||||||||||||||||
| isset( $block_attributes['style']['border']['style'] ) && | ||||||||||||||||||||||||||||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| $border_block_styles['style'] = $block_attributes['style']['border']['style']; | ||||||||||||||||||||||||||||
| if ( ! $has_border_width_support || $skip_width ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border']['width'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Border width. | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| $has_border_width_support && | ||||||||||||||||||||||||||||
| isset( $block_attributes['style']['border']['width'] ) && | ||||||||||||||||||||||||||||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| $border_width = $block_attributes['style']['border']['width']; | ||||||||||||||||||||||||||||
| if ( ! $has_border_color_support || $skip_color ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Side values follow skipped serialization only, as on trunk. | ||||||||||||||||||||||||||||
| foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | ||||||||||||||||||||||||||||
| $has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border'][ $side ] ); | ||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( $skip_width ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border'][ $side ]['width'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // This check handles original unitless implementation. | ||||||||||||||||||||||||||||
| if ( is_numeric( $border_width ) ) { | ||||||||||||||||||||||||||||
| $border_width .= 'px'; | ||||||||||||||||||||||||||||
| if ( $skip_color ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border'][ $side ]['color'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $border_block_styles['width'] = $border_width; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Border color. | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| $has_border_color_support && | ||||||||||||||||||||||||||||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| $preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; | ||||||||||||||||||||||||||||
| $custom_border_color = $block_attributes['style']['border']['color'] ?? null; | ||||||||||||||||||||||||||||
| $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Generates styles for individual border sides. | ||||||||||||||||||||||||||||
| if ( $has_border_color_support || $has_border_width_support ) { | ||||||||||||||||||||||||||||
| foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | ||||||||||||||||||||||||||||
| $border = $block_attributes['style']['border'][ $side ] ?? null; | ||||||||||||||||||||||||||||
| $border_side_values = array( | ||||||||||||||||||||||||||||
| 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, | ||||||||||||||||||||||||||||
| 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, | ||||||||||||||||||||||||||||
| 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| $border_block_styles[ $side ] = $border_side_values; | ||||||||||||||||||||||||||||
| if ( $skip_style ) { | ||||||||||||||||||||||||||||
| unset( $block_attributes['style']['border'][ $side ]['style'] ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Collect classes and styles. | ||||||||||||||||||||||||||||
| return wp_get_border_classes_and_styles( $block_attributes ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Returns border classes and inline styles for block attributes, like the JS | ||||||||||||||||||||||||||||
| * `getBorderClassesAndStyles()`. Does not check block support or skipped | ||||||||||||||||||||||||||||
| * serialization. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @since 7.3.0 | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @param array $block_attributes Block attributes. | ||||||||||||||||||||||||||||
| * @return array Array with `class` and `style` keys, each present only when non-empty. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| function wp_get_border_classes_and_styles( $block_attributes ) { | ||||||||||||||||||||||||||||
| if ( ! is_array( $block_attributes ) ) { | ||||||||||||||||||||||||||||
| return array(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+119
to
+123
Member
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.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $border = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] ) | ||||||||||||||||||||||||||||
| ? $block_attributes['style']['border'] | ||||||||||||||||||||||||||||
| : array(); | ||||||||||||||||||||||||||||
| $border_styles = array(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Unitless radius and width are from the original implementation. | ||||||||||||||||||||||||||||
| if ( isset( $border['radius'] ) ) { | ||||||||||||||||||||||||||||
| $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius']; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( isset( $border['style'] ) ) { | ||||||||||||||||||||||||||||
| $border_styles['style'] = $border['style']; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( isset( $border['width'] ) ) { | ||||||||||||||||||||||||||||
| $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width']; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Preset slugs are strings in theme.json and the editor. | ||||||||||||||||||||||||||||
| $preset_slug = $block_attributes['borderColor'] ?? null; | ||||||||||||||||||||||||||||
| $border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | ||||||||||||||||||||||||||||
| if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) { | ||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| $border_styles[ $side ] = array( | ||||||||||||||||||||||||||||
| 'width' => $border[ $side ]['width'] ?? null, | ||||||||||||||||||||||||||||
| 'color' => $border[ $side ]['color'] ?? null, | ||||||||||||||||||||||||||||
| 'style' => $border[ $side ]['style'] ?? null, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $attributes = array(); | ||||||||||||||||||||||||||||
| $styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) ); | ||||||||||||||||||||||||||||
| $styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if ( ! empty( $styles['classnames'] ) ) { | ||||||||||||||||||||||||||||
| $attributes['class'] = $styles['classnames']; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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.
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.
These
unset()statements can be moved to anelse: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.
Oops. My suggestion included too many deletion lines. Anyway, I think you see what I was trying to show.