Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement: Reduce the use of the _wp_array_get function #51116

Merged
merged 33 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
634aa61
lib/class-wp-duotone-gutenberg.php
aristath May 31, 2023
393ff4a
lib/block-supports/border.php
aristath May 31, 2023
a047545
lib/class-wp-theme-json-resolver-gutenberg.php
aristath May 31, 2023
ce96d3b
lib/class-wp-theme-json-gutenberg.php
aristath May 31, 2023
0bb4d31
lib/block-supports/colors.php
aristath May 31, 2023
d0a4ef0
lib/block-supports/dimensions.php
aristath May 31, 2023
69ee300
bugfix in colors.php
aristath May 31, 2023
73d9f60
fix condition in colors.php
aristath May 31, 2023
a68c19c
lib/block-supports/layout.php
aristath May 31, 2023
3b637e4
lib/block-supports/position.php
aristath May 31, 2023
0e0d2cc
lib/block-supports/settings.php
aristath May 31, 2023
3ab4342
lib/block-supports/spacing.php
aristath May 31, 2023
98ccc2f
lib/block-supports/typography.php
aristath May 31, 2023
2794b0c
Revert "lib/block-supports/settings.php"
aristath May 31, 2023
6a18165
lib/block-supports/settings.php
aristath May 31, 2023
236b6ac
packages/block-library/src/gallery/index.php
aristath May 31, 2023
95949a9
packages/block-library/src/navigation/index.php
aristath May 31, 2023
e0bd282
packages/block-library/src/post-featured-image/index.php
aristath May 31, 2023
2e0cc17
packages/block-library/src/search/index.php
aristath May 31, 2023
d9276fb
packages/style-engine/docs/using-the-style-engine-with-block-supports.md
aristath May 31, 2023
998cb62
further improvements
aristath Jun 23, 2023
b508d1b
typo fix
aristath Jun 23, 2023
f993083
rebase conflict typo fix
aristath Jul 25, 2023
af87ace
Coding standards fix
aristath Jul 25, 2023
bcf4c02
resolve rebase conflicts & refactor
aristath Sep 12, 2023
bd252b4
logic fix
aristath Sep 12, 2023
6634c75
some sugar
aristath Sep 12, 2023
9366fe8
don't use null-coallescing yet (php 7.4+ only)
aristath Sep 12, 2023
5e7dc5a
Revert "don't use null-coallescing yet (php 7.4+ only)"
aristath Sep 12, 2023
a031125
bugfix
aristath Sep 12, 2023
8e31cb9
resolve rebase conflict
aristath Sep 14, 2023
f9ff2b5
readability improvement/simplification
aristath Sep 19, 2023
aa7ec19
missed this one in my previous commit
aristath Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function gutenberg_render_background_support( $block_content, $block ) {
return $block_content;
}

$background_image_source = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'source' ), null );
$background_image_url = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'url' ), null );
$background_size = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundSize' ), 'cover' );
$background_image_source = $block_attributes['style']['background']['backgroundImage']['source'] ?? null;
$background_image_url = $block_attributes['style']['background']['backgroundImage']['url'] ?? null;
$background_size = $block_attributes['style']['background']['backgroundSize'] ?? 'cover';

$background_block_styles = array();

Expand Down
14 changes: 7 additions & 7 deletions lib/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
) {
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
$custom_border_color = _wp_array_get( $block_attributes, array( 'style', 'border', 'color' ), null );
$custom_border_color = $block_attributes['style']['border']['color'] ?? null;
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
}

