WP_Theme_JSON::to_ruleset │ protected static │ WP 5.8.0

Given a selector and a declaration list, creates the corresponding ruleset.

Method of the class: WP_Theme_JSON{}

No Hooks.

Returns

string. The resulting CSS ruleset.

Usage

$result = WP_Theme_JSON::to_ruleset( $selector, $declarations );
$selector(string) (required)
CSS selector.
$declarations(array) (required)
List of declarations.

Changelog

Since 5.8.0 Introduced.
Since 7.1.0 Skip declarations whose value is not a plain string (booleans, arrays, objects, etc.).

WP_Theme_JSON::to_ruleset() code WP 7.1.3

protected static function to_ruleset( $selector, $declarations ) {
	if ( empty( $declarations ) ) {
		return '';
	}

	$declaration_block = array_reduce(
		$declarations,
		static function ( $carry, $element ) {
			$value = $element['value'];

			if ( is_numeric( $value ) ) {
				$value = (string) $value;
			}

			if ( ! is_string( $value ) ) {
				return $carry;
			}

			return $carry .= $element['name'] . ': ' . $value . ';';
		},
		''
	);

	return $selector . '{' . $declaration_block . '}';
}