wp_style_engine_get_styles

函数
wp_style_engine_get_styles ( $block_styles, $options = array() )
Access
Public
参数
  • (array) $block_styles The style object.
    Required:
  • (array) $options { Optional. An array of options. Default empty array. @type string|null $context An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is `null`. When set, the style engine will attempt to store the CSS rules, where a selector is also passed. @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`. @type string $selector Optional. When a selector is passed, the value of `$css` in the return value will comprise a full CSS rule `$selector { ...$css_declarations }`, otherwise, the value will be a concatenated string of CSS declarations. }
    Required:
    Default: array()
返回值
  • (array) { @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag. @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). @type string $classnames Classnames separated by a space. }
定义位置
相关方法
wp_style_engine_get_stylesheet_from_contextwp_style_engine_get_stylesheet_from_css_ruleswp_maybe_inline_styleswp_print_styleswp_enqueue_stored_styles
引入
6.1.0
弃用
-

全局性的公共接口方法,从一个单一的样式对象生成样式,例如。
一个块的attributes.style对象的值或theme.json中的顶级样式。

参见:https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles 和

Supports

使用实例:

$styles = wp_style_engine_get_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );

// Returns `array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' )`
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
	$options = wp_parse_args(
		$options,
		array(
			'selector'                   => null,
			'context'                    => null,
			'convert_vars_to_classnames' => false,
		)
	);

	$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );

	// Output.
	$styles_output = array();

	if ( ! empty( $parsed_styles['declarations'] ) ) {
		$styles_output['css']          = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
		$styles_output['declarations'] = $parsed_styles['declarations'];
		if ( ! empty( $options['context'] ) ) {
			WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
		}
	}

	if ( ! empty( $parsed_styles['classnames'] ) ) {
		$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );
	}

	return array_filter( $styles_output );
}

常见问题

FAQs
查看更多 >