WP_Theme_JSON::get_block_style_variation_selector │ protected static │ WP 6.5.0

Generates a selector for a block style variation.

Method of the class: WP_Theme_JSON{}

No Hooks.

Returns

string. Block selector with block style variation selector added to it.

Usage

$result = WP_Theme_JSON::get_block_style_variation_selector( $variation_name, $block_selector );
$variation_name(string) (required)
Name of the block style variation.
$block_selector(string) (required)
CSS selector for the block.

Changelog

Since 6.5.0 Introduced.

WP_Theme_JSON::get_block_style_variation_selector() code WP 7.1.3

protected static function get_block_style_variation_selector( $variation_name, $block_selector ) {
	$variation_class = ".is-style-$variation_name";

	if ( ! $block_selector ) {
		return $variation_class;
	}

	$limit          = 1;
	$selector_parts = static::split_selector_list( $block_selector );
	$result         = array();

	/*
	 * Append the variation class to each selector's ancestor: the first
	 * run of characters before any combinator (whitespace) or pseudo-class
	 * (`:`). Only the first match is replaced.
	 *
	 * Examples ("custom" variation):
	 * - `.wp-block`              => `.wp-block.is-style-custom`
	 * - `.wp-block .inner`       => `.wp-block.is-style-custom .inner`
	 * - `.wp-block:where(.a .b)` => `.wp-block.is-style-custom:where(.a .b)`
	 * - `:where(.outer .inner)`  => `:where(.outer.is-style-custom .inner)`
	 */
	foreach ( $selector_parts as $part ) {
		$result[] = preg_replace_callback(
			'/[^\s:]+/',
			function ( $matches ) use ( $variation_class ) {
				return $matches[0] . $variation_class;
			},
			$part,
			$limit
		);
	}

	return implode( ', ', $result );
}