Skip to content

Commit 67f3e8c

Browse files
Reuse most of the logic from #39926
1 parent fb8dcec commit 67f3e8c

File tree

8 files changed

+148
-63
lines changed

8 files changed

+148
-63
lines changed

lib/block-supports/layout.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
5454
$wide_max_width_value = wp_strip_all_tags( explode( ';', $wide_max_width_value )[0] );
5555

5656
if ( $content_size || $wide_size ) {
57-
$style = "$selector > :where(:not(.alignleft):not(.alignright)) {";
57+
$style = "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull)) {";
5858
$style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';';
5959
$style .= 'margin-left: auto !important;';
6060
$style .= 'margin-right: auto !important;';

lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php

+91-54
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,53 @@ class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
3232
* path to the value in theme.json & block attributes.
3333
*/
3434
const PROPERTIES_METADATA = array(
35-
'background' => array( 'color', 'gradient' ),
36-
'background-color' => array( 'color', 'background' ),
37-
'border-radius' => array( 'border', 'radius' ),
38-
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
39-
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
40-
'border-bottom-left-radius' => array( 'border', 'radius', 'bottomLeft' ),
41-
'border-bottom-right-radius' => array( 'border', 'radius', 'bottomRight' ),
42-
'border-color' => array( 'border', 'color' ),
43-
'border-width' => array( 'border', 'width' ),
44-
'border-style' => array( 'border', 'style' ),
45-
'border-top-color' => array( 'border', 'top', 'color' ),
46-
'border-top-width' => array( 'border', 'top', 'width' ),
47-
'border-top-style' => array( 'border', 'top', 'style' ),
48-
'border-right-color' => array( 'border', 'right', 'color' ),
49-
'border-right-width' => array( 'border', 'right', 'width' ),
50-
'border-right-style' => array( 'border', 'right', 'style' ),
51-
'border-bottom-color' => array( 'border', 'bottom', 'color' ),
52-
'border-bottom-width' => array( 'border', 'bottom', 'width' ),
53-
'border-bottom-style' => array( 'border', 'bottom', 'style' ),
54-
'border-left-color' => array( 'border', 'left', 'color' ),
55-
'border-left-width' => array( 'border', 'left', 'width' ),
56-
'border-left-style' => array( 'border', 'left', 'style' ),
57-
'color' => array( 'color', 'text' ),
58-
'font-family' => array( 'typography', 'fontFamily' ),
59-
'font-size' => array( 'typography', 'fontSize' ),
60-
'font-style' => array( 'typography', 'fontStyle' ),
61-
'font-weight' => array( 'typography', 'fontWeight' ),
62-
'letter-spacing' => array( 'typography', 'letterSpacing' ),
63-
'line-height' => array( 'typography', 'lineHeight' ),
64-
'margin' => array( 'spacing', 'margin' ),
65-
'margin-top' => array( 'spacing', 'margin', 'top' ),
66-
'margin-right' => array( 'spacing', 'margin', 'right' ),
67-
'margin-bottom' => array( 'spacing', 'margin', 'bottom' ),
68-
'margin-left' => array( 'spacing', 'margin', 'left' ),
69-
'padding' => array( 'spacing', 'padding' ),
70-
'padding-top' => array( 'spacing', 'padding', 'top' ),
71-
'padding-right' => array( 'spacing', 'padding', 'right' ),
72-
'padding-bottom' => array( 'spacing', 'padding', 'bottom' ),
73-
'padding-left' => array( 'spacing', 'padding', 'left' ),
74-
'text-decoration' => array( 'typography', 'textDecoration' ),
75-
'text-transform' => array( 'typography', 'textTransform' ),
76-
'filter' => array( 'filter', 'duotone' ),
35+
'background' => array( 'color', 'gradient' ),
36+
'background-color' => array( 'color', 'background' ),
37+
'border-radius' => array( 'border', 'radius' ),
38+
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
39+
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
40+
'border-bottom-left-radius' => array( 'border', 'radius', 'bottomLeft' ),
41+
'border-bottom-right-radius' => array( 'border', 'radius', 'bottomRight' ),
42+
'border-color' => array( 'border', 'color' ),
43+
'border-width' => array( 'border', 'width' ),
44+
'border-style' => array( 'border', 'style' ),
45+
'border-top-color' => array( 'border', 'top', 'color' ),
46+
'border-top-width' => array( 'border', 'top', 'width' ),
47+
'border-top-style' => array( 'border', 'top', 'style' ),
48+
'border-right-color' => array( 'border', 'right', 'color' ),
49+
'border-right-width' => array( 'border', 'right', 'width' ),
50+
'border-right-style' => array( 'border', 'right', 'style' ),
51+
'border-bottom-color' => array( 'border', 'bottom', 'color' ),
52+
'border-bottom-width' => array( 'border', 'bottom', 'width' ),
53+
'border-bottom-style' => array( 'border', 'bottom', 'style' ),
54+
'border-left-color' => array( 'border', 'left', 'color' ),
55+
'border-left-width' => array( 'border', 'left', 'width' ),
56+
'border-left-style' => array( 'border', 'left', 'style' ),
57+
'color' => array( 'color', 'text' ),
58+
'font-family' => array( 'typography', 'fontFamily' ),
59+
'font-size' => array( 'typography', 'fontSize' ),
60+
'font-style' => array( 'typography', 'fontStyle' ),
61+
'font-weight' => array( 'typography', 'fontWeight' ),
62+
'letter-spacing' => array( 'typography', 'letterSpacing' ),
63+
'line-height' => array( 'typography', 'lineHeight' ),
64+
'margin' => array( 'spacing', 'margin' ),
65+
'margin-top' => array( 'spacing', 'margin', 'top' ),
66+
'margin-right' => array( 'spacing', 'margin', 'right' ),
67+
'margin-bottom' => array( 'spacing', 'margin', 'bottom' ),
68+
'margin-left' => array( 'spacing', 'margin', 'left' ),
69+
'padding' => array( 'spacing', 'padding' ),
70+
'padding-top' => array( 'spacing', 'padding', 'top' ),
71+
'padding-right' => array( 'spacing', 'padding', 'right' ),
72+
'padding-bottom' => array( 'spacing', 'padding', 'bottom' ),
73+
'padding-left' => array( 'spacing', 'padding', 'left' ),
74+
'--wp--style--root--padding' => array( 'spacing', 'padding' ),
75+
'--wp--style--root--padding-top' => array( 'spacing', 'padding', 'top' ),
76+
'--wp--style--root--padding-right' => array( 'spacing', 'padding', 'right' ),
77+
'--wp--style--root--padding-bottom' => array( 'spacing', 'padding', 'bottom' ),
78+
'--wp--style--root--padding-left' => array( 'spacing', 'padding', 'left' ),
79+
'text-decoration' => array( 'typography', 'textDecoration' ),
80+
'text-transform' => array( 'typography', 'textTransform' ),
81+
'filter' => array( 'filter', 'duotone' ),
7782
);
7883

7984
/**
@@ -617,10 +622,10 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets'
617622
* @return string Styles for the block.
618623
*/
619624
public function get_styles_for_block( $block_metadata ) {
620-
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
621-
622-
$selector = $block_metadata['selector'];
623-
$settings = _wp_array_get( $this->theme_json, array( 'settings' ) );
625+
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
626+
$use_root_vars = _wp_array_get( $this->theme_json, array( 'settings', 'useRootVariables' ), array() );
627+
$selector = $block_metadata['selector'];
628+
$settings = _wp_array_get( $this->theme_json, array( 'settings' ) );
624629

625630
// Get a reference to element name from path.
626631
// $block_metadata['path'] = array('styles','elements','link');
@@ -649,9 +654,9 @@ function( $pseudo_selector ) use ( $selector ) {
649654
// element then compute the style properties for it.
650655
// Otherwise just compute the styles for the default selector as normal.
651656
if ( $pseudo_selector && isset( $node[ $pseudo_selector ] ) && isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ] ) && in_array( $pseudo_selector, static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ], true ) ) {
652-
$declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, $this->theme_json );
657+
$declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, $this->theme_json, $selector, $use_root_vars );
653658
} else {
654-
$declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json );
659+
$declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json, $selector, $use_root_vars );
655660
}
656661