// Generate styles for individual border sides.
if ( $has_border_color_support || $has_border_width_support ) {
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
$border = _wp_array_get( $block_attributes, array( 'style', 'border', $side ), null );
$border = $block_attributes['style']['border'][ $side ] ?? null;
$border_side_values = array(
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
Expand Down Expand Up @@ -142,11 +142,11 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
*/
function gutenberg_has_border_feature_support( $block_type, $feature, $default_value = false ) {
// Check if all border support features have been opted into via `"__experimentalBorder": true`.
if (
property_exists( $block_type, 'supports' ) &&
( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default_value ) )
) {
return true;
if ( property_exists( $block_type, 'supports' ) ) {
$block_type_supports_border = $block_type->supports['__experimentalBorder'] ?? $default_value;
if ( true === $block_type_supports_border ) {
return true;
}
}

// Check if the specific feature has been opted into individually
Expand Down
39 changes: 25 additions & 14 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_colors_support( $block_type ) {
$color_support = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( 'color' ), false ) : false;
$has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
$has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
$has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
$has_link_colors_support = _wp_array_get( $color_support, array( 'link' ), false );
$has_button_colors_support = _wp_array_get( $color_support, array( 'button' ), false );
$has_heading_colors_support = _wp_array_get( $color_support, array( 'heading' ), false );
$color_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$color_support = $block_type->supports['color'] ?? false;
}
$has_text_colors_support = true === $color_support ||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
$has_background_colors_support = true === $color_support ||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
$has_gradients_support = $color_support['gradients'] ?? false;
$has_link_colors_support = $color_support['link'] ?? false;
$has_button_colors_support = $color_support['button'] ?? false;
$has_heading_colors_support = $color_support['heading'] ?? false;
$has_color_support = $has_text_colors_support ||
$has_background_colors_support ||
$has_gradients_support ||
Expand Down Expand Up @@ -65,7 +72,7 @@ function gutenberg_register_colors_support( $block_type ) {
* @return array Colors CSS classes and inline styles.
*/
function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
$color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
$color_support = $block_type->supports['color'] ?? false;

if (
is_array( $color_support ) &&
Expand All @@ -74,31 +81,35 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
return array();
}

$has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
$has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
$has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
$has_text_colors_support = true === $color_support ||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
$has_background_colors_support = true === $color_support ||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
$has_gradients_support = $color_support['gradients'] ?? false;
$color_block_styles = array();

// Text colors.
// Check support for text colors.
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
$custom_text_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'text' ), null );
$custom_text_color = $block_attributes['style']['color']['text'] ?? null;
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
}

// Background colors.
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
$custom_background_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'background' ), null );
$custom_background_color = $block_attributes['style']['color']['background'] ?? null;
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
}

// Gradients.

if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
$custom_gradient_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'gradient' ), null );
$custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null;
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/block-supports/dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) {

$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
$dimensions_block_styles = array();
$dimensions_block_styles['minHeight'] = $has_min_height_support && ! $skip_min_height ? _wp_array_get( $block_styles, array( 'dimensions', 'minHeight' ), null ) : null;
$styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
$dimensions_block_styles['minHeight'] = null;
if ( $has_min_height_support && ! $skip_min_height ) {
$dimensions_block_styles['minHeight'] = $block_styles['dimensions']['minHeight'] ?? null;
}
$styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );

if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
Expand Down
33 changes: 21 additions & 12 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function gutenberg_register_layout_support( $block_type ) {
* @return string CSS styles on success. Else, empty string.
*/
function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null ) {
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
$layout_type = $layout['type'] ?? 'default';
$layout_styles = array();

if ( 'default' === $layout_type ) {
Expand Down Expand Up @@ -401,7 +401,10 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );

foreach ( $gap_sides as $gap_side ) {
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
$process_value = $gap_value;
if ( is_array( $gap_value ) ) {
$process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
}
// Get spacing CSS variable from preset value if provided.
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
$index_to_splice = strrpos( $process_value, '|' ) + 1;
Expand Down Expand Up @@ -482,7 +485,10 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );

foreach ( $gap_sides as $gap_side ) {
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
$process_value = $gap_value;
if ( is_array( $gap_value ) ) {
$process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
}
// Get spacing CSS variable from preset value if provided.
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
$index_to_splice = strrpos( $process_value, '|' ) + 1;
Expand Down Expand Up @@ -594,8 +600,11 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
}

$global_settings = gutenberg_get_global_settings();
$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() );
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;
$fallback_layout = $block_type->supports['layout']['default'] ?? array();
if ( empty( $fallback_layout ) ) {
$fallback_layout = $block_type->supports['__experimentalLayout']['default'] ?? array();
}
$used_layout = $block['attrs']['layout'] ?? $fallback_layout;

$class_names = array();
$layout_definitions = gutenberg_get_layout_definitions();
Expand All @@ -606,7 +615,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$used_layout['type'] = 'constrained';
}

$root_padding_aware_alignments = _wp_array_get( $global_settings, array( 'useRootPaddingAwareAlignments' ), false );
$root_padding_aware_alignments = $global_settings['useRootPaddingAwareAlignments'] ?? false;

