Skip to content

Commit 79c20d8

Browse files
committed
Code Modernization: Replace if statements with null coalescing operator.
Developed in #10703 Follow-up to [61464], [61463], [61457], [61456], [61455], [61454], [61453], [61445], [61444], [61443], [61442], [61436], [61435], [61434], [61403], [61433], [61432], [61431], [61430], [61429], [61424], [61404], [61403]. Props soean, westonruter, mukesh27. See #58874. Fixes #64488. git-svn-id: https://develop.svn.wordpress.org/trunk@61470 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6bdb78e commit 79c20d8

17 files changed

Lines changed: 23 additions & 116 deletions

src/wp-admin/includes/class-wp-list-table.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,7 @@ public function get_pagination_arg( $key ) {
344344
if ( 'page' === $key ) {
345345
return $this->get_pagenum();
346346
}
347-
348-
if ( isset( $this->_pagination_args[ $key ] ) ) {
349-
return $this->_pagination_args[ $key ];
350-
}
351-
352-
return 0;
347+
return $this->_pagination_args[ $key ] ?? 0;
353348
}
354349

355350
/**

src/wp-admin/includes/class-wp-screen.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,7 @@ public function get_option( $option, $key = false ) {
554554
return null;
555555
}
556556
if ( $key ) {
557-
if ( isset( $this->_options[ $option ][ $key ] ) ) {
558-
return $this->_options[ $option ][ $key ];
559-
}
560-
return null;
557+
return $this->_options[ $option ][ $key ] ?? null;
561558
}
562559
return $this->_options[ $option ];
563560
}

src/wp-admin/includes/file.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,11 +1097,7 @@ function wp_handle_upload( &$file, $overrides = false, $time = null ) {
10971097
* $_POST['action'] must be set and its value must equal $overrides['action']
10981098
* or this:
10991099
*/
1100-
$action = 'wp_handle_upload';
1101-
if ( isset( $overrides['action'] ) ) {
1102-
$action = $overrides['action'];
1103-
}
1104-
1100+
$action = $overrides['action'] ?? 'wp_handle_upload';
11051101
return _wp_handle_upload( $file, $overrides, $time, $action );
11061102
}
11071103

@@ -1128,11 +1124,7 @@ function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
11281124
* $_POST['action'] must be set and its value must equal $overrides['action']
11291125
* or this:
11301126
*/
1131-
$action = 'wp_handle_sideload';
1132-
if ( isset( $overrides['action'] ) ) {
1133-
$action = $overrides['action'];
1134-
}
1135-
1127+
$action = $overrides['action'] ?? 'wp_handle_sideload';
11361128
return _wp_handle_upload( $file, $overrides, $time, $action );
11371129
}
11381130

src/wp-admin/includes/plugin.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,44 +1971,25 @@ function get_admin_page_parent( $parent_page = '' ) {
19711971
$plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
19721972

19731973
if ( ! empty( $parent_page ) && 'admin.php' !== $parent_page ) {
1974-
if ( isset( $_wp_real_parent_file[ $parent_page ] ) ) {
1975-
$parent_page = $_wp_real_parent_file[ $parent_page ];
1976-
}
1977-
1978-
return $parent_page;
1974+
return $_wp_real_parent_file[ $parent_page ] ?? $parent_page;
19791975
}
19801976

19811977
if ( 'admin.php' === $pagenow && isset( $plugin_page ) ) {
19821978
foreach ( (array) $menu as $parent_menu ) {
19831979
if ( $parent_menu[2] === $plugin_page ) {
19841980
$parent_file = $plugin_page;
1985-
1986-
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
1987-
$parent_file = $_wp_real_parent_file[ $parent_file ];
1988-
}
1989-
1990-
return $parent_file;
1981+
return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
19911982
}
19921983
}
19931984
if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) ) {
19941985
$parent_file = $plugin_page;
1995-
1996-
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
1997-
$parent_file = $_wp_real_parent_file[ $parent_file ];
1998-
}
1999-
2000-
return $parent_file;
1986+
return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
20011987
}
20021988
}
20031989

