@wordpress/style-engine 0.11.0 → 0.14.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 +6 -0
- package/README.md +162 -4
- package/build/styles/utils.js +1 -1
- package/build/styles/utils.js.map +1 -1
- package/build-module/styles/utils.js +1 -1
- package/build-module/styles/utils.js.map +1 -1
- package/class-wp-style-engine-css-declarations.php +167 -0
- package/class-wp-style-engine-css-rule.php +129 -0
- package/class-wp-style-engine-css-rules-store.php +143 -0
- package/class-wp-style-engine-processor.php +135 -0
- package/class-wp-style-engine.php +290 -152
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-css-declarations-test.php +232 -0
- package/phpunit/class-wp-style-engine-css-rule-test.php +127 -0
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +157 -0
- package/phpunit/class-wp-style-engine-processor-test.php +266 -0
- package/phpunit/class-wp-style-engine-test.php +360 -27
- package/src/styles/utils.ts +3 -1
- package/src/test/index.js +41 -2
- package/build-types/index.d.ts +0 -23
- package/build-types/index.d.ts.map +0 -1
- package/build-types/styles/border/index.d.ts +0 -10
- package/build-types/styles/border/index.d.ts.map +0 -1
- package/build-types/styles/color/background.d.ts +0 -14
- package/build-types/styles/color/background.d.ts.map +0 -1
- package/build-types/styles/color/gradient.d.ts +0 -14
- package/build-types/styles/color/gradient.d.ts.map +0 -1
- package/build-types/styles/color/index.d.ts +0 -10
- package/build-types/styles/color/index.d.ts.map +0 -1
- package/build-types/styles/color/text.d.ts +0 -14
- package/build-types/styles/color/text.d.ts.map +0 -1
- package/build-types/styles/constants.d.ts +0 -4
- package/build-types/styles/constants.d.ts.map +0 -1
- package/build-types/styles/index.d.ts +0 -5
- package/build-types/styles/index.d.ts.map +0 -1
- package/build-types/styles/spacing/index.d.ts +0 -6
- package/build-types/styles/spacing/index.d.ts.map +0 -1
- package/build-types/styles/spacing/margin.d.ts +0 -10
- package/build-types/styles/spacing/margin.d.ts.map +0 -1
- package/build-types/styles/spacing/padding.d.ts +0 -10
- package/build-types/styles/spacing/padding.d.ts.map +0 -1
- package/build-types/styles/typography/index.d.ts +0 -14
- package/build-types/styles/typography/index.d.ts.map +0 -1
- package/build-types/styles/utils.d.ts +0 -48
- package/build-types/styles/utils.d.ts.map +0 -1
- package/build-types/types.d.ts +0 -86
- package/build-types/types.d.ts.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -23,13 +23,6 @@ if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
|
23
23
|
* @access private
|
|
24
24
|
*/
|
|
25
25
|
class WP_Style_Engine {
|
|
26
|
-
/**
|
|
27
|
-
* Container for the main instance of the class.
|
|
28
|
-
*
|
|
29
|
-
* @var WP_Style_Engine|null
|
|
30
|
-
*/
|
|
31
|
-
private static $instance = null;
|
|
32
|
-
|
|
33
26
|
/**
|
|
34
27
|
* Style definitions that contain the instructions to
|
|
35
28
|
* parse/output valid Gutenberg styles from a block's attributes.
|
|
@@ -51,7 +44,7 @@ class WP_Style_Engine {
|
|
|
51
44
|
),
|
|
52
45
|
'path' => array( 'color', 'text' ),
|
|
53
46
|
'css_vars' => array(
|
|
54
|
-
'--wp--preset--color--$slug'
|
|
47
|
+
'color' => '--wp--preset--color--$slug',
|
|
55
48
|
),
|
|
56
49
|
'classnames' => array(
|
|
57
50
|
'has-text-color' => true,
|
|
@@ -148,6 +141,9 @@ class WP_Style_Engine {
|
|
|
148
141
|
'individual' => 'padding-%s',
|
|
149
142
|
),
|
|
150
143
|
'path' => array( 'spacing', 'padding' ),
|
|
144
|
+
'css_vars' => array(
|
|
145
|
+
'spacing' => '--wp--preset--spacing--$slug',
|
|
146
|
+
),
|
|
151
147
|
),
|
|
152
148
|
'margin' => array(
|
|
153
149
|
'property_keys' => array(
|
|
@@ -155,6 +151,9 @@ class WP_Style_Engine {
|
|
|
155
151
|
'individual' => 'margin-%s',
|
|
156
152
|
),
|
|
157
153
|
'path' => array( 'spacing', 'margin' ),
|
|
154
|
+
'css_vars' => array(
|
|
155
|
+
'spacing' => '--wp--preset--spacing--$slug',
|
|
156
|
+
),
|
|
158
157
|
),
|
|
159
158
|
),
|
|
160
159
|
'typography' => array(
|
|
@@ -216,22 +215,7 @@ class WP_Style_Engine {
|
|
|
216
215
|
);
|
|
217
216
|
|
|
218
217
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* The instance will be created if it does not exist yet.
|
|
222
|
-
*
|
|
223
|
-
* @return WP_Style_Engine The main instance.
|
|
224
|
-
*/
|
|
225
|
-
public static function get_instance() {
|
|
226
|
-
if ( null === self::$instance ) {
|
|
227
|
-
self::$instance = new self();
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return self::$instance;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
|
|
218
|
+
* Util: Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
|
|
235
219
|
*
|
|
236
220
|
* @param string? $style_value A single css preset value.
|
|
237
221
|
* @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
|
|
@@ -247,7 +231,29 @@ class WP_Style_Engine {
|
|
|
247
231
|
}
|
|
248
232
|
|
|
249
233
|
/**
|
|
250
|
-
*
|
|
234
|
+
* Util: Generates a css var string, eg var(--wp--preset--color--background) from a preset string, eg. `var:preset|space|50`.
|
|
235
|
+
*
|
|
236
|
+
* @param string $style_value A single css preset value.
|
|
237
|
+
* @param array $css_vars The css var patterns used to generate the var string.
|
|
238
|
+
*
|
|
239
|
+
* @return string|null The css var, or null if no match for slug found.
|
|
240
|
+
*/
|
|
241
|
+
protected static function get_css_var_value( $style_value, $css_vars ) {
|
|
242
|
+
foreach ( $css_vars as $property_key => $css_var_pattern ) {
|
|
243
|
+
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
|
|
244
|
+
if ( $slug ) {
|
|
245
|
+
$var = strtr(
|
|
246
|
+
$css_var_pattern,
|
|
247
|
+
array( '$slug' => $slug )
|
|
248
|
+
);
|
|
249
|
+
return "var($var)";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Util: Checks whether an incoming block style value is valid.
|
|
251
257
|
*
|
|
252
258
|
* @param string? $style_value A single css preset value.
|
|
253
259
|
*
|
|
@@ -265,6 +271,80 @@ class WP_Style_Engine {
|
|
|
265
271
|
return true;
|
|
266
272
|
}
|
|
267
273
|
|
|
274
|
+
/**
|
|
275
|
+
* Stores a CSS rule using the provide CSS selector and CSS declarations.
|
|
276
|
+
*
|
|
277
|
+
* @param string $store_key A valid store key.
|
|
278
|
+
* @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.
|
|
279
|
+
* @param array $css_declarations An array of parsed CSS property => CSS value pairs.
|
|
280
|
+
*
|
|
281
|
+
* @return void.
|
|
282
|
+
*/
|
|
283
|
+
public static function store_css_rule( $store_key, $css_selector, $css_declarations ) {
|
|
284
|
+
if ( empty( $store_key ) || empty( $css_selector ) || empty( $css_declarations ) ) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
static::get_store( $store_key )->add_rule( $css_selector )->add_declarations( $css_declarations );
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Returns a store by store key.
|
|
292
|
+
*
|
|
293
|
+
* @param string $store_key A store key.
|
|
294
|
+
*
|
|
295
|
+
* @return WP_Style_Engine_CSS_Rules_Store
|
|
296
|
+
*/
|
|
297
|
+
public static function get_store( $store_key ) {
|
|
298
|
+
return WP_Style_Engine_CSS_Rules_Store::get_store( $store_key );
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Returns classnames and CSS based on the values in a styles object.
|
|
303
|
+
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
304
|
+
*
|
|
305
|
+
* @param array $block_styles The style object.
|
|
306
|
+
* @param array $options array(
|
|
307
|
+
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
308
|
+
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
309
|
+
* );.
|
|
310
|
+
*
|
|
311
|
+
* @return array array(
|
|
312
|
+
* 'declarations' => (array) An array of parsed CSS property => CSS value pairs.
|
|
313
|
+
* 'classnames' => (array) A flat array of classnames.
|
|
314
|
+
* );
|
|
315
|
+
*/
|
|
316
|
+
public static function parse_block_styles( $block_styles, $options ) {
|
|
317
|
+
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
|
|
318
|
+
return array();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
$css_declarations = array();
|
|
322
|
+
$classnames = array();
|
|
323
|
+
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
324
|
+
|
|
325
|
+
// Collect CSS and classnames.
|
|
326
|
+
foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
|
|
327
|
+
if ( empty( $block_styles[ $definition_group_key ] ) ) {
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
foreach ( $definition_group_style as $style_definition ) {
|
|
331
|
+
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
|
|
332
|
+
|
|
333
|
+
if ( ! static::is_valid_style_value( $style_value ) ) {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
$classnames = array_merge( $classnames, static::get_classnames( $style_value, $style_definition ) );
|
|
338
|
+
$css_declarations = array_merge( $css_declarations, static::get_css_declarations( $style_value, $style_definition, $should_skip_css_vars ) );
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return array(
|
|
343
|
+
'classnames' => $classnames,
|
|
344
|
+
'declarations' => $css_declarations,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
268
348
|
/**
|
|
269
349
|
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., 'var:preset|color|heavenly-blue'.
|
|
270
350
|
*
|
|
@@ -304,18 +384,15 @@ class WP_Style_Engine {
|
|
|
304
384
|
/**
|
|
305
385
|
* Returns an array of CSS declarations based on valid block style values.
|
|
306
386
|
*
|
|
307
|
-
* @param array $style_value
|
|
308
|
-
* @param array<string> $style_definition
|
|
309
|
-
* @param boolean $
|
|
387
|
+
* @param array $style_value A single raw style value from the generate() $block_styles array.
|
|
388
|
+
* @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
389
|
+
* @param boolean $should_skip_css_vars Whether to skip compiling CSS var values.
|
|
310
390
|
*
|
|
311
391
|
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
312
392
|
*/
|
|
313
|
-
protected static function get_css_declarations( $style_value, $style_definition, $
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
is_callable( $style_definition['value_func'] )
|
|
317
|
-
) {
|
|
318
|
-
return call_user_func( $style_definition['value_func'], $style_value, $style_definition );
|
|
393
|
+
protected static function get_css_declarations( $style_value, $style_definition, $should_skip_css_vars = false ) {
|
|
394
|
+
if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
|
|
395
|
+
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $should_skip_css_vars );
|
|
319
396
|
}
|
|
320
397
|
|
|
321
398
|
$css_declarations = array();
|
|
@@ -324,16 +401,10 @@ class WP_Style_Engine {
|
|
|
324
401
|
// Build CSS var values from var:? values, e.g, `var(--wp--css--rule-slug )`
|
|
325
402
|
// Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
|
|
326
403
|
if ( is_string( $style_value ) && strpos( $style_value, 'var:' ) !== false ) {
|
|
327
|
-
if ( $
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
$css_var = strtr(
|
|
332
|
-
$css_var_pattern,
|
|
333
|
-
array( '$slug' => $slug )
|
|
334
|
-
);
|
|
335
|
-
$css_declarations[ $style_property_keys['default'] ] = "var($css_var)";
|
|
336
|
-
}
|
|
404
|
+
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
405
|
+
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
|
|
406
|
+
if ( $css_var ) {
|
|
407
|
+
$css_declarations[ $style_property_keys['default'] ] = $css_var;
|
|
337
408
|
}
|
|
338
409
|
}
|
|
339
410
|
return $css_declarations;
|
|
@@ -344,96 +415,23 @@ class WP_Style_Engine {
|
|
|
344
415
|
// for styles such as margins and padding.
|
|
345
416
|
if ( is_array( $style_value ) ) {
|
|
346
417
|
foreach ( $style_value as $key => $value ) {
|
|
347
|
-
$
|
|
348
|
-
|
|
349
|
-
}
|
|
350
|
-
} else {
|
|
351
|
-
$css_declarations[ $style_property_keys['default'] ] = $style_value;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
return $css_declarations;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Returns classname and CSS from a block styles object.
|
|
359
|
-
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
360
|
-
*
|
|
361
|
-
* @param array $block_styles An array of styles from a block's attributes.
|
|
362
|
-
* @param array $options array(
|
|
363
|
-
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
364
|
-
* 'css_vars' => (boolean) Whether to covert CSS values to var() values. If `true` the style engine will try to parse var:? values and output var( --wp--preset--* ) rules. Default is `false`.
|
|
365
|
-
* );.
|
|
366
|
-
*
|
|
367
|
-
* @return array|null array(
|
|
368
|
-
* 'css' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag. Default is a string of inline styles.
|
|
369
|
-
* 'classnames' => (string) Classnames separated by a space.
|
|
370
|
-
* );
|
|
371
|
-
*/
|
|
372
|
-
public function generate( $block_styles, $options ) {
|
|
373
|
-
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
|
|
374
|
-
return null;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
$css_declarations = array();
|
|
378
|
-
$classnames = array();
|
|
379
|
-
$should_return_css_vars = isset( $options['css_vars'] ) && true === $options['css_vars'];
|
|
380
|
-
|
|
381
|
-
// Collect CSS and classnames.
|
|
382
|
-
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
|
|
383
|
-
if ( empty( $block_styles[ $definition_group_key ] ) ) {
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
foreach ( $definition_group_style as $style_definition ) {
|
|
387
|
-
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
|
|
388
|
-
|
|
389
|
-
if ( ! static::is_valid_style_value( $style_value ) ) {
|
|
390
|
-
continue;
|
|
418
|
+
if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
419
|
+
$value = static::get_css_var_value( $value, $style_definition['css_vars'] );
|
|
391
420
|
}
|
|
392
421
|
|
|
393
|
-
$
|
|
394
|
-
$css_declarations = array_merge( $css_declarations, static::get_css_declarations( $style_value, $style_definition, $should_return_css_vars ) );
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// Build CSS rules output.
|
|
399
|
-
$css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
|
|
400
|
-
$filtered_css_declarations = array();
|
|
422
|
+
$individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
|
|
401
423
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
foreach ( $css_declarations as $css_property => $css_value ) {
|
|
405
|
-
$filtered_css_declaration = esc_html( safecss_filter_attr( "{$css_property}: {$css_value}" ) );
|
|
406
|
-
if ( ! empty( $filtered_css_declaration ) ) {
|
|
407
|
-
$filtered_css_declarations[] = $filtered_css_declaration . ';';
|
|
424
|
+
if ( $individual_property && static::is_valid_style_value( $value ) ) {
|
|
425
|
+
$css_declarations[ $individual_property ] = $value;
|
|
408
426
|
}
|
|
409
427
|
}
|
|
428
|
+
} else {
|
|
429
|
+
$css_declarations[ $style_property_keys['default'] ] = $style_value;
|
|
410
430
|
}
|
|
411
431
|
|
|
412
|
-
|
|
413
|
-
$styles_output = array();
|
|
414
|
-
|
|
415
|
-
// Return css, if any.
|
|
416
|
-
if ( ! empty( $filtered_css_declarations ) ) {
|
|
417
|
-
// Return an entire rule if there is a selector.
|
|
418
|
-
if ( $css_selector ) {
|
|
419
|
-
$css_rule = "$css_selector { ";
|
|
420
|
-
$css_rule .= implode( ' ', $filtered_css_declarations );
|
|
421
|
-
$css_rule .= ' }';
|
|
422
|
-
$styles_output['css'] = $css_rule;
|
|
423
|
-
} else {
|
|
424
|
-
$styles_output['css'] = implode( ' ', $filtered_css_declarations );
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// Return classnames, if any.
|
|
429
|
-
if ( ! empty( $classnames ) ) {
|
|
430
|
-
$styles_output['classnames'] = implode( ' ', array_unique( $classnames ) );
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
return $styles_output;
|
|
432
|
+
return $css_declarations;
|
|
434
433
|
}
|
|
435
434
|
|
|
436
|
-
|
|
437
435
|
/**
|
|
438
436
|
* Style value parser that returns a CSS definition array comprising style properties
|
|
439
437
|
* that have keys representing individual style properties, otherwise known as longhand CSS properties.
|
|
@@ -441,12 +439,13 @@ class WP_Style_Engine {
|
|
|
441
439
|
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
|
|
442
440
|
* "border-image-{outset|source|width|repeat|slice}: {value};"
|
|
443
441
|
*
|
|
444
|
-
* @param array
|
|
445
|
-
* @param array
|
|
442
|
+
* @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
|
|
443
|
+
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
444
|
+
* @param boolean $should_skip_css_vars Whether to skip compiling CSS var values.
|
|
446
445
|
*
|
|
447
446
|
* @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
448
447
|
*/
|
|
449
|
-
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition ) {
|
|
448
|
+
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $should_skip_css_vars ) {
|
|
450
449
|
$css_declarations = array();
|
|
451
450
|
|
|
452
451
|
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
|
|
@@ -466,17 +465,12 @@ class WP_Style_Engine {
|
|
|
466
465
|
|
|
467
466
|
// Build a path to the individual rules in definitions.
|
|
468
467
|
$style_definition_path = array( $definition_group_key, $css_property );
|
|
469
|
-
$style_definition = _wp_array_get(
|
|
468
|
+
$style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
|
|
470
469
|
|
|
471
470
|
if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
|
|
472
471
|
// Set a CSS var if there is a valid preset value.
|
|
473
|
-
$
|
|
474
|
-
|
|
475
|
-
$css_var = strtr(
|
|
476
|
-
$individual_property_definition['css_vars'][ $css_property ],
|
|
477
|
-
array( '$slug' => $slug )
|
|
478
|
-
);
|
|
479
|
-
$value = "var($css_var)";
|
|
472
|
+
if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
|
|
473
|
+
$value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
|
|
480
474
|
}
|
|
481
475
|
$individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
|
|
482
476
|
$css_declarations[ $individual_css_property ] = $value;
|
|
@@ -484,33 +478,177 @@ class WP_Style_Engine {
|
|
|
484
478
|
}
|
|
485
479
|
return $css_declarations;
|
|
486
480
|
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Returns compiled CSS from css_declarations.
|
|
484
|
+
*
|
|
485
|
+
* @param array $css_declarations An array of parsed CSS property => CSS value pairs.
|
|
486
|
+
* @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.
|
|
487
|
+
*
|
|
488
|
+
* @return string A compiled CSS string.
|
|
489
|
+
*/
|
|
490
|
+
public static function compile_css( $css_declarations, $css_selector ) {
|
|
491
|
+
if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) {
|
|
492
|
+
return '';
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Return an entire rule if there is a selector.
|
|
496
|
+
if ( $css_selector ) {
|
|
497
|
+
$css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations );
|
|
498
|
+
return $css_rule->get_css();
|
|
499
|
+
}
|
|
500
|
+
$css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations );
|
|
501
|
+
|
|
502
|
+
return $css_declarations->get_declarations_string();
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Returns a compiled stylesheet from stored CSS rules.
|
|
507
|
+
*
|
|
508
|
+
* @param WP_Style_Engine_CSS_Rule[] $css_rules An array of WP_Style_Engine_CSS_Rule objects from a store or otherwise.
|
|
509
|
+
*
|
|
510
|
+
* @return string A compiled stylesheet from stored CSS rules.
|
|
511
|
+
*/
|
|
512
|
+
public static function compile_stylesheet_from_css_rules( $css_rules ) {
|
|
513
|
+
$processor = new WP_Style_Engine_Processor();
|
|
514
|
+
$processor->add_rules( $css_rules );
|
|
515
|
+
return $processor->get_css( array( 'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) );
|
|
516
|
+
}
|
|
487
517
|
}
|
|
488
518
|
|
|
489
519
|
/**
|
|
490
|
-
* Global public interface method to
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
520
|
+
* Global public interface method to generate styles from a single style object, e.g.,
|
|
521
|
+
* the value of a block's attributes.style object or the top level styles in theme.json.
|
|
522
|
+
* See: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles and
|
|
523
|
+
* https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
|
|
494
524
|
*
|
|
495
525
|
* Example usage:
|
|
496
526
|
*
|
|
497
|
-
* $styles =
|
|
498
|
-
* // Returns `'color: #cccccc'`.
|
|
527
|
+
* $styles = wp_style_engine_get_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );
|
|
528
|
+
* // Returns `array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' )`.
|
|
499
529
|
*
|
|
500
530
|
* @access public
|
|
501
531
|
*
|
|
502
|
-
* @param array
|
|
503
|
-
* @param array $options
|
|
532
|
+
* @param array $block_styles The style object.
|
|
533
|
+
* @param array<string|boolean> $options array(
|
|
534
|
+
* 'context' => (string) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
535
|
+
* 'enqueue' => (boolean) When `true` will attempt to store and enqueue for rendering on the frontend.
|
|
536
|
+
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
537
|
+
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
538
|
+
* );.
|
|
504
539
|
*
|
|
505
|
-
* @return array|null array(
|
|
506
|
-
* '
|
|
507
|
-
* '
|
|
540
|
+
* @return array<string|array>|null array(
|
|
541
|
+
* 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
|
|
542
|
+
* 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
|
|
543
|
+
* 'classnames' => (string) Classnames separated by a space.
|
|
508
544
|
* );
|
|
509
545
|
*/
|
|
510
|
-
function
|
|
511
|
-
if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
512
|
-
|
|
513
|
-
|
|
546
|
+
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
|
|
547
|
+
if ( ! class_exists( 'WP_Style_Engine' ) ) {
|
|
548
|
+
return array();
|
|
549
|
+
}
|
|
550
|
+
$defaults = array(
|
|
551
|
+
'selector' => null,
|
|
552
|
+
'context' => 'block-supports',
|
|
553
|
+
'convert_vars_to_classnames' => false,
|
|
554
|
+
'enqueue' => false,
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
$options = wp_parse_args( $options, $defaults );
|
|
558
|
+
$parsed_styles = null;
|
|
559
|
+
|
|
560
|
+
// Block supports styles.
|
|
561
|
+
if ( 'block-supports' === $options['context'] ) {
|
|
562
|
+
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Output.
|
|
566
|
+
$styles_output = array();
|
|
567
|
+
|
|
568
|
+
if ( ! $parsed_styles ) {
|
|
569
|
+
return $styles_output;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if ( ! empty( $parsed_styles['declarations'] ) ) {
|
|
573
|
+
$styles_output['css'] = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
|
|
574
|
+
$styles_output['declarations'] = $parsed_styles['declarations'];
|
|
575
|
+
if ( true === $options['enqueue'] ) {
|
|
576
|
+
WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if ( ! empty( $parsed_styles['classnames'] ) ) {
|
|
581
|
+
$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
return array_filter( $styles_output );
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Returns compiled CSS from a collection of selectors and declarations.
|
|
589
|
+
* This won't add to any store, but is useful for returning a compiled style sheet from any CSS selector + declarations combos.
|
|
590
|
+
*
|
|
591
|
+
* @access public
|
|
592
|
+
*
|
|
593
|
+
* @param array<array> $css_rules array(
|
|
594
|
+
* array(
|
|
595
|
+
* 'selector' => (string) A CSS selector.
|
|
596
|
+
* declarations' => (boolean) An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
597
|
+
* )
|
|
598
|
+
* );.
|
|
599
|
+
* @param array<string> $options array(
|
|
600
|
+
* 'context' => (string) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
601
|
+
* 'enqueue' => (boolean) When `true` will attempt to store and enqueue for rendering on the frontend.
|
|
602
|
+
* );.
|
|
603
|
+
*
|
|
604
|
+
* @return string A compiled CSS string.
|
|
605
|
+
*/
|
|
606
|
+
function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = array() ) {
|
|
607
|
+
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $css_rules ) ) {
|
|
608
|
+
return '';
|
|
514
609
|
}
|
|
515
|
-
|
|
610
|
+
|
|
611
|
+
$defaults = array(
|
|
612
|
+
'context' => 'block-supports',
|
|
613
|
+
'enqueue' => false,
|
|
614
|
+
);
|
|
615
|
+
$options = wp_parse_args( $options, $defaults );
|
|
616
|
+
$css_rule_objects = array();
|
|
617
|
+
|
|
618
|
+
foreach ( $css_rules as $css_rule ) {
|
|
619
|
+
if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) {
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if ( true === $options['enqueue'] ) {
|
|
624
|
+
WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'] );
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
$css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'] );
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
if ( empty( $css_rule_objects ) ) {
|
|
631
|
+
return '';
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects );
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Returns compiled CSS from a store, if found.
|
|
639
|
+
*
|
|
640
|
+
* @access public
|
|
641
|
+
*
|
|
642
|
+
* @param string $store_key A valid store key.
|
|
643
|
+
*
|
|
644
|
+
* @return string A compiled CSS string.
|
|
645
|
+
*/
|
|
646
|
+
function wp_style_engine_get_stylesheet_from_store( $store_key ) {
|
|
647
|
+
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $store_key ) ) {
|
|
648
|
+
return '';
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
$store = WP_Style_Engine::get_store( $store_key );
|
|
652
|
+
|
|
653
|
+
return WP_Style_Engine::compile_stylesheet_from_css_rules( $store->get_all_rules() );
|
|
516
654
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/style-engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "WordPress Style engine.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "08358f53b627a15148c3a3e433cdf58cf8714aa4"
|
|
38
38
|
}
|