函数文档

wp_typography_get_css_variable_inline_style()

💡 云策文档标注

概述

wp_typography_get_css_variable_inline_style() 函数用于生成排版特性的内联样式,如文本装饰、文本转换和字体样式。该函数已在 WordPress 6.1.0 中弃用,建议使用 wp_style_engine_get_styles() 替代。

关键要点

  • 函数已弃用:自 WordPress 6.1.0 起,推荐使用 wp_style_engine_get_styles()。
  • 参数要求:接受三个必需参数:$attributes(块属性数组)、$feature(排版样式中的特性键)、$css_property(内联样式设置的 CSS 属性 slug)。
  • 返回值:返回 CSS 内联样式字符串,或当样式值未找到时返回空。
  • 功能逻辑:根据样式值是否为预设 CSS 变量,生成相应的 CSS 内联样式。

代码示例

function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
	_deprecated_function( __FUNCTION__, '6.1.0', 'wp_style_engine_get_styles()' );

	// Retrieve current attribute value or skip if not found.
	$style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
	if ( ! $style_value ) {
		return;
	}

	// If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
	if ( ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
		return sprintf( '%s:%s;', $css_property, $style_value );
	}

	/*
	 * We have a preset CSS variable as the style.
	 * Get the style value from the string and return CSS style.
	 */
	$index_to_splice = strrpos( $style_value, '|' ) + 1;
	$slug            = substr( $style_value, $index_to_splice );

	// Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
	return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
}

注意事项

  • 弃用警告:使用此函数会触发 _deprecated_function(),建议尽快迁移到 wp_style_engine_get_styles()。
  • 依赖函数:内部使用 _wp_array_get() 来深度访问数组,确保参数传递正确。
  • 版本历史:函数在 WordPress 5.8.0 中引入,6.1.0 中弃用。

📄 原文内容

Generates an inline style for a typography feature e.g. text decoration, text transform, and font style.

Description

See also

Parameters

$attributesarrayrequired
Block’s attributes.
$featurestringrequired
Key for the feature within the typography styles.
$css_propertystringrequired
Slug for the CSS property the inline style sets.

Return

string CSS inline style.

Source

function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
	_deprecated_function( __FUNCTION__, '6.1.0', 'wp_style_engine_get_styles()' );

	// Retrieve current attribute value or skip if not found.
	$style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
	if ( ! $style_value ) {
		return;
	}

	// If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
	if ( ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
		return sprintf( '%s:%s;', $css_property, $style_value );
	}

	/*
	 * We have a preset CSS variable as the style.
	 * Get the style value from the string and return CSS style.
	 */
	$index_to_splice = strrpos( $style_value, '|' ) + 1;
	$slug            = substr( $style_value, $index_to_splice );

	// Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
	return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
}

Changelog

Version Description
6.1.0 Deprecated. Use wp_style_engine_get_styles() introduced in 6.1.0.
5.8.0 Introduced.