Code Modernization: REST API: Use null coalescing operator in place of isset() in ternaries.

Developed as a subset of https://github.com/WordPress/wordpress-develop/pull/10654
Initially developed in https://github.com/WordPress/wordpress-develop/pull/4886

Follow-up to [61424], [61404], [61403].

Props costdev, westonruter.
See #58874, #63430.

Built from https://develop.svn.wordpress.org/trunk@61429


git-svn-id: http://core.svn.wordpress.org/trunk@60741 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter
2026-01-05 04:33:34 +00:00
parent c7dc39302c
commit 74d6d906f2
19 changed files with 34 additions and 37 deletions

View File

@@ -952,7 +952,7 @@ function rest_filter_response_fields( $response, $server, $request ) {
// Skip any sub-properties if their parent prop is already marked for inclusion.
break 2;
}
$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
$ref[ $next ] = $ref[ $next ] ?? array();
$ref = &$ref[ $next ];
}
$last = array_shift( $parts );
@@ -3088,7 +3088,7 @@ function rest_filter_response_by_context( $response_data, $schema, $context ) {
$check = array();
if ( $is_array_type ) {
$check = isset( $schema['items'] ) ? $schema['items'] : array();
$check = $schema['items'] ?? array();
} elseif ( $is_object_type ) {
if ( isset( $schema['properties'][ $key ] ) ) {
$check = $schema['properties'][ $key ];
@@ -3417,7 +3417,7 @@ function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
static function ( $status, $error_data ) {
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
return $error_data['status'] ?? $status;
},
500
);

View File

@@ -1374,7 +1374,7 @@ class WP_REST_Server {
$response = new WP_REST_Response( $available );
$fields = isset( $request['_fields'] ) ? $request['_fields'] : '';
$fields = $request['_fields'] ?? '';
$fields = wp_parse_list( $fields );
if ( empty( $fields ) ) {
$fields[] = '_links';
@@ -1618,7 +1618,7 @@ class WP_REST_Server {
$data['namespace'] = $options['namespace'];
}
$allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;
$allow_batch = $options['allow_batch'] ?? false;
if ( isset( $options['schema'] ) && 'help' === $context ) {
$data['schema'] = call_user_func( $options['schema'] );
@@ -1640,7 +1640,7 @@ class WP_REST_Server {
'methods' => array_keys( $callback['methods'] ),
);
$callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;
$callback_batch = $callback['allow_batch'] ?? $allow_batch;
if ( $callback_batch ) {
$endpoint_data['allow_batch'] = $callback_batch;
@@ -1722,7 +1722,7 @@ class WP_REST_Server {
continue;
}
$single_request = new WP_REST_Request( isset( $args['method'] ) ? $args['method'] : 'POST', $parsed_url['path'] );
$single_request = new WP_REST_Request( $args['method'] ?? 'POST', $parsed_url['path'] );
if ( ! empty( $parsed_url['query'] ) ) {
$query_args = array();
@@ -1767,7 +1767,7 @@ class WP_REST_Server {
$allow_batch = $handler['allow_batch'];
} else {
$route_options = $this->get_route_options( $route );
$allow_batch = isset( $route_options['allow_batch'] ) ? $route_options['allow_batch'] : false;
$allow_batch = $route_options['allow_batch'] ?? false;
}
if ( ! is_array( $allow_batch ) || empty( $allow_batch['v1'] ) ) {

View File

@@ -159,7 +159,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
* @param bool $check_mime Whether to prevent uploads of unsupported image types.
* @param string|null $mime_type The mime type of the file being uploaded (if available).
*/
$prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, isset( $files['file']['type'] ) ? $files['file']['type'] : null );
$prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, $files['file']['type'] ?? null );
// If the upload is an image, check if the server can handle the mime type.
if (

View File

@@ -389,7 +389,7 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
foreach ( $revisioned_meta_keys as $meta_key ) {
// get_metadata_raw is used to avoid retrieving the default value.
$old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );
$new_meta = isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : '';
$new_meta = $meta[ $meta_key ] ?? '';
if ( $new_meta !== $old_meta ) {
$autosave_is_different = true;

View File

@@ -136,7 +136,7 @@ class WP_REST_Block_Directory_Controller extends WP_REST_Controller {
'author_block_rating' => $plugin['author_block_rating'] / 20,
'author_block_count' => (int) $plugin['author_block_count'],
'author' => wp_strip_all_tags( $plugin['author'] ),
'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
'icon' => $plugin['icons']['1x'] ?? 'block-default',
'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
'humanized_updated' => sprintf(
/* translators: %s: Human-readable time difference. */

View File

@@ -58,7 +58,7 @@ class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
unset( $data['content']['rendered'] );
// Add the core wp_pattern_sync_status meta as top level property to the response.
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
$data['wp_pattern_sync_status'] = $data['meta']['wp_pattern_sync_status'] ?? '';
unset( $data['meta']['wp_pattern_sync_status'] );
return $data;
}

View File

@@ -571,7 +571,7 @@ abstract class WP_REST_Controller {
*/
public function get_fields_for_response( $request ) {
$schema = $this->get_item_schema();
$properties = isset( $schema['properties'] ) ? $schema['properties'] : array();
$properties = $schema['properties'] ?? array();
$additional_fields = $this->get_additional_fields();

View File

@@ -573,7 +573,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
if ( rest_is_field_included( 'styles', $fields ) ) {
$raw_data = $theme->get_raw_data();
$data['styles'] = isset( $raw_data['styles'] ) ? $raw_data['styles'] : array();
$data['styles'] = $raw_data['styles'] ?? array();
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

View File

@@ -181,7 +181,7 @@ class WP_REST_Menu_Locations_Controller extends WP_REST_Controller {
$location = $item;
$locations = get_nav_menu_locations();
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
$menu = $locations[ $location->name ] ?? 0;
$fields = $this->get_fields_for_response( $request );
$data = array();
@@ -242,7 +242,7 @@ class WP_REST_Menu_Locations_Controller extends WP_REST_Controller {
);
$locations = get_nav_menu_locations();
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
$menu = $locations[ $location->name ] ?? 0;
if ( $menu ) {
$path = rest_get_route_for_term( $menu );
if ( $path ) {

View File

@@ -100,8 +100,8 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
$query_args['locale'] = get_user_locale();
$query_args['wp-version'] = wp_get_wp_version();
$query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false;
$query_args['pattern-keywords'] = isset( $request['keyword'] ) ? $request['keyword'] : false;
$query_args['pattern-categories'] = $request['category'] ?? false;
$query_args['pattern-keywords'] = $request['keyword'] ?? false;
$query_args = array_filter( $query_args );

View File

@@ -239,7 +239,7 @@ class WP_REST_Settings_Controller extends WP_REST_Controller {
'type' => empty( $args['type'] ) ? null : $args['type'],
'title' => empty( $args['label'] ) ? '' : $args['label'],
'description' => empty( $args['description'] ) ? '' : $args['description'],
'default' => isset( $args['default'] ) ? $args['default'] : null,
'default' => $args['default'] ?? null,
);
$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );

View File

@@ -339,13 +339,13 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
$registered_sidebar = $wp_registered_sidebars[ $id ];
$sidebar['status'] = 'active';
$sidebar['name'] = isset( $registered_sidebar['name'] ) ? $registered_sidebar['name'] : '';
$sidebar['name'] = $registered_sidebar['name'] ?? '';
$sidebar['description'] = isset( $registered_sidebar['description'] ) ? wp_sidebar_description( $id ) : '';
$sidebar['class'] = isset( $registered_sidebar['class'] ) ? $registered_sidebar['class'] : '';
$sidebar['before_widget'] = isset( $registered_sidebar['before_widget'] ) ? $registered_sidebar['before_widget'] : '';
$sidebar['after_widget'] = isset( $registered_sidebar['after_widget'] ) ? $registered_sidebar['after_widget'] : '';
$sidebar['before_title'] = isset( $registered_sidebar['before_title'] ) ? $registered_sidebar['before_title'] : '';
$sidebar['after_title'] = isset( $registered_sidebar['after_title'] ) ? $registered_sidebar['after_title'] : '';
$sidebar['class'] = $registered_sidebar['class'] ?? '';
$sidebar['before_widget'] = $registered_sidebar['before_widget'] ?? '';
$sidebar['after_widget'] = $registered_sidebar['after_widget'] ?? '';
$sidebar['before_title'] = $registered_sidebar['before_title'] ?? '';
$sidebar['after_title'] = $registered_sidebar['after_title'] ?? '';
} else {
$sidebar['status'] = 'inactive';
$sidebar['name'] = $raw_sidebar['name'];
@@ -361,7 +361,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
if ( rest_is_field_included( 'widgets', $fields ) ) {
$sidebars = wp_get_sidebars_widgets();
$widgets = array_filter(
isset( $sidebars[ $sidebar['id'] ] ) ? $sidebars[ $sidebar['id'] ] : array(),
$sidebars[ $sidebar['id'] ] ?? array(),
static function ( $widget_id ) use ( $wp_registered_widgets ) {
return isset( $wp_registered_widgets[ $widget_id ] );
}

View File

@@ -579,7 +579,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : get_stylesheet(),
'wp_theme' => $request['theme'] ?? get_stylesheet(),
);
} elseif ( 'custom' !== $template->source ) {
$changes->post_name = $template->slug;
@@ -914,8 +914,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
return $plugins[ $plugin_basename ]['Name'];
}
return isset( $template_object->plugin ) ?
$template_object->plugin :
return $template_object->plugin ??
$template_object->theme;
case 'site':
return get_bloginfo( 'name' );

View File

@@ -405,7 +405,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
$id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
} else {
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
$id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
$id = $user_cpt['ID'] ?? null;
}
if ( $id ) {

View File

@@ -333,9 +333,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
$prepared_args['search_columns'] = array( 'ID', 'user_login', 'user_nicename', 'display_name' );
}
$search_columns = $request->get_param( 'search_columns' );
$valid_columns = isset( $prepared_args['search_columns'] )
? $prepared_args['search_columns']
: array( 'ID', 'user_login', 'user_nicename', 'user_email', 'display_name' );
$valid_columns = $prepared_args['search_columns'] ?? array( 'ID', 'user_login', 'user_nicename', 'user_email', 'display_name' );
$search_columns_mapping = array(
'id' => 'ID',
'username' => 'user_login',

View File

@@ -611,7 +611,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller {
return array(
'preview' => $this->render_legacy_widget_preview_iframe(
$request['id'],
isset( $request['instance'] ) ? $request['instance'] : null
$request['instance'] ?? null
),
);
}

View File

@@ -533,7 +533,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
$id = $request['id'];
$parsed_id = wp_parse_widget_id( $id );
$id_base = $parsed_id['id_base'];
$number = isset( $parsed_id['number'] ) ? $parsed_id['number'] : null;
$number = $parsed_id['number'] ?? null;
$widget_object = $wp_widget_factory->get_widget_object( $id_base );
$creating = false;
} elseif ( $request['id_base'] ) {

View File

@@ -480,7 +480,7 @@ abstract class WP_REST_Meta_Fields {
'type' => $default_args['type'],
'title' => empty( $args['label'] ) ? '' : $args['label'],
'description' => empty( $args['description'] ) ? '' : $args['description'],
'default' => isset( $args['default'] ) ? $args['default'] : null,
'default' => $args['default'] ?? null,
);
$rest_args = array_merge( $default_args, $rest_args );

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.0-alpha-61428';
$wp_version = '7.0-alpha-61429';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.