From aa99d7e7a34a5f460ddb85f2e009273408fc0585 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 30 Jun 2021 12:33:43 +0000 Subject: [PATCH] Docs: Document the globals used in some REST API methods. See #53399. git-svn-id: https://develop.svn.wordpress.org/trunk@51278 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 8 +++--- ...lass-wp-rest-block-renderer-controller.php | 8 ++++-- .../endpoints/class-wp-rest-controller.php | 9 ++++--- .../class-wp-rest-plugins-controller.php | 27 +++++++++++++++---- .../class-wp-rest-users-controller.php | 2 ++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 9a615419b9..7480fd5c64 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -118,8 +118,8 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f * @global array $wp_rest_additional_fields Holds registered fields, organized * by object type. * - * @param string|array $object_type Object(s) the field is being registered - * to, "post"|"term"|"comment" etc. + * @param string|array $object_type Object(s) the field is being registered to, + * "post"|"term"|"comment" etc. * @param string $attribute The attribute name. * @param array $args { * Optional. An array of arguments used to handle the registered field. @@ -135,6 +135,8 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f * } */ function register_rest_field( $object_type, $attribute, $args = array() ) { + global $wp_rest_additional_fields; + $defaults = array( 'get_callback' => null, 'update_callback' => null, @@ -143,8 +145,6 @@ function register_rest_field( $object_type, $attribute, $args = array() ) { $args = wp_parse_args( $args, $defaults ); - global $wp_rest_additional_fields; - $object_types = (array) $object_type; foreach ( $object_types as $object_type ) { 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 69a45dce88..3e4e8eb794 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 @@ -103,6 +103,8 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { * * @since 5.0.0 * + * @global WP_Post $post Global post object. + * * @param WP_REST_Request $request Request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */ @@ -111,7 +113,7 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0; - if ( 0 < $post_id ) { + if ( $post_id > 0 ) { $post = get_post( $post_id ); if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { @@ -143,6 +145,8 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { * * @since 5.0.0 * + * @global WP_Post $post Global post object. + * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ @@ -151,7 +155,7 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { $post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0; - if ( 0 < $post_id ) { + if ( $post_id > 0 ) { $post = get_post( $post_id ); // Set up postdata since this will be needed if post_id was set. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index 3417a6fe75..b1cb4be457 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -504,11 +504,14 @@ abstract class WP_REST_Controller { * * @since 4.7.0 * + * @global array $wp_rest_additional_fields Holds registered fields, organized by object type. + * * @param string $object_type Optional. The object type. - * @return array Registered additional fields (if any), empty array if none or if the object type could - * not be inferred. + * @return array Registered additional fields (if any), empty array if none or if the object type + * could not be inferred. */ protected function get_additional_fields( $object_type = null ) { + global $wp_rest_additional_fields; if ( ! $object_type ) { $object_type = $this->get_object_type(); @@ -518,8 +521,6 @@ abstract class WP_REST_Controller { return array(); } - global $wp_rest_additional_fields; - if ( ! $wp_rest_additional_fields || ! isset( $wp_rest_additional_fields[ $object_type ] ) ) { return array(); } 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 7cb895670a..a4a843bf83 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 @@ -265,10 +265,14 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller { * * @since 5.5.0 * + * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. + * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function create_item( $request ) { + global $wp_filesystem; + require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; @@ -329,19 +333,32 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller { } if ( is_null( $result ) ) { - global $wp_filesystem; // Pass through the error from WP_Filesystem if one was raised. - if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { - return new WP_Error( 'unable_to_connect_to_filesystem', $wp_filesystem->errors->get_error_message(), array( 'status' => 500 ) ); + if ( $wp_filesystem instanceof WP_Filesystem_Base + && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() + ) { + return new WP_Error( + 'unable_to_connect_to_filesystem', + $wp_filesystem->errors->get_error_message(), + array( 'status' => 500 ) + ); } - return new WP_Error( 'unable_to_connect_to_filesystem', __( 'Unable to connect to the filesystem. Please confirm your credentials.' ), array( 'status' => 500 ) ); + return new WP_Error( + 'unable_to_connect_to_filesystem', + __( 'Unable to connect to the filesystem. Please confirm your credentials.' ), + array( 'status' => 500 ) + ); } $file = $upgrader->plugin_info(); if ( ! $file ) { - return new WP_Error( 'unable_to_determine_installed_plugin', __( 'Unable to determine what plugin was installed.' ), array( 'status' => 500 ) ); + return new WP_Error( + 'unable_to_determine_installed_plugin', + __( 'Unable to determine what plugin was installed.' ), + array( 'status' => 500 ) + ); } if ( 'inactive' !== $request['status'] ) { 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 962ff4b3d9..63ad45b8ec 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 @@ -1173,6 +1173,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller { * * @since 4.7.0 * + * @global WP_Roles $wp_roles WordPress role management object. + * * @param int $user_id User ID. * @param array $roles New user roles. * @return true|WP_Error True if the current user is allowed to make the role change,