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/typography.php

    r56605 r56709  
    2121        }
    2222
    23         $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
     23        $typography_supports = isset( $block_type->supports['typography'] ) ? $block_type->supports['typography'] : false;
    2424        if ( ! $typography_supports ) {
    2525                return;
    2626        }
    2727
    28         $has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    29         $has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    30         $has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    31         $has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
    32         $has_letter_spacing_support  = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false );
    33         $has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    34         $has_text_columns_support    = _wp_array_get( $typography_supports, array( 'textColumns' ), false );
    35         $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    36         $has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    37         $has_writing_mode_support    = _wp_array_get( $typography_supports, array( '__experimentalWritingMode' ), false );
     28        $has_font_family_support     = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
     29        $has_font_size_support       = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
     30        $has_font_style_support      = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
     31        $has_font_weight_support     = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
     32        $has_letter_spacing_support  = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
     33        $has_line_height_support     = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
     34        $has_text_columns_support    = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
     35        $has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
     36        $has_text_transform_support  = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
     37        $has_writing_mode_support    = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;
    3838
    3939        $has_typography_support = $has_font_family_support
    … …  
    9090        }
    9191
    92         $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
     92        $typography_supports = isset( $block_type->supports['typography'] )
     93                ? $block_type->supports['typography']
     94                : false;
    9395        if ( ! $typography_supports ) {
    9496                return array();
    … …  
    99101        }
    100102
    101         $has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    102         $has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    103         $has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    104         $has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
    105         $has_letter_spacing_support  = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false );
    106         $has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    107         $has_text_columns_support    = _wp_array_get( $typography_supports, array( 'textColumns' ), false );
    108         $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    109         $has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    110         $has_writing_mode_support    = _wp_array_get( $typography_supports, array( '__experimentalWritingMode' ), false );
     103        $has_font_family_support     = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
     104        $has_font_size_support       = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
     105        $has_font_style_support      = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
     106        $has_font_weight_support     = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
     107        $has_letter_spacing_support  = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
     108        $has_line_height_support     = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
     109        $has_text_columns_support    = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
     110        $has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
     111        $has_text_transform_support  = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
     112        $has_writing_mode_support    = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;
    111113
    112114        // Whether to skip individual block support features.
    … …  
    170172
    171173        if ( $has_line_height_support && ! $should_skip_line_height ) {
    172                 $typography_block_styles['lineHeight'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'lineHeight' ) );
     174                $typography_block_styles['lineHeight'] = isset( $block_attributes['style']['typography']['lineHeight'] )
     175                        ? $block_attributes['style']['typography']['lineHeight']
     176                        : null;
    173177        }
    174178
    175179        if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
    176                 $typography_block_styles['textColumns'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'textColumns' ), null );
     180                $typography_block_styles['textColumns'] = isset( $block_attributes['style']['typography']['textColumns'] )
     181                        ? $block_attributes['style']['typography']['textColumns']
     182                        : null;
    177183        }
    178184
    … …  
    214220                isset( $block_attributes['style']['typography']['writingMode'] )
    215221        ) {
    216                 $typography_block_styles['writingMode'] = _wp_array_get(
    217                         $block_attributes,
    218                         array( 'style', 'typography', 'writingMode' ),
    219                         null
    220                 );
     222                $typography_block_styles['writingMode'] = isset( $block_attributes['style']['typography']['writingMode'] )
     223                        ? $block_attributes['style']['typography']['writingMode']
     224                        : null;
    221225        }
    222226
Note: See TracChangeset for help on using the changeset viewer.