: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Style Engine: WP_Style_Engine class
* @subpackage StyleEngine
* The main class integrating all other WP_Style_Engine_* classes.
* The Style Engine aims to provide a consistent API for rendering styling for blocks
* across both client-side and server-side applications.
* This class is final and should not be extended.
* This class is for internal Core usage and is not supposed to be used by extenders
* (plugins and/or themes). This is a low-level API that may need to do breaking changes.
* Please, use wp_style_engine_get_styles() instead.
* @since 6.3.0 Added support for text-columns.
* @since 6.4.0 Added support for background.backgroundImage.
* @since 6.5.0 Added support for background.backgroundPosition,
* background.backgroundRepeat and dimensions.aspectRatio.
#[AllowDynamicProperties]
final class WP_Style_Engine {
* Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes.
* For every style definition, the following properties are valid:
* - classnames => (array) an array of classnames to be returned for block styles. The key is a classname or pattern.
* A value of `true` means the classname should be applied always. Otherwise, a valid CSS property (string)
* to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
* - css_vars => (array) an array of key value pairs used to generate CSS var values.
* The key should be the CSS property name that matches the second element of the preset string value,
* i.e., "color" in var:preset|color|somePresetSlug. The value is a CSS var pattern (e.g. `--wp--preset--color--$slug`),
* whose `$slug` fragment will be replaced with the preset slug, which is the third element of the preset string value,
* i.e., `somePresetSlug` in var:preset|color|somePresetSlug.
* - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
* - path => (array) a path that accesses the corresponding style value in the block style object.
* - 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", ... )`.
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
'backgroundImage' => array(
'property_keys' => array(
'default' => 'background-image',
'value_func' => array( self::class, 'get_url_or_value_css_declaration' ),
'path' => array( 'background', 'backgroundImage' ),
'backgroundPosition' => array(
'property_keys' => array(
'default' => 'background-position',
'path' => array( 'background', 'backgroundPosition' ),
'backgroundRepeat' => array(
'property_keys' => array(
'default' => 'background-repeat',
'path' => array( 'background', 'backgroundRepeat' ),
'backgroundSize' => array(
'property_keys' => array(
'default' => 'background-size',
'path' => array( 'background', 'backgroundSize' ),
'property_keys' => array(
'path' => array( 'color', 'text' ),
'color' => '--wp--preset--color--$slug',
'has-text-color' => true,
'has-$slug-color' => 'color',
'property_keys' => array(
'default' => 'background-color',
'path' => array( 'color', 'background' ),
'color' => '--wp--preset--color--$slug',
'has-background' => true,
'has-$slug-background-color' => 'color',
'property_keys' => array(
'default' => 'background',
'path' => array( 'color', 'gradient' ),
'gradient' => '--wp--preset--gradient--$slug',
'has-background' => true,
'has-$slug-gradient-background' => 'gradient',
'property_keys' => array(
'default' => 'border-color',
'individual' => 'border-%s-color',
'path' => array( 'border', 'color' ),
'has-border-color' => true,
'has-$slug-border-color' => 'color',
'property_keys' => array(
'default' => 'border-radius',
'individual' => 'border-%s-radius',
'path' => array( 'border', 'radius' ),
'property_keys' => array(
'default' => 'border-style',
'individual' => 'border-%s-style',
'path' => array( 'border', 'style' ),
'property_keys' => array(
'default' => 'border-width',
'individual' => 'border-%s-width',
'path' => array( 'border', 'width' ),
'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
'path' => array( 'border', 'top' ),
'color' => '--wp--preset--color--$slug',
'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
'path' => array( 'border', 'right' ),
'color' => '--wp--preset--color--$slug',
'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
'path' => array( 'border', 'bottom' ),
'color' => '--wp--preset--color--$slug',
'value_func' => array( self::class, 'get_individual_property_css_declarations' ),
'path' => array( 'border', 'left' ),
'color' => '--wp--preset--color--$slug',
'property_keys' => array(
'default' => 'box-shadow',
'path' => array( 'shadow' ),
'shadow' => '--wp--preset--shadow--$slug',
'property_keys' => array(
'default' => 'aspect-ratio',
'path' => array( 'dimensions', 'aspectRatio' ),
'has-aspect-ratio' => true,
'property_keys' => array(
'default' => 'min-height',
'path' => array( 'dimensions', 'minHeight' ),
'spacing' => '--wp--preset--spacing--$slug',
'property_keys' => array(
'individual' => 'padding-%s',
'path' => array( 'spacing', 'padding' ),
'spacing' => '--wp--preset--spacing--$slug',
'property_keys' => array(
'individual' => 'margin-%s',
'path' => array( 'spacing', 'margin' ),
'spacing' => '--wp--preset--spacing--$slug',
'property_keys' => array(
'default' => 'font-size',
'path' => array( 'typography', 'fontSize' ),
'font-size' => '--wp--preset--font-size--$slug',
'has-$slug-font-size' => 'font-size',
'property_keys' => array(
'default' => 'font-family',
'font-family' => '--wp--preset--font-family--$slug',
'path' => array( 'typography', 'fontFamily' ),
'has-$slug-font-family' => 'font-family',
'property_keys' => array(
'default' => 'font-style',
'path' => array( 'typography', 'fontStyle' ),
'property_keys' => array(
'default' => 'font-weight',
'path' => array( 'typography', 'fontWeight' ),
'property_keys' => array(
'default' => 'line-height',
'path' => array( 'typography', 'lineHeight' ),
'property_keys' => array(
'default' => 'column-count',
'path' => array( 'typography', 'textColumns' ),
'textDecoration' => array(
'property_keys' => array(
'default' => 'text-decoration',
'path' => array( 'typography', 'textDecoration' ),
'textTransform' => array(
'property_keys' => array(
'default' => 'text-transform',
'path' => array( 'typography', 'textTransform' ),
'letterSpacing' => array(
'property_keys' => array(
'default' => 'letter-spacing',
'path' => array( 'typography', 'letterSpacing' ),
* Util: Extracts the slug in kebab case from a preset string,
* e.g. `heavenly-blue` from `var:preset|color|heavenlyBlue`.
* @param string $style_value A single CSS preset value.
* @param string $property_key The CSS property that is the second element of the preset string.
* @return string The slug, or empty string if not found.
protected static function get_slug_from_preset_value( $style_value, $property_key ) {
if ( is_string( $style_value ) && is_string( $property_key )
&& str_contains( $style_value, "var:preset|{$property_key}|" )
$index_to_splice = strrpos( $style_value, '|' ) + 1;
return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
* Util: Generates a CSS var string, e.g. `var(--wp--preset--color--background)`
* from a preset string such as `var:preset|space|50`.
* @param string $style_value A single CSS preset value.
* @param string[] $css_vars An associate array of CSS var patterns
* used to generate the var string.
* @return string The CSS var, or an empty string if no match for slug found.
protected static function get_css_var_value( $style_value, $css_vars ) {
foreach ( $css_vars as $property_key => $css_var_pattern ) {
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
if ( static::is_valid_style_value( $slug ) ) {
array( '$slug' => $slug )
* Util: Checks whether an incoming block style value is valid.
* @param string $style_value A single CSS preset value.
protected static function is_valid_style_value( $style_value ) {
return '0' === $style_value || ! empty( $style_value );
* Stores a CSS rule using the provided CSS selector and CSS declarations.
* @since 6.6.0 Added the `$rules_group` parameter.
* @param string $store_name A valid store key.
* @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.
* @param string[] $css_declarations An associative array of CSS definitions,
* e.g. `array( "$property" => "$value", "$property" => "$value" )`.
* @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, or a CSS nested @rule,
* such as `@media (min-width: 80rem)` or `@layer module`.
public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ) {
if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) {
static::get_store( $store_name )->add_rule( $css_selector, $rules_group )->add_declarations( $css_declarations );
* Returns a store by store key.
* @param string $store_name A store key.
* @return WP_Style_Engine_CSS_Rules_Store|null
public static function get_store( $store_name ) {
return WP_Style_Engine_CSS_Rules_Store::get_store( $store_name );
* Returns classnames and CSS based on the values in a styles object.
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
* @param array $block_styles The style object.
* @param array $options {
* Optional. An array of options. Default empty array.
* @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.
* @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 }`,
* otherwise, the value will be a concatenated string
* @type string[] $classnames Array of class names.
* @type string[] $declarations An associative array of CSS definitions,
* e.g. `array( "$property" => "$value", "$property" => "$value" )`.
public static function parse_block_styles( $block_styles, $options ) {
'declarations' => array(),
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
// Collect CSS and classnames.
foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
if ( empty( $block_styles[ $definition_group_key ] ) ) {
foreach ( $definition_group_style as $style_definition ) {
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
if ( ! static::is_valid_style_value( $style_value ) ) {
$parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], static::get_classnames( $style_value, $style_definition ) );
$parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], static::get_css_declarations( $style_value, $style_definition, $options ) );
* Returns classnames, and generates classname(s) from a CSS preset property pattern,
* e.g. `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`.
* @param string $style_value A single raw style value or CSS preset property
* from the `$block_styles` array.
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
* @return string[] An array of CSS classnames, or empty array if there are none.
protected static function get_classnames( $style_value, $style_definition ) {
if ( empty( $style_value ) ) {
if ( ! empty( $style_definition['classnames'] ) ) {
foreach ( $style_definition['classnames'] as $classname => $property_key ) {
if ( true === $property_key ) {
$classnames[] = $classname;
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
* Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA.
* One day, if there are no stored schemata, we could allow custom patterns or
* generate classnames based on other properties
* such as a path or a value or a prefix passed in options.
$classnames[] = strtr( $classname, array( '$slug' => $slug ) );
* Returns an array of CSS declarations based on valid block style values.
* @param mixed $style_value A single raw style value from $block_styles array.
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
* @param array $options {