20041990
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) {
20051991
$parent_file = $pagenow;
2006-
2007-
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
2008-
$parent_file = $_wp_real_parent_file[ $parent_file ];
2009-
}
2010-
2011-
return $parent_file;
1992+
return $_wp_real_parent_file[ $parent_file ] ?? $parent_file;
20121993
}
20131994

20141995
foreach ( array_keys( (array) $submenu ) as $parent_page ) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ public function get_all_registered() {
170170
* @return array[] Array whose keys are block style names and whose values are block style properties.
171171
*/
172172
public function get_registered_styles_for_block( $block_name ) {
173-
if ( isset( $this->registered_block_styles[ $block_name ] ) ) {
174-
return $this->registered_block_styles[ $block_name ];
175-
}
176-
return array();
173+
return $this->registered_block_styles[ $block_name ] ?? array();
177174
}
178175

179176
/**

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,7 @@ public function add_child( WP_Comment $child ) {
323323
* @return WP_Comment|false Returns the comment object if found, otherwise false.
324324
*/
325325
public function get_child( $child_id ) {
326-
if ( isset( $this->children[ $child_id ] ) ) {
327-
return $this->children[ $child_id ];
328-
}
329-
330-
return false;
326+
return $this->children[ $child_id ] ?? false;
331327
}
332328

333329
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,7 @@ public function query( $handle, $status = 'registered' ) {
477477
switch ( $status ) {
478478
case 'registered':
479479
case 'scripts': // Back compat.
480-
if ( isset( $this->registered[ $handle ] ) ) {
481-
return $this->registered[ $handle ];
482-
}
483-
return false;
480+
return $this->registered[ $handle ] ?? false;
484481

485482
case 'enqueued':
486483
case 'queue': // Back compat.

src/wp-includes/class-wp-plugin-dependencies.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ public static function get_dependents( $slug ) {
200200
* @return array An array of dependency plugin slugs.
201201
*/
202202
public static function get_dependencies( $plugin_file ) {
203-
if ( isset( self::$dependencies[ $plugin_file ] ) ) {
204-
return self::$dependencies[ $plugin_file ];
205-
}
206-
207-
return array();
203+
return self::$dependencies[ $plugin_file ] ?? array();
208204
}
209205

210206
/**
@@ -354,12 +350,7 @@ public static function get_dependency_filepath( $slug ) {
354350
*/
355351
public static function get_dependency_data( $slug ) {
356352
$dependency_api_data = self::get_dependency_api_data();
357-
358-
if ( isset( $dependency_api_data[ $slug ] ) ) {
359-
return $dependency_api_data[ $slug ];
360-
}
361-
362-
return false;
353+
return $dependency_api_data[ $slug ] ?? false;
363354
}
364355

365356
/**

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,11 +1860,7 @@ public function set_404() {
18601860
* @return mixed Contents of the query variable.
18611861
*/
18621862
public function get( $query_var, $default_value = '' ) {
1863-
if ( isset( $this->query_vars[ $query_var ] ) ) {
1864-
return $this->query_vars[ $query_var ];
1865-
}
1866-
1867-
return $default_value;
1863+
return $this->query_vars[ $query_var ] ?? $default_value;
18681864
}
18691865

18701866
/**
@@ -4066,12 +4062,7 @@ public function get_queried_object() {
40664062
*/
40674063
public function get_queried_object_id() {
40684064
$this->get_queried_object();
4069-
4070-
if ( isset( $this->queried_object_id ) ) {
4071-
return $this->queried_object_id;
4072-
}
4073-
4074-
return 0;
4065+
return $this->queried_object_id ?? 0;
40754066
}
40764067

40774068
/**

src/wp-includes/class-wp-user-meta-session-tokens.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ protected function prepare_session( $session ) {
6060
*/
6161
protected function get_session( $verifier ) {
6262
$sessions = $this->get_sessions();
63-
64-
if ( isset( $sessions[ $verifier ] ) ) {
65-
return $sessions[ $verifier ];
66-
}
67-
68-
return null;
63+
return $sessions[ $verifier ] ?? null;
6964
}
7065

7166
/**

0 commit comments

Comments
 (0)