WP_Theme_JSON::compute_preset_classes()protected staticWP 5.8.0

Given a settings array, returns the generated rulesets for the preset classes.

Method of the class: WP_Theme_JSON{}

No Hooks.

Return

String. The result of processing the presets.

Usage

$result = WP_Theme_JSON::compute_preset_classes( $settings, $selector, $origins );
$settings(array) (required)
Settings to process.
$selector(string) (required)
Selector wrapping the classes.
$origins(string[]) (required)
List of origins to process.

Changelog

Since 5.8.0 Introduced.
Since 5.9.0 Added the $origins parameter.

WP_Theme_JSON::compute_preset_classes() code WP 6.4.3

protected static function compute_preset_classes( $settings, $selector, $origins ) {
	if ( static::ROOT_BLOCK_SELECTOR === $selector ) {
		/*
		 * Classes at the global level do not need any CSS prefixed,
		 * and we don't want to increase its specificity.
		 */
		$selector = '';
	}

	$stylesheet = '';
	foreach ( static::PRESETS_METADATA as $preset_metadata ) {
		if ( empty( $preset_metadata['classes'] ) ) {
			continue;
		}
		$slugs = static::get_settings_slugs( $settings, $preset_metadata, $origins );
		foreach ( $preset_metadata['classes'] as $class => $property ) {
			foreach ( $slugs as $slug ) {
				$css_var    = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug );
				$class_name = static::replace_slug_in_string( $class, $slug );

				// $selector is often empty, so we can save ourselves the `append_to_selector()` call then.
				$new_selector = '' === $selector ? $class_name : static::append_to_selector( $selector, $class_name );
				$stylesheet  .= static::to_ruleset(
					$new_selector,
					array(
						array(
							'name'  => $property,
							'value' => 'var(' . $css_var . ') !important',
						),
					)
				);
			}
		}
	}

	return $stylesheet;
}