WP_Theme_JSON::get_block_style_variation_feature_selectorprotected staticWP 7.0.0

Applies a block style variation class to a feature selector.

Feature selectors can target a different element than the block's root selector. For example, the Button block's root selector targets the inner link, while its dimensions width selector targets the outer wrapper. Apply the variation class directly to the selector that will receive the declarations instead of deriving it by subtracting the root selector from the feature selector.

Method of the class: WP_Theme_JSON{}

No Hooks.

Returns

String. Feature selector with block style variation selector added.

Usage

$result = WP_Theme_JSON::get_block_style_variation_feature_selector( $style_variation, $feature_selector );
$style_variation(array) (required)
Style variation metadata.
$feature_selector(string) (required)
CSS selector for the feature.

Changelog

Since 7.0.0 Introduced.

WP_Theme_JSON::get_block_style_variation_feature_selector() code WP 7.1

protected static function get_block_style_variation_feature_selector( $style_variation, $feature_selector ) {
	$variation_path = $style_variation['path'] ?? array();
	$variation_name = $style_variation['name'] ?? ( is_array( $variation_path ) ? end( $variation_path ) : null );

	if ( ! $variation_name ) {
		return $style_variation['selector'] ?? $feature_selector;
	}

	$variation_class = ".is-style-$variation_name";
	$selector_parts  = static::split_selector_list( $feature_selector );
	$selector_parts  = array_map(
		static function ( $selector ) use ( $variation_class ) {
			$prefix = $variation_class . ' ';

			if ( str_starts_with( $selector, $prefix ) ) {
				return substr( $selector, strlen( $prefix ) );
			}

			return $selector;
		},
		$selector_parts
	);

	return static::get_block_style_variation_selector(
		$variation_name,
		implode( ', ', $selector_parts )
	);
}