WP_Style_Engine

Core class for parsing block styles and compiling CSS. This is a low-level internal API.

Source: wp-includes/style-engine/class-wp-style-engine.php
Since: 6.1.0
Access: Private (use wp_style_engine_get_styles() instead)

Note: This class is final and should not be extended. It is for internal Core usage and may have breaking changes. Use the public functions in style-engine.php instead.

Constants

BLOCK_STYLE_DEFINITIONS_METADATA

Note: This class is final and should not be extended. It is for internal Core usage and may have breaking changes. Use the public functions in style-engine.php instead.

php
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
    'background' => array( /* ... */ ),
    'color'      => array( /* ... */ ),
    'border'     => array( /* ... */ ),
    'shadow'     => array( /* ... */ ),
    'dimensions' => array( /* ... */ ),
    'spacing'    => array( /* ... */ ),
    'typography' => array( /* ... */ ),
);

Definition Structure

KeyTypeDescription
classnamesarrayClassnames to return; key is pattern, value is true or property key for slug substitution
css_varsarrayCSS var patterns for preset conversion (e.g., --wp--preset--color--$slug)
property_keysarrayCSS properties (default and optionally individual for box-model styles)
patharrayPath to style value in block style object
value_funccallableCustom function to generate CSS declarations

Methods

parse_block_styles()

Note: This class is final and should not be extended. It is for internal Core usage and may have breaking changes. Use the public functions in style-engine.php instead.

php
public static function parse_block_styles( array $block_styles, array $options ): array

Parameters

ParameterTypeDescription
$block_stylesarrayThe style object
$optionsarrayConfiguration options

Options

KeyTypeDefaultDescription
convert_vars_to_classnamesboolfalseSkip converting var:preset|* to CSS vars
selectorstringCSS selector (not used in parsing, passed through)

Returns

KeyTypeDescription
classnamesarrayArray of CSS class names
declarationsarrayAssociative array of CSS property => value

compile_css()

Style definitions containing instructions for parsing and outputting valid Gutenberg styles from block attributes.

php
public static function compile_css( array $css_declarations, string $css_selector ): string

Parameters

ParameterTypeDescription
$css_declarationsarrayAssociative array of CSS definitions
$css_selectorstringCSS selector; if empty, returns declarations only

Returns

Returns classnames and CSS based on values in a styles object.


compile_stylesheet_from_css_rules()

Returns compiled CSS from CSS declarations.

php
public static function compile_stylesheet_from_css_rules( 
    WP_Style_Engine_CSS_Rule[] $css_rules, 
    array $options = array() 
): string

Parameters

ParameterTypeDescription
$css_rulesarrayArray of WP_Style_Engine_CSS_Rule objects
$optionsarrayConfiguration options

Options

KeyTypeDefaultDescription
contextstring|nullStore name
optimizeboolfalseCombine rules with identical declarations
prettifyboolSCRIPT_DEBUGAdd formatting

Returns

Compiled CSS string. If selector is provided, returns full rule: $selector { declarations }. Otherwise, returns concatenated declarations.


store_css_rule()

Returns compiled stylesheet from CSS rule objects.

php
public static function store_css_rule( 
    string $store_name, 
    string $css_selector, 
    array $css_declarations, 
    string $rules_group = '' 
): void

Compiled CSS string.

Parameters

ParameterTypeDescription
$store_namestringStore key (e.g., block-supports)
$css_selectorstringCSS selector
$css_declarationsarrayCSS property => value pairs
$rules_groupstringParent selector or @rule (e.g., @media (min-width: 80rem))

get_store()

Stores a CSS rule in a named store for later retrieval.

php
public static function get_store( string $store_name ): ?WP_Style_Engine_CSS_Rules_Store

Parameters

ParameterTypeDescription
$store_namestringStore key

Returns

Since: 6.1.0
Since: 6.6.0 Added $rules_group parameter


Protected Methods

get_slug_from_preset_value()

Returns a store by its name.

php
protected static function get_slug_from_preset_value( string $style_value, string $property_key ): string

Parameters

ParameterTypeDescription
$style_valuestringCSS preset value (e.g., var:preset|color|heavenlyBlue)
$property_keystringProperty key to match (e.g., color)

Returns

WP_Style_Engine_CSS_Rules_Store instance, or null if invalid.


get_css_var_value()

Extracts the slug in kebab-case from a preset string.

php
protected static function get_css_var_value( string $style_value, array $css_vars ): string

Parameters

ParameterTypeDescription
$style_valuestringPreset value (e.g., var:preset|color|someSlug)
$css_varsarrayCSS var patterns (e.g., array( 'color' => '--wp--preset--color--$slug' ))

Returns

Slug in kebab-case (e.g., heavenly-blue), or empty string if not found.


is_valid_style_value()

Generates a CSS var string from a preset string.

php
protected static function is_valid_style_value( mixed $style_value ): bool

Returns

CSS var (e.g., var(--wp--preset--color--some-slug)), or empty string.


get_classnames()

Checks whether a style value is valid.

php
protected static function get_classnames( string $style_value, array $style_definition ): array

Parameters

ParameterTypeDescription
$style_valuestringRaw style value or preset
$style_definitionarrayStyle definition from BLOCK_STYLE_DEFINITIONS_METADATA

Returns

true if value is '0' or non-empty; false otherwise.


get_css_declarations()

Returns classnames from a style value and definition.

php
protected static function get_css_declarations( 
    mixed $style_value, 
    array $style_definition, 
    array $options = array() 
): array

Parameters

ParameterTypeDescription
$style_valuemixedRaw style value
$style_definitionarrayStyle definition
$optionsarrayOptions including convert_vars_to_classnames

Returns

Array of CSS class names.


get_individual_property_css_declarations()

Returns CSS declarations from a style value.

php
protected static function get_individual_property_css_declarations( 
    array $style_value, 
    array $individual_property_definition, 
    array $options = array() 
): array

Associative array of CSS declarations.

  • border-{top|right|bottom|left}-{color|width|style}
  • border-image-{outset|source|width|repeat|slice}

get_url_or_value_css_declaration()

Parser for individual property declarations (e.g., border-top-color).

php
protected static function get_url_or_value_css_declaration( 
    mixed $style_value, 
    array $style_definition 
): array

Handles styles like:

Returns

Parser for URL-based style values (e.g., background-image).