From ae515caebf6f56fc7d5bbdd65e8140f70bd04288 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 20 Jun 2023 17:54:19 +0000 Subject: [PATCH] Editor: Introduce `WP_Theme_JSON::prepend_to_selector()` to improve code quality and performance. The `WP_Theme_JSON::append_to_selector()` method was previously used for both appending and prepending which violated the single responsibility principle. It resulted in additional conditionals which also came at a performance cost, particularly because the method is called over 1,000 times during a regular WordPress request. With the new `WP_Theme_JSON::prepend_to_selector()` method, there are now two distinct methods for the two distinct purposes. The now useless third parameter on `WP_Theme_JSON::append_to_selector()` has been removed (rather than deprecated), which is acceptable given that it is a protected method on a class that is not intended for extensions. Props bor0, costdev, flixos90, isabel_brison, oandregal, spacedmonkey. Fixes #58193. See #58457. Built from https://develop.svn.wordpress.org/trunk@55950 git-svn-id: http://core.svn.wordpress.org/trunk@55462 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-theme-json.php | 46 +++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index 6b21f343b1..74f3971bf2 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -795,20 +795,45 @@ class WP_Theme_JSON { * * @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 ); } @@ -900,7 +925,7 @@ class WP_Theme_JSON { $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 . ' ' ); } static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector ); } @@ -1537,10 +1562,13 @@ class WP_Theme_JSON { $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, diff --git a/wp-includes/version.php b/wp-includes/version.php index fb601e3fd6..6ffd42b459 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-55949'; +$wp_version = '6.3-alpha-55950'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.