Skip to content

Commit e77aaf1

Browse files
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: * [WordPress/gutenberg#51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function] Follow-up to [55851], [56382]. Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya. Fixes #59405. git-svn-id: https://develop.svn.wordpress.org/trunk@56709 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a52838d commit e77aaf1

15 files changed

+189
-105
lines changed

src/wp-includes/block-supports/background.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ function wp_render_background_support( $block_content, $block ) {
5858
return $block_content;
5959
}
6060

61-
$background_image_source = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'source' ), null );
62-
$background_image_url = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'url' ), null );
63-
$background_size = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundSize' ), 'cover' );
61+
$background_image_source = isset( $block_attributes['style']['background']['backgroundImage']['source'] )
62+
? $block_attributes['style']['background']['backgroundImage']['source']
63+
: null;
64+
$background_image_url = isset( $block_attributes['style']['background']['backgroundImage']['url'] )
65+
? $block_attributes['style']['background']['backgroundImage']['url']
66+
: null;
67+
$background_size = isset( $block_attributes['style']['background']['backgroundSize'] )
68+
? $block_attributes['style']['background']['backgroundSize']
69+
: 'cover';
6470

6571
$background_block_styles = array();
6672

src/wp-includes/block-supports/border.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
102102
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
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
}
108108

109109
// Generates styles for individual border sides.
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,
115115
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
@@ -152,11 +152,13 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
152152
*/
153153
function 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

162164
// Check if the specific feature has been opted into individually

src/wp-includes/block-supports/colors.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616
* @param WP_Block_Type $block_type Block Type.
1717
*/
1818
function 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 ||
2835
$has_gradients_support ||
@@ -74,7 +81,7 @@ function wp_register_colors_support( $block_type ) {
7481
* @return array Colors CSS classes and inline styles.
7582
*/
7683
function 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 (
8087
is_array( $color_support ) &&
@@ -83,29 +90,33 @@ function wp_apply_colors_support( $block_type, $block_attributes ) {
8390
return array();
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

91102
// Text colors.
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
}
97108

98109
// Background colors.
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
}
104115

105116
// Gradients.
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
}
111122

src/wp-includes/block-supports/dimensions.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpc
6868

6969
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
7070
$dimensions_block_styles = array();
71-
$dimensions_block_styles['minHeight'] = $has_min_height_support && ! $skip_min_height ? _wp_array_get( $block_styles, array( 'dimensions', 'minHeight' ), null ) : null;
72-
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
71+
$dimensions_block_styles['minHeight'] = null;
72+
if ( $has_min_height_support && ! $skip_min_height ) {
73+
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
74+
? $block_styles['dimensions']['minHeight']
75+
: null;
76+
}
77+
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
7378

7479
if ( ! empty( $styles['css'] ) ) {
7580
$attributes['style'] = $styles['css'];

src/wp-includes/block-supports/elements.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
175175
continue;
176176
}
177177

178-
$element_style_object = _wp_array_get( $element_block_styles, array( $element_type ), null );
178+
$element_style_object = isset( $element_block_styles[ $element_type ] ) ? $element_block_styles[ $element_type ] : null;
179179

