Make WordPress Core

Changeset 55950


Ignore:
Timestamp:
06/20/2023 05:52:32 PM (13 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r55945 r55950  
    796796     * @since 5.8.0
    797797     * @since 6.1.0 Added append position.
     798     * @since 6.3.0 Removed append position parameter.
    798799     *
    799800     * @param string $selector  Original selector.
    800801     * @param string $to_append Selector to append.
    801      * @param string $position  A position sub-selector should be appended. Default 'right'.
    802802     * @return string The new selector.
    803803     */
    804     protected static function append_to_selector( $selector, $to_append, $position = 'right' ) {
     804    protected static function append_to_selector( $selector, $to_append ) {
    805805        if ( ! str_contains( $selector, ',' ) ) {
    806             return 'right' === $position ? $selector . $to_append : $to_append . $selector;
     806            return $selector . $to_append;
    807807        }
    808808        $new_selectors = array();
    809809        $selectors     = explode( ',', $selector );
    810810        foreach ( $selectors as $sel ) {
    811             $new_selectors[] = 'right' === $position ? $sel . $to_append : $to_append . $sel;
     811            $new_selectors[] = $sel . $to_append;
     812        }
     813        return implode( ',', $new_selectors );
     814    }
     815
     816    /**
     817     * Prepends a sub-selector to an existing one.
     818     *
     819     * Given the compounded $selector "h1, h2, h3"
     820     * and the $to_prepend selector ".some-class " the result will be
     821     * ".some-class h1, .some-class  h2, .some-class  h3".
     822     *
     823     * @since 6.3.0
     824     *
     825     * @param string $selector   Original selector.
     826     * @param string $to_prepend Selector to prepend.
     827     * @return string The new selector.
     828     */
     829    protected static function prepend_to_selector( $selector, $to_prepend ) {
     830        if ( ! str_contains( $selector, ',' ) ) {
     831            return $to_prepend . $selector;
     832        }
     833        $new_selectors = array();
     834        $selectors     = explode( ',', $selector );
     835        foreach ( $selectors as $sel ) {
     836            $new_selectors[] = $to_prepend . $sel;
    812837        }
    813838        return implode( ',', $new_selectors );
     
    901926                        break;
    902927                    }
    903                     $element_selector[] = static::append_to_selector( $el_selector, $selector . ' ', 'left' );
     928                    $element_selector[] = static::prepend_to_selector( $el_selector, $selector . ' ' );
    904929                }
    905930                static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
     
    15381563            foreach ( $preset_metadata['classes'] as $class => $property ) {
    15391564                foreach ( $slugs as $slug ) {
    1540                     $css_var     = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug );
    1541                     $class_name  = static::replace_slug_in_string( $class, $slug );
    1542                     $stylesheet .= static::to_ruleset(
    1543                         static::append_to_selector( $selector, $class_name ),
     1565                    $css_var    = static::replace_slug_in_string( $preset_metadata['css_vars'], $slug );
     1566                    $class_name = static::replace_slug_in_string( $class, $slug );
     1567
     1568                    // $selector is often empty, so we can save ourselves the `append_to_selector()` call then.
     1569                    $new_selector = '' === $selector ? $class_name : static::append_to_selector( $selector, $class_name );
     1570                    $stylesheet  .= static::to_ruleset(
     1571                        $new_selector,
    15441572                        array(
    15451573                            array(
Note: See TracChangeset for help on using the changeset viewer.