@@ -32,48 +32,53 @@ class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
32
32
* path to the value in theme.json & block attributes.
33
33
*/
34
34
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 ' ),
77
82
);
78
83
79
84
/**
@@ -617,10 +622,10 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets'
617
622
* @return string Styles for the block.
618
623
*/
619
624
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 ' ) );
624
629
625
630
// Get a reference to element name from path.
626
631
// $block_metadata['path'] = array('styles','elements','link');
@@ -649,9 +654,9 @@ function( $pseudo_selector ) use ( $selector ) {
649
654
// element then compute the style properties for it.
650
655
// Otherwise just compute the styles for the default selector as normal.
651
656
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 );
653
658
} 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 );
655
660
}
656
661
657
662
$ block_rules = '' ;
@@ -698,6 +703,13 @@ function( $pseudo_selector ) use ( $selector ) {
698
703
if ( static ::ROOT_BLOCK_SELECTOR === $ selector ) {
699
704
$ block_gap_value = _wp_array_get ( $ this ->theme_json , array ( 'styles ' , 'spacing ' , 'blockGap ' ), '0.5em ' );
700
705
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
+
701
713
$ block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; } ' ;
702
714
$ block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; } ' ;
703
715
$ 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 ) {
754
766
* )
755
767
* ```
756
768
*
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.
762
776
*/
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 ) {
764
778
if ( null === $ properties ) {
765
779
$ properties = static ::PROPERTIES_METADATA ;
766
780
}
@@ -770,9 +784,23 @@ protected static function compute_style_properties( $styles, $settings = array()
770
784
return $ declarations ;
771
785
}
772
786
787
+ $ root_variable_duplicates = array ();
788
+
773
789
foreach ( $ properties as $ css_property => $ value_path ) {
774
790
$ value = static ::get_property_value ( $ styles , $ value_path , $ theme_json );
775
791
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
+
776
804
// Look up protected properties, keyed by value path.
777
805
// Skip protected properties that are explicitly set to `null`.
778
806
if ( is_array ( $ value_path ) ) {
@@ -797,6 +825,14 @@ protected static function compute_style_properties( $styles, $settings = array()
797
825
);
798
826
}
799
827
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
+
800
836
return $ declarations ;
801
837
}
802
838
@@ -985,6 +1021,7 @@ protected static function get_property_value( $styles, $path, $theme_json = null
985
1021
*/
986
1022
const VALID_SETTINGS = array (
987
1023
'appearanceTools ' => null ,
1024
+ 'useRootVariables ' => null ,
988
1025
'border ' => array (
989
1026
'color ' => null ,
990
1027
'radius ' => null ,
0 commit comments