WP_Theme_JSON::compute_style_properties()
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,
)
Метод класса: WP_Theme_JSON{}
Хуков нет.
Возвращает
Массив
. Returns the modified $declarations.
Использование
$result = WP_Theme_JSON::compute_style_properties( $styles, $settings, $properties, $theme_json, $selector, $use_root_padding );
- $styles(массив) (обязательный)
- Styles to process.
- $settings(массив)
- Theme settings.
По умолчанию: array() - $properties(массив)
- Properties metadata.
По умолчанию: null - $theme_json(массив)
- Theme JSON array.
По умолчанию: null - $selector(строка)
- The style block selector.
По умолчанию: null - $use_root_padding(true|false)
- Whether to add custom properties at root level.
По умолчанию: null
Список изменений
С версии 5.8.0 | Введена. |
С версии 5.9.0 | Added the $settings and $properties parameters. |
С версии 6.1.0 | Added $theme_json, $selector, and $use_root_padding parameters. |
Код WP_Theme_JSON::compute_style_properties() WP Theme JSON::compute style properties WP 6.2.2
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 ( // TODO: Replace array_key_exists() with isset() check once WordPress drops // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067. array_key_exists( $path_string, static::PROTECTED_PROPERTIES ) && _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; } // Calculates fluid typography rules where available. if ( 'font-size' === $css_property ) { /* * wp_get_typography_font_size_value() will check * if fluid typography has been activated and also * whether the incoming value can be converted to a fluid value. * Values that already have a clamp() function will not pass the test, * and therefore the original $value will be returned. */ $value = wp_get_typography_font_size_value( array( 'size' => $value ) ); } $declarations[] = array( 'name' => $css_property, 'value' => $value, ); } // If a variable value is added to the root, the corresponding property should be removed. foreach ( $root_variable_duplicates as $duplicate ) { $discard = array_search( $duplicate, array_column( $declarations, 'name' ), true ); if ( is_numeric( $discard ) ) { array_splice( $declarations, $discard, 1 ); } } return $declarations; }