@wordpress/style-engine 0.15.1-next.957ca95e4c.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -30
- package/README.md +36 -32
- package/build/index.js +6 -2
- package/build/index.js.map +1 -1
- package/build/styles/index.js +3 -1
- package/build/styles/index.js.map +1 -1
- package/build/styles/outline/index.js +47 -0
- package/build/styles/outline/index.js.map +1 -0
- package/build-module/index.js +6 -2
- package/build-module/index.js.map +1 -1
- package/build-module/styles/index.js +2 -1
- package/build-module/styles/index.js.map +1 -1
- package/build-module/styles/outline/index.js +38 -0
- package/build-module/styles/outline/index.js.map +1 -0
- package/build-types/index.d.ts +6 -2
- package/build-types/index.d.ts.map +1 -1
- package/build-types/styles/index.d.ts +1 -1
- package/build-types/styles/index.d.ts.map +1 -1
- package/build-types/styles/outline/index.d.ts +10 -0
- package/build-types/styles/outline/index.d.ts.map +1 -0
- package/class-wp-style-engine-css-declarations.php +14 -15
- package/class-wp-style-engine-css-rule.php +14 -14
- package/class-wp-style-engine-css-rules-store.php +7 -7
- package/class-wp-style-engine-processor.php +18 -6
- package/class-wp-style-engine.php +82 -185
- package/docs/using-the-style-engine-with-block-supports.md +151 -0
- package/package.json +4 -3
- package/phpunit/class-wp-style-engine-css-declarations-test.php +142 -95
- package/phpunit/class-wp-style-engine-css-rule-test.php +50 -21
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +75 -58
- package/phpunit/class-wp-style-engine-processor-test.php +85 -37
- package/phpunit/{class-wp-style-engine-test.php → style-engine-test.php} +76 -50
- package/src/index.ts +6 -2
- package/src/styles/index.ts +2 -0
- package/src/styles/outline/index.ts +55 -0
- package/style-engine.php +154 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -37,9 +37,9 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
37
37
|
/**
|
|
38
38
|
* Constructor
|
|
39
39
|
*
|
|
40
|
-
* @param string
|
|
41
|
-
* @param
|
|
42
|
-
*
|
|
40
|
+
* @param string $selector The CSS selector.
|
|
41
|
+
* @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
|
|
42
|
+
* or a WP_Style_Engine_CSS_Declarations object.
|
|
43
43
|
*/
|
|
44
44
|
public function __construct( $selector = '', $declarations = array() ) {
|
|
45
45
|
$this->set_selector( $selector );
|
|
@@ -47,7 +47,7 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Sets the selector.
|
|
51
51
|
*
|
|
52
52
|
* @param string $selector The CSS selector.
|
|
53
53
|
*
|
|
@@ -59,7 +59,7 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* Sets the declarations.
|
|
63
63
|
*
|
|
64
64
|
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
|
|
65
65
|
* or a WP_Style_Engine_CSS_Declarations object.
|
|
@@ -83,16 +83,16 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
86
|
+
* Gets the declarations object.
|
|
87
87
|
*
|
|
88
|
-
* @return WP_Style_Engine_CSS_Declarations
|
|
88
|
+
* @return WP_Style_Engine_CSS_Declarations The declarations object.
|
|
89
89
|
*/
|
|
90
90
|
public function get_declarations() {
|
|
91
91
|
return $this->declarations;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
*
|
|
95
|
+
* Gets the full selector.
|
|
96
96
|
*
|
|
97
97
|
* @return string
|
|
98
98
|
*/
|
|
@@ -101,18 +101,18 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
|
-
*
|
|
104
|
+
* Gets the CSS.
|
|
105
105
|
*
|
|
106
|
-
* @param
|
|
107
|
-
* @param number
|
|
106
|
+
* @param bool $should_prettify Whether to add spacing, new lines and indents.
|
|
107
|
+
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
|
|
108
108
|
*
|
|
109
109
|
* @return string
|
|
110
110
|
*/
|
|
111
111
|
public function get_css( $should_prettify = false, $indent_count = 0 ) {
|
|
112
112
|
$rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
|
|
113
113
|
$declarations_indent = $should_prettify ? $indent_count + 1 : 0;
|
|
114
|
-
$
|
|
115
|
-
$
|
|
114
|
+
$suffix = $should_prettify ? "\n" : '';
|
|
115
|
+
$spacer = $should_prettify ? ' ' : '';
|
|
116
116
|
$selector = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
|
|
117
117
|
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
|
|
118
118
|
|
|
@@ -120,6 +120,6 @@ class WP_Style_Engine_CSS_Rule {
|
|
|
120
120
|
return '';
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
return "{$rule_indent}{$selector}{$
|
|
123
|
+
return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
|
|
124
124
|
}
|
|
125
125
|
}
|
|
@@ -42,7 +42,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
42
42
|
protected $rules = array();
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Gets an instance of the store.
|
|
46
46
|
*
|
|
47
47
|
* @param string $store_name The name of the store.
|
|
48
48
|
*
|
|
@@ -61,7 +61,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
64
|
+
* Gets an array of all available stores.
|
|
65
65
|
*
|
|
66
66
|
* @return WP_Style_Engine_CSS_Rules_Store[]
|
|
67
67
|
*/
|
|
@@ -79,7 +79,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
82
|
+
* Sets the store name.
|
|
83
83
|
*
|
|
84
84
|
* @param string $name The store name.
|
|
85
85
|
*
|
|
@@ -90,7 +90,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* Gets the store name.
|
|
94
94
|
*
|
|
95
95
|
* @return string
|
|
96
96
|
*/
|
|
@@ -99,7 +99,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* Gets an array of all rules.
|
|
103
103
|
*
|
|
104
104
|
* @return WP_Style_Engine_CSS_Rule[]
|
|
105
105
|
*/
|
|
@@ -108,7 +108,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
*
|
|
111
|
+
* Gets a WP_Style_Engine_CSS_Rule object by its selector.
|
|
112
112
|
* If the rule does not exist, it will be created.
|
|
113
113
|
*
|
|
114
114
|
* @param string $selector The CSS selector.
|
|
@@ -132,7 +132,7 @@ class WP_Style_Engine_CSS_Rules_Store {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
135
|
+
* Removes a selector from the store.
|
|
136
136
|
*
|
|
137
137
|
* @param string $selector The CSS selector.
|
|
138
138
|
*
|
|
@@ -19,7 +19,7 @@ if ( class_exists( 'WP_Style_Engine_Processor' ) ) {
|
|
|
19
19
|
class WP_Style_Engine_Processor {
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* A collection of Style Engine Store objects.
|
|
23
23
|
*
|
|
24
24
|
* @var WP_Style_Engine_CSS_Rules_Store[]
|
|
25
25
|
*/
|
|
@@ -39,7 +39,16 @@ class WP_Style_Engine_Processor {
|
|
|
39
39
|
*
|
|
40
40
|
* @return WP_Style_Engine_Processor Returns the object to allow chaining methods.
|
|
41
41
|
*/
|
|
42
|
-
public function add_store(
|
|
42
|
+
public function add_store( $store ) {
|
|
43
|
+
if ( ! $store instanceof WP_Style_Engine_CSS_Rules_Store ) {
|
|
44
|
+
_doing_it_wrong(
|
|
45
|
+
__METHOD__,
|
|
46
|
+
__( '$store must be an instance of WP_Style_Engine_CSS_Rules_Store', 'default' ),
|
|
47
|
+
'6.1.0'
|
|
48
|
+
);
|
|
49
|
+
return $this;
|
|
50
|
+
}
|
|
51
|
+
|
|
43
52
|
$this->stores[ $store->get_name() ] = $store;
|
|
44
53
|
|
|
45
54
|
return $this;
|
|
@@ -56,6 +65,7 @@ class WP_Style_Engine_Processor {
|
|
|
56
65
|
if ( ! is_array( $css_rules ) ) {
|
|
57
66
|
$css_rules = array( $css_rules );
|
|
58
67
|
}
|
|
68
|
+
|
|
59
69
|
foreach ( $css_rules as $rule ) {
|
|
60
70
|
$selector = $rule->get_selector();
|
|
61
71
|
if ( isset( $this->css_rules[ $selector ] ) ) {
|
|
@@ -71,10 +81,12 @@ class WP_Style_Engine_Processor {
|
|
|
71
81
|
/**
|
|
72
82
|
* Get the CSS rules as a string.
|
|
73
83
|
*
|
|
74
|
-
* @param array $options
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
84
|
+
* @param array $options {
|
|
85
|
+
* Optional. An array of options. Default empty array.
|
|
86
|
+
*
|
|
87
|
+
* @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
|
|
88
|
+
* @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.
|
|
89
|
+
* }
|
|
78
90
|
*
|
|
79
91
|
* @return string The computed CSS.
|
|
80
92
|
*/
|
|
@@ -12,12 +12,11 @@ if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Class WP_Style_Engine.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
* CSS styles generation.
|
|
17
|
+
* The Style Engine aims to provide a consistent API for rendering styling for blocks across both client-side and server-side applications.
|
|
19
18
|
*
|
|
20
|
-
* This class is for internal
|
|
19
|
+
* This class is for internal Core usage and is not supposed to be used by extenders (plugins and/or themes).
|
|
21
20
|
* This is a low-level API that may need to do breaking changes. Please, use wp_style_engine_get_styles instead.
|
|
22
21
|
*
|
|
23
22
|
* @access private
|
|
@@ -35,6 +34,8 @@ class WP_Style_Engine {
|
|
|
35
34
|
* - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
|
|
36
35
|
* - path => (array) a path that accesses the corresponding style value in the block style object.
|
|
37
36
|
* - 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", ... )`.
|
|
37
|
+
*
|
|
38
|
+
* @var array
|
|
38
39
|
*/
|
|
39
40
|
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
|
|
40
41
|
'color' => array(
|
|
@@ -217,13 +218,13 @@ class WP_Style_Engine {
|
|
|
217
218
|
/**
|
|
218
219
|
* Util: Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
|
|
219
220
|
*
|
|
220
|
-
* @param string
|
|
221
|
-
* @param string
|
|
221
|
+
* @param string $style_value A single CSS preset value.
|
|
222
|
+
* @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
|
|
222
223
|
*
|
|
223
224
|
* @return string The slug, or empty string if not found.
|
|
224
225
|
*/
|
|
225
226
|
protected static function get_slug_from_preset_value( $style_value, $property_key ) {
|
|
226
|
-
if ( is_string( $style_value ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
|
|
227
|
+
if ( is_string( $style_value ) && is_string( $property_key ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
|
|
227
228
|
$index_to_splice = strrpos( $style_value, '|' ) + 1;
|
|
228
229
|
return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
|
|
229
230
|
}
|
|
@@ -231,10 +232,10 @@ class WP_Style_Engine {
|
|
|
231
232
|
}
|
|
232
233
|
|
|
233
234
|
/**
|
|
234
|
-
* Util: Generates a
|
|
235
|
+
* Util: Generates a CSS var string, e.g., var(--wp--preset--color--background) from a preset string such as `var:preset|space|50`.
|
|
235
236
|
*
|
|
236
|
-
* @param string
|
|
237
|
-
* @param
|
|
237
|
+
* @param string $style_value A single CSS preset value.
|
|
238
|
+
* @param string[] $css_vars An associate array of CSS var patterns used to generate the var string.
|
|
238
239
|
*
|
|
239
240
|
* @return string The css var, or an empty string if no match for slug found.
|
|
240
241
|
*/
|
|
@@ -257,7 +258,7 @@ class WP_Style_Engine {
|
|
|
257
258
|
*
|
|
258
259
|
* @param string? $style_value A single css preset value.
|
|
259
260
|
*
|
|
260
|
-
* @return
|
|
261
|
+
* @return bool
|
|
261
262
|
*/
|
|
262
263
|
protected static function is_valid_style_value( $style_value ) {
|
|
263
264
|
return '0' === $style_value || ! empty( $style_value );
|
|
@@ -266,9 +267,9 @@ class WP_Style_Engine {
|
|
|
266
267
|
/**
|
|
267
268
|
* Stores a CSS rule using the provided CSS selector and CSS declarations.
|
|
268
269
|
*
|
|
269
|
-
* @param string
|
|
270
|
-
* @param string
|
|
271
|
-
* @param
|
|
270
|
+
* @param string $store_name A valid store key.
|
|
271
|
+
* @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.
|
|
272
|
+
* @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
272
273
|
*
|
|
273
274
|
* @return void.
|
|
274
275
|
*/
|
|
@@ -294,16 +295,21 @@ class WP_Style_Engine {
|
|
|
294
295
|
* Returns classnames and CSS based on the values in a styles object.
|
|
295
296
|
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
296
297
|
*
|
|
298
|
+
* @since 6.1.0
|
|
299
|
+
*
|
|
297
300
|
* @param array $block_styles The style object.
|
|
298
|
-
* @param array $options
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* )
|
|
301
|
+
* @param array $options {
|
|
302
|
+
* Optional. An array of options. Default empty array.
|
|
303
|
+
*
|
|
304
|
+
* @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`.
|
|
305
|
+
* @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 }`,
|
|
306
|
+
* otherwise, the value will be a concatenated string of CSS declarations.
|
|
307
|
+
* }
|
|
302
308
|
*
|
|
303
|
-
* @return array
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
309
|
+
* @return array {
|
|
310
|
+
* @type string $classnames Classnames separated by a space.
|
|
311
|
+
* @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
312
|
+
* }
|
|
307
313
|
*/
|
|
308
314
|
public static function parse_block_styles( $block_styles, $options ) {
|
|
309
315
|
$parsed_styles = array(
|
|
@@ -335,12 +341,12 @@ class WP_Style_Engine {
|
|
|
335
341
|
}
|
|
336
342
|
|
|
337
343
|
/**
|
|
338
|
-
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., 'var:preset
|
|
344
|
+
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., '`var:preset|<PRESET_TYPE>|<PRESET_SLUG>`'.
|
|
339
345
|
*
|
|
340
|
-
* @param array
|
|
341
|
-
* @param array
|
|
346
|
+
* @param array $style_value A single raw style value or css preset property from the $block_styles array.
|
|
347
|
+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
342
348
|
*
|
|
343
|
-
* @return array
|
|
349
|
+
* @return array|string[] An array of CSS classnames, or empty array.
|
|
344
350
|
*/
|
|
345
351
|
protected static function get_classnames( $style_value, $style_definition ) {
|
|
346
352
|
if ( empty( $style_value ) ) {
|
|
@@ -357,10 +363,12 @@ class WP_Style_Engine {
|
|
|
357
363
|
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
|
|
358
364
|
|
|
359
365
|
if ( $slug ) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
366
|
+
/*
|
|
367
|
+
* Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
368
|
+
* One day, if there are no stored schemata, we could allow custom patterns or
|
|
369
|
+
* generate classnames based on other properties
|
|
370
|
+
* such as a path or a value or a prefix passed in options.
|
|
371
|
+
*/
|
|
364
372
|
$classnames[] = strtr( $classname, array( '$slug' => $slug ) );
|
|
365
373
|
}
|
|
366
374
|
}
|
|
@@ -372,15 +380,19 @@ class WP_Style_Engine {
|
|
|
372
380
|
/**
|
|
373
381
|
* Returns an array of CSS declarations based on valid block style values.
|
|
374
382
|
*
|
|
375
|
-
* @
|
|
376
|
-
* @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
377
|
-
* @param array $options array(
|
|
378
|
-
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
379
|
-
* );.
|
|
383
|
+
* @since 6.1.0
|
|
380
384
|
*
|
|
381
|
-
* @
|
|
385
|
+
* @param array $style_value A single raw style value from $block_styles array.
|
|
386
|
+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
387
|
+
* @param array $options {
|
|
388
|
+
* Optional. An array of options. Default empty array.
|
|
389
|
+
*
|
|
390
|
+
* @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`.
|
|
391
|
+
* }
|
|
392
|
+
*
|
|
393
|
+
* @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
382
394
|
*/
|
|
383
|
-
protected static function get_css_declarations( $style_value, $style_definition, $options ) {
|
|
395
|
+
protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) {
|
|
384
396
|
if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
|
|
385
397
|
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
|
|
386
398
|
}
|
|
@@ -389,8 +401,10 @@ class WP_Style_Engine {
|
|
|
389
401
|
$style_property_keys = $style_definition['property_keys'];
|
|
390
402
|
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
391
403
|
|
|
392
|
-
|
|
393
|
-
|
|
404
|
+
/*
|
|
405
|
+
* Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`.
|
|
406
|
+
* Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
|
|
407
|
+
*/
|
|
394
408
|
if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
|
|
395
409
|
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
396
410
|
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
|
|
@@ -401,9 +415,11 @@ class WP_Style_Engine {
|
|
|
401
415
|
return $css_declarations;
|
|
402
416
|
}
|
|
403
417
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
418
|
+
/*
|
|
419
|
+
* Default rule builder.
|
|
420
|
+
* If the input contains an array, assume box model-like properties
|
|
421
|
+
* for styles such as margins and padding.
|
|
422
|
+
*/
|
|
407
423
|
if ( is_array( $style_value ) ) {
|
|
408
424
|
// Bail out early if the `'individual'` property is not defined.
|
|
409
425
|
if ( ! isset( $style_property_keys['individual'] ) ) {
|
|
@@ -436,22 +452,26 @@ class WP_Style_Engine {
|
|
|
436
452
|
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
|
|
437
453
|
* "border-image-{outset|source|width|repeat|slice}: {value};"
|
|
438
454
|
*
|
|
439
|
-
* @param array $style_value A single raw
|
|
440
|
-
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
441
|
-
* @param array $options
|
|
442
|
-
*
|
|
443
|
-
* );.
|
|
455
|
+
* @param array $style_value A single raw style value from $block_styles array.
|
|
456
|
+
* @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'.
|
|
457
|
+
* @param array $options {
|
|
458
|
+
* Optional. An array of options. Default empty array.
|
|
444
459
|
*
|
|
445
|
-
*
|
|
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
|
+
* }
|
|
462
|
+
*
|
|
463
|
+
* @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
446
464
|
*/
|
|
447
|
-
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options ) {
|
|
465
|
+
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) {
|
|
448
466
|
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
|
|
449
467
|
return array();
|
|
450
468
|
}
|
|
451
469
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
470
|
+
/*
|
|
471
|
+
* The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
|
|
472
|
+
* We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
|
|
473
|
+
* The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
|
|
474
|
+
*/
|
|
455
475
|
$definition_group_key = $individual_property_definition['path'][0];
|
|
456
476
|
$individual_property_key = $individual_property_definition['path'][1];
|
|
457
477
|
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
@@ -481,8 +501,8 @@ class WP_Style_Engine {
|
|
|
481
501
|
/**
|
|
482
502
|
* Returns compiled CSS from css_declarations.
|
|
483
503
|
*
|
|
484
|
-
* @param
|
|
485
|
-
* @param string
|
|
504
|
+
* @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
505
|
+
* @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.
|
|
486
506
|
*
|
|
487
507
|
* @return string A compiled CSS string.
|
|
488
508
|
*/
|
|
@@ -505,141 +525,18 @@ class WP_Style_Engine {
|
|
|
505
525
|
* Returns a compiled stylesheet from stored CSS rules.
|
|
506
526
|
*
|
|
507
527
|
* @param WP_Style_Engine_CSS_Rule[] $css_rules An array of WP_Style_Engine_CSS_Rule objects from a store or otherwise.
|
|
528
|
+
* @param array $options {
|
|
529
|
+
* Optional. An array of options. Default empty array.
|
|
530
|
+
*
|
|
531
|
+
* @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
|
|
532
|
+
* @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.
|
|
533
|
+
* }
|
|
508
534
|
*
|
|
509
535
|
* @return string A compiled stylesheet from stored CSS rules.
|
|
510
536
|
*/
|
|
511
|
-
public static function compile_stylesheet_from_css_rules( $css_rules ) {
|
|
537
|
+
public static function compile_stylesheet_from_css_rules( $css_rules, $options = array() ) {
|
|
512
538
|
$processor = new WP_Style_Engine_Processor();
|
|
513
539
|
$processor->add_rules( $css_rules );
|
|
514
|
-
return $processor->get_css();
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
/**
|
|
519
|
-
* Global public interface method to generate styles from a single style object, e.g.,
|
|
520
|
-
* the value of a block's attributes.style object or the top level styles in theme.json.
|
|
521
|
-
* See: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles and
|
|
522
|
-
* https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
|
|
523
|
-
*
|
|
524
|
-
* Example usage:
|
|
525
|
-
*
|
|
526
|
-
* $styles = wp_style_engine_get_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );
|
|
527
|
-
* // Returns `array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' )`.
|
|
528
|
-
*
|
|
529
|
-
* @access public
|
|
530
|
-
*
|
|
531
|
-
* @param array $block_styles The style object.
|
|
532
|
-
* @param array<string|boolean> $options array(
|
|
533
|
-
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
534
|
-
* When set, the style engine will attempt to store the CSS rules, where a selector is also passed.
|
|
535
|
-
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
536
|
-
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
537
|
-
* );.
|
|
538
|
-
*
|
|
539
|
-
* @return array<string|array> array(
|
|
540
|
-
* 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
|
|
541
|
-
* 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
|
|
542
|
-
* 'classnames' => (string) Classnames separated by a space.
|
|
543
|
-
* );
|
|
544
|
-
*/
|
|
545
|
-
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
|
|
546
|
-
if ( ! class_exists( 'WP_Style_Engine' ) ) {
|
|
547
|
-
return array();
|
|
540
|
+
return $processor->get_css( $options );
|
|
548
541
|
}
|
|
549
|
-
|
|
550
|
-
$options = wp_parse_args(
|
|
551
|
-
$options,
|
|
552
|
-
array(
|
|
553
|
-
'selector' => null,
|
|
554
|
-
'context' => null,
|
|
555
|
-
'convert_vars_to_classnames' => false,
|
|
556
|
-
)
|
|
557
|
-
);
|
|
558
|
-
|
|
559
|
-
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
|
|
560
|
-
|
|
561
|
-
// Output.
|
|
562
|
-
$styles_output = array();
|
|
563
|
-
|
|
564
|
-
if ( ! empty( $parsed_styles['declarations'] ) ) {
|
|
565
|
-
$styles_output['css'] = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
|
|
566
|
-
$styles_output['declarations'] = $parsed_styles['declarations'];
|
|
567
|
-
if ( ! empty( $options['context'] ) ) {
|
|
568
|
-
WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
if ( ! empty( $parsed_styles['classnames'] ) ) {
|
|
573
|
-
$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
return array_filter( $styles_output );
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* Returns compiled CSS from a collection of selectors and declarations.
|
|
581
|
-
* This won't add to any store, but is useful for returning a compiled style sheet from any CSS selector + declarations combos.
|
|
582
|
-
*
|
|
583
|
-
* @access public
|
|
584
|
-
*
|
|
585
|
-
* @param array<array> $css_rules array(
|
|
586
|
-
* array(
|
|
587
|
-
* 'selector' => (string) A CSS selector.
|
|
588
|
-
* declarations' => (boolean) An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
589
|
-
* )
|
|
590
|
-
* );.
|
|
591
|
-
* @param array<string> $options array(
|
|
592
|
-
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
593
|
-
* When set, the style engine will attempt to store the CSS rules.
|
|
594
|
-
* );.
|
|
595
|
-
*
|
|
596
|
-
* @return string A compiled CSS string.
|
|
597
|
-
*/
|
|
598
|
-
function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = array() ) {
|
|
599
|
-
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $css_rules ) ) {
|
|
600
|
-
return '';
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
$options = wp_parse_args(
|
|
604
|
-
$options,
|
|
605
|
-
array(
|
|
606
|
-
'context' => null,
|
|
607
|
-
)
|
|
608
|
-
);
|
|
609
|
-
|
|
610
|
-
$css_rule_objects = array();
|
|
611
|
-
foreach ( $css_rules as $css_rule ) {
|
|
612
|
-
if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) {
|
|
613
|
-
continue;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
if ( ! empty( $options['context'] ) ) {
|
|
617
|
-
WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'] );
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
$css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'] );
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
if ( empty( $css_rule_objects ) ) {
|
|
624
|
-
return '';
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects );
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* Returns compiled CSS from a store, if found.
|
|
632
|
-
*
|
|
633
|
-
* @access public
|
|
634
|
-
*
|
|
635
|
-
* @param string $store_name A valid store name.
|
|
636
|
-
*
|
|
637
|
-
* @return string A compiled CSS string.
|
|
638
|
-
*/
|
|
639
|
-
function wp_style_engine_get_stylesheet_from_store( $store_name ) {
|
|
640
|
-
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $store_name ) ) {
|
|
641
|
-
return '';
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $store_name )->get_all_rules() );
|
|
645
542
|
}
|