657662
$block_rules = '';
@@ -698,6 +703,13 @@ function( $pseudo_selector ) use ( $selector ) {
698703
if ( static::ROOT_BLOCK_SELECTOR === $selector ) {
699704
$block_gap_value = _wp_array_get( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ), '0.5em' );
700705

706+
if ( $use_root_vars ) {
707+
$block_rules .= '.wp-site-blocks { padding-top: var(--wp--style--root--padding-top); padding-bottom: var(--wp--style--root--padding-bottom); }';
708+
$block_rules .= '.wp-site-blocks > * { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); }';
709+
$block_rules .= '.wp-site-blocks > * > .alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1); margin-left: calc(var(--wp--style--root--padding-left) * -1); }';
710+
$block_rules .= '.alignfull > :where([class*="wp-block-"]:not(.alignfull),p,h1,h2,h3,h4,h5,h6, ul,ol) { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); }';
711+
}
712+
701713
$block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }';
702714
$block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
703715
$block_rules .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
@@ -754,13 +766,15 @@ protected function get_block_classes( $style_nodes ) {
754766
* )
755767
* ```
756768
*
757-
* @param array $styles Styles to process.
758-
* @param array $settings Theme settings.
759-
* @param array $properties Properties metadata.
760-
* @param array $theme_json Theme JSON array.
761-
* @return array Returns the modified $declarations.
769+
* @param array $styles Styles to process.
770+
* @param array $settings Theme settings.
771+
* @param array $properties Properties metadata.
772+
* @param array $theme_json Theme JSON array.
773+
* @param string $selector The style block selector.
774+
* @param boolean $use_root_vars Whether to add custom properties at root level.
775+
* @return array Returns the modified $declarations.
762776
*/
763-
protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null ) {
777+
protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null, $selector = null, $use_root_vars = null ) {
764778
if ( null === $properties ) {
765779
$properties = static::PROPERTIES_METADATA;
766780
}
@@ -770,9 +784,23 @@ protected static function compute_style_properties( $styles, $settings = array()
770784
return $declarations;
771785
}
772786

787+
$root_variable_duplicates = array();
788+
773789
foreach ( $properties as $css_property => $value_path ) {
774790
$value = static::get_property_value( $styles, $value_path, $theme_json );
775791

792+
if ( strpos( $css_property, '--wp--style--root--' ) === 0 && static::ROOT_BLOCK_SELECTOR !== $selector ) {
793+
continue;
794+
}
795+
// We don't support string values.
796+
if ( '--wp--style--root--padding' === $css_property && is_string( $value ) ) {
797+
continue;
798+
}
799+
800+
if ( strpos( $css_property, '--wp--style--root--' ) === 0 && $use_root_vars ) {
801+
$root_variable_duplicates[] = substr( $css_property, strlen( '--wp--style--root--' ) );
802+
}
803+
776804
// Look up protected properties, keyed by value path.
777805
// Skip protected properties that are explicitly set to `null`.
778806
if ( is_array( $value_path ) ) {
@@ -797,6 +825,14 @@ protected static function compute_style_properties( $styles, $settings = array()
797825
);
798826
}
799827

828+
// If a variable value is added to the root, the corresponding property should be removed.
829+
foreach ( $root_variable_duplicates as $duplicate ) {
830+
$discard = array_search( $duplicate, array_column( $declarations, 'name' ), true );
831+
if ( $discard ) {
832+
array_splice( $declarations, $discard, 1 );
833+
}
834+
}
835+
800836
return $declarations;
801837
}
802838

@@ -985,6 +1021,7 @@ protected static function get_property_value( $styles, $path, $theme_json = null
9851021
*/
9861022
const VALID_SETTINGS = array(
9871023
'appearanceTools' => null,
1024+
'useRootVariables' => null,
9881025
'border' => array(
9891026
'color' => null,
9901027
'radius' => null,

lib/compat/wordpress-6.1/theme.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": 2,
33
"settings": {
44
"appearanceTools": false,
5+
"useRootVariables": false,
56
"border": {
67
"color": false,
78
"radius": false,
@@ -349,6 +350,14 @@
349350
}
350351
}
351352
},
352-
"spacing": { "blockGap": "24px" }
353+
"spacing": {
354+
"blockGap": "24px",
355+
"padding": {
356+
"top": "0px",
357+
"right": "0px",
358+
"bottom": "0px",
359+
"left": "0px"
360+
}
361+
}
353362
}
354363
}

packages/block-editor/src/hooks/style.js

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export function getInlineStyles( styles = {} ) {
6868
const ignoredStyles = [ 'spacing.blockGap' ];
6969
const output = {};
7070
Object.keys( STYLE_PROPERTY ).forEach( ( propKey ) => {
71+
if ( STYLE_PROPERTY[ propKey ].rootOnly ) {
72+
return;
73+
}
7174
const path = STYLE_PROPERTY[ propKey ].value;
7275
const subPaths = STYLE_PROPERTY[ propKey ].properties;
7376
// Ignore styles on elements because they are handled on the server.

packages/block-editor/src/layouts/flow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default {
132132
? `
133133
${ appendSelectors(
134134
selector,
135-
'> :where(:not(.alignleft):not(.alignright))'
135+
'> :where(:not(.alignleft):not(.alignright):not(.alignfull))'
136136
) } {
137137
max-width: ${ contentSize ?? wideSize };
138138
margin-left: auto !important;

packages/blocks/src/api/constants.js

+11
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
199199
support: [ 'typography', '__experimentalLetterSpacing' ],
200200
useEngine: true,
201201
},
202+
'--wp--style--root--padding': {
203+
value: [ 'spacing', 'padding' ],
204+
support: [ 'spacing', 'padding' ],
205+
properties: {
206+
'--wp--style--root--padding-top': 'top',
207+
'--wp--style--root--padding-right': 'right',
208+
'--wp--style--root--padding-bottom': 'bottom',
209+
'--wp--style--root--padding-left': 'left',
210+
},
211+
rootOnly: true,
212+
},
202213
};
203214

