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