@@ -51,7 +51,7 @@ class WP_Style_Engine {
51
51
),
52
52
'path ' => array ( 'color ' , 'text ' ),
53
53
'css_vars ' => array (
54
- '--wp--preset--color--$slug ' => ' color ' ,
54
+ 'color ' => ' --wp--preset--color--$slug ' ,
55
55
),
56
56
'classnames ' => array (
57
57
'has-text-color ' => true ,
@@ -148,13 +148,19 @@ class WP_Style_Engine {
148
148
'individual ' => 'padding-%s ' ,
149
149
),
150
150
'path ' => array ( 'spacing ' , 'padding ' ),
151
+ 'css_vars ' => array (
152
+ 'spacing ' => '--wp--preset--spacing--$slug ' ,
153
+ ),
151
154
),
152
155
'margin ' => array (
153
156
'property_keys ' => array (
154
157
'default ' => 'margin ' ,
155
158
'individual ' => 'margin-%s ' ,
156
159
),
157
160
'path ' => array ( 'spacing ' , 'margin ' ),
161
+ 'css_vars ' => array (
162
+ 'spacing ' => '--wp--preset--spacing--$slug ' ,
163
+ ),
158
164
),
159
165
),
160
166
'typography ' => array (
@@ -246,6 +252,28 @@ protected static function get_slug_from_preset_value( $style_value, $property_ke
246
252
return null ;
247
253
}
248
254
255
+ /**
256
+ * Generates a css var string, eg var(--wp--preset--color--background) from a preset string, eg. `var:preset|space|50`.
257
+ *
258
+ * @param string $style_value A single css preset value.
259
+ * @param array $css_vars The css var patterns used to generate the var string.
260
+ *
261
+ * @return string|null The css var, or null if no match for slug found.
262
+ */
263
+ protected static function get_css_var_value ( $ style_value , $ css_vars ) {
264
+ foreach ( $ css_vars as $ property_key => $ css_var_pattern ) {
265
+ $ slug = static ::get_slug_from_preset_value ( $ style_value , $ property_key );
266
+ if ( $ slug ) {
267
+ $ var = strtr (
268
+ $ css_var_pattern ,
269
+ array ( '$slug ' => $ slug )
270
+ );
271
+ return "var( $ var) " ;
272
+ }
273
+ }
274
+ return null ;
275
+ }
276
+
249
277
/**
250
278
* Checks whether an incoming block style value is valid.
251
279
*
@@ -315,7 +343,7 @@ protected static function get_css_declarations( $style_value, $style_definition,
315
343
isset ( $ style_definition ['value_func ' ] ) &&
316
344
is_callable ( $ style_definition ['value_func ' ] )
317
345
) {
318
- return call_user_func ( $ style_definition ['value_func ' ], $ style_value , $ style_definition );
346
+ return call_user_func ( $ style_definition ['value_func ' ], $ style_value , $ style_definition, $ should_return_css_vars );
319
347
}
320
348
321
349
$ css_declarations = array ();
@@ -325,15 +353,9 @@ protected static function get_css_declarations( $style_value, $style_definition,
325
353
// Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
326
354
if ( is_string ( $ style_value ) && strpos ( $ style_value , 'var: ' ) !== false ) {
327
355
if ( $ should_return_css_vars && ! empty ( $ style_definition ['css_vars ' ] ) ) {
328
- foreach ( $ style_definition ['css_vars ' ] as $ css_var_pattern => $ property_key ) {
329
- $ slug = static ::get_slug_from_preset_value ( $ style_value , $ property_key );
330
- if ( $ slug ) {
331
- $ css_var = strtr (
332
- $ css_var_pattern ,
333
- array ( '$slug ' => $ slug )
334
- );
335
- $ css_declarations [ $ style_property_keys ['default ' ] ] = "var( $ css_var) " ;
336
- }
356
+ $ css_var = static ::get_css_var_value ( $ style_value , $ style_definition ['css_vars ' ] );
357
+ if ( $ css_var ) {
358
+ $ css_declarations [ $ style_property_keys ['default ' ] ] = $ css_var ;
337
359
}
338
360
}
339
361
return $ css_declarations ;
@@ -344,8 +366,13 @@ protected static function get_css_declarations( $style_value, $style_definition,
344
366
// for styles such as margins and padding.
345
367
if ( is_array ( $ style_value ) ) {
346
368
foreach ( $ style_value as $ key => $ value ) {
347
- $ individual_property = sprintf ( $ style_property_keys ['individual ' ], _wp_to_kebab_case ( $ key ) );
348
- $ css_declarations [ $ individual_property ] = $ value ;
369
+ if ( is_string ( $ value ) && strpos ( $ value , 'var: ' ) !== false && $ should_return_css_vars && ! empty ( $ style_definition ['css_vars ' ] ) ) {
370
+ $ value = static ::get_css_var_value ( $ value , $ style_definition ['css_vars ' ] );
371
+ }
372
+ $ individual_property = sprintf ( $ style_property_keys ['individual ' ], _wp_to_kebab_case ( $ key ) );
373
+ if ( $ value ) {
374
+ $ css_declarations [ $ individual_property ] = $ value ;
375
+ }
349
376
}
350
377
} else {
351
378
$ css_declarations [ $ style_property_keys ['default ' ] ] = $ style_value ;
@@ -441,12 +468,13 @@ public function generate( $block_styles, $options ) {
441
468
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
442
469
* "border-image-{outset|source|width|repeat|slice}: {value};"
443
470
*
444
- * @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
445
- * @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
471
+ * @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
472
+ * @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
473
+ * @param boolean $should_return_css_vars Whether to try to build and return CSS var values.
446
474
*
447
475
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
448
476
*/
449
- protected static function get_individual_property_css_declarations ( $ style_value , $ individual_property_definition ) {
477
+ protected static function get_individual_property_css_declarations ( $ style_value , $ individual_property_definition, $ should_return_css_vars ) {
450
478
$ css_declarations = array ();
451
479
452
480
if ( ! is_array ( $ style_value ) || empty ( $ style_value ) || empty ( $ individual_property_definition ['path ' ] ) ) {
@@ -470,13 +498,8 @@ protected static function get_individual_property_css_declarations( $style_value
470
498
471
499
if ( $ style_definition && isset ( $ style_definition ['property_keys ' ]['individual ' ] ) ) {
472
500
// Set a CSS var if there is a valid preset value.
473
- $ slug = isset ( $ individual_property_definition ['css_vars ' ][ $ css_property ] ) ? static ::get_slug_from_preset_value ( $ value , $ css_property ) : null ;
474
- if ( $ slug ) {
475
- $ css_var = strtr (
476
- $ individual_property_definition ['css_vars ' ][ $ css_property ],
477
- array ( '$slug ' => $ slug )
478
- );
479
- $ value = "var( $ css_var) " ;
501
+ if ( is_string ( $ value ) && strpos ( $ value , 'var: ' ) !== false && $ should_return_css_vars && ! empty ( $ individual_property_definition ['css_vars ' ] ) ) {
502
+ $ value = static ::get_css_var_value ( $ value , $ individual_property_definition ['css_vars ' ] );
480
503
}
481
504
$ individual_css_property = sprintf ( $ style_definition ['property_keys ' ]['individual ' ], $ individual_property_key );
482
505
$ css_declarations [ $ individual_css_property ] = $ value ;
0 commit comments