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 use of _wp_array_get #5244

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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 src/wp-includes/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ function wp_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;
aristath marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not yet consensus to use ?? in Core. See https://core.trac.wordpress.org/ticket/58874. IMHO I think this should not yet to be introduced until its Trac ticket has consensus to move forward. Then the work here can be updated as part of that ticket.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback @hellofromtonya!

That ticket is about converting all existing isset( $var ) ? $var : 'default' checks in Core to use ?? where appropriate. It has nothing to do with this PR - which removes unnecessary calls to _wp_array_get. It should not be a part of that ticket... the fact that both PRs use ?? is conincidental. We could convert this PR to use isset() checks and it would be just as valuable and valid as it is now.

The fact that we don't yet use ?? anywhere in Core does not mean that it's not allowed, or that it should not be used. When WordPress had to support PHP 5.6 we could not use it because we had to be compatible with that PHP version. However, "could not use" is not the same as "WordPress does not allow".

The ??operator is a part of the PHP language - just like == or &&. The only difference is the supported PHP versions, something which is no longer blocking us.

aristath marked this conversation as resolved.
Show resolved Hide resolved
$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 src/wp-includes/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ function wp_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;
}

// Generates 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 @@ -152,11 +152,11 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
*/
function wp_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' ) ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$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 src/wp-includes/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_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' ) ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$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 @@ -74,7 +81,7 @@ function wp_register_colors_support( $block_type ) {
* @return array Colors CSS classes and inline styles.
*/
function wp_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 @@ -83,29 +90,33 @@ function wp_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.
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 src/wp-includes/block-supports/dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpc

$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 = wp_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 = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );

if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
continue;
}

$element_style_object = _wp_array_get( $element_block_styles, array( $element_type ), null );
$element_style_object = $element_block_styles[ $element_type ] ?? null;

// Process primary element type styles.
if ( $element_style_object ) {
Expand All @@ -201,7 +201,7 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
// Process related elements e.g. h1-h6 for headings.
if ( isset( $element_config['elements'] ) ) {
foreach ( $element_config['elements'] as $element ) {
$element_style_object = _wp_array_get( $element_block_styles, array( $element ), null );
$element_style_object = $element_block_styles[ $element ] ?? null;

if ( $element_style_object ) {
wp_style_engine_get_styles(
Expand Down
33 changes: 21 additions & 12 deletions src/wp-includes/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function wp_register_layout_support( $block_type ) {
* @return string CSS styles on success. Else, empty string.
*/
function wp_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 @@ -414,7 +414,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
$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 @@ -495,7 +498,10 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
$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 @@ -598,8 +604,11 @@ function wp_render_layout_support_flag( $block_content, $block ) {
}

$global_settings = wp_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 = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;

$class_names = array();
$layout_definitions = wp_get_layout_definitions();
Expand All @@ -611,7 +620,7 @@ function wp_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 &&
Expand Down Expand Up @@ -641,9 +650,9 @@ function wp_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 @@ -656,7 +665,7 @@ function wp_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.
* Regex for CSS value borrowed from `safecss_filter_attr`, and used here
Expand All @@ -670,16 +679,16 @@ function wp_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 = wp_get_layout_style(
Expand Down
10 changes: 5 additions & 5 deletions src/wp-includes/block-supports/position.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function wp_render_position_support( $block_content, $block ) {
}

$global_settings = wp_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 @@ -63,11 +63,11 @@ function wp_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 @@ -78,7 +78,7 @@ function wp_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 src/wp-includes/block-supports/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function _wp_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 @@ -82,7 +82,7 @@ function _wp_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 src/wp-includes/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ function wp_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 = wp_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 = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );

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