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
This commit is contained in:
Felix Arntz 2023-06-20 17:54:19 +00:00
parent b1fed29f55
commit ae515caebf
2 changed files with 38 additions and 10 deletions

View File

@ -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,

View File

@ -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.