PluginProbe
Gutenberg / trunk
Gutenberg vtrunk
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
gutenberg / build / scripts / style-engine / class-wp-style-engine-gutenberg.php

class-wp-style-engine-gutenberg.php in Gutenberg trunk, at build/scripts/style-engine/class-wp-style-engine-gutenberg.php

750 lines 28.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP_Style_Engine_Gutenberg
4 *
5 * Generates classnames and block styles.
6 *
7 * @package gutenberg
8 */
9
10 if ( ! class_exists( 'WP_Style_Engine_Gutenberg' ) ) {
11
12 /**
13 * Class WP_Style_Engine_Gutenberg.
14 *
15 * The Style Engine aims to provide a consistent API for rendering styling for blocks across both client-side and server-side applications.
16 *
17 * This class is for internal Core usage and is not supposed to be used by extenders (plugins and/or themes).
18 * This class is final and should not be extended.
19 * This is a low-level API that may need to do breaking changes. Please, use wp_style_engine_get_styles instead.
20 *
21 * @access private
22 */
23 final class WP_Style_Engine_Gutenberg {
24 /**
25 * Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes.
26 *
27 * For every style definition, the following properties are valid:
28 *
29 * - classnames => (array) an array of classnames to be returned for block styles. The key is a classname or pattern.
30 * A value of `true` means the classname should be applied always. Otherwise, a valid CSS property (string)
31 * to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
32 * - css_vars => (array) an array of key value pairs used to generate CSS var values.
33 * The key should be the CSS property name that matches the second element of the preset string value,
34 * i.e., "color" in var:preset|color|somePresetSlug. The value is a CSS var pattern (e.g. `--wp--preset--color--$slug`),
35 * whose `$slug` fragment will be replaced with the preset slug, which is the third element of the preset string value,
36 * i.e., `somePresetSlug` in var:preset|color|somePresetSlug.
37 * - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
38 * - path => (array) a path that accesses the corresponding style value in the block style object.
39 * - value_func => (string) the name of a function to generate a CSS definition array for a particular style object. The output of this function should be `array( "$property" => "$value", ... )`.
40 *
41 * @var array
42 */
43 const BLOCK_STYLE_DEFINITIONS_METADATA = array(
44 'background' => array(
45 'backgroundImage' => array(
46 'property_keys' => array(
47 'default' => 'background-image',
48 ),
49 'value_func' => array( self::class, 'get_url_or_value_css_declaration' ),
50 'path' => array( 'background', 'backgroundImage' ),
51 ),
52 'backgroundPosition' => array(
53 'property_keys' => array(
54 'default' => 'background-position',
55 ),
56 'path' => array( 'background', 'backgroundPosition' ),
57 ),
58 'backgroundRepeat' => array(
59 'property_keys' => array(
60 'default' => 'background-repeat',
61 ),
62 'path' => array( 'background', 'backgroundRepeat' ),
63 ),
64 'backgroundSize' => array(
65 'property_keys' => array(
66 'default' => 'background-size',
67 ),
68 'path' => array( 'background', 'backgroundSize' ),
69 ),
70 'backgroundAttachment' => array(
71 'property_keys' => array(
72 'default' => 'background-attachment',
73 ),
74 'path' => array( 'background', 'backgroundAttachment' ),
75 ),
76 'gradient' => array(
77 'property_keys' => array(
78 'default' => 'background-image',
79 ),
80 'css_vars' => array(
81 'gradient' => '--wp--preset--gradient--$slug',
82 ),
83 'path' => array( 'background', 'gradient' ),
84 'classnames' => array(
85 'has-background' => true,
86 ),
87 ),
88 ),
89 'color' => array(
90 'text' => array(
91 'property_keys' => array(
92 'default' => 'color',
93 ),
94 'path' => array( 'color', 'text' ),
95 'css_vars' => array(
96 'color' => '--wp--preset--color--$slug',
97 ),
98 'classnames' => array(
99 'has-text-color' => true,
100 'has-$slug-color' => 'color',
101 ),
102 ),
103 'background' => array(
104 'property_keys' => array(
105 'default' => 'background-color',
106 ),
107 'path' => array( 'color', 'background' ),
108 'css_vars' => array(
109 'color' => '--wp--preset--color--$slug',
110 ),
111 'classnames' => array(
112 'has-background' => true,
113 'has-$slug-background-color' => 'color',
114 ),
115 ),
116 'gradient' => array(
117 'property_keys' => array(
118 'default' => 'background',
119 ),
120 'css_vars' => array(
121 'gradient' => '--wp--preset--gradient--$slug',
122 ),
123 'path' => array( 'color', 'gradient' ),
124 'classnames' => array(
125 'has-background' => true,
126 'has-$slug-gradient-background' => 'gradient',
127 ),
128 ),
129 ),
130 'border' => array(
131 'color' => array(
132 'property_keys' => array(
133 'default' => 'border-color',
134 'individual' => 'border-%s-color',
135 ),
136 'path' => array( 'border', 'color' ),
137 'classnames' => array(
138 'has-border-color' => true,
139 'has-$slug-border-color' => 'color',
140 ),
141 ),
142 'radius' => array(
143 'property_keys' => array(
144 'default' => 'border-radius',
145 'individual' => 'border-%s-radius',
146 ),
147 'path' => array( 'border', 'radius' ),
148 'css_vars' => array(
149 'border-radius' => '--wp--preset--border-radius--$slug',
150 ),
151 ),
152 'style' => array(
153 'property_keys' => array(
154 'default' => 'border-style',
155 'individual' => 'border-%s-style',
156 ),
157 'path' => array( 'border', 'style' ),
158 ),
159 'width' => array(
160 'property_keys' => array(
161 'default' => 'border-width',
162 'individual' => 'border-%s-width',
163 ),
164 'path' => array( 'border', 'width' ),
165 ),
166 'top' => array(
167 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
168 'path' => array( 'border', 'top' ),
169 'css_vars' => array(
170 'color' => '--wp--preset--color--$slug',
171 ),
172 ),
173 'right' => array(
174 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
175 'path' => array( 'border', 'right' ),
176 'css_vars' => array(
177 'color' => '--wp--preset--color--$slug',
178 ),
179 ),
180 'bottom' => array(
181 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
182 'path' => array( 'border', 'bottom' ),
183 'css_vars' => array(
184 'color' => '--wp--preset--color--$slug',
185 ),
186 ),
187 'left' => array(
188 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
189 'path' => array( 'border', 'left' ),
190 'css_vars' => array(
191 'color' => '--wp--preset--color--$slug',
192 ),
193 ),
194 ),
195 'shadow' => array(
196 'shadow' => array(
197 'property_keys' => array(
198 'default' => 'box-shadow',
199 ),
200 'path' => array( 'shadow' ),
201 'css_vars' => array(
202 'shadow' => '--wp--preset--shadow--$slug',
203 ),
204 ),
205 ),
206 'dimensions' => array(
207 'aspectRatio' => array(
208 'property_keys' => array(
209 'default' => 'aspect-ratio',
210 ),
211 'path' => array( 'dimensions', 'aspectRatio' ),
212 'classnames' => array(
213 'has-aspect-ratio' => true,
214 ),
215 ),
216 'height' => array(
217 'property_keys' => array(
218 'default' => 'height',
219 ),
220 'path' => array( 'dimensions', 'height' ),
221 ),
222 'minHeight' => array(
223 'property_keys' => array(
224 'default' => 'min-height',
225 ),
226 'path' => array( 'dimensions', 'minHeight' ),
227 'css_vars' => array(
228 'dimension' => '--wp--preset--dimension--$slug',
229 ),
230 ),
231 'minWidth' => array(
232 'property_keys' => array(
233 'default' => 'min-width',
234 ),
235 'path' => array( 'dimensions', 'minWidth' ),
236 'css_vars' => array(
237 'dimension' => '--wp--preset--dimension--$slug',
238 ),
239 ),
240 'objectFit' => array(
241 'property_keys' => array(
242 'default' => 'object-fit',
243 ),
244 'path' => array( 'dimensions', 'objectFit' ),
245 ),
246 'width' => array(
247 'property_keys' => array(
248 'default' => 'width',
249 ),
250 'path' => array( 'dimensions', 'width' ),
251 'css_vars' => array(
252 'dimension' => '--wp--preset--dimension--$slug',
253 ),
254 ),
255 ),
256 'spacing' => array(
257 'padding' => array(
258 'property_keys' => array(
259 'default' => 'padding',
260 'individual' => 'padding-%s',
261 ),
262 'path' => array( 'spacing', 'padding' ),
263 'css_vars' => array(
264 'spacing' => '--wp--preset--spacing--$slug',
265 ),
266 ),
267 'margin' => array(
268 'property_keys' => array(
269 'default' => 'margin',
270 'individual' => 'margin-%s',
271 ),
272 'path' => array( 'spacing', 'margin' ),
273 'css_vars' => array(
274 'spacing' => '--wp--preset--spacing--$slug',
275 ),
276 ),
277 ),
278 'typography' => array(
279 'fontSize' => array(
280 'property_keys' => array(
281 'default' => 'font-size',
282 ),
283 'css_vars' => array(
284 'font-size' => '--wp--preset--font-size--$slug',
285 ),
286 'path' => array( 'typography', 'fontSize' ),
287 'classnames' => array(
288 'has-$slug-font-size' => 'font-size',
289 ),
290 ),
291 'fontFamily' => array(
292 'property_keys' => array(
293 'default' => 'font-family',
294 ),
295 'css_vars' => array(
296 'font-family' => '--wp--preset--font-family--$slug',
297 ),
298 'path' => array( 'typography', 'fontFamily' ),
299 'classnames' => array(
300 'has-$slug-font-family' => 'font-family',
301 ),
302 ),
303 'fontStyle' => array(
304 'property_keys' => array(
305 'default' => 'font-style',
306 ),
307 'path' => array( 'typography', 'fontStyle' ),
308 ),
309 'fontWeight' => array(
310 'property_keys' => array(
311 'default' => 'font-weight',
312 ),
313 'path' => array( 'typography', 'fontWeight' ),
314 ),
315 'lineHeight' => array(
316 'property_keys' => array(
317 'default' => 'line-height',
318 ),
319 'path' => array( 'typography', 'lineHeight' ),
320 ),
321 'textColumns' => array(
322 'property_keys' => array(
323 'default' => 'column-count',
324 ),
325 'path' => array( 'typography', 'textColumns' ),
326 ),
327 'textDecoration' => array(
328 'property_keys' => array(
329 'default' => 'text-decoration',
330 ),
331 'path' => array( 'typography', 'textDecoration' ),
332 ),
333 'textIndent' => array(
334 'property_keys' => array(
335 'default' => 'text-indent',
336 ),
337 'path' => array( 'typography', 'textIndent' ),
338 ),
339 'textShadow' => array(
340 'property_keys' => array(
341 'default' => 'text-shadow',
342 ),
343 'css_vars' => array(
344 'text-shadow' => '--wp--preset--text-shadow--$slug',
345 ),
346 'path' => array( 'typography', 'textShadow' ),
347 'classnames' => array(
348 'has-$slug-text-shadow' => 'text-shadow',
349 ),
350 ),
351 'textTransform' => array(
352 'property_keys' => array(
353 'default' => 'text-transform',
354 ),
355 'path' => array( 'typography', 'textTransform' ),
356 ),
357 'letterSpacing' => array(
358 'property_keys' => array(
359 'default' => 'letter-spacing',
360 ),
361 'path' => array( 'typography', 'letterSpacing' ),
362 ),
363 'writingMode' => array(
364 'property_keys' => array(
365 'default' => 'writing-mode',
366 ),
367 'path' => array( 'typography', 'writingMode' ),
368 ),
369 ),
370 );
371
372 /**
373 * Util: Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
374 *
375 * @param string $style_value A single CSS preset value.
376 * @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
377 *
378 * @return string The slug, or empty string if not found.
379 */
380 protected static function get_slug_from_preset_value( $style_value, $property_key ) {
381 if ( is_string( $style_value ) && is_string( $property_key ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
382 $index_to_splice = strrpos( $style_value, '|' ) + 1;
383 return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
384 }
385 return '';
386 }
387
388 /**
389 * Util: Generates a CSS var string, e.g., var(--wp--preset--color--background) from a preset string such as `var:preset|space|50`.
390 *
391 * @param string $style_value A single CSS preset value.
392 * @param string[] $css_vars An associate array of CSS var patterns used to generate the var string.
393 *
394 * @return string The css var, or an empty string if no match for slug found.
395 */
396 protected static function get_css_var_value( $style_value, $css_vars ) {
397 foreach ( $css_vars as $property_key => $css_var_pattern ) {
398 $slug = static::get_slug_from_preset_value( $style_value, $property_key );
399 if ( static::is_valid_style_value( $slug ) ) {
400 $var = strtr(
401 $css_var_pattern,
402 array( '$slug' => $slug )
403 );
404 return "var($var)";
405 }
406 }
407 return '';
408 }
409
410 /**
411 * Util: Checks whether an incoming block style value is valid.
412 *
413 * @param string? $style_value A single css preset value.
414 *
415 * @return bool
416 */
417 protected static function is_valid_style_value( $style_value ) {
418 return '0' === $style_value || ! empty( $style_value );
419 }
420
421 /**
422 * Stores a CSS rule using the provided CSS selector and CSS declarations.
423 *
424 * @param string $store_name A valid store key.
425 * @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
426 * @param string[]|WP_Style_Engine_CSS_Declarations_Gutenberg $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
427 * or a WP_Style_Engine_CSS_Declarations_Gutenberg object.
428 * @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.
429 *
430 * @return void.
431 */
432 public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ) {
433 if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) {
434 return;
435 }
436 static::get_store( $store_name )->add_rule( $css_selector, $rules_group )->add_declarations( $css_declarations );
437 }
438
439 /**
440 * Returns a store by store key.
441 *
442 * @param string $store_name A store key.
443 *
444 * @return WP_Style_Engine_CSS_Rules_Store_Gutenberg
445 */
446 public static function get_store( $store_name ) {
447 return WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( $store_name );
448 }
449
450 /**
451 * Returns classnames and CSS based on the values in a styles object.
452 * Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
453 *
454 * @since 6.1.0
455 *
456 * @param array $block_styles The style object.
457 * @param array $options {
458 * Optional. An array of options. Default empty array.
459 *
460 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
461 * @type string $selector Optional. When a selector is passed, the value of `$css` in the return value will comprise a full CSS rule `$selector { ...$css_declarations }`,
462 * otherwise, the value will be a concatenated string of CSS declarations.
463 * }
464 *
465 * @return array {
466 * @type string $classnames Classnames separated by a space.
467 * @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
468 * }
469 */
470 public static function parse_block_styles( $block_styles, $options ) {
471 $parsed_styles = array(
472 'classnames' => array(),
473 'declarations' => array(),
474 );
475 if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
476 return $parsed_styles;
477 }
478
479 // Collect CSS and classnames.
480 foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
481 if ( empty( $block_styles[ $definition_group_key ] ) ) {
482 continue;
483 }
484 foreach ( $definition_group_style as $style_definition ) {
485 $style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
486
487 if ( ! static::is_valid_style_value( $style_value ) ) {
488 continue;
489 }
490
491 $classnames = static::get_classnames( $style_value, $style_definition );
492 if ( ! empty( $classnames ) ) {
493 $parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], $classnames );
494 }
495
496 $css_declarations = static::get_css_declarations( $style_value, $style_definition, $options );
497 if ( ! empty( $css_declarations ) ) {
498 /*
499 * Combine background gradient and background image into a single
500 * comma-separated background-image value, matching the JS style engine.
501 */
502 if ( isset( $css_declarations['background-image'] ) && isset( $parsed_styles['declarations']['background-image'] ) ) {
503 $css_declarations['background-image'] = $css_declarations['background-image'] . ', ' . $parsed_styles['declarations']['background-image'];
504 }
505 $parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], $css_declarations );
506 }
507 }
508 }
509
510 return $parsed_styles;
511 }
512
513 /**
514 * Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., '`var:preset|<PRESET_TYPE>|<PRESET_SLUG>`'.
515 *
516 * @param array $style_value A single raw style value or css preset property from the $block_styles array.
517 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
518 *
519 * @return array|string[] An array of CSS classnames, or empty array.
520 */
521 protected static function get_classnames( $style_value, $style_definition ) {
522 if ( empty( $style_value ) ) {
523 return array();
524 }
525
526 $classnames = array();
527 if ( ! empty( $style_definition['classnames'] ) ) {
528 foreach ( $style_definition['classnames'] as $classname => $property_key ) {
529 if ( true === $property_key ) {
530 $classnames[] = $classname;
531 continue;
532 }
533
534 $slug = static::get_slug_from_preset_value( $style_value, $property_key );
535
536 if ( $slug ) {
537 /*
538 * Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA.
539 * One day, if there are no stored schemata, we could allow custom patterns or
540 * generate classnames based on other properties
541 * such as a path or a value or a prefix passed in options.
542 */
543 $classnames[] = strtr( $classname, array( '$slug' => $slug ) );
544 }
545 }
546 }
547
548 return $classnames;
549 }
550
551 /**
552 * Returns an array of CSS declarations based on valid block style values.
553 *
554 * @since 6.1.0
555 *
556 * @param array $style_value A single raw style value from $block_styles array.
557 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
558 * @param array $options {
559 * Optional. An array of options. Default empty array.
560 *
561 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
562 * }
563 *
564 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
565 */
566 protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) {
567 if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
568 return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
569 }
570
571 $css_declarations = array();
572 $style_property_keys = $style_definition['property_keys'];
573 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
574
575 /*
576 * Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`.
577 * Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
578 */
579 if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
580 if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
581 $css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
582 if ( static::is_valid_style_value( $css_var ) ) {
583 $css_declarations[ $style_property_keys['default'] ] = $css_var;
584 }
585 }
586 return $css_declarations;
587 }
588
589 /*
590 * Default rule builder.
591 * If the input contains an array, assume box model-like properties
592 * for styles such as margins and padding.
593 */
594 if ( is_array( $style_value ) ) {
595 // Bail out early if the `'individual'` property is not defined.
596 if ( ! isset( $style_property_keys['individual'] ) ) {
597 return $css_declarations;
598 }
599
600 foreach ( $style_value as $key => $value ) {
601 if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
602 $value = static::get_css_var_value( $value, $style_definition['css_vars'] );
603 }
604
605 $individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
606
607 if ( $individual_property && static::is_valid_style_value( $value ) ) {
608 $css_declarations[ $individual_property ] = $value;
609 }
610 }
611
612 return $css_declarations;
613 }
614
615 $css_declarations[ $style_property_keys['default'] ] = $style_value;
616 return $css_declarations;
617 }
618
619 /**
620 * Style value parser that returns a CSS definition array comprising style properties
621 * that have keys representing individual style properties, otherwise known as longhand CSS properties.
622 * e.g., "$style_property-$individual_feature: $value;", which could represent the following:
623 * "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
624 * "border-image-{outset|source|width|repeat|slice}: {value};"
625 *
626 * @param array $style_value A single raw style value from $block_styles array.
627 * @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA representing an individual property of a CSS property, e.g., 'top' in 'border-top'.
628 * @param array $options {
629 * Optional. An array of options. Default empty array.
630 *
631 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
632 * }
633 *
634 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
635 */
636 protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) {
637 if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
638 return array();
639 }
640
641 /*
642 * The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
643 * We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
644 * The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
645 */
646 $definition_group_key = $individual_property_definition['path'][0];
647 $individual_property_key = $individual_property_definition['path'][1];
648 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
649 $css_declarations = array();
650
651 foreach ( $style_value as $css_property => $value ) {
652 if ( empty( $value ) ) {
653 continue;
654 }
655
656 // Build a path to the individual rules in definitions.
657 $style_definition_path = array( $definition_group_key, $css_property );
658 $style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
659
660 if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
661 // Set a CSS var if there is a valid preset value.
662 if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
663 $value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
664 }
665 $individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
666 $css_declarations[ $individual_css_property ] = $value;
667 }
668 }
669 return $css_declarations;
670 }
671
672 /**
673 * Style value parser that constructs a CSS definition array comprising a single CSS property and value.
674 * If the provided value is an array containing a `url` property, the function will return a CSS definition array
675 * with a single property and value, with `url` escaped and injected into a CSS `url()` function,
676 * e.g., array( 'background-image' => "url( '...' )" ).
677 *
678 * @param array $style_value A single raw style value from $block_styles array.
679 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
680 *
681 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
682 */
683 protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) {
684 if ( empty( $style_value ) ) {
685 return array();
686 }
687
688 $css_declarations = array();
689
690 if ( isset( $style_definition['property_keys']['default'] ) ) {
691 $value = null;
692
693 if ( ! empty( $style_value['url'] ) ) {
694 $value = "url('" . $style_value['url'] . "')";
695 } elseif ( is_string( $style_value ) ) {
696 $value = $style_value;
697 }
698
699 if ( null !== $value ) {
700 $css_declarations[ $style_definition['property_keys']['default'] ] = $value;
701 }
702 }
703
704 return $css_declarations;
705 }
706
707 /**
708 * Returns compiled CSS from css_declarations.
709 *
710 * @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
711 * @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
712 *
713 * @return string A compiled CSS string.
714 */
715 public static function compile_css( $css_declarations, $css_selector ) {
716 if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) {
717 return '';
718 }
719
720 // Return an entire rule if there is a selector.
721 if ( $css_selector ) {
722 $css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $css_selector, $css_declarations );
723 return $css_rule->get_css();
724 }
725
726 $css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $css_declarations );
727 return $css_declarations->get_declarations_string();
728 }
729
730 /**
731 * Returns a compiled stylesheet from stored CSS rules.
732 *
733 * @param WP_Style_Engine_CSS_Rule_Gutenberg[] $css_rules An array of WP_Style_Engine_CSS_Rule_Gutenberg objects from a store or otherwise.
734 * @param array $options {
735 * Optional. An array of options. Default empty array.
736 *
737 * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
738 * @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
739 * }
740 *
741 * @return string A compiled stylesheet from stored CSS rules.
742 */
743 public static function compile_stylesheet_from_css_rules( $css_rules, $options = array() ) {
744 $processor = new WP_Style_Engine_Processor_Gutenberg();
745 $processor->add_rules( $css_rules );
746 return $processor->get_css( $options );
747 }
748 }
749 }
750