Skip to content

Commit ddb3799

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

18 files changed

+33
-36
lines changed

src/wp-includes/rest-api.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ function rest_filter_response_fields( $response, $server, $request ) {
952952
// Skip any sub-properties if their parent prop is already marked for inclusion.
953953
break 2;
954954
}
955-
$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
955+
$ref[ $next ] = $ref[ $next ] ?? array();
956956
$ref = &$ref[ $next ];
957957
}
958958
$last = array_shift( $parts );
@@ -3088,7 +3088,7 @@ function rest_filter_response_by_context( $response_data, $schema, $context ) {
30883088
$check = array();
30893089

30903090
if ( $is_array_type ) {
3091-
$check = isset( $schema['items'] ) ? $schema['items'] : array();
3091+
$check = $schema['items'] ?? array();
30923092
} elseif ( $is_object_type ) {
30933093
if ( isset( $schema['properties'][ $key ] ) ) {
30943094
$check = $schema['properties'][ $key ];
@@ -3417,7 +3417,7 @@ function rest_convert_error_to_response( $error ) {
34173417
$status = array_reduce(
34183418
$error->get_all_error_data(),
34193419
static function ( $status, $error_data ) {
3420-
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
3420+
return $error_data['status'] ?? $status;
34213421
},
34223422
500
34233423
);

src/wp-includes/rest-api/class-wp-rest-server.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ public function get_index( $request ) {
13741374

13751375
$response = new WP_REST_Response( $available );
13761376

1377-
$fields = isset( $request['_fields'] ) ? $request['_fields'] : '';
1377+
$fields = $request['_fields'] ?? '';
13781378
$fields = wp_parse_list( $fields );
13791379
if ( empty( $fields ) ) {
13801380
$fields[] = '_links';
@@ -1618,7 +1618,7 @@ public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
16181618
$data['namespace'] = $options['namespace'];
16191619
}
16201620

1621-
$allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;
1621+
$allow_batch = $options['allow_batch'] ?? false;
16221622

16231623
if ( isset( $options['schema'] ) && 'help' === $context ) {
16241624
$data['schema'] = call_user_func( $options['schema'] );
@@ -1640,7 +1640,7 @@ public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
16401640
'methods' => array_keys( $callback['methods'] ),
16411641
);
16421642

1643-
$callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;
1643+
$callback_batch = $callback['allow_batch'] ?? $allow_batch;
16441644

16451645
if ( $callback_batch ) {
16461646
$endpoint_data['allow_batch'] = $callback_batch;
@@ -1722,7 +1722,7 @@ public function serve_batch_request_v1( WP_REST_Request $batch_request ) {
17221722
continue;
17231723
}
17241724

1725-
$single_request = new WP_REST_Request( isset( $args['method'] ) ? $args['method'] : 'POST', $parsed_url['path'] );
1725+
$single_request = new WP_REST_Request( $args['method'] ?? 'POST', $parsed_url['path'] );
17261726

17271727
if ( ! empty( $parsed_url['query'] ) ) {
17281728
$query_args = array();
@@ -1767,7 +1767,7 @@ public function serve_batch_request_v1( WP_REST_Request $batch_request ) {
17671767
$allow_batch = $handler['allow_batch'];
17681768
} else {
17691769
$route_options = $this->get_route_options( $route );
1770-
$allow_batch = isset( $route_options['allow_batch'] ) ? $route_options['allow_batch'] : false;
1770+
$allow_batch = $route_options['allow_batch'] ?? false;
17711771
}
17721772

17731773
if ( ! is_array( $allow_batch ) || empty( $allow_batch['v1'] ) ) {

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function create_item_permissions_check( $request ) {
159159
* @param bool $check_mime Whether to prevent uploads of unsupported image types.
160160
* @param string|null $mime_type The mime type of the file being uploaded (if available).
161161
*/
162-
$prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, isset( $files['file']['type'] ) ? $files['file']['type'] : null );
162+
$prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, $files['file']['type'] ?? null );
163163

164164
// If the upload is an image, check if the server can handle the mime type.
165165
if (

src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public function create_post_autosave( $post_data, array $meta = array() ) {
389389
foreach ( $revisioned_meta_keys as $meta_key ) {
390390
// get_metadata_raw is used to avoid retrieving the default value.
391391
$old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );
392-
$new_meta = isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : '';
392+
$new_meta = $meta[ $meta_key ] ?? '';
393393

394394
if ( $new_meta !== $old_meta ) {
395395
$autosave_is_different = true;

src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function prepare_item_for_response( $item, $request ) {
136136
'author_block_rating' => $plugin['author_block_rating'] / 20,
137137
'author_block_count' => (int) $plugin['author_block_count'],
138138
'author' => wp_strip_all_tags( $plugin['author'] ),
139-
'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
139+
'icon' => $plugin['icons']['1x'] ?? 'block-default',
140140
'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
141141
'humanized_updated' => sprintf(
142142
/* translators: %s: Human-readable time difference. */

src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function filter_response_by_context( $data, $context ) {
5858
unset( $data['content']['rendered'] );
5959

6060
// Add the core wp_pattern_sync_status meta as top level property to the response.
61-
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
61+
$data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status'] ?? '';
6262
unset( $data['meta']['wp_pattern_sync_status'] );
6363
return $data;
6464
}

src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ protected function get_object_type() {
571571
*/
572572
public function get_fields_for_response( $request ) {
573573
$schema = $this->get_item_schema();
574-
$properties = isset( $schema['properties'] ) ? $schema['properties'] : array();
574+
$properties = $schema['properties'] ?? array();
575575

576576
$additional_fields = $this->get_additional_fields();
577577

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

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

574574
if ( rest_is_field_included( 'styles', $fields ) ) {
575575
$raw_data = $theme->get_raw_data();
576-
$data['styles'] = isset( $raw_data['styles'] ) ? $raw_data['styles'] : array();
576+
$data['styles'] = $raw_data['styles'] ?? array();
577577
}
578578

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

src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function prepare_item_for_response( $item, $request ) {
181181
$location = $item;
182182

183183
$locations = get_nav_menu_locations();
184-
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
184+
$menu = $locations[ $location->name ] ?? 0;
185185

186186
$fields = $this->get_fields_for_response( $request );
187187
$data = array();
@@ -242,7 +242,7 @@ protected function prepare_links( $location ) {
242242
);
243243

244244
$locations = get_nav_menu_locations();
245-
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
245+
$menu = $locations[ $location->name ] ?? 0;
246246
if ( $menu ) {
247247
$path = rest_get_route_for_term( $menu );
248248
if ( $path ) {

src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public function get_items( $request ) {
100100

101101
$query_args['locale'] = get_user_locale();
102102
$query_args['wp-version'] = wp_get_wp_version();
103-
$query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false;
104-
$query_args['pattern-keywords'] = isset( $request['keyword'] ) ? $request['keyword'] : false;
103+
$query_args['pattern-categories'] = $request['category'] ?? false;
104+
$query_args['pattern-keywords'] = $request['keyword'] ?? false;
105105

106106
$query_args = array_filter( $query_args );
107107

0 commit comments

Comments
 (0)