Skip to content

Commit 085b2a4

Browse files
westonruteraaronjorbin
authored andcommitted
Code Modernization: Menus, Template, XML-RPC: Use null coalescing operator instead of isset() ternaries.
Developed as a subset of WordPress#10654 Initially developed in WordPress#4886 Follow-up to [61453], [61445], [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@61454 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 16d4572 commit 085b2a4

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

src/wp-admin/includes/nav-menu.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
*/
2323
function _wp_ajax_menu_quick_search( $request = array() ) {
2424
$args = array();
25-
$type = isset( $request['type'] ) ? $request['type'] : '';
26-
$object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
27-
$query = isset( $request['q'] ) ? $request['q'] : '';
28-
$response_format = isset( $request['response-format'] ) ? $request['response-format'] : '';
25+
$type = $request['type'] ?? '';
26+
$object_type = $request['object_type'] ?? '';
27+
$query = $request['q'] ?? '';
28+
$response_format = $request['response-format'] ?? '';
2929

3030
if ( ! $response_format || ! in_array( $response_format, array( 'json', 'markup' ), true ) ) {
3131
$response_format = 'json';
@@ -1183,19 +1183,19 @@ function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
11831183
}
11841184

11851185
$args = array(
1186-
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
1187-
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
1188-
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
1189-
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
1190-
'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
1191-
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
1192-
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
1193-
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
1194-
'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
1195-
'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
1196-
'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
1197-
'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
1198-
'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
1186+
'menu-item-db-id' => $_item_object_data['menu-item-db-id'] ?? '',
1187+
'menu-item-object-id' => $_item_object_data['menu-item-object-id'] ?? '',
1188+
'menu-item-object' => $_item_object_data['menu-item-object'] ?? '',
1189+
'menu-item-parent-id' => $_item_object_data['menu-item-parent-id'] ?? '',
1190+
'menu-item-position' => $_item_object_data['menu-item-position'] ?? '',
1191+
'menu-item-type' => $_item_object_data['menu-item-type'] ?? '',
1192+
'menu-item-title' => $_item_object_data['menu-item-title'] ?? '',
1193+
'menu-item-url' => $_item_object_data['menu-item-url'] ?? '',
1194+
'menu-item-description' => $_item_object_data['menu-item-description'] ?? '',
1195+
'menu-item-attr-title' => $_item_object_data['menu-item-attr-title'] ?? '',
1196+
'menu-item-target' => $_item_object_data['menu-item-target'] ?? '',
1197+
'menu-item-classes' => $_item_object_data['menu-item-classes'] ?? '',
1198+
'menu-item-xfn' => $_item_object_data['menu-item-xfn'] ?? '',
11991199
);
12001200

12011201
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
@@ -1447,7 +1447,7 @@ function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selecte
14471447

14481448
$args = array();
14491449
foreach ( $post_fields as $field ) {
1450-
$args[ $field ] = isset( $_POST[ $field ][ $_key ] ) ? $_POST[ $field ][ $_key ] : '';
1450+
$args[ $field ] = $_POST[ $field ][ $_key ] ?? '';
14511451
}
14521452

14531453
$menu_item_db_id = wp_update_nav_menu_item(

src/wp-admin/link-parse-opml.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function startElement( $parser, $tag_name, $attrs ) { // phpcs:ignore WordPress.
5555
// Save the data away.
5656
$names[] = $name;
5757
$urls[] = $url;
58-
$targets[] = isset( $attrs['TARGET'] ) ? $attrs['TARGET'] : '';
59-
$feeds[] = isset( $attrs['XMLURL'] ) ? $attrs['XMLURL'] : '';
60-
$descriptions[] = isset( $attrs['DESCRIPTION'] ) ? $attrs['DESCRIPTION'] : '';
58+
$targets[] = $attrs['TARGET'] ?? '';
59+
$feeds[] = $attrs['XMLURL'] ?? '';
60+
$descriptions[] = $attrs['DESCRIPTION'] ?? '';
6161
} // End if outline.
6262
}
6363

src/wp-admin/nav-menus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
$num_locations = count( array_keys( $locations ) );
5353

5454
// Allowed actions: add, update, delete.
55-
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
55+
$action = $_REQUEST['action'] ?? 'edit';
5656

5757
/*
5858
* If a JSON blob of navigation menu data is found, expand it and inject it

src/wp-includes/author-template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function get_the_author_meta( $field = '', $user_id = false ) {
169169

170170
if ( ! $user_id ) {
171171
global $authordata;
172-
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
172+
$user_id = $authordata->ID ?? 0;
173173
} else {
174174
$authordata = get_userdata( $user_id );
175175
}
@@ -178,7 +178,7 @@ function get_the_author_meta( $field = '', $user_id = false ) {
178178
$field = 'user_' . $field;
179179
}
180180

181-
$value = isset( $authordata->$field ) ? $authordata->$field : '';
181+
$value = $authordata->$field ?? '';
182182

183183
/**
184184
* Filters the value of the requested user metadata.
@@ -502,7 +502,7 @@ function wp_list_authors( $args = '' ) {
502502
}
503503

504504
foreach ( $authors as $author_id ) {
505-
$posts = isset( $post_counts[ $author_id ] ) ? $post_counts[ $author_id ] : 0;
505+
$posts = $post_counts[ $author_id ] ?? 0;
506506

507507
if ( ! $posts && $parsed_args['hide_empty'] ) {
508508
continue;

src/wp-includes/category-template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
963963
// Assemble the data that will be used to generate the tag cloud markup.
964964
$tags_data = array();
965965
foreach ( $tags as $key => $tag ) {
966-
$tag_id = isset( $tag->id ) ? $tag->id : $key;
966+
$tag_id = $tag->id ?? $key;
967967

968968
$count = $counts[ $key ];
969969
$real_count = $real_counts[ $key ];

src/wp-includes/class-wp-xmlrpc-server.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ protected function _insert_post( $user, $content_struct ) {
16801680
}
16811681

16821682
// Handle enclosures.
1683-
$enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
1683+
$enclosure = $post_data['enclosure'] ?? null;
16841684
$this->add_enclosure_if_new( $post_id, $enclosure );
16851685

16861686
$this->attach_uploads( $post_id, $post_data['post_content'] );
@@ -1986,7 +1986,7 @@ public function wp_getPosts( $args ) {
19861986

19871987
$username = $args[1];
19881988
$password = $args[2];
1989-
$filter = isset( $args[3] ) ? $args[3] : array();
1989+
$filter = $args[3] ?? array();
19901990

19911991
if ( isset( $args[4] ) ) {
19921992
$fields = $args[4];
@@ -2457,7 +2457,7 @@ public function wp_getTerms( $args ) {
24572457
$username = $args[1];
24582458
$password = $args[2];
24592459
$taxonomy = $args[3];
2460-
$filter = isset( $args[4] ) ? $args[4] : array();
2460+
$filter = $args[4] ?? array();
24612461

24622462
$user = $this->login( $username, $password );
24632463
if ( ! $user ) {
@@ -2615,7 +2615,7 @@ public function wp_getTaxonomies( $args ) {
26152615

26162616
$username = $args[1];
26172617
$password = $args[2];
2618-
$filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
2618+
$filter = $args[3] ?? array( 'public' => true );
26192619

26202620
if ( isset( $args[4] ) ) {
26212621
$fields = $args[4];
@@ -2764,7 +2764,7 @@ public function wp_getUsers( $args ) {
27642764

27652765
$username = $args[1];
27662766
$password = $args[2];
2767-
$filter = isset( $args[3] ) ? $args[3] : array();
2767+
$filter = $args[3] ?? array();
27682768

27692769
if ( isset( $args[4] ) ) {
27702770
$fields = $args[4];
@@ -3658,7 +3658,7 @@ public function wp_getComments( $args ) {
36583658

36593659
$username = $args[1];
36603660
$password = $args[2];
3661-
$struct = isset( $args[3] ) ? $args[3] : array();
3661+
$struct = $args[3] ?? array();
36623662

36633663
$user = $this->login( $username, $password );
36643664
if ( ! $user ) {
@@ -4433,7 +4433,7 @@ public function wp_getMediaLibrary( $args ) {
44334433

44344434
$username = $args[1];
44354435
$password = $args[2];
4436-
$struct = isset( $args[3] ) ? $args[3] : array();
4436+
$struct = $args[3] ?? array();
44374437

44384438
$user = $this->login( $username, $password );
44394439
if ( ! $user ) {
@@ -4624,7 +4624,7 @@ public function wp_getPostTypes( $args ) {
46244624

46254625
$username = $args[1];
46264626
$password = $args[2];
4627-
$filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
4627+
$filter = $args[3] ?? array( 'public' => true );
46284628

46294629
if ( isset( $args[4] ) ) {
46304630
$fields = $args[4];
@@ -5375,7 +5375,7 @@ public function mw_newPost( $args ) {
53755375
$username = $args[1];
53765376
$password = $args[2];
53775377
$content_struct = $args[3];
5378-
$publish = isset( $args[4] ) ? $args[4] : 0;
5378+
$publish = $args[4] ?? 0;
53795379

53805380
$user = $this->login( $username, $password );
53815381
if ( ! $user ) {
@@ -5491,8 +5491,8 @@ public function mw_newPost( $args ) {
54915491
$post_author = $content_struct['wp_author_id'];
54925492
}
54935493

5494-
$post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : '';
5495-
$post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : '';
5494+
$post_title = $content_struct['title'] ?? '';
5495+
$post_content = $content_struct['description'] ?? '';
54965496

54975497
$post_status = $publish ? 'publish' : 'draft';
54985498

@@ -5510,10 +5510,10 @@ public function mw_newPost( $args ) {
55105510
}
55115511
}
55125512

5513-
$post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : '';
5514-
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';
5513+
$post_excerpt = $content_struct['mt_excerpt'] ?? '';
5514+
$post_more = $content_struct['mt_text_more'] ?? '';
55155515

5516-
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();
5516+
$tags_input = $content_struct['mt_keywords'] ?? array();
55175517

55185518
if ( isset( $content_struct['mt_allow_comments'] ) ) {
55195519
if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
@@ -5661,7 +5661,7 @@ public function mw_newPost( $args ) {
56615661
}
56625662

56635663
// Handle enclosures.
5664-
$enclosure = isset( $content_struct['enclosure'] ) ? $content_struct['enclosure'] : null;
5664+
$enclosure = $content_struct['enclosure'] ?? null;
56655665
$this->add_enclosure_if_new( $post_id, $enclosure );
56665666

56675667
$this->attach_uploads( $post_id, $post_content );
@@ -5771,7 +5771,7 @@ public function mw_editPost( $args ) {
57715771
$username = $args[1];
57725772
$password = $args[2];
57735773
$content_struct = $args[3];
5774-
$publish = isset( $args[4] ) ? $args[4] : 0;
5774+
$publish = $args[4] ?? 0;
57755775

57765776
$user = $this->login( $username, $password );
57775777
if ( ! $user ) {
@@ -5955,7 +5955,7 @@ public function mw_editPost( $args ) {
59555955
$post_excerpt = $content_struct['mt_excerpt'];
59565956
}
59575957

5958-
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';
5958+
$post_more = $content_struct['mt_text_more'] ?? '';
59595959

59605960
$post_status = $publish ? 'publish' : 'draft';
59615961
if ( isset( $content_struct[ "{$post_type}_status" ] ) ) {
@@ -5972,7 +5972,7 @@ public function mw_editPost( $args ) {
59725972
}
59735973
}
59745974

5975-
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();
5975+
$tags_input = $content_struct['mt_keywords'] ?? array();
59765976

59775977
if ( 'publish' === $post_status || 'private' === $post_status ) {
59785978
if ( 'page' === $post_type && ! current_user_can( 'publish_pages' ) ) {
@@ -6080,7 +6080,7 @@ public function mw_editPost( $args ) {
60806080
}
60816081

60826082
// Handle enclosures.
6083-
$enclosure = isset( $content_struct['enclosure'] ) ? $content_struct['enclosure'] : null;
6083+
$enclosure = $content_struct['enclosure'] ?? null;
60846084
$this->add_enclosure_if_new( $post_id, $enclosure );
60856085

60866086
$this->attach_uploads( $post_id, $post_content );
@@ -7059,7 +7059,7 @@ public function pingback_ping( $args ) {
70597059
$remote_source = preg_replace( '/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/', "\n\n", $remote_source );
70607060

70617061
preg_match( '|<title>([^<]*?)</title>|is', $remote_source, $matchtitle );
7062-
$title = isset( $matchtitle[1] ) ? $matchtitle[1] : '';
7062+
$title = $matchtitle[1] ?? '';
70637063
if ( empty( $title ) ) {
70647064
return $this->pingback_error( 32, __( 'A title on that page cannot be found.' ) );
70657065
}

src/wp-includes/general-template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4658,7 +4658,7 @@ function paginate_links( $args = '' ) {
46584658
$url_parts = explode( '?', $pagenum_link );
46594659

46604660
// Get max pages and current page out of the current query, if available.
4661-
$total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
4661+
$total = $wp_query->max_num_pages ?? 1;
46624662
$current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
46634663

46644664
// Append the format placeholder to the base URL.
@@ -4697,7 +4697,7 @@ function paginate_links( $args = '' ) {
46974697
if ( isset( $url_parts[1] ) ) {
46984698
// Find the format argument.
46994699
$format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
4700-
$format_query = isset( $format[1] ) ? $format[1] : '';
4700+
$format_query = $format[1] ?? '';
47014701
wp_parse_str( $format_query, $format_args );
47024702

47034703
// Find the query args of the requested URL.

src/wp-includes/link-template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@ function get_pagenum_link( $pagenum = 1, $escape = true ) {
24382438
$request = remove_query_arg( 'paged' );
24392439

24402440
$home_root = parse_url( home_url() );
2441-
$home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
2441+
$home_root = $home_root['path'] ?? '';
24422442
$home_root = preg_quote( $home_root, '|' );
24432443

24442444
$request = preg_replace( '|^' . $home_root . '|i', '', $request );

src/wp-includes/nav-menu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
319319
$_menu = wp_get_nav_menu_object( $menu_id );
320320

321321
$args = array(
322-
'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
323-
'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
322+
'description' => $menu_data['description'] ?? '',
323+
'name' => $menu_data['menu-name'] ?? '',
324324
'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
325325
'slug' => null,
326326
);

0 commit comments

Comments
 (0)