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

    r56382 r56709  
    103103        ) {
    104104                $preset_border_color          = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
    105                 $custom_border_color          = _wp_array_get( $block_attributes, array( 'style', 'border', 'color' ), null );
     105                $custom_border_color          = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null;
    106106                $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
    107107        }
    … …  
    110110        if ( $has_border_color_support || $has_border_width_support ) {
    111111                foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
    112                         $border                       = _wp_array_get( $block_attributes, array( 'style', 'border', $side ), null );
     112                        $border                       = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null;
    113113                        $border_side_values           = array(
    114114                                'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
    … …  
    153153function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
    154154        // Check if all border support features have been opted into via `"__experimentalBorder": true`.
    155         if (
    156                 property_exists( $block_type, 'supports' ) &&
    157                 ( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default_value ) )
    158         ) {
    159                 return true;
     155        if ( property_exists( $block_type, 'supports' ) ) {
     156                $block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] )
     157                        ? $block_type->supports['__experimentalBorder']
     158                        : $default_value;
     159                if ( true === $block_type_supports_border ) {
     160                        return true;
     161                }
    160162        }
    161163
Note: See TracChangeset for help on using the changeset viewer.