Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme_JSON: Use existing append_to_selector for pseudo elements #43167

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,28 @@ public static function remove_insecure_properties( $theme_json ) {
);

/**
* This converts selectors like '.wp-element-button, .wp-block-button__link'
* to an array, so that the block selector is added to both parts of the selector.
* Function that appends a sub-selector to a existing one.
*
* @param string $element The string with all the element's selectors.
* @param string $selector The string we want to append to the selectors.
* @param string $position The position we wand to append the selector in.
* @return string element selector.
* 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".
*
* @since 5.8.0
* @since 6.1.0 Added append position.
*
* @param string $selector Original selector.
* @param string $to_append Selector to append.
* @param string $position A position sub-selector should be appended. Default: 'right'.
* @return string
*/
private static function appendToSelector( $element, $selector, $position = 0 ) {
$element_selector = array();
$el_selectors = explode( ',', $element );
foreach ( $el_selectors as $el_selector_item ) {
if ( 0 === $position ) {
$element_selector[] = $selector . $el_selector_item;
} else {
$element_selector[] = $el_selector_item . $selector;
}
protected static function append_to_selector( $selector, $to_append, $position = 'right' ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use left|right because of the str_pad, but we can also use start|end.

$new_selectors = array();
$selectors = explode( ',', $selector );
foreach ( $selectors as $sel ) {
$new_selectors[] = 'right' === $position ? $sel . $to_append : $to_append . $sel;
}
$element_selector = implode( ',', $element_selector );
return $element_selector;

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

/**
Expand Down Expand Up @@ -440,7 +442,7 @@ protected static function get_blocks_metadata() {
break;
}

$element_selector = static::appendToSelector( $el_selector, $selector . ' ', 0 );
$element_selector = static::append_to_selector( $el_selector, $selector . ' ', 'left' );
}
static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = $element_selector;
}
Expand Down Expand Up @@ -501,12 +503,9 @@ protected static function get_style_nodes( $theme_json, $selectors = array() ) {

if ( isset( $theme_json['styles']['elements'][ $element ][ $pseudo_selector ] ) ) {

$element_selector = array();
$element_selector = static::appendToSelector( static::ELEMENTS[ $element ], $pseudo_selector, 1 );

$nodes[] = array(
'path' => array( 'styles', 'elements', $element ),
'selector' => $element_selector,
'selector' => static::append_to_selector( static::ELEMENTS[ $element ], $pseudo_selector ),
);
}
}
Expand Down Expand Up @@ -590,12 +589,9 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) {
foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) {

$block_selector = array();
$block_selector = static::appendToSelector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector, 1 );

$nodes[] = array(
'path' => array( 'styles', 'blocks', $name, 'elements', $element ),
'selector' => $block_selector,
'selector' => static::append_to_selector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector ),
);
}
}
Expand Down