Skip to content

Commit 5889df3

Browse files
Block Supports: Add width support to dimensions
1 parent 7c3e145 commit 5889df3

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/wp-includes/block-supports/dimensions.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,32 @@ function wp_register_dimensions_support( $block_type ) {
5151
* @return array Block dimensions CSS classes and inline styles.
5252
*/
5353
function wp_apply_dimensions_support( $block_type, $block_attributes ) {
54-
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
55-
return array();
56-
}
57-
5854
$attributes = array();
5955

60-
// Width support to be added in near future.
56+
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
57+
return $attributes;
58+
}
6159

62-
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
63-
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
60+
$block_styles = $block_attributes['style'] ?? null;
6461

6562
if ( ! $block_styles ) {
6663
return $attributes;
6764
}
6865

69-
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
70-
$dimensions_block_styles = array();
71-
$dimensions_block_styles['minHeight'] = null;
72-
if ( $has_min_height_support && ! $skip_min_height ) {
73-
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
74-
? $block_styles['dimensions']['minHeight']
75-
: null;
66+
$dimensions_block_styles = array();
67+
$supported_features = array( 'minHeight', 'width' );
68+
69+
foreach ( $supported_features as $feature ) {
70+
$has_support = block_has_support( $block_type, array( 'dimensions', $feature ), false );
71+
$skip_serialization = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', $feature );
72+
73+
$dimensions_block_styles[ $feature ] = null;
74+
75+
if ( $has_support && ! $skip_serialization ) {
76+
$dimensions_block_styles[ $feature ] = $block_styles['dimensions'][ $feature ] ?? null;
77+
}
7678
}
79+
7780
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
7881

7982
if ( ! empty( $styles['css'] ) ) {

src/wp-includes/class-wp-theme-json.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class WP_Theme_JSON {
237237
* @since 6.5.0 Added `aspect-ratio` property.
238238
* @since 6.6.0 Added `background-[image|position|repeat|size]` properties.
239239
* @since 6.7.0 Added `background-attachment` property.
240+
* @since 7.0.0 Added `dimensions.width`.
240241
* @var array
241242
*/
242243
const PROPERTIES_METADATA = array(
@@ -301,6 +302,7 @@ class WP_Theme_JSON {
301302
'text-transform' => array( 'typography', 'textTransform' ),
302303
'filter' => array( 'filter', 'duotone' ),
303304
'box-shadow' => array( 'shadow' ),
305+
'width' => array( 'dimensions', 'width' ),
304306
'writing-mode' => array( 'typography', 'writingMode' ),
305307
);
306308

@@ -395,6 +397,7 @@ class WP_Theme_JSON {
395397
* @since 6.6.0 Added support for 'dimensions.aspectRatios', 'dimensions.defaultAspectRatios',
396398
* 'typography.defaultFontSizes', and 'spacing.defaultSpacingSizes'.
397399
* @since 6.9.0 Added support for `border.radiusSizes`.
400+
* @since 7.0.0 Added support for `dimensions.width`.
398401
* @var array
399402
*/
400403
const VALID_SETTINGS = array(
@@ -434,6 +437,7 @@ class WP_Theme_JSON {
434437
'aspectRatios' => null,
435438
'defaultAspectRatios' => null,
436439
'minHeight' => null,
440+
'width' => null,
437441
),
438442
'layout' => array(
439443
'contentSize' => null,
@@ -527,6 +531,7 @@ class WP_Theme_JSON {
527531
* @since 6.3.0 Added support for `typography.textColumns`.
528532
* @since 6.5.0 Added support for `dimensions.aspectRatio`.
529533
* @since 6.6.0 Added `background` sub properties to top-level only.
534+
* @since 7.0.0 Added support for `dimensions.width`.
530535
* @var array
531536
*/
532537
const VALID_STYLES = array(
@@ -555,6 +560,7 @@ class WP_Theme_JSON {
555560
'dimensions' => array(
556561
'aspectRatio' => null,
557562
'minHeight' => null,
563+
'width' => null,
558564
),
559565
'filter' => array(
560566
'duotone' => null,
@@ -644,11 +650,13 @@ class WP_Theme_JSON {
644650
* generated under their own feature level selector rather than the block's.
645651
*
646652
* @since 6.1.0
653+
* @since 7.0.0 Added support for `dimensions`.
647654
* @var string[]
648655
*/
649656
const BLOCK_SUPPORT_FEATURE_LEVEL_SELECTORS = array(
650657
'__experimentalBorder' => 'border',
651658
'color' => 'color',
659+
'dimensions' => 'dimensions',
652660
'spacing' => 'spacing',
653661
'typography' => 'typography',
654662
);
@@ -724,6 +732,7 @@ public static function get_element_class_name( $element ) {
724732
* @since 6.2.0 Added `dimensions.minHeight` and `position.sticky`.
725733
* @since 6.4.0 Added `background.backgroundImage`.
726734
* @since 6.5.0 Added `background.backgroundSize` and `dimensions.aspectRatio`.
735+
* @since 7.0.0 Added `dimensions.width`.
727736
* @var array
728737
*/
729738
const APPEARANCE_TOOLS_OPT_INS = array(
@@ -739,6 +748,7 @@ public static function get_element_class_name( $element ) {
739748
array( 'color', 'caption' ),
740749
array( 'dimensions', 'aspectRatio' ),
741750
array( 'dimensions', 'minHeight' ),
751+
array( 'dimensions', 'width' ),
742752
array( 'position', 'sticky' ),
743753
array( 'spacing', 'blockGap' ),
744754
array( 'spacing', 'margin' ),

src/wp-includes/style-engine/class-wp-style-engine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ final class WP_Style_Engine {
219219
'spacing' => '--wp--preset--spacing--$slug',
220220
),
221221
),
222+
'width' => array(
223+
'property_keys' => array(
224+
'default' => 'width',
225+
),
226+
'path' => array( 'dimensions', 'width' ),
227+
),
222228
),
223229
'spacing' => array(
224230
'padding' => array(

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ public function test_get_settings_appearance_true_opts_in() {
281281
'dimensions' => array(
282282
'aspectRatio' => true,
283283
'minHeight' => true,
284+
'width' => true,
284285
),
285286
'position' => array(
286287
'sticky' => true,
@@ -319,6 +320,7 @@ public function test_get_settings_appearance_true_opts_in() {
319320
'dimensions' => array(
320321
'aspectRatio' => true,
321322
'minHeight' => true,
323+
'width' => true,
322324
),
323325
'position' => array(
324326
'sticky' => true,

0 commit comments

Comments
 (0)