180180
// Process primary element type styles.
181181
if ( $element_style_object ) {
@@ -201,7 +201,9 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
201201
// Process related elements e.g. h1-h6 for headings.
202202
if ( isset( $element_config['elements'] ) ) {
203203
foreach ( $element_config['elements'] as $element ) {
204-
$element_style_object = _wp_array_get( $element_block_styles, array( $element ), null );
204+
$element_style_object = isset( $element_block_styles[ $element ] )
205+
? $element_block_styles[ $element ]
206+
: null;
205207

206208
if ( $element_style_object ) {
207209
wp_style_engine_get_styles(

src/wp-includes/block-supports/layout.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
414414
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
415415

416416
foreach ( $gap_sides as $gap_side ) {
417-
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
417+
$process_value = $gap_value;
418+
if ( is_array( $gap_value ) ) {
419+
$process_value = isset( $gap_value[ $gap_side ] ) ? $gap_value[ $gap_side ] : $fallback_gap_value;
420+
}
418421
// Get spacing CSS variable from preset value if provided.
419422
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
420423
$index_to_splice = strrpos( $process_value, '|' ) + 1;
@@ -495,7 +498,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
495498
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
496499

497500
foreach ( $gap_sides as $gap_side ) {
498-
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
501+
$process_value = $gap_value;
502+
if ( is_array( $gap_value ) ) {
503+
$process_value = isset( $gap_value[ $gap_side ] ) ? $gap_value[ $gap_side ] : $fallback_gap_value;
504+
}
499505
// Get spacing CSS variable from preset value if provided.
500506
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
501507
$index_to_splice = strrpos( $process_value, '|' ) + 1;
@@ -612,8 +618,15 @@ function wp_render_layout_support_flag( $block_content, $block ) {
612618
}
613619

614620
$global_settings = wp_get_global_settings();
615-
$fallback_layout = ! empty( _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) ) ? _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) : _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
616-
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;
621+
$fallback_layout = isset( $block_type->supports['layout']['default'] )
622+
? $block_type->supports['layout']['default']
623+
: array();
624+
if ( empty( $fallback_layout ) ) {
625+
$fallback_layout = isset( $block_type->supports['__experimentalLayout']['default'] )
626+
? $block_type->supports['__experimentalLayout']['default']
627+
: array();
628+
}
629+
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;
617630

618631
$class_names = array();
619632
$layout_definitions = wp_get_layout_definitions();
@@ -624,7 +637,9 @@ function wp_render_layout_support_flag( $block_content, $block ) {
624637
$used_layout['type'] = 'constrained';
625638
}
626639

627-
$root_padding_aware_alignments = _wp_array_get( $global_settings, array( 'useRootPaddingAwareAlignments' ), false );
640+
$root_padding_aware_alignments = isset( $global_settings['useRootPaddingAwareAlignments'] )
641+
? $global_settings['useRootPaddingAwareAlignments']
642+
: false;
628643

629644
if (
630645
$root_padding_aware_alignments &&
@@ -654,9 +669,13 @@ function wp_render_layout_support_flag( $block_content, $block ) {
654669

655670
// Get classname for layout type.
656671
if ( isset( $used_layout['type'] ) ) {
657-
$layout_classname = _wp_array_get( $layout_definitions, array( $used_layout['type'], 'className' ), '' );
672+
$layout_classname = isset( $layout_definitions[ $used_layout['type'] ]['className'] )
673+
? $layout_definitions[ $used_layout['type'] ]['className']
674+
: '';
658675
} else {
659-
$layout_classname = _wp_array_get( $layout_definitions, array( 'default', 'className' ), '' );
676+
$layout_classname = isset( $layout_definitions['default']['className'] )
677+
? $layout_definitions['default']['className']
678+
: '';
660679
}
661680

662681
if ( $layout_classname && is_string( $layout_classname ) ) {
@@ -669,7 +688,9 @@ function wp_render_layout_support_flag( $block_content, $block ) {
669688
*/
670689
if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
671690

672-
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
691+
$gap_value = isset( $block['attrs']['style']['spacing']['blockGap'] )
692+
? $block['attrs']['style']['spacing']['blockGap']
693+
: null;
673694
/*
674695
* Skip if gap value contains unsupported characters.
675696
* Regex for CSS value borrowed from `safecss_filter_attr`, and used here
@@ -683,16 +704,22 @@ function wp_render_layout_support_flag( $block_content, $block ) {
683704
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
684705
}
685706

686-
$fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' );
687-
$block_spacing = _wp_array_get( $block, array( 'attrs', 'style', 'spacing' ), null );
707+
$fallback_gap_value = isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] )
708+
? $block_type->supports['spacing']['blockGap']['__experimentalDefault']
709+
: '0.5em';
710+
$block_spacing = isset( $block['attrs']['style']['spacing'] )
711+
? $block['attrs']['style']['spacing']
712+
: null;
688713

689714
/*
690715
* If a block's block.json skips serialization for spacing or spacing.blockGap,
691716
* don't apply the user-defined value to the styles.
692717
*/
693718
$should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
694719

695-
$block_gap = _wp_array_get( $global_settings, array( 'spacing', 'blockGap' ), null );
720+
$block_gap = isset( $global_settings['spacing']['blockGap'] )
721+
? $global_settings['spacing']['blockGap']
722+
: null;
696723
$has_block_gap_support = isset( $block_gap );
697724

698725
$style = wp_get_layout_style(

src/wp-includes/block-supports/position.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function wp_render_position_support( $block_content, $block ) {
5151
}
5252

5353
$global_settings = wp_get_global_settings();
54-
$theme_has_sticky_support = _wp_array_get( $global_settings, array( 'position', 'sticky' ), false );
55-
$theme_has_fixed_support = _wp_array_get( $global_settings, array( 'position', 'fixed' ), false );
54+
$theme_has_sticky_support = isset( $global_settings['position']['sticky'] ) ? $global_settings['position']['sticky'] : false;
55+
$theme_has_fixed_support = isset( $global_settings['position']['fixed'] ) ? $global_settings['position']['fixed'] : false;
5656

5757
// Only allow output for position types that the theme supports.
5858
$allowed_position_types = array();
@@ -63,11 +63,11 @@ function wp_render_position_support( $block_content, $block ) {
6363
$allowed_position_types[] = 'fixed';
6464
}
6565

66-
$style_attribute = _wp_array_get( $block, array( 'attrs', 'style' ), null );
66+
$style_attribute = isset( $block['attrs']['style'] ) ? $block['attrs']['style'] : null;
6767
$class_name = wp_unique_id( 'wp-container-' );
6868
$selector = ".$class_name";
6969
$position_styles = array();
70-
$position_type = _wp_array_get( $style_attribute, array( 'position', 'type' ), '' );
70+
$position_type = isset( $style_attribute['position']['type'] ) ? $style_attribute['position']['type'] : '';
7171
$wrapper_classes = array();
7272

7373
if (
@@ -78,7 +78,7 @@ function wp_render_position_support( $block_content, $block ) {
7878
$sides = array( 'top', 'right', 'bottom', 'left' );
7979

8080
foreach ( $sides as $side ) {
81-
$side_value = _wp_array_get( $style_attribute, array( 'position', $side ) );
81+
$side_value = isset( $style_attribute['position'][ $side ] ) ? $style_attribute['position'][ $side ] : null;
8282
if ( null !== $side_value ) {
8383
/*
8484
* For fixed or sticky top positions,

src/wp-includes/block-supports/settings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function _wp_add_block_level_presets_class( $block_content, $block ) {
4545
}
4646

4747
// return early if no settings are found on the block attributes.
48-
$block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null );
48+
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
4949
if ( empty( $block_settings ) ) {
5050
return $block_content;
5151
}
@@ -82,7 +82,7 @@ function _wp_add_block_level_preset_styles( $pre_render, $block ) {
8282
}
8383

8484
// return early if no settings are found on the block attributes.
85-
$block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null );
85+
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
8686
if ( empty( $block_settings ) ) {
8787
return null;
8888
}

0 commit comments

Comments
 (0)