if ( $root_padding_aware_alignments && isset( $used_layout['type'] ) && 'constrained' === $used_layout['type'] ) {
$class_names[] = 'has-global-padding';
Expand All @@ -632,9 +641,9 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {

// Get classname for layout type.
if ( isset( $used_layout['type'] ) ) {
$layout_classname = _wp_array_get( $layout_definitions, array( $used_layout['type'], 'className' ), '' );
$layout_classname = $layout_definitions[ $used_layout['type'] ]['className'] ?? '';
} else {
$layout_classname = _wp_array_get( $layout_definitions, array( 'default', 'className' ), '' );
$layout_classname = $layout_definitions['default']['className'] ?? '';
}

if ( $layout_classname && is_string( $layout_classname ) ) {
Expand All @@ -647,7 +656,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
*/
if ( ! current_theme_supports( 'disable-layout-styles' ) ) {

$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
$gap_value = $block['attrs']['style']['spacing']['blockGap'] ?? null;

/*
* Skip if gap value contains unsupported characters.
Expand All @@ -662,16 +671,16 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
}

$fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' );
$block_spacing = _wp_array_get( $block, array( 'attrs', 'style', 'spacing' ), null );
$fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
$block_spacing = $block['attrs']['style']['spacing'] ?? null;

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

$block_gap = _wp_array_get( $global_settings, array( 'spacing', 'blockGap' ), null );
$block_gap = $global_settings['spacing']['blockGap'] ?? null;
$has_block_gap_support = isset( $block_gap );

$style = gutenberg_get_layout_style(
Expand Down
10 changes: 5 additions & 5 deletions lib/block-supports/position.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function gutenberg_render_position_support( $block_content, $block ) {
}

$global_settings = gutenberg_get_global_settings();
$theme_has_sticky_support = _wp_array_get( $global_settings, array( 'position', 'sticky' ), false );
$theme_has_fixed_support = _wp_array_get( $global_settings, array( 'position', 'fixed' ), false );
$theme_has_sticky_support = $global_settings['position']['sticky'] ?? false;
$theme_has_fixed_support = $global_settings['position']['fixed'] ?? false;

// Only allow output for position types that the theme supports.
$allowed_position_types = array();
Expand All @@ -56,11 +56,11 @@ function gutenberg_render_position_support( $block_content, $block ) {
$allowed_position_types[] = 'fixed';
}

$style_attribute = _wp_array_get( $block, array( 'attrs', 'style' ), null );
$style_attribute = $block['attrs']['style'] ?? null;
$class_name = wp_unique_id( 'wp-container-' );
$selector = ".$class_name";
$position_styles = array();
$position_type = _wp_array_get( $style_attribute, array( 'position', 'type' ), '' );
$position_type = $style_attribute['position']['type'] ?? '';
$wrapper_classes = array();

if (
Expand All @@ -71,7 +71,7 @@ function gutenberg_render_position_support( $block_content, $block ) {
$sides = array( 'top', 'right', 'bottom', 'left' );

foreach ( $sides as $side ) {
$side_value = _wp_array_get( $style_attribute, array( 'position', $side ) );
$side_value = $style_attribute['position'][ $side ] ?? null;
if ( null !== $side_value ) {
/*
* For fixed or sticky top positions,
Expand Down
4 changes: 2 additions & 2 deletions lib/block-supports/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function _gutenberg_add_block_level_presets_class( $block_content, $block ) {
}

// return early if no settings are found on the block attributes.
$block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null );
$block_settings = $block['attrs']['settings'] ?? null;
if ( empty( $block_settings ) ) {
return $block_content;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ function _gutenberg_add_block_level_preset_styles( $pre_render, $block ) {
}

// return early if no settings are found on the block attributes.
$block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null );
$block_settings = $block['attrs']['settings'] ?? null;
if ( empty( $block_settings ) ) {
return null;
}
Expand Down
19 changes: 13 additions & 6 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
return $attributes;
}

$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
$spacing_block_styles = array();
$spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
$spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null;
$styles = gutenberg_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
$spacing_block_styles = array(
'padding' => null,
'margin' => null,
);
if ( $has_padding_support && ! $skip_padding ) {
$spacing_block_styles['padding'] = $block_styles['spacing']['padding'] ?? null;
}
if ( $has_margin_support && ! $skip_margin ) {
$spacing_block_styles['margin'] = $block_styles['spacing']['margin'] ?? null;
}
$styles = gutenberg_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );

if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
Expand Down
Loading
Loading