Skip to content

Commit 2d1512f

Browse files
committed
fix: use wp_style_engine_get_stylesheet_from_css_rules to generate styles
1 parent 3c0c069 commit 2d1512f

File tree

1 file changed

+23
-17
lines changed
  • src/plugins/global-settings/preset-controls

1 file changed

+23
-17
lines changed

src/plugins/global-settings/preset-controls/index.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function load_json_file( $json_path ) {
6868
}
6969

7070
/**
71-
* Generate CSS variables based on the property (e.g., fontSizes, spacing).
71+
* Generate CSS variable style defintions based on the property (e.g., fontSizes, spacing).
7272
* The given presets will be overriden it match with a preset from custom.
7373
*
7474
* @param array $property
@@ -77,19 +77,18 @@ private function load_json_file( $json_path ) {
7777
* @param bool $isTheme
7878
* @return mixed
7979
*/
80-
public function generate_css_variables( $property, $presets, $prefix, $isTheme = false ) {
80+
public function generate_css_variables_styles( $property, $presets, $prefix, $isTheme = false ) {
8181
$filter_name = current_filter();
8282
$custom_presets = $this->custom_presets[ $property ] ?? [];
83-
84-
$css = "";
8583

86-
8784
$presets_by_slug = [];
8885
// Convert presets into an associative array with key 'slug'
8986
foreach ( $presets as $preset ) {
9087
$presets_by_slug[ $preset[ 'slug' ] ] = $preset;
9188
}
9289

90+
// There is no need to generate custom presets in the editor.
91+
// The custom presets are generated dynamically.
9392
if ( $filter_name !== 'stackable_inline_editor_styles' ) {
9493
// Override values in base presets if it exist in custom presets
9594
foreach ( $custom_presets as $custom ) {
@@ -98,20 +97,25 @@ public function generate_css_variables( $property, $presets, $prefix, $isTheme =
9897
}
9998
}
10099

100+
// Build the CSS variables array.
101101
// If custom presets or using stackable presets, use the given size.
102102
// If using theme presets, use WP generated --wp-preset to support theme.json specific
103103
// configuration (fluid, clamping, etc.)
104+
$css_vars = [];
104105
foreach ( $presets_by_slug as $slug => $preset ) {
105-
$is_custom = $preset[ '__is_custom' ] ?? false;
106+
$is_custom = $preset['__is_custom'] ?? false;
106107

107108
$value = $is_custom || ! $isTheme
108109
? $preset['size']
109110
: "var(--wp--preset--$prefix--$slug)";
110111

111-
$css .= "--stk--preset--$prefix--$slug: $value;\n";
112+
$css_vars[ "--stk--preset--$prefix--$slug" ] = $value;
112113
}
113114

114-
return $css;
115+
return array(
116+
'selector' => ':root',
117+
'declarations' => $css_vars,
118+
);
115119
}
116120

117121
/**
@@ -133,40 +137,42 @@ public function deepGet( $array, $keys ) {
133137
*/
134138
public function add_preset_controls_styles( $current_css ) {
135139
$this->load_presets();
136-
137-
$generated_css = "\n/* Global Preset Controls */\n";
138-
$generated_css .= ":root {\n";
140+
$generated_styles = array();
139141

140142
foreach ( self::PRESET_MAPPING as $key => $value ) {
141143
if ( ! empty( $this->deepGet( $this->theme_presets, $value[ 'settings' ] )[ 'theme' ] ) ) {
142-
$generated_css .= $this->generate_css_variables(
144+
$styles = $this->generate_css_variables_styles(
143145
$key,
144146
$this->deepGet( $this->theme_presets, $value[ 'settings' ] )[ 'theme' ],
145147
$value[ 'prefix' ],
146148
true
147149
);
150+
$generated_styles[] = $styles;
151+
148152
} elseif ( ! empty( $this->deepGet( $this->default_presets, $value[ 'settings' ] )[ 'default' ] ) ) {
149-
$generated_css .= $this->generate_css_variables(
153+
$styles = $this->generate_css_variables_styles(
150154
$key,
151155
$this->deepGet( $this->default_presets, $value[ 'settings' ] )[ 'default' ],
152156
$value[ 'prefix' ],
153157
true
154158
);
159+
$generated_styles[] = $styles;
155160
} else {
156-
$generated_css .= $this->generate_css_variables(
161+
$styles = $this->generate_css_variables_styles(
157162
$key,
158163
$this->deepGet( $this->stackable_presets, $value[ 'settings' ] ),
159164
$value[ 'prefix' ],
160165
);
166+
$generated_styles[] = $styles;
161167
}
162168
}
163169

164-
$generated_css .= "}\n";
165-
170+
$generated_css = wp_style_engine_get_stylesheet_from_css_rules( $generated_styles );
166171
if ( ! $generated_css ) {
167172
return $current_css;
168173
}
169-
174+
175+
$current_css .= "\n/* Global Preset Controls */\n";
170176
$current_css .= $generated_css;
171177

172178
return apply_filters( 'stackable_frontend_css' , $current_css );

0 commit comments

Comments
 (0)