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/block-supports/colors.php

    r56604 r56709  
    1717 */
    1818function wp_register_colors_support( $block_type ) {
    19         $color_support                 = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( 'color' ), false ) : false;
    20         $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
    21         $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
    22         $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
    23         $has_link_colors_support       = _wp_array_get( $color_support, array( 'link' ), false );
    24         $has_button_colors_support     = _wp_array_get( $color_support, array( 'button' ), false );
    25         $has_heading_colors_support    = _wp_array_get( $color_support, array( 'heading' ), false );
     19        $color_support = false;
     20        if ( property_exists( $block_type, 'supports' ) ) {
     21                $color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
     22        }
     23        $has_text_colors_support       = true === $color_support ||
     24                ( isset( $color_support['text'] ) && $color_support['text'] ) ||
     25                ( is_array( $color_support ) && ! isset( $color_support['text'] ) );
     26        $has_background_colors_support = true === $color_support ||
     27                ( isset( $color_support['background'] ) && $color_support['background'] ) ||
     28                ( is_array( $color_support ) && ! isset( $color_support['background'] ) );
     29        $has_gradients_support         = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
     30        $has_link_colors_support       = isset( $color_support['link'] ) ? $color_support['link'] : false;
     31        $has_button_colors_support     = isset( $color_support['button'] ) ? $color_support['button'] : false;
     32        $has_heading_colors_support    = isset( $color_support['heading'] ) ? $color_support['heading'] : false;
    2633        $has_color_support             = $has_text_colors_support ||
    2734                $has_background_colors_support ||
    … …  
    7582 */
    7683function wp_apply_colors_support( $block_type, $block_attributes ) {
    77         $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
     84        $color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
    7885
    7986        if (
    … …  
    8491        }
    8592
    86         $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
    87         $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
    88         $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
     93        $has_text_colors_support       = true === $color_support ||
     94                ( isset( $color_support['text'] ) && $color_support['text'] ) ||
     95                ( is_array( $color_support ) && ! isset( $color_support['text'] ) );
     96        $has_background_colors_support = true === $color_support ||
     97                ( isset( $color_support['background'] ) && $color_support['background'] ) ||
     98                ( is_array( $color_support ) && ! isset( $color_support['background'] ) );
     99        $has_gradients_support         = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
    89100        $color_block_styles            = array();
    90101
    … …  
    92103        if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
    93104                $preset_text_color          = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
    94                 $custom_text_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'text' ), null );
     105                $custom_text_color          = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null;
    95106                $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
    96107        }
    … …  
    99110        if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
    100111                $preset_background_color          = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
    101                 $custom_background_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'background' ), null );
     112                $custom_background_color          = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null;
    102113                $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
    103114        }
    … …  
    106117        if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
    107118                $preset_gradient_color          = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
    108                 $custom_gradient_color          = _wp_array_get( $block_attributes, array( 'style', 'color', 'gradient' ), null );
     119                $custom_gradient_color          = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null;
    109120                $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
    110121        }
Note: See TracChangeset for help on using the changeset viewer.