PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.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 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / style-engine / class-wp-style-engine-gutenberg.php

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

688 lines 26.1 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 ),
77 'color' => array(
78 'text' => array(
79 'property_keys' => array(
80 'default' => 'color',
81 ),
82 'path' => array( 'color', 'text' ),
83 'css_vars' => array(
84 'color' => '--wp--preset--color--$slug',
85 ),
86 'classnames' => array(
87 'has-text-color' => true,
88 'has-$slug-color' => 'color',
89 ),
90 ),
91 'background' => array(
92 'property_keys' => array(
93 'default' => 'background-color',
94 ),
95 'path' => array( 'color', 'background' ),
96 'css_vars' => array(
97 'color' => '--wp--preset--color--$slug',
98 ),
99 'classnames' => array(
100 'has-background' => true,
101 'has-$slug-background-color' => 'color',
102 ),
103 ),
104 'gradient' => array(
105 'property_keys' => array(
106 'default' => 'background',
107 ),
108 'css_vars' => array(
109 'gradient' => '--wp--preset--gradient--$slug',
110 ),
111 'path' => array( 'color', 'gradient' ),
112 'classnames' => array(
113 'has-background' => true,
114 'has-$slug-gradient-background' => 'gradient',
115 ),
116 ),
117 ),
118 'border' => array(
119 'color' => array(
120 'property_keys' => array(
121 'default' => 'border-color',
122 'individual' => 'border-%s-color',
123 ),
124 'path' => array( 'border', 'color' ),
125 'classnames' => array(
126 'has-border-color' => true,
127 'has-$slug-border-color' => 'color',
128 ),
129 ),
130 'radius' => array(
131 'property_keys' => array(
132 'default' => 'border-radius',
133 'individual' => 'border-%s-radius',
134 ),
135 'path' => array( 'border', 'radius' ),
136 'css_vars' => array(
137 'border-radius' => '--wp--preset--border-radius--$slug',
138 ),
139 ),
140 'style' => array(
141 'property_keys' => array(
142 'default' => 'border-style',
143 'individual' => 'border-%s-style',
144 ),
145 'path' => array( 'border', 'style' ),
146 ),
147 'width' => array(
148 'property_keys' => array(
149 'default' => 'border-width',
150 'individual' => 'border-%s-width',
151 ),
152 'path' => array( 'border', 'width' ),
153 ),
154 'top' => array(
155 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
156 'path' => array( 'border', 'top' ),
157 'css_vars' => array(
158 'color' => '--wp--preset--color--$slug',
159 ),
160 ),
161 'right' => array(
162 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
163 'path' => array( 'border', 'right' ),
164 'css_vars' => array(
165 'color' => '--wp--preset--color--$slug',
166 ),
167 ),
168 'bottom' => array(
169 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
170 'path' => array( 'border', 'bottom' ),
171 'css_vars' => array(
172 'color' => '--wp--preset--color--$slug',
173 ),
174 ),
175 'left' => array(
176 'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
177 'path' => array( 'border', 'left' ),
178 'css_vars' => array(
179 'color' => '--wp--preset--color--$slug',
180 ),
181 ),
182 ),
183 'shadow' => array(
184 'shadow' => array(
185 'property_keys' => array(
186 'default' => 'box-shadow',
187 ),
188 'path' => array( 'shadow' ),
189 'css_vars' => array(
190 'shadow' => '--wp--preset--shadow--$slug',
191 ),
192 ),
193 ),
194 'dimensions' => array(
195 'aspectRatio' => array(
196 'property_keys' => array(
197 'default' => 'aspect-ratio',
198 ),
199 'path' => array( 'dimensions', 'aspectRatio' ),
200 'classnames' => array(
201 'has-aspect-ratio' => true,
202 ),
203 ),
204 'minHeight' => array(
205 'property_keys' => array(
206 'default' => 'min-height',
207 ),
208 'path' => array( 'dimensions', 'minHeight' ),
209 'css_vars' => array(
210 'spacing' => '--wp--preset--spacing--$slug',
211 ),
212 ),
213 'width' => array(
214 'property_keys' => array(
215 'default' => 'width',
216 ),
217 'path' => array( 'dimensions', 'width' ),
218 ),
219 ),
220 'spacing' => array(
221 'padding' => array(
222 'property_keys' => array(
223 'default' => 'padding',
224 'individual' => 'padding-%s',
225 ),
226 'path' => array( 'spacing', 'padding' ),
227 'css_vars' => array(
228 'spacing' => '--wp--preset--spacing--$slug',
229 ),
230 ),
231 'margin' => array(
232 'property_keys' => array(
233 'default' => 'margin',
234 'individual' => 'margin-%s',
235 ),
236 'path' => array( 'spacing', 'margin' ),
237 'css_vars' => array(
238 'spacing' => '--wp--preset--spacing--$slug',
239 ),
240 ),
241 ),
242 'typography' => array(
243 'fontSize' => array(
244 'property_keys' => array(
245 'default' => 'font-size',
246 ),
247 'css_vars' => array(
248 'font-size' => '--wp--preset--font-size--$slug',
249 ),
250 'path' => array( 'typography', 'fontSize' ),
251 'classnames' => array(
252 'has-$slug-font-size' => 'font-size',
253 ),
254 ),
255 'fontFamily' => array(
256 'property_keys' => array(
257 'default' => 'font-family',
258 ),
259 'css_vars' => array(
260 'font-family' => '--wp--preset--font-family--$slug',
261 ),
262 'path' => array( 'typography', 'fontFamily' ),
263 'classnames' => array(
264 'has-$slug-font-family' => 'font-family',
265 ),
266 ),
267 'fontStyle' => array(
268 'property_keys' => array(
269 'default' => 'font-style',
270 ),
271 'path' => array( 'typography', 'fontStyle' ),
272 ),
273 'fontWeight' => array(
274 'property_keys' => array(
275 'default' => 'font-weight',
276 ),
277 'path' => array( 'typography', 'fontWeight' ),
278 ),
279 'lineHeight' => array(
280 'property_keys' => array(
281 'default' => 'line-height',
282 ),
283 'path' => array( 'typography', 'lineHeight' ),
284 ),
285 'textColumns' => array(
286 'property_keys' => array(
287 'default' => 'column-count',
288 ),
289 'path' => array( 'typography', 'textColumns' ),
290 ),
291 'textDecoration' => array(
292 'property_keys' => array(
293 'default' => 'text-decoration',
294 ),
295 'path' => array( 'typography', 'textDecoration' ),
296 ),
297 'textTransform' => array(
298 'property_keys' => array(
299 'default' => 'text-transform',
300 ),
301 'path' => array( 'typography', 'textTransform' ),
302 ),
303 'letterSpacing' => array(
304 'property_keys' => array(
305 'default' => 'letter-spacing',
306 ),
307 'path' => array( 'typography', 'letterSpacing' ),
308 ),
309 'writingMode' => array(
310 'property_keys' => array(
311 'default' => 'writing-mode',
312 ),
313 'path' => array( 'typography', 'writingMode' ),
314 ),
315 ),
316 );
317
318 /**
319 * Util: Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
320 *
321 * @param string $style_value A single CSS preset value.
322 * @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
323 *
324 * @return string The slug, or empty string if not found.
325 */
326 protected static function get_slug_from_preset_value( $style_value, $property_key ) {
327 if ( is_string( $style_value ) && is_string( $property_key ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
328 $index_to_splice = strrpos( $style_value, '|' ) + 1;
329 return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
330 }
331 return '';
332 }
333
334 /**
335 * Util: Generates a CSS var string, e.g., var(--wp--preset--color--background) from a preset string such as `var:preset|space|50`.
336 *
337 * @param string $style_value A single CSS preset value.
338 * @param string[] $css_vars An associate array of CSS var patterns used to generate the var string.
339 *
340 * @return string The css var, or an empty string if no match for slug found.
341 */
342 protected static function get_css_var_value( $style_value, $css_vars ) {
343 foreach ( $css_vars as $property_key => $css_var_pattern ) {
344 $slug = static::get_slug_from_preset_value( $style_value, $property_key );
345 if ( static::is_valid_style_value( $slug ) ) {
346 $var = strtr(
347 $css_var_pattern,
348 array( '$slug' => $slug )
349 );
350 return "var($var)";
351 }
352 }
353 return '';
354 }
355
356 /**
357 * Util: Checks whether an incoming block style value is valid.
358 *
359 * @param string? $style_value A single css preset value.
360 *
361 * @return bool
362 */
363 protected static function is_valid_style_value( $style_value ) {
364 return '0' === $style_value || ! empty( $style_value );
365 }
366
367 /**
368 * Stores a CSS rule using the provided CSS selector and CSS declarations.
369 *
370 * @param string $store_name A valid store key.
371 * @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.
372 * @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
373 * @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`.
374 *
375 * @return void.
376 */
377 public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ) {
378 if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) {
379 return;
380 }
381 static::get_store( $store_name )->add_rule( $css_selector, $rules_group )->add_declarations( $css_declarations );
382 }
383
384 /**
385 * Returns a store by store key.
386 *
387 * @param string $store_name A store key.
388 *
389 * @return WP_Style_Engine_CSS_Rules_Store_Gutenberg
390 */
391 public static function get_store( $store_name ) {
392 return WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( $store_name );
393 }
394
395 /**
396 * Returns classnames and CSS based on the values in a styles object.
397 * Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
398 *
399 * @since 6.1.0
400 *
401 * @param array $block_styles The style object.
402 * @param array $options {
403 * Optional. An array of options. Default empty array.
404 *
405 * @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`.
406 * @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 }`,
407 * otherwise, the value will be a concatenated string of CSS declarations.
408 * }
409 *
410 * @return array {
411 * @type string $classnames Classnames separated by a space.
412 * @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
413 * }
414 */
415 public static function parse_block_styles( $block_styles, $options ) {
416 $parsed_styles = array(
417 'classnames' => array(),
418 'declarations' => array(),
419 );
420 if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
421 return $parsed_styles;
422 }
423
424 // Collect CSS and classnames.
425 foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
426 if ( empty( $block_styles[ $definition_group_key ] ) ) {
427 continue;
428 }
429 foreach ( $definition_group_style as $style_definition ) {
430 $style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
431
432 if ( ! static::is_valid_style_value( $style_value ) ) {
433 continue;
434 }
435
436 $classnames = static::get_classnames( $style_value, $style_definition );
437 if ( ! empty( $classnames ) ) {
438 $parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], $classnames );
439 }
440
441 $css_declarations = static::get_css_declarations( $style_value, $style_definition, $options );
442 if ( ! empty( $css_declarations ) ) {
443 $parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], $css_declarations );
444 }
445 }
446 }
447
448 return $parsed_styles;
449 }
450
451 /**
452 * Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., '`var:preset|<PRESET_TYPE>|<PRESET_SLUG>`'.
453 *
454 * @param array $style_value A single raw style value or css preset property from the $block_styles array.
455 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
456 *
457 * @return array|string[] An array of CSS classnames, or empty array.
458 */
459 protected static function get_classnames( $style_value, $style_definition ) {
460 if ( empty( $style_value ) ) {
461 return array();
462 }
463
464 $classnames = array();
465 if ( ! empty( $style_definition['classnames'] ) ) {
466 foreach ( $style_definition['classnames'] as $classname => $property_key ) {
467 if ( true === $property_key ) {
468 $classnames[] = $classname;
469 continue;
470 }
471
472 $slug = static::get_slug_from_preset_value( $style_value, $property_key );
473
474 if ( $slug ) {
475 /*
476 * Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA.
477 * One day, if there are no stored schemata, we could allow custom patterns or
478 * generate classnames based on other properties
479 * such as a path or a value or a prefix passed in options.
480 */
481 $classnames[] = strtr( $classname, array( '$slug' => $slug ) );
482 }
483 }
484 }
485
486 return $classnames;
487 }
488
489 /**
490 * Returns an array of CSS declarations based on valid block style values.
491 *
492 * @since 6.1.0
493 *
494 * @param array $style_value A single raw style value from $block_styles array.
495 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
496 * @param array $options {
497 * Optional. An array of options. Default empty array.
498 *
499 * @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`.
500 * }
501 *
502 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
503 */
504 protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) {
505 if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
506 return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
507 }
508
509 $css_declarations = array();
510 $style_property_keys = $style_definition['property_keys'];
511 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
512
513 /*
514 * Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`.
515 * Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
516 */
517 if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
518 if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
519 $css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
520 if ( static::is_valid_style_value( $css_var ) ) {
521 $css_declarations[ $style_property_keys['default'] ] = $css_var;
522 }
523 }
524 return $css_declarations;
525 }
526
527 /*
528 * Default rule builder.
529 * If the input contains an array, assume box model-like properties
530 * for styles such as margins and padding.
531 */
532 if ( is_array( $style_value ) ) {
533 // Bail out early if the `'individual'` property is not defined.
534 if ( ! isset( $style_property_keys['individual'] ) ) {
535 return $css_declarations;
536 }
537
538 foreach ( $style_value as $key => $value ) {
539 if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
540 $value = static::get_css_var_value( $value, $style_definition['css_vars'] );
541 }
542
543 $individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
544
545 if ( $individual_property && static::is_valid_style_value( $value ) ) {
546 $css_declarations[ $individual_property ] = $value;
547 }
548 }
549
550 return $css_declarations;
551 }
552
553 $css_declarations[ $style_property_keys['default'] ] = $style_value;
554 return $css_declarations;
555 }
556
557 /**
558 * Style value parser that returns a CSS definition array comprising style properties
559 * that have keys representing individual style properties, otherwise known as longhand CSS properties.
560 * e.g., "$style_property-$individual_feature: $value;", which could represent the following:
561 * "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
562 * "border-image-{outset|source|width|repeat|slice}: {value};"
563 *
564 * @param array $style_value A single raw style value from $block_styles array.
565 * @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'.
566 * @param array $options {
567 * Optional. An array of options. Default empty array.
568 *
569 * @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`.
570 * }
571 *
572 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
573 */
574 protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) {
575 if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
576 return array();
577 }
578
579 /*
580 * The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
581 * We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
582 * The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
583 */
584 $definition_group_key = $individual_property_definition['path'][0];
585 $individual_property_key = $individual_property_definition['path'][1];
586 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
587 $css_declarations = array();
588
589 foreach ( $style_value as $css_property => $value ) {
590 if ( empty( $value ) ) {
591 continue;
592 }
593
594 // Build a path to the individual rules in definitions.
595 $style_definition_path = array( $definition_group_key, $css_property );
596 $style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
597
598 if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
599 // Set a CSS var if there is a valid preset value.
600 if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
601 $value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
602 }
603 $individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
604 $css_declarations[ $individual_css_property ] = $value;
605 }
606 }
607 return $css_declarations;
608 }
609
610 /**
611 * Style value parser that constructs a CSS definition array comprising a single CSS property and value.
612 * If the provided value is an array containing a `url` property, the function will return a CSS definition array
613 * with a single property and value, with `url` escaped and injected into a CSS `url()` function,
614 * e.g., array( 'background-image' => "url( '...' )" ).
615 *
616 * @param array $style_value A single raw style value from $block_styles array.
617 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
618 *
619 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
620 */
621 protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) {
622 if ( empty( $style_value ) ) {
623 return array();
624 }
625
626 $css_declarations = array();
627
628 if ( isset( $style_definition['property_keys']['default'] ) ) {
629 $value = null;
630
631 if ( ! empty( $style_value['url'] ) ) {
632 $value = "url('" . $style_value['url'] . "')";
633 } elseif ( is_string( $style_value ) ) {
634 $value = $style_value;
635 }
636
637 if ( null !== $value ) {
638 $css_declarations[ $style_definition['property_keys']['default'] ] = $value;
639 }
640 }
641
642 return $css_declarations;
643 }
644
645 /**
646 * Returns compiled CSS from css_declarations.
647 *
648 * @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
649 * @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.
650 *
651 * @return string A compiled CSS string.
652 */
653 public static function compile_css( $css_declarations, $css_selector ) {
654 if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) {
655 return '';
656 }
657
658 // Return an entire rule if there is a selector.
659 if ( $css_selector ) {
660 $css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $css_selector, $css_declarations );
661 return $css_rule->get_css();
662 }
663
664 $css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $css_declarations );
665 return $css_declarations->get_declarations_string();
666 }
667
668 /**
669 * Returns a compiled stylesheet from stored CSS rules.
670 *
671 * @param WP_Style_Engine_CSS_Rule_Gutenberg[] $css_rules An array of WP_Style_Engine_CSS_Rule_Gutenberg objects from a store or otherwise.
672 * @param array $options {
673 * Optional. An array of options. Default empty array.
674 *
675 * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
676 * @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.
677 * }
678 *
679 * @return string A compiled stylesheet from stored CSS rules.
680 */
681 public static function compile_stylesheet_from_css_rules( $css_rules, $options = array() ) {
682 $processor = new WP_Style_Engine_Processor_Gutenberg();
683 $processor->add_rules( $css_rules );
684 return $processor->get_css( $options );
685 }
686 }
687 }
688