WooCommerce Code Reference

Styles_Helper
in package

This class should guarantee that our work with the DOMDocument is unified and safe.

Table of Contents

$empty_block_styles  : array<string|int, mixed>
Default empty styles block structure.
clamp_to_static_px()  : string|null
Convert a CSS clamp() value to a static px value for email clients.
convert_to_px()  : string|null
Convert a CSS value to a static px value for email clients.
extend_block_styles()  : array<string|int, mixed>
Extend block styles with CSS declarations.
get_block_styles()  : array<string|int, mixed>
Get block styles.
get_normalized_block_styles()  : array<string|int, mixed>
Get normalized block styles by translating color slugs to actual color values.
get_styles_from_block()  : array<string|int, mixed>
Wrapper for wp_style_engine_get_styles which ensures all values are returned.
is_color_literal()  : bool
Whether a value is a literal color rather than a palette slug.
palette_definitions()  : array<string|int, mixed>
Every color the palette defines, one entry per slug, highest priority origin first.
parse_styles_to_array()  : array<string|int, mixed>
Parse styles string to array.
parse_value()  : float
Parse number value from a string.
remove_css_unit()  : string
Remove the CSS unit from a string.
resolve_color_from_palette()  : string
Resolve a color slug against the palette.

Properties

Methods

clamp_to_static_px()

Convert a CSS clamp() value to a static px value for email clients.

public static clamp_to_static_px(string $clamp_str[, string $strategy = 'min' ]) : string|null
Parameters
$clamp_str : string

The clamp() CSS string (e.g., "clamp(30px, 5vw, 50px)").

$strategy : string = 'min'

"min"|"max"|"avg" — which strategy to use.

Return values
string|null — The static pixel value (e.g., 30px).

convert_to_px()

Convert a CSS value to a static px value for email clients.

public static convert_to_px(string $input[, bool $use_fallback = true ][, int|null $base_font_size = 16 ]) : string|null

This is mostly for use in font size, spacing, etc.

Parameters
$input : string

The CSS value to convert.

$use_fallback : bool = true

Whether to use the fallback value if the input is not a valid CSS value.

$base_font_size : int|null = 16

The base font size to use for conversion.

Return values
string|null — The static pixel value (e.g., 30px).

extend_block_styles()

Extend block styles with CSS declarations.

public static extend_block_styles(array<string|int, mixed> $block_styles, array<string|int, mixed> $css_declarations) : array<string|int, mixed>
Parameters
$block_styles : array<string|int, mixed>

WP_Style_Engine styles array (must contain 'declarations' and 'css' keys).

$css_declarations : array<string|int, mixed>

An associative array of CSS definitions, e.g. array( "$property" => "$value", "$property" => "$value" ).

Return values
array<string|int, mixed> — { @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag. @type string[] $declarations An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. @type string $classnames Classnames separated by a space. }

get_block_styles()

Get block styles.

public static get_block_styles(array<string|int, mixed> $block_attributes, Rendering_Context $rendering_context, array<string|int, mixed> $properties) : array<string|int, mixed>
Parameters
$block_attributes : array<string|int, mixed>

Block attributes.

$rendering_context : Rendering_Context

Rendering context.

$properties : array<string|int, mixed>

List of style properties to include. Supported values: 'spacing', 'padding', 'margin', 'border', 'border-width', 'border-style', 'border-radius', 'border-color', 'background', 'background-color', 'color', 'typography', 'font-size', 'font-family', 'font-weight', 'text-align'.

Return values
array<string|int, mixed> — { @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag. @type string[] $declarations An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. @type string $classnames Classnames separated by a space. }

get_normalized_block_styles()

Get normalized block styles by translating color slugs to actual color values.

public static get_normalized_block_styles(array<string|int, mixed> $block_attributes, Rendering_Context $rendering_context) : array<string|int, mixed>

This method handles the normalization of color-related attributes like backgroundColor, textColor, borderColor, and linkColor by translating them from slugs to actual color values using the rendering context.

Parameters
$block_attributes : array<string|int, mixed>

Block attributes containing color slugs.

$rendering_context : Rendering_Context

Rendering context for color translation.

Return values
array<string|int, mixed> — Normalized block styles with translated color values.

get_styles_from_block()

Wrapper for wp_style_engine_get_styles which ensures all values are returned.

public static get_styles_from_block(array<string|int, mixed> $block_styles[, bool $skip_convert_vars = false ]) : array<string|int, mixed>
Parameters
$block_styles : array<string|int, mixed>

Array of block styles.

$skip_convert_vars : bool = false

If true, --wp_preset--spacing--x type values will be left in the original var:preset:spacing:x format.

Return values
array<string|int, mixed> — { @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag. @type string[] $declarations An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. @type string $classnames Classnames separated by a space. }

is_color_literal()

Whether a value is a literal color rather than a palette slug.

public static is_color_literal(string $value) : bool

Only a value a slug could never be counts: a complete hex value, or functional notation of any name, since a slug contains neither # nor a bracket. A bare word is a slug, including one that names a CSS color -- classic themes register those, and the email cannot see their color.

Parameters
$value : string

Value of a color block attribute.

Return values
bool —

palette_definitions()

Every color the palette defines, one entry per slug, highest priority origin first.

public static palette_definitions(array<string|int, mixed> $settings) : array<string|int, mixed>

WP_Theme_JSON layers origins so a later one wins, so they are read in reverse of that order. One entry per slug matters because a lookup reads the first and the stylesheet's cascade reads the last, which would give a block a different color by route. An entry needs a usable slug and color to be kept, and an incomplete one does not claim its slug.

Parameters
$settings : array<string|int, mixed>

Theme settings array, as WP_Theme_JSON::get_settings() returns it.

Return values
array<string|int, mixed> — Flat list of color definitions, one per slug, each with a usable slug and color.

resolve_color_from_palette()

Resolve a color slug against the palette.

public static resolve_color_from_palette(array<string|int, mixed> $settings, string $color_slug) : string

Searches every origin, highest priority first: a slug the user defined in the email's own global styles (custom) beats the theme's, which beats core's.

A slug no origin defines returns an empty string, so callers omit the declaration and the block keeps the surrounding color. A literal color passes through.

Parameters
$settings : array<string|int, mixed>

Theme settings array, as WP_Theme_JSON::get_settings() returns it.

$color_slug : string

Color slug, or a literal color.

Return values
string — The color value, or an empty string for a slug the palette does not define.