WP_Style_Engine::get_css_declarations( array $style_value, array $style_definition, array $options = array() ): string[]
Returns an array of CSS declarations based on valid block style values.
Parameters
-
$style_value
array Required -
A single raw style value from $block_styles array.
-
$style_definition
array Required -
A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
-
$options
array Optional -
An array of options.
convert_vars_to_classnames
boolWhether to skip converting incoming CSS var patterns, e.g.,var:preset|<PRESET_TYPE>|<PRESET_SLUG>
, to var( --wp--preset--* ) values. Defaultfalse
.
Default:
array()
Return
string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
Source
File: wp-includes/style-engine/class-wp-style-engine.php
.
View all references
protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) {
if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
}
$css_declarations = array();
$style_property_keys = $style_definition['property_keys'];
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
/*
* Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`.
* Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
*/
if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
if ( static::is_valid_style_value( $css_var ) ) {
$css_declarations[ $style_property_keys['default'] ] = $css_var;
}
}
return $css_declarations;
}
/*
* Default rule builder.
* If the input contains an array, assume box model-like properties
* for styles such as margins and padding.
*/
if ( is_array( $style_value ) ) {
// Bail out early if the `'individual'` property is not defined.
if ( ! isset( $style_property_keys['individual'] ) ) {
return $css_declarations;
}
foreach ( $style_value as $key => $value ) {
if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
$value = static::get_css_var_value( $value, $style_definition['css_vars'] );
}
$individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
if ( $individual_property && static::is_valid_style_value( $value ) ) {
$css_declarations[ $individual_property ] = $value;
}
}
return $css_declarations;
}
$css_declarations[ $style_property_keys['default'] ] = $style_value;
return $css_declarations;
}
Changelog
Version | Description |
---|---|
6.1.0 | Introduced. |