Skip to content

Commit 14ada27

Browse files
westonruterMamaduka
authored andcommitted
Code Modernization: Use null coalescing operator in place of isset() in ternaries. (#74335)
Co-authored-by: westonruter <[email protected]> Co-authored-by: Mamaduka <[email protected]>
1 parent 69254a5 commit 14ada27

File tree

36 files changed

+102
-138
lines changed

36 files changed

+102
-138
lines changed

lib/block-editor-settings.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,15 @@ function gutenberg_get_block_editor_settings( $settings ) {
7575
// These settings may need to be updated based on data coming from theme.json sources.
7676
if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
7777
$colors_by_origin = $settings['__experimentalFeatures']['color']['palette'];
78-
$settings['colors'] = isset( $colors_by_origin['custom'] ) ?
79-
$colors_by_origin['custom'] : (
80-
isset( $colors_by_origin['theme'] ) ?
81-
$colors_by_origin['theme'] :
82-
$colors_by_origin['default']
83-
);
78+
$settings['colors'] = $colors_by_origin['custom'] ?? $colors_by_origin['theme'] ?? $colors_by_origin['default'];
8479
}
8580
if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) {
8681
$gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients'];
87-
$settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
88-
$gradients_by_origin['custom'] : (
89-
isset( $gradients_by_origin['theme'] ) ?
90-
$gradients_by_origin['theme'] :
91-
$gradients_by_origin['default']
92-
);
82+
$settings['gradients'] = $gradients_by_origin['custom'] ?? $gradients_by_origin['theme'] ?? $gradients_by_origin['default'];
9383
}
9484
if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
9585
$font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes'];
96-
$settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
97-
$font_sizes_by_origin['custom'] : (
98-
isset( $font_sizes_by_origin['theme'] ) ?
99-
$font_sizes_by_origin['theme'] :
100-
$font_sizes_by_origin['default']
101-
);
86+
$settings['fontSizes'] = $font_sizes_by_origin['custom'] ?? $font_sizes_by_origin['theme'] ?? $font_sizes_by_origin['default'];
10287
}
10388
if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) {
10489
$settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom'];
@@ -131,12 +116,7 @@ function gutenberg_get_block_editor_settings( $settings ) {
131116

132117
if ( isset( $settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) {
133118
$spacing_sizes_by_origin = $settings['__experimentalFeatures']['spacing']['spacingSizes'];
134-
$settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ?
135-
$spacing_sizes_by_origin['custom'] : (
136-
isset( $spacing_sizes_by_origin['theme'] ) ?
137-
$spacing_sizes_by_origin['theme'] :
138-
$spacing_sizes_by_origin['default']
139-
);
119+
$settings['spacingSizes'] = $spacing_sizes_by_origin['custom'] ?? $spacing_sizes_by_origin['theme'] ?? $spacing_sizes_by_origin['default'];
140120
}
141121

142122
return $settings;

lib/block-supports/layout.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
228228
if ( 'default' === $layout_type ) {
229229
if ( $has_block_gap_support ) {
230230
if ( is_array( $gap_value ) ) {
231-
$gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
231+
$gap_value = $gap_value['top'] ?? null;
232232
}
233233
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
234234
// Get spacing CSS variable from preset value if provided.
@@ -258,9 +258,9 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
258258
}
259259
}
260260
} elseif ( 'constrained' === $layout_type ) {
261-
$content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : '';
262-
$wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : '';
263-
$justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'center';
261+
$content_size = $layout['contentSize'] ?? '';
262+
$wide_size = $layout['wideSize'] ?? '';
263+
$justify_content = $layout['justifyContent'] ?? 'center';
264264

265265
$all_max_width_value = $content_size ? $content_size : $wide_size;
266266
$wide_max_width_value = $wide_size ? $wide_size : $content_size;
@@ -345,7 +345,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
345345

346346
if ( $has_block_gap_support ) {
347347
if ( is_array( $gap_value ) ) {
348-
$gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
348+
$gap_value = $gap_value['top'] ?? null;
349349
}
350350
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
351351
// Get spacing CSS variable from preset value if provided.
@@ -375,7 +375,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
375375
}
376376
}
377377
} elseif ( 'flex' === $layout_type ) {
378-
$layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal';
378+
$layout_orientation = $layout['orientation'] ?? 'horizontal';
379379

380380
$justify_content_options = array(
381381
'left' => 'flex-start',
@@ -651,7 +651,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
651651
$child_layout_declarations = array();
652652
$child_layout_styles = array();
653653

654-
$self_stretch = isset( $block['attrs']['style']['layout']['selfStretch'] ) ? $block['attrs']['style']['layout']['selfStretch'] : null;
654+
$self_stretch = $block['attrs']['style']['layout']['selfStretch'] ?? null;
655655

656656
if ( 'fixed' === $self_stretch && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
657657
$child_layout_declarations['flex-basis'] = $block['attrs']['style']['layout']['flexSize'];
@@ -660,8 +660,8 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
660660
$child_layout_declarations['flex-grow'] = '1';
661661
}
662662

663-
$column_start = isset( $block['attrs']['style']['layout']['columnStart'] ) ? $block['attrs']['style']['layout']['columnStart'] : null;
664-
$column_span = isset( $block['attrs']['style']['layout']['columnSpan'] ) ? $block['attrs']['style']['layout']['columnSpan'] : null;
663+
$column_start = $block['attrs']['style']['layout']['columnStart'] ?? null;
664+
$column_span = $block['attrs']['style']['layout']['columnSpan'] ?? null;
665665
if ( $column_start && $column_span ) {
666666
$child_layout_declarations['grid-column'] = "$column_start / span $column_span";
667667
} elseif ( $column_start ) {
@@ -670,8 +670,8 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
670670
$child_layout_declarations['grid-column'] = "span $column_span";
671671
}
672672

673-
$row_start = isset( $block['attrs']['style']['layout']['rowStart'] ) ? $block['attrs']['style']['layout']['rowStart'] : null;
674-
$row_span = isset( $block['attrs']['style']['layout']['rowSpan'] ) ? $block['attrs']['style']['layout']['rowSpan'] : null;
673+
$row_start = $block['attrs']['style']['layout']['rowStart'] ?? null;
674+
$row_span = $block['attrs']['style']['layout']['rowSpan'] ?? null;
675675
if ( $row_start && $row_span ) {
676676
$child_layout_declarations['grid-row'] = "$row_start / span $row_span";
677677
} elseif ( $row_start ) {
@@ -685,8 +685,8 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
685685
'declarations' => $child_layout_declarations,
686686
);
687687

688-
$minimum_column_width = isset( $block['parentLayout']['minimumColumnWidth'] ) ? $block['parentLayout']['minimumColumnWidth'] : null;
689-
$column_count = isset( $block['parentLayout']['columnCount'] ) ? $block['parentLayout']['columnCount'] : null;
688+
$minimum_column_width = $block['parentLayout']['minimumColumnWidth'] ?? null;
689+
$column_count = $block['parentLayout']['columnCount'] ?? null;
690690

691691
/*
692692
* If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
@@ -1051,7 +1051,7 @@ function ( $parsed_block, $source_block, $parent_block ) {
10511051
* @return string Filtered block content.
10521052
*/
10531053
function gutenberg_restore_group_inner_container( $block_content, $block ) {
1054-
$tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div';
1054+
$tag_name = $block['attrs']['tagName'] ?? 'div';
10551055
$group_with_inner_container_regex = sprintf(
10561056
'/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
10571057
preg_quote( $tag_name, '/' )

lib/block-supports/spacing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
4646
$attributes = array();
4747
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
4848
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
49-
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
49+
$block_styles = $block_attributes['style'] ?? null;
5050

5151
if ( ! $block_styles ) {
5252
return $attributes;

lib/block-supports/typography.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
119119
$typography_block_styles = array();
120120
if ( $has_font_size_support && ! $should_skip_font_size ) {
121121
$preset_font_size = array_key_exists( 'fontSize', $block_attributes ) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null;
122-
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null;
122+
$custom_font_size = $block_attributes['style']['typography']['fontSize'] ?? null;
123123
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : gutenberg_get_typography_font_size_value(
124124
array(
125125
'size' => $custom_font_size,
@@ -382,11 +382,11 @@ function gutenberg_get_typography_value_and_unit( $raw_value, $options = array()
382382
* @return string|null A font-size value using clamp().
383383
*/
384384
function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
385-
$maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null;
386-
$minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null;
387-
$maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null;
388-
$minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null;
389-
$scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null;
385+
$maximum_viewport_width_raw = $args['maximum_viewport_width'] ?? null;
386+
$minimum_viewport_width_raw = $args['minimum_viewport_width'] ?? null;
387+
$maximum_font_size_raw = $args['maximum_font_size'] ?? null;
388+
$minimum_font_size_raw = $args['minimum_font_size'] ?? null;
389+
$scale_factor = $args['scale_factor'] ?? null;
390390

391391
// Normalizes the minimum font size in order to use the value for calculations.
392392
$minimum_font_size = gutenberg_get_typography_value_and_unit( $minimum_font_size_raw );
@@ -395,7 +395,7 @@ function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
395395
* We get a 'preferred' unit to keep units consistent when calculating,
396396
* otherwise the result will not be accurate.
397397
*/
398-
$font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem';
398+
$font_size_unit = $minimum_font_size['unit'] ?? 'rem';
399399

400400
// Grabs the maximum font size and normalize it in order to use the value for calculations.
401401
$maximum_font_size = gutenberg_get_typography_value_and_unit(
@@ -529,8 +529,8 @@ function gutenberg_get_typography_font_size_value( $preset, $settings = array()
529529
return $preset['size'];
530530
}
531531

532-
$fluid_settings = isset( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();
533-
$layout_settings = isset( $settings['layout'] ) ? $settings['layout'] : array();
532+
$fluid_settings = $typography_settings['fluid'] ?? array();
533+
$layout_settings = $settings['layout'] ?? array();
534534

535535
// Defaults.
536536
$default_maximum_viewport_width = '1600px';
@@ -541,7 +541,7 @@ function gutenberg_get_typography_font_size_value( $preset, $settings = array()
541541
$default_minimum_font_size_limit = '14px';
542542

543543
// Defaults overrides.
544-
$minimum_viewport_width = isset( $fluid_settings['minViewportWidth'] ) ? $fluid_settings['minViewportWidth'] : $default_minimum_viewport_width;
544+
$minimum_viewport_width = $fluid_settings['minViewportWidth'] ?? $default_minimum_viewport_width;
545545
$maximum_viewport_width = isset( $layout_settings['wideSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $layout_settings['wideSize'] ) ) ? $layout_settings['wideSize'] : $default_maximum_viewport_width;
546546
if ( isset( $fluid_settings['maxViewportWidth'] ) ) {
547547
$maximum_viewport_width = $fluid_settings['maxViewportWidth'];
@@ -550,8 +550,8 @@ function gutenberg_get_typography_font_size_value( $preset, $settings = array()
550550
$minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit;
551551

552552
// Try to grab explicit min and max fluid font sizes.
553-
$minimum_font_size_raw = isset( $fluid_font_size_settings['min'] ) ? $fluid_font_size_settings['min'] : null;
554-
$maximum_font_size_raw = isset( $fluid_font_size_settings['max'] ) ? $fluid_font_size_settings['max'] : null;
553+
$minimum_font_size_raw = $fluid_font_size_settings['min'] ?? null;
554+
$maximum_font_size_raw = $fluid_font_size_settings['max'] ?? null;
555555

556556
// Font sizes.
557557
$preferred_size = gutenberg_get_typography_value_and_unit( $preset['size'] );

lib/blocks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ function gutenberg_reregister_core_block_types() {
6363
* $script_handle,
6464
* $script_uri,
6565
* $script_dependencies,
66-
* - isset( $script_asset['version'] ) ? $script_asset['version'] : false
67-
* + isset( $script_asset['version'] ) ? $script_asset['version'] : false,
66+
* - $script_asset['version'] ?? false
67+
* + $script_asset['version'] ?? false,
6868
* + array( 'strategy' => 'defer' )
6969
* );
7070
* if ( ! $result ) {

lib/class-wp-rest-global-styles-controller-gutenberg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ public function get_theme_item( $request ) {
595595

596596
if ( rest_is_field_included( 'styles', $fields ) ) {
597597
$raw_data = $theme->get_raw_data();
598-
$data['styles'] = isset( $raw_data['styles'] ) ? $raw_data['styles'] : array();
598+
$data['styles'] = $raw_data['styles'] ?? array();
599599
}
600600

601601
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

lib/class-wp-theme-json-gutenberg.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ public function get_custom_css() {
15701570
*/
15711571
public function get_base_custom_css() {
15721572
_deprecated_function( __METHOD__, 'Gutenberg 18.6.0', 'get_stylesheet' );
1573-
return isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : '';
1573+
return $this->theme_json['styles']['css'] ?? '';
15741574
}
15751575

15761576
/**
@@ -1586,9 +1586,7 @@ public function get_block_custom_css_nodes() {
15861586
// Add the global styles block CSS.
15871587
if ( isset( $this->theme_json['styles']['blocks'] ) ) {
15881588
foreach ( $this->theme_json['styles']['blocks'] as $name => $node ) {
1589-
$custom_block_css = isset( $this->theme_json['styles']['blocks'][ $name ]['css'] )
1590-
? $this->theme_json['styles']['blocks'][ $name ]['css']
1591-
: null;
1589+
$custom_block_css = $this->theme_json['styles']['blocks'][ $name ]['css'] ?? null;
15921590
if ( $custom_block_css ) {
15931591
$block_nodes[] = array(
15941592
'name' => $name,
@@ -1632,8 +1630,8 @@ public function get_custom_templates() {
16321630
foreach ( $this->theme_json['customTemplates'] as $item ) {
16331631
if ( isset( $item['name'] ) ) {
16341632
$custom_templates[ $item['name'] ] = array(
1635-
'title' => isset( $item['title'] ) ? $item['title'] : '',
1636-
'postTypes' => isset( $item['postTypes'] ) ? $item['postTypes'] : array( 'page' ),
1633+
'title' => $item['title'] ?? '',
1634+
'postTypes' => $item['postTypes'] ?? array( 'page' ),
16371635
);
16381636
}
16391637
}
@@ -1656,8 +1654,8 @@ public function get_template_parts() {
16561654
foreach ( $this->theme_json['templateParts'] as $item ) {
16571655
if ( isset( $item['name'] ) ) {
16581656
$template_parts[ $item['name'] ] = array(
1659-
'title' => isset( $item['title'] ) ? $item['title'] : '',
1660-
'area' => isset( $item['area'] ) ? $item['area'] : '',
1657+
'title' => $item['title'] ?? '',
1658+
'area' => $item['area'] ?? '',
16611659
);
16621660
}
16631661
}
@@ -1723,7 +1721,7 @@ protected function get_layout_styles( $block_metadata, $options = array() ) {
17231721
}
17241722
}
17251723

1726-
$selector = isset( $block_metadata['selector'] ) ? $block_metadata['selector'] : '';
1724+
$selector = $block_metadata['selector'] ?? '';
17271725
$has_block_gap_support = isset( $this->theme_json['settings']['spacing']['blockGap'] );
17281726
$has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.
17291727
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
@@ -2977,7 +2975,7 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) {
29772975
$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );
29782976

29792977
// Process pseudo-selectors for this variation (e.g., :hover, :focus).
2980-
$block_name = isset( $block_metadata['name'] ) ? $block_metadata['name'] : ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 3 ? static::get_block_name_from_metadata_path( $block_metadata ) : null );
2978+
$block_name = $block_metadata['name'] ?? ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 3 ? static::get_block_name_from_metadata_path( $block_metadata ) : null );
29812979
$variation_pseudo_declarations = static::process_pseudo_selectors( $style_variation_node, $style_variation['selector'], $settings, $block_name );
29822980
$style_variation_declarations = array_merge( $style_variation_declarations, $variation_pseudo_declarations );
29832981

@@ -3038,7 +3036,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
30383036
)
30393037
);
30403038

3041-
$pseudo_selector = isset( $pseudo_matches[0] ) ? $pseudo_matches[0] : null;
3039+
$pseudo_selector = $pseudo_matches[0] ?? null;
30423040

30433041
/*
30443042
* If the current selector is a pseudo selector that's defined in the allow list for the current
@@ -3055,7 +3053,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
30553053
// For block pseudo-selectors, we need to get the block data first, then access the pseudo-selector.
30563054
$block_name = static::get_block_name_from_metadata_path( $block_metadata ); // 'core/button'
30573055
$block_data = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $block_name ), array() );
3058-
$pseudo_data = isset( $block_data[ $block_pseudo_selector ] ) ? $block_data[ $block_pseudo_selector ] : array();
3056+
$pseudo_data = $block_data[ $block_pseudo_selector ] ?? array();
30593057

30603058
$declarations = static::compute_style_properties( $pseudo_data, $settings, null, $this->theme_json, $selector, $use_root_padding );
30613059
} else {
@@ -3198,9 +3196,9 @@ public function get_root_layout_rules( $selector, $block_metadata, $options = ar
31983196
* as custom properties on the body element so all blocks can use them.
31993197
*/
32003198
if ( isset( $settings['layout']['contentSize'] ) || isset( $settings['layout']['wideSize'] ) ) {
3201-
$content_size = isset( $settings['layout']['contentSize'] ) ? $settings['layout']['contentSize'] : $settings['layout']['wideSize'];
3199+
$content_size = $settings['layout']['contentSize'] ?? $settings['layout']['wideSize'];
32023200
$content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial';
3203-
$wide_size = isset( $settings['layout']['wideSize'] ) ? $settings['layout']['wideSize'] : $settings['layout']['contentSize'];
3201+
$wide_size = $settings['layout']['wideSize'] ?? $settings['layout']['contentSize'];
32043202
$wide_size = static::is_safe_css_declaration( 'max-width', $wide_size ) ? $wide_size : 'initial';
32053203
$css .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';
32063204
$css .= '--wp--style--global--wide-size: ' . $wide_size . '; }';
@@ -4652,8 +4650,8 @@ private static function convert_variables_to_value( $styles, $values ) {
46524650
$fallback,
46534651
),
46544652
array(
4655-
isset( $values[ $key_in_values ] ) ? $values[ $key_in_values ] : $rule_to_replace,
4656-
isset( $values[ $fallback ] ) ? $values[ $fallback ] : $fallback,
4653+
$values[ $key_in_values ] ?? $rule_to_replace,
4654+
$values[ $fallback ] ?? $fallback,
46574655
),
46584656
$resolved_style
46594657
);

0 commit comments

Comments
 (0)