204215
export const __EXPERIMENTAL_ELEMENTS = {

packages/edit-site/src/components/global-styles/test/use-global-styles-output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ describe( 'global styles renderer', () => {
445445

446446
expect( toStyles( tree, blockSelectors ) ).toEqual(
447447
'body {margin: 0;}' +
448-
'body{background-color: red;margin: 10px;padding: 10px;}h1{font-size: 42px;}a{color: blue;}a:hover{color: orange;}a:focus{color: orange;}.wp-block-group{margin-top: 10px;margin-right: 20px;margin-bottom: 30px;margin-left: 40px;padding-top: 11px;padding-right: 22px;padding-bottom: 33px;padding-left: 44px;}h1,h2,h3,h4,h5,h6{color: orange;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{color: hotpink;}h1 a:hover,h2 a:hover,h3 a:hover,h4 a:hover,h5 a:hover,h6 a:hover{color: red;}h1 a:focus,h2 a:focus,h3 a:focus,h4 a:focus,h5 a:focus,h6 a:focus{color: red;}' +
448+
'body{background-color: red;--wp--style--root--padding-top: 10px;--wp--style--root--padding-right: 10px;--wp--style--root--padding-bottom: 10px;--wp--style--root--padding-left: 10px;margin: 10px;padding: 10px;}h1{font-size: 42px;}a{color: blue;}a:hover{color: orange;}a:focus{color: orange;}.wp-block-group{margin-top: 10px;margin-right: 20px;margin-bottom: 30px;margin-left: 40px;padding-top: 11px;padding-right: 22px;padding-bottom: 33px;padding-left: 44px;}h1,h2,h3,h4,h5,h6{color: orange;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{color: hotpink;}h1 a:hover,h2 a:hover,h3 a:hover,h4 a:hover,h5 a:hover,h6 a:hover{color: red;}h1 a:focus,h2 a:focus,h3 a:focus,h4 a:focus,h5 a:focus,h6 a:focus{color: red;}' +
449449
'.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }' +
450450
'.has-white-color{color: var(--wp--preset--color--white) !important;}.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}.has-white-border-color{border-color: var(--wp--preset--color--white) !important;}.has-black-color{color: var(--wp--preset--color--black) !important;}.has-black-background-color{background-color: var(--wp--preset--color--black) !important;}.has-black-border-color{border-color: var(--wp--preset--color--black) !important;}h1.has-blue-color,h2.has-blue-color,h3.has-blue-color,h4.has-blue-color,h5.has-blue-color,h6.has-blue-color{color: var(--wp--preset--color--blue) !important;}h1.has-blue-background-color,h2.has-blue-background-color,h3.has-blue-background-color,h4.has-blue-background-color,h5.has-blue-background-color,h6.has-blue-background-color{background-color: var(--wp--preset--color--blue) !important;}h1.has-blue-border-color,h2.has-blue-border-color,h3.has-blue-border-color,h4.has-blue-border-color,h5.has-blue-border-color,h6.has-blue-border-color{border-color: var(--wp--preset--color--blue) !important;}'
451451
);

0 commit comments

Comments
 (0)