From 58a22ce90a1b56da2b3beee78454129db158decd Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 23 May 2023 05:35:43 -0700 Subject: [PATCH] Introduce `prepend_to_selector()` to avoid additional if checks and follow single responsibility principle (#50266) * Introduce prepend_to_selector() method to avoid additional if checks and follow single responsibility principle. * Don't call append_to_selector() when selector is likely empty. * Remove parameter completely instead of deprecating. * Correct doc block. --- lib/class-wp-theme-json-gutenberg.php | 46 +++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index dc9c25c8f90a5d..9e7a7316ffdd29 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -811,20 +811,45 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n * * @since 5.8.0 * @since 6.1.0 Added append position. + * @since 6.3.0 Removed append position parameter. * * @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 The new selector. */ - protected static function append_to_selector( $selector, $to_append, $position = 'right' ) { + protected static function append_to_selector( $selector, $to_append ) { if ( ! str_contains( $selector, ',' ) ) { - return 'right' === $position ? $selector . $to_append : $to_append . $selector; + return $selector . $to_append; } $new_selectors = array(); $selectors = explode( ',', $selector ); foreach ( $selectors as $sel ) { - $new_selectors[] = 'right' === $position ? $sel . $to_append : $to_append . $sel; + $new_selectors[] = $sel . $to_append; + } + return implode( ',', $new_selectors ); + } + + /** + * Prepends a sub-selector to an existing one. + * + * Given the compounded $selector "h1, h2, h3" + * and the $to_prepend selector ".some-class " the result will be + * ".some-class h1, .some-class h2, .some-class h3". + * + * @since 6.3.0 + * + * @param string $selector Original selector. + * @param string $to_prepend Selector to prepend. + * @return string The new selector. + */ + protected static function prepend_to_selector( $selector, $to_prepend ) { + if ( ! str_contains( $selector, ',' ) ) { + return $to_prepend . $selector; + } + $new_selectors = array(); + $selectors = explode( ',', $selector ); + foreach ( $selectors as $sel ) { + $new_selectors[] = $to_prepend . $sel; } return implode( ',', $new_selectors ); } @@ -1530,10 +1555,13 @@ protected static function compute_preset_classes( $settings, $selector, $origins $slugs = static::get_settings_slugs( $settings, $preset_metadata, $origins ); foreach ( $preset_metadata['classes'] as $class => $property ) { foreach ( $slugs as $slug ) { - $css_var = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug ); - $class_name = static::replace_slug_in_string( $class, $slug ); - $stylesheet .= static::to_ruleset( - static::append_to_selector( $selector, $class_name ), + $css_var = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug ); + $class_name = static::replace_slug_in_string( $class, $slug ); + + // $selector is often empty, so we can save ourselves the `append_to_selector()` call then. + $new_selector = '' === $selector ? $class_name : static::append_to_selector( $selector, $class_name ); + $stylesheet .= static::to_ruleset( + $new_selector, array( array( 'name' => $property, @@ -3458,7 +3486,7 @@ protected static function get_block_element_selectors( $root_selector ) { $element_selector = array( $el_selector ); break; } - $element_selector[] = static::append_to_selector( $el_selector, $selector . ' ', 'left' ); + $element_selector[] = static::prepend_to_selector( $el_selector, $selector . ' ' ); } $element_selectors[ $el_name ] = implode( ',', $element_selector ); }