WP_Theme_JSON::append_to_selector
Appends a sub-selector to an existing one.
Given the compounded $selector "h1, h2, h3" and the $to_append selector ".some-class" the result will be "h1.some-class, h2.some-class, h3.some-class".
Method of the class: WP_Theme_JSON{}
No Hooks.
Returns
String. The new selector.
Usage
$result = WP_Theme_JSON::append_to_selector( $selector, $to_append );
- $selector(string) (required)
- Original selector.
- $to_append(string) (required)
- Selector to append.
Changelog
| Since 5.8.0 | Introduced. |
| Since 6.1.0 | Added append position. |
| Since 6.3.0 | Removed append position parameter. |
WP_Theme_JSON::append_to_selector() WP Theme JSON::append to selector code WP 7.1
protected static function append_to_selector( $selector, $to_append ) {
if ( ! str_contains( $selector, ',' ) ) {
return trim( $selector, " \t\n" ) . $to_append;
}
/**
* Check for an opportunity to skip the more-costly selector splitting.
* This should be possible if there are no comments, strings, functions,
* URLs, escapes, or comment declaration openers (CDOs).
*
* Note that this means the fast-path will not apply for selectors like
* the following incomplete list:
*
* - `[class ~= "wide"]`
* - `.wp-block:is(.is-style-a, .is-style-b)`
* - `:nth-child(1)`
*
* These syntax forms all present opportunities where a comma may not
* separate selectors. If none of the start characters are present,
* there should be no way for a comma to mean anything other than a
* comma token. The exception are syntax errors, which are not handled here.
*
* @see https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values
*/
if ( strlen( $selector ) === strcspn( $selector, '/\'"(<\\' ) ) {
return preg_replace( '~[ \t\n]*,[ \t\n]*~', "{$to_append}, ", trim( $selector, " \t\n" ) ) . $to_append;
}
$new_selectors = array();
$selectors = static::split_selector_list( $selector );
foreach ( $selectors as $sel ) {
$new_selectors[] = $sel . $to_append;
}
return implode( ', ', $new_selectors );
}