wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_build_state_selector › WordPress Function

Since7.1.0
Deprecatedn/a
wp_build_state_selector ( $base_selector, $block_selector, $state )
Parameters: (3)
  • (string) $base_selector Block-instance scoping selector.
    Required: Yes
  • (string|null) $block_selector Block or feature selector from metadata.
    Required: Yes
  • (string) $state Pseudo-state selector.
    Required: Yes
Returns:
  • (string) Scoped selector.
Defined at:
Codex:

Builds a scoped selector from a block selector and optional pseudo-state.



Source

function wp_build_state_selector( $base_selector, $block_selector, $state ) {
	if ( ! is_string( $block_selector ) || '' === trim( $block_selector ) ) {
		return $base_selector . $state;
	}

	$selectors        = wp_split_selector_list( $block_selector );
	$scoped_selectors = array();

	foreach ( $selectors as $selector ) {
		$selector = trim( $selector );
		if ( '' === $selector ) {
			continue;
		}

		/*
		 * Replace only the leading block selector part (e.g. class name,
		 * attribute selector, ID, or tag name) with the block instance selector.
		 * Preserve anything after that prefix, including modifier classes on the
		 * same element and combinators without spaces.
		 */
		if ( preg_match( '/^([.#]?[-_a-zA-Z0-9]+|\[[^\]]+\])/', $selector, $matches ) ) {
			$scoped_selectors[] = $base_selector . substr( $selector, strlen( $matches[0] ) ) . $state;
			continue;
		}

		$scoped_selectors[] = $base_selector . $state;
	}

	return empty( $scoped_selectors )
		? $base_selector . $state
		: implode( ', ', $scoped_selectors );
}