WP_Theme_JSON::get_property_value( array $styles, array $path, array $theme_json = null ): string|array

Returns the style property for the given path.


Description

It also converts CSS Custom Property stored as "var:preset|color|secondary" to the form "–wp–preset–color–secondary".

It also converts references to a path to the value stored at that location, e.g.
{ "ref": "style.color.background" } => "#fff".


Top ↑

Parameters

$styles array Required
Styles subtree.
$path array Required
Which property to process.
$theme_json array Optional
Theme JSON array.

Default: null


Top ↑

Return

string|array Style property value.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php. View all references

protected static function get_property_value( $styles, $path, $theme_json = null ) {
	$value = _wp_array_get( $styles, $path, '' );

	if ( '' === $value || null === $value ) {
		// No need to process the value further.
		return '';
	}

	/*
	 * This converts references to a path to the value at that path
	 * where the values is an array with a "ref" key, pointing to a path.
	 * For example: { "ref": "style.color.background" } => "#fff".
	 */
	if ( is_array( $value ) && array_key_exists( 'ref', $value ) ) {
		$value_path = explode( '.', $value['ref'] );
		$ref_value  = _wp_array_get( $theme_json, $value_path );
		// Only use the ref value if we find anything.
		if ( ! empty( $ref_value ) && is_string( $ref_value ) ) {
			$value = $ref_value;
		}

		if ( is_array( $ref_value ) && array_key_exists( 'ref', $ref_value ) ) {
			$path_string      = json_encode( $path );
			$ref_value_string = json_encode( $ref_value );
			_doing_it_wrong(
				'get_property_value',
				sprintf(
					/* translators: 1: theme.json, 2: Value name, 3: Value path, 4: Another value name. */
					__( 'Your %1$s file uses a dynamic value (%2$s) for the path at %3$s. However, the value at %3$s is also a dynamic value (pointing to %4$s) and pointing to another dynamic value is not supported. Please update %3$s to point directly to %4$s.' ),
					'theme.json',
					$ref_value_string,
					$path_string,
					$ref_value['ref']
				),
				'6.1.0'
			);
		}
	}

	if ( is_array( $value ) ) {
		return $value;
	}

	// Convert custom CSS properties.
	$prefix     = 'var:';
	$prefix_len = strlen( $prefix );
	$token_in   = '|';
	$token_out  = '--';
	if ( 0 === strncmp( $value, $prefix, $prefix_len ) ) {
		$unwrapped_name = str_replace(
			$token_in,
			$token_out,
			substr( $value, $prefix_len )
		);
		$value          = "var(--wp--$unwrapped_name)";
	}

	return $value;
}


Top ↑

Changelog

Changelog
Version Description
6.1.0 Added the $theme_json parameter.
5.9.0 Added support for values of array type, which are returned as is.
5.8.0 Introduced.

Top ↑

User Contributed Notes

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