Skip to content

Commit 5ed1e08

Browse files
committed
Code Modernization: Editor: Use null coalescing operator instead of isset() ternaries.
Developed as a subset of #10654 Initially developed in #4886 Follow-up to [61430], [61429], [61424], [61404], [61403]. Props costdev, westonruter. See #58874, #63430. git-svn-id: https://develop.svn.wordpress.org/trunk@61431 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 60c63c2 commit 5ed1e08

12 files changed

+59
-99
lines changed

src/wp-admin/edit-form-advanced.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
*/
7070
$post_ID = isset( $post_ID ) ? (int) $post_ID : 0;
7171
$user_ID = isset( $user_ID ) ? (int) $user_ID : 0;
72-
$action = isset( $action ) ? $action : '';
72+
$action = $action ?? '';
7373

7474
if ( (int) get_option( 'page_for_posts' ) === $post->ID && empty( $post->post_content ) ) {
7575
add_action( 'edit_form_after_title', '_wp_posts_page_notice' );

src/wp-admin/site-editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static function ( $classes ) {
287287

288288
wp_add_inline_script(
289289
'wp-blocks',
290-
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( isset( $editor_settings['blockCategories'] ) ? $editor_settings['blockCategories'] : array(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ),
290+
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $editor_settings['blockCategories'] ?? array(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ),
291291
'after'
292292
);
293293

src/wp-includes/block-editor.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -566,30 +566,15 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
566566
// These settings may need to be updated based on data coming from theme.json sources.
567567
if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
568568
$colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette'];
569-
$editor_settings['colors'] = isset( $colors_by_origin['custom'] ) ?
570-
$colors_by_origin['custom'] : (
571-
isset( $colors_by_origin['theme'] ) ?
572-
$colors_by_origin['theme'] :
573-
$colors_by_origin['default']
574-
);
569+
$editor_settings['colors'] = $colors_by_origin['custom'] ?? $colors_by_origin['theme'] ?? $colors_by_origin['default'];
575570
}
576571
if ( isset( $editor_settings['__experimentalFeatures']['color']['gradients'] ) ) {
577572
$gradients_by_origin = $editor_settings['__experimentalFeatures']['color']['gradients'];
578-
$editor_settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
579-
$gradients_by_origin['custom'] : (
580-
isset( $gradients_by_origin['theme'] ) ?
581-
$gradients_by_origin['theme'] :
582-
$gradients_by_origin['default']
583-
);
573+
$editor_settings['gradients'] = $gradients_by_origin['custom'] ?? $gradients_by_origin['theme'] ?? $gradients_by_origin['default'];
584574
}
585575
if ( isset( $editor_settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
586576
$font_sizes_by_origin = $editor_settings['__experimentalFeatures']['typography']['fontSizes'];
587-
$editor_settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
588-
$font_sizes_by_origin['custom'] : (
589-
isset( $font_sizes_by_origin['theme'] ) ?
590-
$font_sizes_by_origin['theme'] :
591-
$font_sizes_by_origin['default']
592-
);
577+
$editor_settings['fontSizes'] = $font_sizes_by_origin['custom'] ?? $font_sizes_by_origin['theme'] ?? $font_sizes_by_origin['default'];
593578
}
594579
if ( isset( $editor_settings['__experimentalFeatures']['color']['custom'] ) ) {
595580
$editor_settings['disableCustomColors'] = ! $editor_settings['__experimentalFeatures']['color']['custom'];
@@ -622,12 +607,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
622607

623608
if ( isset( $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) {
624609
$spacing_sizes_by_origin = $editor_settings['__experimentalFeatures']['spacing']['spacingSizes'];
625-
$editor_settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ?
626-
$spacing_sizes_by_origin['custom'] : (
627-
isset( $spacing_sizes_by_origin['theme'] ) ?
628-
$spacing_sizes_by_origin['theme'] :
629-
$spacing_sizes_by_origin['default']
630-
);
610+
$editor_settings['spacingSizes'] = $spacing_sizes_by_origin['custom'] ?? $spacing_sizes_by_origin['theme'] ?? $spacing_sizes_by_origin['default'];
631611
}
632612

633613
$editor_settings['__unstableResolvedAssets'] = _wp_get_iframed_editor_assets();

src/wp-includes/block-template-utils.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ function _get_block_templates_files( $template_type, $query = array() ) {
382382
}
383383

384384
// Prepare metadata from $query.
385-
$slugs_to_include = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
386-
$slugs_to_skip = isset( $query['slug__not_in'] ) ? $query['slug__not_in'] : array();
387-
$area = isset( $query['area'] ) ? $query['area'] : null;
388-
$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
385+
$slugs_to_include = $query['slug__in'] ?? array();
386+
$slugs_to_skip = $query['slug__not_in'] ?? array();
387+
$area = $query['area'] ?? null;
388+
$post_type = $query['post_type'] ?? '';
389389

390390
$stylesheet = get_stylesheet();
391391
$template = get_template();
@@ -1115,7 +1115,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11151115
return $templates;
11161116
}
11171117

1118-
$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
1118+
$post_type = $query['post_type'] ?? '';
11191119
$wp_query_args = array(
11201120
'post_status' => array( 'auto-draft', 'draft', 'publish' ),
11211121
'post_type' => $template_type,
@@ -1702,8 +1702,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
17021702
return $changes;
17031703
}
17041704

1705-
$meta = isset( $changes->meta_input ) ? $changes->meta_input : array();
1706-
$terms = isset( $changes->tax_input ) ? $changes->tax_input : array();
1705+
$meta = $changes->meta_input ?? array();
1706+
$terms = $changes->tax_input ?? array();
17071707

17081708
if ( empty( $changes->ID ) ) {
17091709
// There's no post object for this template in the database for this template yet.

src/wp-includes/block-template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,10 @@ function _resolve_template_for_new_post( $wp_query ) {
373373
remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
374374

375375
// Pages.
376-
$page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null;
376+
$page_id = $wp_query->query['page_id'] ?? null;
377377

378378
// Posts, including custom post types.
379-
$p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null;
379+
$p = $wp_query->query['p'] ?? null;
380380

381381
$post_id = $page_id ? $page_id : $p;
382382
$post = get_post( $post_id );

src/wp-includes/blocks.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
171171
$module_uri = get_block_asset_url( $module_path_norm );
172172

173173
$module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
174-
$module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
175-
$block_version = isset( $metadata['version'] ) ? $metadata['version'] : false;
176-
$module_version = isset( $module_asset['version'] ) ? $module_asset['version'] : $block_version;
174+
$module_dependencies = $module_asset['dependencies'] ?? array();
175+
$block_version = $metadata['version'] ?? false;
176+
$module_version = $module_asset['version'] ?? $block_version;
177177

178178
$supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity'];
179179
$is_interactive = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] );
@@ -248,18 +248,17 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
248248

249249
// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
250250
$script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array();
251-
$script_handle = isset( $script_asset['handle'] ) ?
252-
$script_asset['handle'] :
251+
$script_handle = $script_asset['handle'] ??
253252
generate_block_asset_handle( $metadata['name'], $field_name, $index );
254253
if ( wp_script_is( $script_handle, 'registered' ) ) {
255254
return $script_handle;
256255
}
257256

258257
$script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
259258
$script_uri = get_block_asset_url( $script_path_norm );
260-
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
261-
$block_version = isset( $metadata['version'] ) ? $metadata['version'] : false;
262-
$script_version = isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version;
259+
$script_dependencies = $script_asset['dependencies'] ?? array();
260+
$block_version = $metadata['version'] ?? false;
261+
$script_version = $script_asset['version'] ?? $block_version;
263262
$script_args = array();
264263
if ( 'viewScript' === $field_name && $script_uri ) {
265264
$script_args['strategy'] = 'defer';
@@ -1068,9 +1067,7 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po
10681067
}
10691068
}
10701069

1071-
$previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
1072-
? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
1073-
: array();
1070+
$previously_ignored_hooked_blocks = $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
10741071

10751072
$parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
10761073
array_merge(
@@ -1404,9 +1401,7 @@ function update_ignored_hooked_blocks_postmeta( $post ) {
14041401
$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
14051402
$root_block = parse_blocks( $serialized_block )[0];
14061403

1407-
$ignored_hooked_blocks = isset( $root_block['attrs']['metadata']['ignoredHookedBlocks'] )
1408-
? $root_block['attrs']['metadata']['ignoredHookedBlocks']
1409-
: array();
1404+
$ignored_hooked_blocks = $root_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
14101405

14111406
if ( ! empty( $ignored_hooked_blocks ) ) {
14121407
$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
@@ -1823,7 +1818,7 @@ function traverse_and_serialize_block( $block, $pre_callback = null, $post_callb
18231818
}
18241819

18251820
$block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
1826-
$block_content .= isset( $post_markup ) ? $post_markup : '';
1821+
$block_content .= $post_markup ?? '';
18271822

18281823
++$block_index;
18291824
}
@@ -1983,7 +1978,7 @@ function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_cal
19831978
}
19841979

19851980
$result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
1986-
$result .= isset( $post_markup ) ? $post_markup : '';
1981+
$result .= $post_markup ?? '';
19871982
}
19881983

19891984
return $result;
@@ -2598,7 +2593,7 @@ function wp_migrate_old_typography_shape( $metadata ) {
25982593
);
25992594

26002595
foreach ( $typography_keys as $typography_key ) {
2601-
$support_for_key = isset( $metadata['supports'][ $typography_key ] ) ? $metadata['supports'][ $typography_key ] : null;
2596+
$support_for_key = $metadata['supports'][ $typography_key ] ?? null;
26022597

26032598
if ( null !== $support_for_key ) {
26042599
_doing_it_wrong(

src/wp-includes/class-wp-block-metadata-registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static function get_metadata( $file_or_folder ) {
178178
// Get the block name from the path.
179179
$block_name = self::default_identifier_callback( $file_or_folder );
180180

181-
return isset( $collection['metadata'][ $block_name ] ) ? $collection['metadata'][ $block_name ] : null;
181+
return $collection['metadata'][ $block_name ] ?? null;
182182
}
183183

184184
/**

src/wp-includes/class-wp-block-templates-registry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ public function register( $template_name, $args = array() ) {
7575
$template->theme = $theme_name;
7676
$template->plugin = $plugin;
7777
$template->author = null;
78-
$template->content = isset( $args['content'] ) ? $args['content'] : '';
78+
$template->content = $args['content'] ?? '';
7979
$template->source = 'plugin';
8080
$template->slug = $slug;
8181
$template->type = 'wp_template';
82-
$template->title = isset( $args['title'] ) ? $args['title'] : $template_name;
83-
$template->description = isset( $args['description'] ) ? $args['description'] : '';
82+
$template->title = $args['title'] ?? $template_name;
83+
$template->description = $args['description'] ?? '';
8484
$template->status = 'publish';
8585
$template->origin = 'plugin';
8686
$template->is_custom = ! isset( $default_template_types[ $template_name ] );
87-
$template->post_types = isset( $args['post_types'] ) ? $args['post_types'] : array();
87+
$template->post_types = $args['post_types'] ?? array();
8888
}
8989

9090
$this->registered_templates[ $template_name ] = $template;

src/wp-includes/class-wp-block-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public function __get( $name ) {
383383
if ( count( $this->{$new_name} ) > 1 ) {
384384
return $this->{$new_name};
385385
}
386-
return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
386+
return $this->{$new_name}[0] ?? null;
387387
}
388388

389389
/**

src/wp-includes/class-wp-block.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ public function refresh_parsed_block_dependents() {
222222
*/
223223
public function __get( $name ) {
224224
if ( 'attributes' === $name ) {
225-
$this->attributes = isset( $this->parsed_block['attrs'] ) ?
226-
$this->parsed_block['attrs'] :
225+
$this->attributes = $this->parsed_block['attrs'] ??
227226
array();
228227

229228
if ( ! is_null( $this->block_type ) ) {
@@ -312,9 +311,7 @@ private function process_block_bindings() {
312311
*/
313312
foreach ( $supported_block_attributes as $attribute_name ) {
314313
// Retain any non-pattern override bindings that might be present.
315-
$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
316-
? $bindings[ $attribute_name ]
317-
: array( 'source' => 'core/pattern-overrides' );
314+
$updated_bindings[ $attribute_name ] = $bindings[ $attribute_name ] ?? array( 'source' => 'core/pattern-overrides' );
318315
}
319316
$bindings = $updated_bindings;
320317
/*

0 commit comments

Comments
 (0)