Make WordPress Core


Ignore:
Timestamp:
08/10/2024 10:58:07 PM (2 months ago)
Author:
peterwilsoncc
Message:

Code Quality: Clarify variable names in dependency classes.

Renames several variables in the WP_Scripts and WP_Styles classes to clarify their purpose for developers reading the code.

Props peterwilsoncc, sergeybiryukov.
See #61607.

File:
1 edited

Legend:

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

    r57533 r58878  
    290290        }
    291291
    292         $src               = $obj->src;
    293         $strategy          = $this->get_eligible_loading_strategy( $handle );
    294         $intended_strategy = (string) $this->get_data( $handle, 'strategy' );
    295         $cond_before      = '';
    296         $cond_after        = '';
    297         $conditional       = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
     292        $src                   = $obj->src;
     293        $strategy              = $this->get_eligible_loading_strategy( $handle );
     294        $intended_strategy     = (string) $this->get_data( $handle, 'strategy' );
     295        $ie_conditional_prefix = '';
     296        $ie_conditional_suffix = '';
     297        $conditional           = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
    298298
    299299        if ( ! $this->is_delayed_strategy( $intended_strategy ) ) {
     
    321321
    322322        if ( $conditional ) {
    323             $cond_before = "<!--[if {$conditional}]>\n";
    324             $cond_after = "<![endif]-->\n";
     323            $ie_conditional_prefix = "<!--[if {$conditional}]>\n";
     324            $ie_conditional_suffix = "<![endif]-->\n";
    325325        }
    326326
     
    329329
    330330        if ( $before_script || $after_script ) {
    331             $inline_script_tag = $cond_before . $before_script . $after_script . $cond_after;
     331            $inline_script_tag = $ie_conditional_prefix . $before_script . $after_script . $ie_conditional_suffix;
    332332        } else {
    333333            $inline_script_tag = '';
     
    354354             * @param string $handle Script handle.
    355355             */
    356             $srce = apply_filters( 'script_loader_src', $src, $handle );
     356            $filtered_src = apply_filters( 'script_loader_src', $src, $handle );
    357357
    358358            if (
    359                 $this->in_default_dir( $srce )
     359                $this->in_default_dir( $filtered_src )
    360360                && ( $before_script || $after_script || $translations_stop_concat || $this->is_delayed_strategy( $strategy ) )
    361361            ) {
     
    365365                _print_scripts();
    366366                $this->reset();
    367             } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
     367            } elseif ( $this->in_default_dir( $filtered_src ) && ! $conditional ) {
    368368                $this->print_code     .= $this->print_extra_script( $handle, false );
    369369                $this->concat         .= "$handle,";
     
    379379
    380380        if ( $has_conditional_data ) {
    381             echo $cond_before;
     381            echo $ie_conditional_prefix;
    382382        }
    383383
     
    385385
    386386        if ( $has_conditional_data ) {
    387             echo $cond_after;
     387            echo $ie_conditional_suffix;
    388388        }
    389389
     
    426426            $attr['data-wp-strategy'] = $intended_strategy;
    427427        }
    428         $tag  = $translations . $cond_before . $before_script;
     428        $tag  = $translations . $ie_conditional_prefix . $before_script;
    429429        $tag .= wp_get_script_tag( $attr );
    430         $tag .= $after_script . $cond_after;
     430        $tag .= $after_script . $ie_conditional_suffix;
    431431
    432432        /**
     
    627627    public function set_group( $handle, $recursion, $group = false ) {
    628628        if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) {
    629             $grp = 1;
     629            $calculated_group = 1;
    630630        } else {
    631             $grp = (int) $this->get_data( $handle, 'group' );
    632         }
    633 
    634         if ( false !== $group && $grp > $group ) {
    635             $grp = $group;
    636         }
    637 
    638         return parent::set_group( $handle, $recursion, $grp );
     631            $calculated_group = (int) $this->get_data( $handle, 'group' );
     632        }
     633
     634        if ( false !== $group && $calculated_group > $group ) {
     635            $calculated_group = $group;
     636        }
     637
     638        return parent::set_group( $handle, $recursion, $calculated_group );
    639639    }
    640640
     
    724724     */
    725725    public function all_deps( $handles, $recursion = false, $group = false ) {
    726         $r = parent::all_deps( $handles, $recursion, $group );
     726        $result = parent::all_deps( $handles, $recursion, $group );
    727727        if ( ! $recursion ) {
    728728            /**
     
    735735            $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
    736736        }
    737         return $r;
     737        return $result;
    738738    }
    739739
     
    890890     */
    891891    private function get_eligible_loading_strategy( $handle ) {
    892         $intended = (string) $this->get_data( $handle, 'strategy' );
     892        $intended_strategy = (string) $this->get_data( $handle, 'strategy' );
    893893
    894894        // Bail early if there is no intended strategy.
    895         if ( ! $intended ) {
     895        if ( ! $intended_strategy ) {
    896896            return '';
    897897        }
     
    901901         * strategies, since 'async' can fallback to 'defer', but not vice-versa.
    902902         */
    903         $initial = ( 'defer' === $intended ) ? array( 'defer' ) : null;
    904 
    905         $eligible = $this->filter_eligible_strategies( $handle, $initial );
     903        $initial_strategy = ( 'defer' === $intended_strategy ) ? array( 'defer' ) : null;
     904
     905        $eligible_strategies = $this->filter_eligible_strategies( $handle, $initial_strategy );
    906906
    907907        // Return early once we know the eligible strategy is blocking.
    908         if ( empty( $eligible ) ) {
     908        if ( empty( $eligible_strategies ) ) {
    909909            return '';
    910910        }
    911911
    912         return in_array( 'async', $eligible, true ) ? 'async' : 'defer';
     912        return in_array( 'async', $eligible_strategies, true ) ? 'async' : 'defer';
    913913    }
    914914
     
    918918     * @since 6.3.0
    919919     *
    920      * @param string              $handle   The script handle.
    921      * @param string[]|null       $eligible Optional. The list of strategies to filter. Default null.
    922      * @param array<string, true> $checked  Optional. An array of already checked script handles, used to avoid recursive loops.
     920     * @param string              $handle              The script handle.
     921     * @param string[]|null       $eligible_strategies Optional. The list of strategies to filter. Default null.
     922     * @param array<string, true> $checked             Optional. An array of already checked script handles, used to avoid recursive loops.
    923923     * @return string[] A list of eligible loading strategies that could be used.
    924924     */
    925     private function filter_eligible_strategies( $handle, $eligible = null, $checked = array() ) {
     925    private function filter_eligible_strategies( $handle, $eligible_strategies = null, $checked = array() ) {
    926926        // If no strategies are being passed, all strategies are eligible.
    927         if ( null === $eligible ) {
    928             $eligible = $this->delayed_strategies;
     927        if ( null === $eligible_strategies ) {
     928            $eligible_strategies = $this->delayed_strategies;
    929929        }
    930930
    931931        // If this handle was already checked, return early.
    932932        if ( isset( $checked[ $handle ] ) ) {
    933             return $eligible;
     933            return $eligible_strategies;
    934934        }
    935935
     
    939939        // If this handle isn't registered, don't filter anything and return.
    940940        if ( ! isset( $this->registered[ $handle ] ) ) {
    941             return $eligible;
     941            return $eligible_strategies;
    942942        }
    943943
    944944        // If the handle is not enqueued, don't filter anything and return.
    945945        if ( ! $this->query( $handle, 'enqueued' ) ) {
    946             return $eligible;
     946            return $eligible_strategies;
    947947        }
    948948
     
    962962        // If the intended strategy is 'defer', filter out 'async'.
    963963        if ( 'defer' === $intended_strategy ) {
    964             $eligible = array( 'defer' );
     964            $eligible_strategies = array( 'defer' );
    965965        }
    966966
     
    970970        foreach ( $dependents as $dependent ) {
    971971            // Bail early once we know the eligible strategy is blocking.
    972             if ( empty( $eligible ) ) {
     972            if ( empty( $eligible_strategies ) ) {
    973973                return array();
    974974            }
    975975
    976             $eligible = $this->filter_eligible_strategies( $dependent, $eligible, $checked );
    977         }
    978 
    979         return $eligible;
     976            $eligible_strategies = $this->filter_eligible_strategies( $dependent, $eligible_strategies, $checked );
     977        }
     978
     979        return $eligible_strategies;
    980980    }
    981981
Note: See TracChangeset for help on using the changeset viewer.