WP_Theme_JSON::compute_style_properties( array $styles, array $settings = array(), array $properties = null, array $theme_json = null, string $selector = null, boolean $use_root_padding = null ): array

Given a styles array, it extracts the style properties and adds them to the $declarations array following the format:

Description

array( ‘name’ => ‘property_name’, ‘value’ => ‘property_value, )

Parameters

$stylesarrayrequired
Styles to process.
$settingsarrayoptional
Theme settings.

Default:array()

$propertiesarrayoptional
Properties metadata.

Default:null

$theme_jsonarrayoptional
Theme JSON array.

Default:null

$selectorstringoptional
The style block selector.

Default:null

$use_root_paddingbooleanoptional
Whether to add custom properties at root level.

Default:null

Return

array Returns the modified $declarations.

Source

			$new_prefix        = $new_key . $token;
			$flattened_subtree = static::flatten_tree( $value, $new_prefix, $token );
			foreach ( $flattened_subtree as $subtree_key => $subtree_value ) {
				$result[ $subtree_key ] = $subtree_value;
			}
		} else {
			$result[ $new_key ] = $value;
		}
	}
	return $result;
}

/**
 * Given a styles array, it extracts the style properties
 * and adds them to the $declarations array following the format:
 *
 *     array(
 *       'name'  => 'property_name',
 *       'value' => 'property_value,
 *     )
 *
 * @since 5.8.0
 * @since 5.9.0 Added the `$settings` and `$properties` parameters.
 * @since 6.1.0 Added `$theme_json`, `$selector`, and `$use_root_padding` parameters.
 *
 * @param array   $styles Styles to process.
 * @param array   $settings Theme settings.
 * @param array   $properties Properties metadata.
 * @param array   $theme_json Theme JSON array.
 * @param string  $selector The style block selector.
 * @param boolean $use_root_padding Whether to add custom properties at root level.
 * @return array  Returns the modified $declarations.
 */
protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null, $selector = null, $use_root_padding = null ) {
	if ( null === $properties ) {
		$properties = static::PROPERTIES_METADATA;
	}

	$declarations = array();
	if ( empty( $styles ) ) {
		return $declarations;
	}

	$root_variable_duplicates = array();

	foreach ( $properties as $css_property => $value_path ) {
		$value = static::get_property_value( $styles, $value_path, $theme_json );

		if ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) {
			continue;
		}
		/*
		 * Root-level padding styles don't currently support strings with CSS shorthand values.
		 * This may change: https://github.com/WordPress/gutenberg/issues/40132.
		 */
		if ( '--wp--style--root--padding' === $css_property && is_string( $value ) ) {
			continue;
		}

		if ( str_starts_with( $css_property, '--wp--style--root--' ) && $use_root_padding ) {
			$root_variable_duplicates[] = substr( $css_property, strlen( '--wp--style--root--' ) );
		}

		/*
		 * Look up protected properties, keyed by value path.
		 * Skip protected properties that are explicitly set to `null`.
		 */
		if ( is_array( $value_path ) ) {
			$path_string = implode( '.', $value_path );
			if (
				isset( static::PROTECTED_PROPERTIES[ $path_string ] ) &&
				_wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null
			) {
				continue;
			}
		}

		// Skip if empty and not "0" or value represents array of longhand values.
		$has_missing_value = empty( $value ) && ! is_numeric( $value );
		if ( $has_missing_value || is_array( $value ) ) {
			continue;
		}

Changelog

VersionDescription
6.1.0Added $theme_json, $selector, and $use_root_padding parameters.
5.9.0Added the $settings and $properties parameters.
5.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.