Skip to content

Commit fb52a40

Browse files
committed
Code Modernization: Taxonomy, Posts/Post Types, Options/Meta APIs, Query, General: Use null coalescing operator instead of isset() ternaries.
Developed as a subset of #10654 Initially developed in #4886 Follow-up to [61444], [61443], [61442], [61436], [61435], [61434], [61403], [61433], [61432], [61431], [61430], [61429], [61424], [61404], [61403]. Props costdev, westonruter. See #58874, #63430. git-svn-id: https://develop.svn.wordpress.org/trunk@61445 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 14b647c commit fb52a40

File tree

12 files changed

+38
-41
lines changed

12 files changed

+38
-41
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct( $args = array() ) {
7676
parent::__construct(
7777
array(
7878
'plural' => 'posts',
79-
'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
79+
'screen' => $args['screen'] ?? null,
8080
)
8181
);
8282

@@ -531,7 +531,7 @@ protected function formats_dropdown( $post_type ) {
531531
return;
532532
}
533533

534-
$displayed_post_format = isset( $_GET['post_format'] ) ? $_GET['post_format'] : '';
534+
$displayed_post_format = $_GET['post_format'] ?? '';
535535
?>
536536
<label for="filter-by-format" class="screen-reader-text">
537537
<?php
@@ -1264,7 +1264,7 @@ public function column_comments( $post ) {
12641264
?>
12651265
<div class="post-com-count-wrapper">
12661266
<?php
1267-
$pending_comments = isset( $this->comment_pending_count[ $post->ID ] ) ? $this->comment_pending_count[ $post->ID ] : 0;
1267+
$pending_comments = $this->comment_pending_count[ $post->ID ] ?? 0;
12681268

12691269
$this->comments_bubble( $post->ID, $pending_comments );
12701270
?>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct( $args = array() ) {
4141
array(
4242
'plural' => 'tags',
4343
'singular' => 'tag',
44-
'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
44+
'screen' => $args['screen'] ?? null,
4545
)
4646
);
4747

src/wp-admin/includes/post.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ function edit_post( $post_data = null ) {
429429
}
430430
}
431431

432-
$attachment_data = isset( $post_data['attachments'][ $post_id ] ) ? $post_data['attachments'][ $post_id ] : array();
432+
$attachment_data = $post_data['attachments'][ $post_id ] ?? array();
433433

434434
/** This filter is documented in wp-admin/includes/media.php */
435435
$translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
@@ -1014,7 +1014,7 @@ function add_meta( $post_id ) {
10141014

10151015
$metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
10161016
$metakeyinput = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
1017-
$metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : '';
1017+
$metavalue = $_POST['metavalue'] ?? '';
10181018
if ( is_string( $metavalue ) ) {
10191019
$metavalue = trim( $metavalue );
10201020
}
@@ -2043,7 +2043,7 @@ function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
20432043
* Ignoring sanitization to avoid altering meta. Ignoring the nonce check because
20442044
* this is hooked on inner core hooks where a valid nonce was already checked.
20452045
*/
2046-
$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;
2046+
$posted_data = $_POST['data']['wp_autosave'] ?? $_POST;
20472047

20482048
$post_type = get_post_type( $new_autosave['post_parent'] );
20492049

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' )
619619
$clause['alias'] = $alias;
620620

621621
// Determine the data type.
622-
$_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
622+
$_meta_type = $clause['type'] ?? '';
623623
$meta_type = $this->get_cast_for_type( $_meta_type );
624624
$clause['cast'] = $meta_type;
625625

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function __get( $key ) {
236236
$data = new stdClass();
237237
$columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' );
238238
foreach ( $columns as $column ) {
239-
$data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
239+
$data->{$column} = $this->{$column} ?? null;
240240
}
241241

242242
return sanitize_term( $data, $data->taxonomy, 'raw' );

src/wp-includes/class-wp.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function parse_request( $extra_query_vars = '' ) {
167167
$error = '404';
168168
$this->did_permalink = true;
169169

170-
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
170+
$pathinfo = $_SERVER['PATH_INFO'] ?? '';
171171
list( $pathinfo ) = explode( '?', $pathinfo );
172172
$pathinfo = str_replace( '%', '%25', $pathinfo );
173173

@@ -539,7 +539,7 @@ public function send_headers() {
539539
}
540540

541541
if ( is_singular() ) {
542-
$post = isset( $wp_query->post ) ? $wp_query->post : null;
542+
$post = $wp_query->post ?? null;
543543

544544
// Only set X-Pingback for single posts that allow pings.
545545
if ( $post && pings_open( $post ) ) {
@@ -669,7 +669,7 @@ public function register_globals() {
669669

670670
$GLOBALS['query_string'] = $this->query_string;
671671
$GLOBALS['posts'] = & $wp_query->posts;
672-
$GLOBALS['post'] = isset( $wp_query->post ) ? $wp_query->post : null;
672+
$GLOBALS['post'] = $wp_query->post ?? null;
673673
$GLOBALS['request'] = $wp_query->request;
674674

675675
if ( $wp_query->is_single() || $wp_query->is_page() ) {
@@ -755,7 +755,7 @@ public function handle_404() {
755755
$content_found = true;
756756

757757
if ( is_singular() ) {
758-
$post = isset( $wp_query->post ) ? $wp_query->post : null;
758+
$post = $wp_query->post ?? null;
759759
$next = '<!--nextpage-->';
760760

761761
// Check for paged content that exceeds the max number of pages.

src/wp-includes/functions.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ function do_enclose( $content, $post ) {
952952
$headers = wp_get_http_headers( $url );
953953
if ( $headers ) {
954954
$len = isset( $headers['Content-Length'] ) ? (int) $headers['Content-Length'] : 0;
955-
$type = isset( $headers['Content-Type'] ) ? $headers['Content-Type'] : '';
955+
$type = $headers['Content-Type'] ?? '';
956956
$allowed_types = array( 'video', 'audio' );
957957

958958
// Check to see if we can figure out the mime type from the extension.
@@ -3690,7 +3690,7 @@ function wp_nonce_ays( $action ) {
36903690
get_bloginfo( 'name' )
36913691
);
36923692

3693-
$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
3693+
$redirect_to = $_REQUEST['redirect_to'] ?? '';
36943694

36953695
$html = $title;
36963696
$html .= '</p><p>';
@@ -6139,8 +6139,8 @@ function wp_trigger_error( $function_name, $message, $error_level = E_USER_NOTIC
61396139
* @return bool Whether the server is running lighttpd < 1.5.0.
61406140
*/
61416141
function is_lighttpd_before_150() {
6142-
$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '' );
6143-
$server_parts[1] = isset( $server_parts[1] ) ? $server_parts[1] : '';
6142+
$server_parts = explode( '/', $_SERVER['SERVER_SOFTWARE'] ?? '' );
6143+
$server_parts[1] = $server_parts[1] ?? '';
61446144

61456145
return ( 'lighttpd' === $server_parts[0] && -1 === version_compare( $server_parts[1], '1.5.0' ) );
61466146
}
@@ -7121,9 +7121,9 @@ function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = ar
71217121
while (
71227122
$tortoise
71237123
&&
7124-
( $evanescent_hare = isset( $override[ $hare ] ) ? $override[ $hare ] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
7124+
( $evanescent_hare = $override[ $hare ] ?? call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
71257125
&&
7126-
( $hare = isset( $override[ $evanescent_hare ] ) ? $override[ $evanescent_hare ] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
7126+
( $hare = $override[ $evanescent_hare ] ?? call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
71277127
) {
71287128
if ( $_return_loop ) {
71297129
$return[ $tortoise ] = true;
@@ -7137,7 +7137,7 @@ function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = ar
71377137
}
71387138

71397139
// Increment tortoise by one step.
7140-
$tortoise = isset( $override[ $tortoise ] ) ? $override[ $tortoise ] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
7140+
$tortoise = $override[ $tortoise ] ?? call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
71417141
}
71427142

71437143
return false;
@@ -7266,7 +7266,7 @@ function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pr
72667266
if ( in_array( $call['function'], array( 'do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array' ), true ) ) {
72677267
$caller[] = "{$call['function']}('{$call['args'][0]}')";
72687268
} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ), true ) ) {
7269-
$filename = isset( $call['args'][0] ) ? $call['args'][0] : '';
7269+
$filename = $call['args'][0] ?? '';
72707270
$caller[] = $call['function'] . "('" . str_replace( $truncate_paths, '', wp_normalize_path( $filename ) ) . "')";
72717271
} else {
72727272
$caller[] = $call['function'];

src/wp-includes/option.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ function wp_user_settings() {
17551755
function get_user_setting( $name, $default_value = false ) {
17561756
$all_user_settings = get_all_user_settings();
17571757

1758-
return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default_value;
1758+
return $all_user_settings[ $name ] ?? $default_value;
17591759
}
17601760

17611761
/**

src/wp-includes/post-template.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ function the_title_attribute( $args = '' ) {
118118
function get_the_title( $post = 0 ) {
119119
$post = get_post( $post );
120120

121-
$post_title = isset( $post->post_title ) ? $post->post_title : '';
122-
$post_id = isset( $post->ID ) ? $post->ID : 0;
121+
$post_title = $post->post_title ?? '';
122+
$post_id = $post->ID ?? 0;
123123

124124
if ( ! is_admin() ) {
125125
if ( ! empty( $post->post_password ) ) {
@@ -191,7 +191,7 @@ function the_guid( $post = 0 ) {
191191
$post = get_post( $post );
192192

193193
$post_guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
194-
$post_id = isset( $post->ID ) ? $post->ID : 0;
194+
$post_id = $post->ID ?? 0;
195195

196196
/**
197197
* Filters the escaped Global Unique Identifier (guid) of the post.
@@ -221,8 +221,8 @@ function the_guid( $post = 0 ) {
221221
function get_the_guid( $post = 0 ) {
222222
$post = get_post( $post );
223223

224-
$post_guid = isset( $post->guid ) ? $post->guid : '';
225-
$post_id = isset( $post->ID ) ? $post->ID : 0;
224+
$post_guid = $post->guid ?? '';
225+
$post_id = $post->ID ?? 0;
226226

227227
/**
228228
* Filters the Global Unique Identifier (guid) of the post.

src/wp-includes/post.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,10 +2201,7 @@ function _get_custom_object_labels( $data_object, $nohier_vs_hier_defaults ) {
22012201
}
22022202

22032203
if ( ! isset( $data_object->labels['name_admin_bar'] ) ) {
2204-
$data_object->labels['name_admin_bar'] =
2205-
isset( $data_object->labels['singular_name'] )
2206-
? $data_object->labels['singular_name']
2207-
: $data_object->name;
2204+
$data_object->labels['name_admin_bar'] = $data_object->labels['singular_name'] ?? $data_object->name;
22082205
}
22092206

22102207
if ( ! isset( $data_object->labels['menu_name'] ) && isset( $data_object->labels['name'] ) ) {
@@ -2856,7 +2853,7 @@ function get_post_custom_values( $key = '', $post_id = 0 ) {
28562853

28572854
$custom = get_post_custom( $post_id );
28582855

2859-
return isset( $custom[ $key ] ) ? $custom[ $key ] : null;
2856+
return $custom[ $key ] ?? null;
28602857
}
28612858

28622859
/**
@@ -4730,11 +4727,11 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
47304727

47314728
// These variables are needed by compact() later.
47324729
$post_content_filtered = $postarr['post_content_filtered'];
4733-
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
4730+
$post_author = $postarr['post_author'] ?? $user_id;
47344731
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
47354732
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
4736-
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
4737-
$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
4733+
$pinged = $postarr['pinged'] ?? '';
4734+
$import_id = $postarr['import_id'] ?? 0;
47384735

47394736
/*
47404737
* The 'wp_insert_post_parent' filter expects all variables to be present.
@@ -4746,7 +4743,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
47464743
$menu_order = 0;
47474744
}
47484745

4749-
$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
4746+
$post_password = $postarr['post_password'] ?? '';
47504747
if ( 'private' === $post_status ) {
47514748
$post_password = '';
47524749
}
@@ -4815,7 +4812,7 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
48154812
$post_name = wp_unique_post_slug( $post_name, $post_id, $post_status, $post_type, $post_parent );
48164813

48174814
// Don't unslash.
4818-
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
4815+
$post_mime_type = $postarr['post_mime_type'] ?? '';
48194816

48204817
// Expected_slashed (everything!).
48214818
$data = compact(

0 commit comments

Comments
 (0)