WP_Style_Engine::get_css_declarations
Returns an array of CSS declarations based on valid block style values.
Method of the class: WP_Style_Engine{}
No Hooks.
Returns
String[]. An associative array of CSS definitions, e.g. array( "$property" => "$value", "$property" => "$value" ).
Usage
$result = WP_Style_Engine::get_css_declarations( $style_value, $style_definition, $options );
- $style_value(mixed) (required)
- A single raw style value from
$block_stylesarray. - $style_definition(array) (required)
- A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
- $options(array)
An array of options.
Default:
empty array- convert_vars_to_classnames(true|false)
Whether to skip converting incoming CSS var patterns, e.g.var:preset|<PRESET_TYPE>|<PRESET_SLUG>, tovar( --wp--preset--* )values.
Default: false
- convert_vars_to_classnames(true|false)
Changelog
| Since 6.1.0 | Introduced. |
WP_Style_Engine::get_css_declarations() WP Style Engine::get css declarations code WP 6.9.1
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;
}