Skip to content

Commit 482650a

Browse files
authored
Make global styles available to all themes (#34334)
They just differ on what data they have: 1. Without theme.json and no experimental-link-color support either: the stylesheet contains the core presets & styles. 2. Without theme.json but experimental-link-color support: the stylesheet contains the core and theme presets, plus the core styles if any. 3. With theme.json: the stylesheet contains the core and theme presets, plus the result of merging core & theme styles.
1 parent 0e5d2a1 commit 482650a

3 files changed

+55
-35
lines changed

lib/class-wp-theme-json-gutenberg.php

+40-25
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,33 @@ private function get_css_variables( $nodes ) {
799799
* style-property-one: value;
800800
* }
801801
*
802-
* Additionally, it'll also create new rulesets
803-
* as classes for each preset value such as:
802+
* @param array $style_nodes Nodes with styles.
803+
*
804+
* @return string The new stylesheet.
805+
*/
806+
private function get_block_classes( $style_nodes ) {
807+
$block_rules = '';
808+
809+
foreach ( $style_nodes as $metadata ) {
810+
if ( null === $metadata['selector'] ) {
811+
continue;
812+
}
813+
814+
$node = _wp_array_get( $this->theme_json, $metadata['path'], array() );
815+
$selector = $metadata['selector'];
816+
$declarations = self::compute_style_properties( $node );
817+
$block_rules .= self::to_ruleset( $selector, $declarations );
818+
819+
if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
820+
$block_rules .= '.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); margin-bottom: 0; }';
821+
}
822+
}
823+
824+
return $block_rules;
825+
}
826+
827+
/**
828+
* Creates new rulesets as classes for each preset value such as:
804829
*
805830
* .has-value-color {
806831
* color: value;
@@ -821,30 +846,14 @@ private function get_css_variables( $nodes ) {
821846
* p.has-value-gradient-background {
822847
* background: value;
823848
* }
824-
*
825-
* @param array $style_nodes Nodes with styles.
849+
826850
* @param array $setting_nodes Nodes with settings.
827851
*
828852
* @return string The new stylesheet.
829853
*/
830-
private function get_block_styles( $style_nodes, $setting_nodes ) {
831-
$block_rules = '';
832-
foreach ( $style_nodes as $metadata ) {
833-
if ( null === $metadata['selector'] ) {
834-
continue;
835-
}
836-
837-
$node = _wp_array_get( $this->theme_json, $metadata['path'], array() );
838-
$selector = $metadata['selector'];
839-
$declarations = self::compute_style_properties( $node );
840-
$block_rules .= self::to_ruleset( $selector, $declarations );
841-
842-
if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
843-
$block_rules .= '.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); margin-bottom: 0; }';
844-
}
845-
}
846-
854+
private function get_preset_classes( $setting_nodes ) {
847855
$preset_rules = '';
856+
848857
foreach ( $setting_nodes as $metadata ) {
849858
if ( null === $metadata['selector'] ) {
850859
continue;
@@ -855,7 +864,7 @@ private function get_block_styles( $style_nodes, $setting_nodes ) {
855864
$preset_rules .= self::compute_preset_classes( $node, $selector );
856865
}
857866

858-
return $block_rules . $preset_rules;
867+
return $preset_rules;
859868
}
860869

861870
/**
@@ -1053,7 +1062,11 @@ private static function get_setting_nodes( $theme_json, $selectors = array() ) {
10531062
* Returns the stylesheet that results of processing
10541063
* the theme.json structure this object represents.
10551064
*
1056-
* @param string $type Type of stylesheet we want accepts 'all', 'block_styles', and 'css_variables'.
1065+
* @param string $type Type of stylesheet. It accepts:
1066+
* 'all': css variables, block classes, preset classes. The default.
1067+
* 'block_styles': only block & preset classes.
1068+
* 'css_variables': only css variables.
1069+
* 'presets': only css variables and preset classes.
10571070
* @return string Stylesheet.
10581071
*/
10591072
public function get_stylesheet( $type = 'all' ) {
@@ -1063,11 +1076,13 @@ public function get_stylesheet( $type = 'all' ) {
10631076

10641077
switch ( $type ) {
10651078
case 'block_styles':
1066-
return $this->get_block_styles( $style_nodes, $setting_nodes );
1079+
return $this->get_block_classes( $style_nodes ) . $this->get_preset_classes( $setting_nodes );
10671080
case 'css_variables':
10681081
return $this->get_css_variables( $setting_nodes );
1082+
case 'presets':
1083+
return $this->get_css_variables( $setting_nodes ) . $this->get_preset_classes( $setting_nodes );
10691084
default:
1070-
return $this->get_css_variables( $setting_nodes ) . $this->get_block_styles( $style_nodes, $setting_nodes );
1085+
return $this->get_css_variables( $setting_nodes ) . $this->get_block_classes( $style_nodes ) . $this->get_preset_classes( $setting_nodes );
10711086
}
10721087
}
10731088

lib/class-wp-theme-json-resolver-gutenberg.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,17 @@ public static function get_user_data() {
407407
* @return WP_Theme_JSON_Gutenberg
408408
*/
409409
public static function get_merged_data( $settings = array(), $origin = 'user' ) {
410-
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $settings );
411-
412410
$result = new WP_Theme_JSON_Gutenberg();
413411
$result->merge( self::get_core_data() );
412+
413+
if (
414+
! get_theme_support( 'experimental-link-color' ) && // link color support needs the presets CSS variables regardless of the presence of theme.json file.
415+
! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support()
416+
) {
417+
return $result;
418+
}
419+
420+
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $settings );
414421
$result->merge( self::get_theme_data( $theme_support_data ) );
415422

416423
if ( 'user' === $origin ) {

lib/global-styles.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* the corresponding stylesheet.
1111
*
1212
* @param WP_Theme_JSON_Gutenberg $tree Input tree.
13-
* @param string $type Type of stylesheet we want accepts 'all', 'block_styles', and 'css_variables'.
13+
* @param string $type Type of stylesheet we want accepts 'all', 'block_styles', 'css_variables', and 'presets'.
1414
*
1515
* @return string Stylesheet.
1616
*/
@@ -48,16 +48,14 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree, $type = 'al
4848
* and enqueues the resulting stylesheet.
4949
*/
5050
function gutenberg_experimental_global_styles_enqueue_assets() {
51-
if (
52-
! get_theme_support( 'experimental-link-color' ) && // link color support needs the presets CSS variables regardless of the presence of theme.json file.
53-
! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
54-
return;
55-
}
56-
5751
$settings = gutenberg_get_default_block_editor_settings();
5852
$all = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $settings );
5953

60-
$stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all );
54+
$type = 'all';
55+
if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
56+
$type = 'presets';
57+
}
58+
$stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all, $type );
6159
if ( empty( $stylesheet ) ) {
6260
return;
6361
}

0 commit comments

Comments
 (0)