Make WordPress Core


Ignore:
Timestamp:
09/26/2023 01:45:23 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Editor: Reduce the use of the _wp_array_get() function to improve performance.

_wp_array_get() is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.

This commit aims to further optimize its usage:

  • In many cases, _wp_array_get() can be replaced with a much simpler and faster isset() check.
  • The isset() function is capable of checking nested arrays, so isset( $foo['a']['b']['c'] ) will return false even if $foo['a'] is unset, without throwing any errors or warnings.
  • When _wp_array_get() cannot be directly replaced with isset(), it would be good practice to wrap it in an isset() function so that _wp_array_get() only runs when it needs to.

Original PR from Gutenberg repository:

Follow-up to [55851], [56382].

Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.

File:
1 edited

Legend:

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

    r56643 r56709  
    924924                        // Keep backwards compatibility for support.color.__experimentalDuotone.
    925925                        if ( null === $duotone_selector ) {
    926                                 $duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), null );
     926                                $duotone_support = isset( $block_type->supports['color']['__experimentalDuotone'] )
     927                                        ? $block_type->supports['color']['__experimentalDuotone']
     928                                        : null;
    927929
    928930                                if ( $duotone_support ) {
    … …  
    11611163        public function get_custom_css() {
    11621164                // Add the global styles root CSS.
    1163                 $stylesheet = _wp_array_get( $this->theme_json, array( 'styles', 'css' ), '' );
     1165                $stylesheet = isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : '';
    11641166
    11651167                // Add the global styles block CSS.
    11661168                if ( isset( $this->theme_json['styles']['blocks'] ) ) {
    11671169                        foreach ( $this->theme_json['styles']['blocks'] as $name => $node ) {
    1168                                 $custom_block_css = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $name, 'css' ) );
     1170                                $custom_block_css = isset( $this->theme_json['styles']['blocks'][ $name ]['css'] )
     1171                                        ? $this->theme_json['styles']['blocks'][ $name ]['css']
     1172                                        : null;
    11691173                                if ( $custom_block_css ) {
    11701174                                        $selector    = static::$blocks_metadata[ $name ]['selector'];
    … …  
    12851289
    12861290                $selector                 = isset( $block_metadata['selector'] ) ? $block_metadata['selector'] : '';
    1287                 $has_block_gap_support    = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null;
     1291                $has_block_gap_support    = isset( $this->theme_json['settings']['spacing']['blockGap'] );
    12881292                $has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.
    12891293                $node                     = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
    … …  
    13011305                                $block_gap_value = static::ROOT_BLOCK_SELECTOR === $selector ? '0.5em' : null;
    13021306                                if ( ! empty( $block_type ) ) {
    1303                                         $block_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), null );
     1307                                        $block_gap_value = isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] )
     1308                                                ? $block_type->supports['spacing']['blockGap']['__experimentalDefault']
     1309                                                : null;
    13041310                                }
    13051311                        } else {
    … …  
    13271333                                        }
    13281334
    1329                                         $class_name    = _wp_array_get( $layout_definition, array( 'className' ), false );
    1330                                         $spacing_rules = _wp_array_get( $layout_definition, array( 'spacingStyles' ), array() );
     1335                                        $class_name    = isset( $layout_definition['className'] ) ? $layout_definition['className'] : false;
     1336                                        $spacing_rules = isset( $layout_definition['spacingStyles'] ) ? $layout_definition['spacingStyles'] : array();
    13311337
    13321338                                        if (
    … …  
    13841390                        $valid_display_modes = array( 'block', 'flex', 'grid' );
    13851391                        foreach ( $layout_definitions as $layout_definition ) {
    1386                                 $class_name       = _wp_array_get( $layout_definition, array( 'className' ), false );
    1387                                 $base_style_rules = _wp_array_get( $layout_definition, array( 'baseStyles' ), array() );
     1392                                $class_name       = isset( $layout_definition['className'] ) ? $layout_definition['className'] : false;
     1393                                $base_style_rules = isset( $layout_definition['baseStyles'] ) ? $layout_definition['baseStyles'] : array();
    13881394
    13891395                                if (
    … …  
    18131819        protected static function compute_theme_vars( $settings ) {
    18141820                $declarations  = array();
    1815                 $custom_values = _wp_array_get( $settings, array( 'custom' ), array() );
     1821                $custom_values = isset( $settings['custom'] ) ? $settings['custom'] : array();
    18161822                $css_vars      = static::flatten_tree( $custom_values );
    18171823                foreach ( $css_vars as $key => $value ) {
    … …  
    23272333                $use_root_padding     = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
    23282334                $selector             = $block_metadata['selector'];
    2329                 $settings             = _wp_array_get( $this->theme_json, array( 'settings' ) );
     2335                $settings             = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array();
    23302336                $feature_declarations = static::get_feature_declarations_for_node( $block_metadata, $node );
    23312337
    … …  
    24672473        public function get_root_layout_rules( $selector, $block_metadata ) {
    24682474                $css              = '';
    2469                 $settings         = _wp_array_get( $this->theme_json, array( 'settings' ) );
     2475                $settings         = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array();
    24702476                $use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];
    24712477
    … …  
    25162522                $css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
    25172523
    2518                 $block_gap_value       = _wp_array_get( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ), '0.5em' );
    2519                 $has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null;
     2524                $block_gap_value       = isset( $this->theme_json['styles']['spacing']['blockGap'] ) ? $this->theme_json['styles']['spacing']['blockGap'] : '0.5em';
     2525                $has_block_gap_support = isset( $this->theme_json['settings']['spacing']['blockGap'] );
    25202526                if ( $has_block_gap_support ) {
    25212527                        $block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
    … …  
    33563362         */
    33573363        public function set_spacing_sizes() {
    3358                 $spacing_scale = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() );
     3364                $spacing_scale = isset( $this->theme_json['settings']['spacing']['spacingScale'] )
     3365                        ? $this->theme_json['settings']['spacing']['spacingScale']
     3366                        : array();
    33593367
    33603368                if ( ! isset( $spacing_scale['steps'] )
    … …  
    35953603                }
    35963604
    3597                 $settings = _wp_array_get( $this->theme_json, array( 'settings' ) );
     3605                $settings = isset( $this->theme_json['settings'] )
     3606                        ? $this->theme_json['settings']
     3607                        : array();
    35983608
    35993609                foreach ( $metadata['selectors'] as $feature => $feature_selectors ) {
Note: See TracChangeset for help on using the changeset viewer.