From 95a6a54aa6e04bd5673776ae6585e150dcc8d0f6 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 30 Oct 2024 11:23:16 +0100 Subject: [PATCH 1/5] Simplify ternary operators and remove redundant type casting. --- .../rest-api/endpoints/class-wp-rest-comments-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-font-faces-controller.php | 2 +- .../endpoints/class-wp-rest-font-families-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-revisions-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-terms-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-themes-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-users-controller.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index c129ef6e130c4..98b9a85fc3bbf 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -955,7 +955,7 @@ public function delete_item( $request ) { return $comment; } - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; /** * Filters whether a comment can be trashed via the REST API. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php index ec59baa5d6c8f..d4865531ca1b4 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php @@ -417,7 +417,7 @@ public function delete_item( $request ) { ); } - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; // We don't support trashing for font faces. if ( ! $force ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php index c2d70d0dd6916..cdce89c50373d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php @@ -204,7 +204,7 @@ public function create_item( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item( $request ) { - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; // We don't support trashing for font families. if ( ! $force ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index fb5fa29231a4f..10bc79a90d101 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -473,7 +473,7 @@ public function delete_item( $request ) { return $revision; } - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; // We don't support trashing for revisions. if ( ! $force ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 78c9d1ca2b0d8..6d31c597020ac 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -743,7 +743,7 @@ public function delete_item( $request ) { return $term; } - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; // We don't support trashing for terms. if ( ! $force ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index be12707b3c750..e0a9c6801ee28 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -623,7 +623,7 @@ public function get_item_schema() { ), ); - foreach ( get_registered_theme_features() as $feature => $config ) { + foreach ( get_registered_theme_features() as $config ) { if ( ! is_array( $config['show_in_rest'] ) ) { continue; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index c8d9b11a11d20..61a4654ed832f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -896,7 +896,7 @@ public function delete_item( $request ) { $id = $user->ID; $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] ); - $force = isset( $request['force'] ) ? (bool) $request['force'] : false; + $force = isset( $request['force'] ) && $request['force']; // We don't support trashing for users. if ( ! $force ) { From ace40721c8b01dd3b7be1ed80003452b4e540de9 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 30 Oct 2024 12:38:23 +0100 Subject: [PATCH 2/5] Use strict comparisons to verify empty arrays. --- src/wp-includes/rest-api/class-wp-rest-server.php | 6 +++--- .../endpoints/class-wp-rest-attachments-controller.php | 2 +- .../rest-api/endpoints/class-wp-rest-posts-controller.php | 2 +- .../endpoints/class-wp-rest-templates-controller.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 17d620f6fb6e0..8e76b71ba6c33 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -738,7 +738,7 @@ public static function get_compact_response_links( $response ) { // Relation now changes from '$uri' to '$curie:$relation'. $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); preg_match( '!' . $rel_regex . '!', $rel, $matches ); - if ( $matches ) { + if ( array() !== $matches ) { $new_rel = $curie['name'] . ':' . $matches[1]; $used_curies[ $curie['name'] ] = $curie; $links[ $new_rel ] = $items; @@ -749,7 +749,7 @@ public static function get_compact_response_links( $response ) { } // Push the curies onto the start of the links array. - if ( $used_curies ) { + if ( array() !== $used_curies ) { $links['curies'] = array_values( $used_curies ); } @@ -836,7 +836,7 @@ protected function embed_links( $data, $embed = true ) { } } - if ( ! empty( $embedded ) ) { + if ( array() !== $embedded ) { $data['_embedded'] = $embedded; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 0c98a729e43f9..18868f4b4754b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -567,7 +567,7 @@ public function edit_media_item( $request ) { ); } - if ( 0 === count( $modifiers ) ) { + if ( array() === $modifiers ) { return new WP_Error( 'rest_image_not_edited', __( 'The image was not edited. Edit the image before applying the changes.' ), diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index c4db4871df644..5a7eccf67381d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -2721,7 +2721,7 @@ public function get_item_schema() { // Emit a _doing_it_wrong warning if user tries to add new properties using this filter. $new_fields = array_diff( array_keys( $schema['properties'] ), $schema_fields ); - if ( count( $new_fields ) > 0 ) { + if ( array() !== $new_fields ) { _doing_it_wrong( __METHOD__, sprintf( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index 43780fb4e677b..9f26e030c5039 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -459,7 +459,7 @@ public function create_item( $request ) { return $post_id; } $posts = get_block_templates( array( 'wp_id' => $post_id ), $this->post_type ); - if ( ! count( $posts ) ) { + if ( 0 === count( $posts ) ) { return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) ); } $id = $posts[0]->id; From e4efe7d3d50f1e502914b79496fc9ae9ea923b18 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 30 Oct 2024 19:58:44 +0100 Subject: [PATCH 3/5] Combine the if statements and remove any redundant `elseif` statements. --- ...lass-wp-rest-block-renderer-controller.php | 18 ++--- .../class-wp-rest-comments-controller.php | 45 ++++++----- ...est-global-styles-revisions-controller.php | 4 +- .../class-wp-rest-plugins-controller.php | 2 +- .../class-wp-rest-posts-controller.php | 81 +++++++++---------- .../class-wp-rest-revisions-controller.php | 4 +- .../class-wp-rest-terms-controller.php | 6 +- .../class-wp-rest-users-controller.php | 20 ++--- .../class-wp-rest-widgets-controller.php | 10 +-- 9 files changed, 94 insertions(+), 96 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php index 3e4e8eb794d37..685b33902f9cc 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php @@ -125,16 +125,14 @@ public function get_item_permissions_check( $request ) { ) ); } - } else { - if ( ! current_user_can( 'edit_posts' ) ) { - return new WP_Error( - 'block_cannot_read', - __( 'Sorry, you are not allowed to read blocks as this user.' ), - array( - 'status' => rest_authorization_required_code(), - ) - ); - } + } elseif ( ! current_user_can( 'edit_posts' ) ) { + return new WP_Error( + 'block_cannot_read', + __( 'Sorry, you are not allowed to read blocks as this user.' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); } return true; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 98b9a85fc3bbf..47dcc707714a4 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -134,7 +134,9 @@ public function get_items_permissions_check( $request ) { __( 'Sorry, you are not allowed to read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); - } elseif ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) { + } + + if ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) { return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to read comments without a post.' ), @@ -484,15 +486,16 @@ public function create_item_permissions_check( $request ) { ); } - if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) { - if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) { - return new WP_Error( - 'rest_comment_invalid_author_ip', - /* translators: %s: Request parameter. */ - sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), - array( 'status' => rest_authorization_required_code() ) - ); - } + if ( isset( $request['author_ip'] ) + && ! current_user_can( 'moderate_comments' ) + && ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) + ) { + return new WP_Error( + 'rest_comment_invalid_author_ip', + /* translators: %s: Request parameter. */ + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), + array( 'status' => rest_authorization_required_code() ) + ); } if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { @@ -623,14 +626,14 @@ public function create_item( $request ) { } // Honor the discussion setting that requires a name and email address of the comment author. - if ( get_option( 'require_name_email' ) ) { - if ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) { - return new WP_Error( - 'rest_comment_author_data_required', - __( 'Creating a comment requires valid author name and email values.' ), - array( 'status' => 400 ) - ); - } + if ( get_option( 'require_name_email' ) + && ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) + ) { + return new WP_Error( + 'rest_comment_author_data_required', + __( 'Creating a comment requires valid author name and email values.' ), + array( 'status' => 400 ) + ); } if ( ! isset( $prepared_comment['comment_author_email'] ) ) { @@ -1807,10 +1810,8 @@ protected function check_read_post_permission( $post, $request ) { protected function check_read_permission( $comment, $request ) { if ( ! empty( $comment->comment_post_ID ) ) { $post = get_post( $comment->comment_post_ID ); - if ( $post ) { - if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) { - return true; - } + if ( $post && ( 1 === (int) $comment->comment_approved ) && $this->check_read_post_permission( $post, $request ) ) { + return true; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php index 54285fa560bee..0d3792c1ea8fd 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php @@ -213,7 +213,9 @@ public function get_items( $request ) { __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) ); - } elseif ( ! $offset && $page > $max_pages ) { + } + + if ( ! $offset && $page > $max_pages ) { return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php index d0dd5fce4cd9c..89fa98e8060b0 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php @@ -772,7 +772,7 @@ protected function handle_plugin_status( $plugin, $new_status, $current_status ) * @return bool */ public function validate_plugin_param( $file ) { - if ( ! is_string( $file ) || ! preg_match( '/' . self::PATTERN . '/u', $file ) ) { + if ( ! is_string( $file ) || 1 !== preg_match( '/' . self::PATTERN . '/u', $file ) ) { return false; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 5a7eccf67381d..ec78557683679 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -557,15 +557,13 @@ public function get_item_permissions_check( $request ) { ); } - if ( $post && ! empty( $request->get_query_params()['password'] ) ) { - // Check post password, and return error if invalid. - if ( ! hash_equals( $post->post_password, $request->get_query_params()['password'] ) ) { - return new WP_Error( - 'rest_post_incorrect_password', - __( 'Incorrect post password.' ), - array( 'status' => 403 ) - ); - } + // Check post password, and return error if invalid. + if ( $post && ! empty( $request->get_query_params()['password'] ) && ! hash_equals( $post->post_password, $request->get_query_params()['password'] ) ) { + return new WP_Error( + 'rest_post_incorrect_password', + __( 'Incorrect post password.' ), + array( 'status' => 403 ) + ); } // Allow access to all password protected posts if the context is edit. @@ -1396,14 +1394,16 @@ protected function prepare_item_for_database( $request ) { } } - if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) { - if ( ! empty( $prepared_post->ID ) && post_password_required( $prepared_post->ID ) ) { - return new WP_Error( - 'rest_invalid_field', - __( 'A password protected post can not be set to sticky.' ), - array( 'status' => 400 ) - ); - } + if ( ! empty( $schema['properties']['sticky'] ) + && ! empty( $request['sticky'] ) + && ! empty( $prepared_post->ID ) + && post_password_required( $prepared_post->ID ) + ) { + return new WP_Error( + 'rest_invalid_field', + __( 'A password protected post can not be set to sticky.' ), + array( 'status' => 400 ) + ); } // Parent. @@ -1551,16 +1551,15 @@ protected function handle_featured_media( $featured_media, $post_id ) { $result = set_post_thumbnail( $post_id, $featured_media ); if ( $result ) { return true; - } else { - return new WP_Error( - 'rest_invalid_featured_media', - __( 'Invalid featured media ID.' ), - array( 'status' => 400 ) - ); } - } else { - return delete_post_thumbnail( $post_id ); + return new WP_Error( + 'rest_invalid_featured_media', + __( 'Invalid featured media ID.' ), + array( 'status' => 400 ) + ); } + + return delete_post_thumbnail( $post_id ); } /** @@ -1696,11 +1695,7 @@ protected function check_is_post_type_allowed( $post_type ) { $post_type = get_post_type_object( $post_type ); } - if ( ! empty( $post_type ) && ! empty( $post_type->show_in_rest ) ) { - return true; - } - - return false; + return ! empty( $post_type ) && ! empty( $post_type->show_in_rest ); } /** @@ -1741,11 +1736,8 @@ public function check_read_permission( $post ) { * If there isn't a parent, but the status is set to inherit, assume * it's published (as per get_post_status()). */ - if ( 'inherit' === $post->post_status ) { - return true; - } - return false; + return 'inherit' === $post->post_status; } /** @@ -2266,16 +2258,17 @@ protected function get_available_actions( $post, $request ) { $rels[] = 'https://api.w.org/action-unfiltered-html'; } - if ( 'post' === $post_type->name ) { - if ( current_user_can( $post_type->cap->edit_others_posts ) && current_user_can( $post_type->cap->publish_posts ) ) { - $rels[] = 'https://api.w.org/action-sticky'; - } + if ( 'post' === $post_type->name + && current_user_can( $post_type->cap->edit_others_posts ) + && current_user_can( $post_type->cap->publish_posts ) + ) { + $rels[] = 'https://api.w.org/action-sticky'; } - if ( post_type_supports( $post_type->name, 'author' ) ) { - if ( current_user_can( $post_type->cap->edit_others_posts ) ) { - $rels[] = 'https://api.w.org/action-assign-author'; - } + if ( post_type_supports( $post_type->name, 'author' ) + && current_user_can( $post_type->cap->edit_others_posts ) + ) { + $rels[] = 'https://api.w.org/action-assign-author'; } $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); @@ -2487,7 +2480,9 @@ public function get_item_schema() { foreach ( $post_type_attributes as $attribute ) { if ( isset( $fixed_schemas[ $this->post_type ] ) && ! in_array( $attribute, $fixed_schemas[ $this->post_type ], true ) ) { continue; - } elseif ( ! isset( $fixed_schemas[ $this->post_type ] ) && ! post_type_supports( $this->post_type, $attribute ) ) { + } + + if ( ! isset( $fixed_schemas[ $this->post_type ] ) && ! post_type_supports( $this->post_type, $attribute ) ) { continue; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index 10bc79a90d101..55adc041b5039 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -320,7 +320,9 @@ public function get_items( $request ) { __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) ); - } elseif ( ! $offset && $page > $max_pages ) { + } + + if ( ! $offset && $page > $max_pages ) { return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 6d31c597020ac..009f5bedc9ace 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -288,10 +288,8 @@ public function get_items( $request ) { if ( 0 === $request['parent'] ) { // Only query top-level terms. $prepared_args['parent'] = 0; - } else { - if ( $request['parent'] ) { - $prepared_args['parent'] = $request['parent']; - } + } elseif ( $request['parent'] ) { + $prepared_args['parent'] = $request['parent']; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 61a4654ed832f..778cbb150a3aa 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -455,7 +455,9 @@ public function get_item_permissions_check( $request ) { __( 'Sorry, you are not allowed to list users.' ), array( 'status' => rest_authorization_required_code() ) ); - } elseif ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) { + } + + if ( ! count_user_posts( $user->ID, $types ) && ! current_user_can( 'edit_user', $user->ID ) && ! current_user_can( 'list_users' ) ) { return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.' ), @@ -908,14 +910,14 @@ public function delete_item( $request ) { ); } - if ( ! empty( $reassign ) ) { - if ( $reassign === $id || ! get_userdata( $reassign ) ) { - return new WP_Error( - 'rest_user_invalid_reassign', - __( 'Invalid user ID for reassignment.' ), - array( 'status' => 400 ) - ); - } + if ( ! empty( $reassign ) + && ( $reassign === $id || ! get_userdata( $reassign ) ) + ) { + return new WP_Error( + 'rest_user_invalid_reassign', + __( 'Invalid user ID for reassignment.' ), + array( 'status' => 400 ) + ); } $request->set_param( 'context', 'edit' ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php index f2d17186b62d7..d620dc0c1c59f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php @@ -328,11 +328,11 @@ public function update_item( $request ) { } } - if ( $request->has_param( 'sidebar' ) ) { - if ( $sidebar_id !== $request['sidebar'] ) { - $sidebar_id = $request['sidebar']; - wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ); - } + if ( $request->has_param( 'sidebar' ) + && $sidebar_id !== $request['sidebar'] + ) { + $sidebar_id = $request['sidebar']; + wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ); } $request['context'] = 'edit'; From 128605f7238a0618cb178692c99b6cf4d634cf79 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Wed, 30 Oct 2024 23:11:02 +0100 Subject: [PATCH 4/5] 1. Replace simple `if/else` statements with ternary operators. 2. Refactor complex `if/continue` statements into two simpler `continue` statements. 3. Refactor return values in certain methods, such as creating arrays all at once instead of adding values to an array one by one. --- .../rest-api/class-wp-rest-response.php | 8 ++--- .../rest-api/class-wp-rest-server.php | 32 +++++++------------ .../class-wp-rest-attachments-controller.php | 8 ++--- .../class-wp-rest-menu-items-controller.php | 5 ++- .../class-wp-rest-posts-controller.php | 8 ++--- .../class-wp-rest-site-health-controller.php | 16 ++++------ .../class-wp-rest-taxonomies-controller.php | 18 +++++++---- .../class-wp-rest-terms-controller.php | 7 +--- .../fields/class-wp-rest-meta-fields.php | 13 ++------ 9 files changed, 41 insertions(+), 74 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-response.php b/src/wp-includes/rest-api/class-wp-rest-response.php index c6ea11be83ee3..a5e4b9533d165 100644 --- a/src/wp-includes/rest-api/class-wp-rest-response.php +++ b/src/wp-includes/rest-api/class-wp-rest-response.php @@ -85,11 +85,9 @@ public function remove_link( $rel, $href = null ) { return; } - if ( $href ) { - $this->links[ $rel ] = wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' ); - } else { - $this->links[ $rel ] = array(); - } + $this->links[ $rel ] = $href + ? wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' ) + : array(); if ( ! $this->links[ $rel ] ) { unset( $this->links[ $rel ] ); diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 8e76b71ba6c33..feedc373a0dea 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -364,11 +364,7 @@ public function serve_request( $path = null ) { } if ( empty( $path ) ) { - if ( isset( $_SERVER['PATH_INFO'] ) ) { - $path = $_SERVER['PATH_INFO']; - } else { - $path = '/'; - } + $path = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '/'; } $request = new WP_REST_Request( $_SERVER['REQUEST_METHOD'], $path ); @@ -1161,16 +1157,14 @@ protected function match_request_to_handler( $request ) { } } - if ( $with_namespace ) { - $routes = array_merge( ...$with_namespace ); - } else { - $routes = $this->get_routes(); - } + $routes = array() !== $with_namespace + ? array_merge( ...$with_namespace ) + : $this->get_routes(); foreach ( $routes as $route => $handlers ) { $match = preg_match( '@^' . $route . '$@i', $path, $matches ); - if ( ! $match ) { + if ( 1 !== $match ) { continue; } @@ -1286,11 +1280,9 @@ protected function respond_to_request( $request, $route, $handler, $response ) { $dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler ); // Allow plugins to halt the request via this filter. - if ( null !== $dispatch_result ) { - $response = $dispatch_result; - } else { - $response = call_user_func( $handler['callback'], $request ); - } + $response = null === $dispatch_result + ? call_user_func( $handler['callback'], $request ) + : $dispatch_result; } /** @@ -1316,11 +1308,9 @@ protected function respond_to_request( $request, $route, $handler, $response ) { */ $response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request ); - if ( is_wp_error( $response ) ) { - $response = $this->error_to_response( $response ); - } else { - $response = rest_ensure_response( $response ); - } + $response = is_wp_error( $response ) + ? $this->error_to_response( $response ) + : rest_ensure_response( $response ); $response->set_matched_route( $route ); $response->set_matched_handler( $handler ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 18868f4b4754b..4609cd0c69e7d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -457,11 +457,9 @@ public function update_item( $request ) { * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. */ public function post_process_item( $request ) { - switch ( $request['action'] ) { - case 'create-image-subsizes': - require_once ABSPATH . 'wp-admin/includes/image.php'; - wp_update_image_subsizes( $request['id'] ); - break; + if ( 'create-image-subsizes' === $request['action'] ) { + require_once ABSPATH . 'wp-admin/includes/image.php'; + wp_update_image_subsizes( $request['id'] ); } $request['context'] = 'edit'; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php index ee69643813577..d5711e92a3818 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php @@ -1014,11 +1014,10 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul */ protected function get_menu_id( $menu_item_id ) { $menu_ids = wp_get_post_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); - $menu_id = 0; if ( $menu_ids && ! is_wp_error( $menu_ids ) ) { - $menu_id = array_shift( $menu_ids ); + return array_shift( $menu_ids ); } - return $menu_id; + return 0; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index ec78557683679..e7e2c14654ff4 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -2000,12 +2000,8 @@ public function prepare_item_for_response( $item, $request ) { } if ( rest_is_field_included( 'template', $fields ) ) { - $template = get_page_template_slug( $post->ID ); - if ( $template ) { - $data['template'] = $template; - } else { - $data['template'] = ''; - } + $template = get_page_template_slug( $post->ID ); + $data['template'] = $template ? $template : ''; } if ( rest_is_field_included( 'format', $fields ) ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-site-health-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-site-health-controller.php index 41e15337add5f..7b355f02e1858 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-site-health-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-site-health-controller.php @@ -295,19 +295,15 @@ public function get_directory_sizes() { $data = array(); if ( isset( $value['size'] ) ) { - if ( is_string( $value['size'] ) ) { - $data['size'] = sanitize_text_field( $value['size'] ); - } else { - $data['size'] = (int) $value['size']; - } + $data['size'] = is_string( $value['size'] ) + ? sanitize_text_field( $value['size'] ) + : (int) $value['size']; } if ( isset( $value['debug'] ) ) { - if ( is_string( $value['debug'] ) ) { - $data['debug'] = sanitize_text_field( $value['debug'] ); - } else { - $data['debug'] = (int) $value['debug']; - } + $data['debug'] = is_string( $value['debug'] ) + ? sanitize_text_field( $value['debug'] ) + : (int) $value['debug']; } if ( ! empty( $value['raw'] ) ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php index 0b15f3494c4b5..13af63c8ef0cf 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php @@ -126,7 +126,11 @@ public function get_items( $request ) { $data = array(); foreach ( $taxonomies as $tax_type => $value ) { - if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) { + if ( empty( $value->show_in_rest ) ) { + continue; + } + + if ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) { continue; } @@ -441,12 +445,12 @@ public function get_item_schema() { * @return array Collection parameters. */ public function get_collection_params() { - $new_params = array(); - $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); - $new_params['type'] = array( - 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ), - 'type' => 'string', + return array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + 'type' => array( + 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ), + 'type' => 'string', + ), ); - return $new_params; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 009f5bedc9ace..cefa6ce3c5951 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -165,12 +165,7 @@ public function check_read_terms_permission_for_post( $post, $request ) { } // Otherwise grant access if the post is readable by the logged-in user. - if ( current_user_can( 'read_post', $post->ID ) ) { - return true; - } - - // Otherwise, deny access. - return false; + return current_user_can( 'read_post', $post->ID ); } /** diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index aa0bc644bc1ce..25dd4524f9564 100644 --- a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php +++ b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php @@ -85,12 +85,7 @@ public function get_value( $object_id, $request ) { $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false ); if ( $args['single'] ) { - if ( empty( $all_values ) ) { - $value = $args['schema']['default']; - } else { - $value = $all_values[0]; - } - + $value = empty( $all_values ) ? $args['schema']['default'] : $all_values[0]; $value = $this->prepare_value_for_response( $value, $request, $args ); } else { $value = array(); @@ -554,11 +549,7 @@ public function get_field_schema() { * @return mixed Value prepared for output. If a non-JsonSerializable object, null. */ public static function prepare_value( $value, $request, $args ) { - if ( $args['single'] ) { - $schema = $args['schema']; - } else { - $schema = $args['schema']['items']; - } + $schema = $args['single'] ? $args['schema'] : $args['schema']['items']; if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) { $value = static::get_empty_value_for_type( $schema['type'] ); From f0ae81de655ab36aba417bf0eccd51826492bd03 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Thu, 31 Oct 2024 13:45:13 +0100 Subject: [PATCH 5/5] 1. Remove unncessary variables. 2. Reorders order of expressions in if conditions to optimize performance. 3. Fixes issues with callling stating methods dynamically and non-static method statically. 4. Merges isset() and unset() statements. 5. Explicitly return null in WP_REST_Posts_Controller::handle_terms(). --- src/wp-includes/rest-api/class-wp-rest-request.php | 10 +++++----- src/wp-includes/rest-api/class-wp-rest-server.php | 2 +- .../class-wp-rest-attachments-controller.php | 9 ++++----- .../endpoints/class-wp-rest-autosaves-controller.php | 5 ++--- .../class-wp-rest-block-types-controller.php | 2 +- .../endpoints/class-wp-rest-blocks-controller.php | 3 +-- .../endpoints/class-wp-rest-comments-controller.php | 7 +++---- .../endpoints/class-wp-rest-font-faces-controller.php | 6 +++--- .../class-wp-rest-font-families-controller.php | 11 +++++------ .../class-wp-rest-navigation-fallback-controller.php | 4 +--- .../endpoints/class-wp-rest-plugins-controller.php | 5 ++++- .../endpoints/class-wp-rest-posts-controller.php | 4 +++- .../endpoints/class-wp-rest-revisions-controller.php | 2 +- .../class-wp-rest-template-autosaves-controller.php | 3 +-- .../endpoints/class-wp-rest-terms-controller.php | 6 ++---- .../class-wp-rest-url-details-controller.php | 10 +++++----- .../endpoints/class-wp-rest-users-controller.php | 11 ++++------- .../endpoints/class-wp-rest-widgets-controller.php | 5 ++--- .../rest-api/fields/class-wp-rest-meta-fields.php | 3 +-- 19 files changed, 49 insertions(+), 59 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index 6ed6ce667432c..ffae8d251bc9a 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -199,7 +199,7 @@ public static function canonicalize_header_name( $key ) { * @return string|null String value if set, null otherwise. */ public function get_header( $key ) { - $key = $this->canonicalize_header_name( $key ); + $key = static::canonicalize_header_name( $key ); if ( ! isset( $this->headers[ $key ] ) ) { return null; @@ -217,7 +217,7 @@ public function get_header( $key ) { * @return array|null List of string values if set, null otherwise. */ public function get_header_as_array( $key ) { - $key = $this->canonicalize_header_name( $key ); + $key = static::canonicalize_header_name( $key ); if ( ! isset( $this->headers[ $key ] ) ) { return null; @@ -235,7 +235,7 @@ public function get_header_as_array( $key ) { * @param string $value Header value, or list of values. */ public function set_header( $key, $value ) { - $key = $this->canonicalize_header_name( $key ); + $key = static::canonicalize_header_name( $key ); $value = (array) $value; $this->headers[ $key ] = $value; @@ -250,7 +250,7 @@ public function set_header( $key, $value ) { * @param string $value Header value, or list of values. */ public function add_header( $key, $value ) { - $key = $this->canonicalize_header_name( $key ); + $key = static::canonicalize_header_name( $key ); $value = (array) $value; if ( ! isset( $this->headers[ $key ] ) ) { @@ -268,7 +268,7 @@ public function add_header( $key, $value ) { * @param string $key Header name. */ public function remove_header( $key ) { - $key = $this->canonicalize_header_name( $key ); + $key = static::canonicalize_header_name( $key ); unset( $this->headers[ $key ] ); } diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index feedc373a0dea..a38da2f532e10 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1249,7 +1249,7 @@ protected function respond_to_request( $request, $route, $handler, $response ) { $response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request ); // Check permission specified on the route. - if ( ! is_wp_error( $response ) && ! empty( $handler['permission_callback'] ) ) { + if ( ! empty( $handler['permission_callback'] ) && ! is_wp_error( $response ) ) { $permission = call_user_func( $handler['permission_callback'], $request ); if ( is_wp_error( $permission ) ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 4609cd0c69e7d..4c6e72705b0f0 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -443,9 +443,8 @@ public function update_item( $request ) { wp_after_insert_post( $attachment, true, $attachment_before ); $response = $this->prepare_item_for_response( $attachment, $request ); - $response = rest_ensure_response( $response ); - return $response; + return rest_ensure_response( $response ); } /** @@ -720,7 +719,7 @@ public function edit_media_item( $request ) { $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] ); // Copy the EXIF metadata from the original attachment if not generated for the edited image. - if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) { + if ( isset( $image_meta['image_meta'], $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) { // Merge but skip empty values. foreach ( (array) $image_meta['image_meta'] as $key => $value ) { if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) { @@ -1140,7 +1139,7 @@ protected function upload_from_data( $data, $headers, $time = null ) { 'type' => $type, ); - $size_check = self::check_upload_size( $file_data ); + $size_check = $this->check_upload_size( $file_data ); if ( is_wp_error( $size_check ) ) { return $size_check; } @@ -1308,7 +1307,7 @@ protected function upload_from_file( $files, $headers, $time = null ) { $overrides['action'] = 'wp_handle_mock_upload'; } - $size_check = self::check_upload_size( $files['file'] ); + $size_check = $this->check_upload_size( $files['file'] ); if ( is_wp_error( $size_check ) ) { return $size_check; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php index 5545625df4609..ab615f1ff0600 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php @@ -285,8 +285,7 @@ public function get_item( $request ) { ); } - $response = $this->prepare_item_for_response( $autosave, $request ); - return $response; + return $this->prepare_item_for_response( $autosave, $request ); } /** @@ -418,7 +417,7 @@ public function create_post_autosave( $post_data, array $meta = array() ) { $revision_id = _wp_put_post_revision( $post_data, true ); } - if ( is_wp_error( $revision_id ) || 0 === $revision_id ) { + if ( 0 === $revision_id || is_wp_error( $revision_id ) ) { return $revision_id; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 263f2a8ef2066..53f853ad0fb2c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -305,7 +305,7 @@ public function prepare_item_for_response( $item, $request ) { if ( rest_is_field_included( $extra_field, $fields ) ) { if ( isset( $block_type->$extra_field ) ) { $field = $block_type->$extra_field; - if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) { + if ( is_array( $field ) && in_array( $extra_field, $deprecated_fields, true ) ) { // Since the schema only allows strings or null (but no arrays), we return the first array item. $field = ! empty( $field ) ? array_shift( $field ) : ''; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php index 6f600a04945f0..21c78a7fbd82d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php @@ -54,8 +54,7 @@ public function filter_response_by_context( $data, $context ) { * It doesn't make sense for a pattern to have rendered content on its own, * since rendering a block requires it to be inside a post or a page. */ - unset( $data['title']['rendered'] ); - unset( $data['content']['rendered'] ); + unset( $data['title']['rendered'], $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'] : ''; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 47dcc707714a4..a915167a16011 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -429,10 +429,9 @@ public function get_item( $request ) { return $comment; } - $data = $this->prepare_item_for_response( $comment, $request ); - $response = rest_ensure_response( $data ); + $data = $this->prepare_item_for_response( $comment, $request ); - return $response; + return rest_ensure_response( $data ); } /** @@ -616,7 +615,7 @@ public function create_item( $request ) { && empty( $prepared_comment['comment_author_email'] ) && empty( $prepared_comment['comment_author_url'] ); - if ( is_user_logged_in() && $missing_author ) { + if ( $missing_author && is_user_logged_in() ) { $user = wp_get_current_user(); $prepared_comment['user_id'] = $user->ID; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php index d4865531ca1b4..038388b0cd256 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php @@ -249,9 +249,9 @@ public function sanitize_font_face_settings( $value ) { $schema = $this->get_item_schema()['properties']['font_face_settings']['properties']; // Sanitize settings based on callbacks in the schema. - foreach ( $settings as $key => $value ) { - $sanitize_callback = $schema[ $key ]['arg_options']['sanitize_callback']; - $settings[ $key ] = call_user_func( $sanitize_callback, $value ); + foreach ( $settings as $setting_key => $setting_value ) { + $sanitize_callback = $schema[ $setting_key ]['arg_options']['sanitize_callback']; + $settings[ $setting_key ] = call_user_func( $sanitize_callback, $setting_value ); } return $settings; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php index cdce89c50373d..304770d3a8684 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php @@ -110,7 +110,7 @@ public function validate_font_family_settings( $value, $request ) { if ( isset( $settings['slug'] ) ) { return new WP_Error( 'rest_invalid_param', - /* translators: %s: Name of parameter being updated: font_family_settings[slug]". */ + /* translators: %s: Name of parameter being updated: "font_family_settings[slug]". */ sprintf( __( '%s cannot be updated.' ), 'font_family_settings[slug]' ), array( 'status' => 400 ) ); @@ -154,9 +154,9 @@ public function sanitize_font_family_settings( $value ) { $schema = $this->get_item_schema()['properties']['font_family_settings']['properties']; // Sanitize settings based on callbacks in the schema. - foreach ( $settings as $key => $value ) { - $sanitize_callback = $schema[ $key ]['arg_options']['sanitize_callback']; - $settings[ $key ] = call_user_func( $sanitize_callback, $value ); + foreach ( $settings as $setting_key => $setting_value ) { + $sanitize_callback = $schema[ $setting_key ]['arg_options']['sanitize_callback']; + $settings[ $setting_key ] = call_user_func( $sanitize_callback, $setting_value ); } return $settings; @@ -534,8 +534,7 @@ protected function prepare_item_for_database( $request ) { $prepared_post->post_name = sanitize_title( $settings['slug'] ); // Remove duplicate information from settings. - unset( $settings['name'] ); - unset( $settings['slug'] ); + unset( $settings['name'], $settings['slug'] ); $prepared_post->post_content = wp_json_encode( $settings ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php index 575a1a900e1d1..d696c39d1420e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php @@ -106,9 +106,7 @@ public function get_item( $request ) { return rest_ensure_response( new WP_Error( 'no_fallback_menu', __( 'No fallback menu found.' ), array( 'status' => 404 ) ) ); } - $response = $this->prepare_item_for_response( $post, $request ); - - return $response; + return $this->prepare_item_for_response( $post, $request ); } /** diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php index 89fa98e8060b0..00e14b93b2d1c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php @@ -694,7 +694,10 @@ protected function get_plugin_status( $plugin ) { * @return true|WP_Error */ protected function plugin_status_permission_check( $plugin, $new_status, $current_status ) { - if ( is_multisite() && ( 'network-active' === $current_status || 'network-active' === $new_status ) && ! current_user_can( 'manage_network_plugins' ) ) { + if ( ( 'network-active' === $current_status || 'network-active' === $new_status ) + && is_multisite() + && ! current_user_can( 'manage_network_plugins' ) + ) { return new WP_Error( 'rest_cannot_manage_network_plugins', __( 'Sorry, you are not allowed to manage network plugins.' ), diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index e7e2c14654ff4..33d2eb877d98c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1197,7 +1197,7 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul } // Map to proper WP_Query orderby param. - if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) { + if ( isset( $query_args['orderby'], $request['orderby'] ) ) { $orderby_mappings = array( 'id' => 'ID', 'include' => 'post__in', @@ -1648,6 +1648,8 @@ protected function handle_terms( $post_id, $request ) { return $result; } } + + return null; } /** diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index 55adc041b5039..2b4cce0c5ee6c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -540,7 +540,7 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul } // Map to proper WP_Query orderby param. - if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) { + if ( isset( $query_args['orderby'], $request['orderby'] ) ) { $orderby_mappings = array( 'id' => 'ID', 'include' => 'post__in', diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php index c996894a5933c..bc7059c28ce40 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php @@ -220,8 +220,7 @@ public function get_item( $request ) { ); } - $response = $this->prepare_item_for_response( $autosave, $request ); - return $response; + return $this->prepare_item_for_response( $autosave, $request ); } /** diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index cefa6ce3c5951..7b12ff138397a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -1196,9 +1196,7 @@ public function get_collection_params() { */ protected function check_is_taxonomy_allowed( $taxonomy ) { $taxonomy_obj = get_taxonomy( $taxonomy ); - if ( $taxonomy_obj && ! empty( $taxonomy_obj->show_in_rest ) ) { - return true; - } - return false; + + return $taxonomy_obj && ! empty( $taxonomy_obj->show_in_rest ); } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php index ab126f981c340..5e7eb0be2d665 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php @@ -150,7 +150,7 @@ public function parse_url_details( $request ) { $remote_url_response = $this->get_remote_url( $url ); // Exit if we don't have a valid body or it's empty. - if ( is_wp_error( $remote_url_response ) || empty( $remote_url_response ) ) { + if ( empty( $remote_url_response ) || is_wp_error( $remote_url_response ) ) { return $remote_url_response; } @@ -333,7 +333,7 @@ private function get_icon( $html, $url ) { return $icon; } $parsed_url = parse_url( $url ); - if ( isset( $parsed_url['scheme'] ) && isset( $parsed_url['host'] ) ) { + if ( isset( $parsed_url['scheme'], $parsed_url['host'] ) ) { $root_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/'; $icon = WP_Http::make_absolute_url( $icon, $root_url ); } @@ -406,7 +406,7 @@ private function get_image( $meta_elements, $url ) { // Attempt to convert relative URLs to absolute. $parsed_url = parse_url( $url ); - if ( isset( $parsed_url['scheme'] ) && isset( $parsed_url['host'] ) ) { + if ( isset( $parsed_url['scheme'], $parsed_url['host'] ) ) { $root_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/'; $image = WP_Http::make_absolute_url( $image, $root_url ); } @@ -426,8 +426,8 @@ private function get_image( $meta_elements, $url ) { */ private function prepare_metadata_for_output( $metadata ) { $metadata = html_entity_decode( $metadata, ENT_QUOTES, get_bloginfo( 'charset' ) ); - $metadata = wp_strip_all_tags( $metadata ); - return $metadata; + + return wp_strip_all_tags( $metadata ); } /** diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 778cbb150a3aa..e5df77851e3a1 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -482,10 +482,9 @@ public function get_item( $request ) { return $user; } - $user = $this->prepare_item_for_response( $user, $request ); - $response = rest_ensure_response( $user ); + $user = $this->prepare_item_for_response( $user, $request ); - return $response; + return rest_ensure_response( $user ); } /** @@ -509,9 +508,8 @@ public function get_current_item( $request ) { $user = wp_get_current_user(); $response = $this->prepare_item_for_response( $user, $request ); - $response = rest_ensure_response( $response ); - return $response; + return rest_ensure_response( $response ); } /** @@ -814,9 +812,8 @@ public function update_item( $request ) { do_action( 'rest_after_insert_user', $user, $request, false ); $response = $this->prepare_item_for_response( $user, $request ); - $response = rest_ensure_response( $response ); - return $response; + return rest_ensure_response( $response ); } /** diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php index d620dc0c1c59f..5746ab5f48cff 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php @@ -690,9 +690,8 @@ public function prepare_item_for_response( $item, $request ) { 'instance' => null, ); - if ( - rest_is_field_included( 'rendered', $fields ) && - 'wp_inactive_widgets' !== $sidebar_id + if ( 'wp_inactive_widgets' !== $sidebar_id + && rest_is_field_included( 'rendered', $fields ) ) { $prepared['rendered'] = trim( wp_render_widget( $widget_id, $sidebar_id ) ); } diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index 25dd4524f9564..1f85ba78488a6 100644 --- a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php +++ b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php @@ -317,8 +317,7 @@ function ( $stored_value ) use ( $meta_key, $subtype, $value ) { $remove_key = $remove_keys[0]; - unset( $to_remove[ $remove_key ] ); - unset( $to_add[ $add_key ] ); + unset( $to_remove[ $remove_key ], $to_add[ $add_key ] ); } /*