From 624def0ec3ae39b2f3f8486e76d3f817b315a41c Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Wed, 2 Nov 2016 06:53:32 +0000 Subject: [PATCH] REST API: Add update and delete endpoints to /users/me Now that /users/me is a standalone resource, it should have all the standard endpoints for a resource. Props pento. Fixes #38521 (hopefully). Built from https://develop.svn.wordpress.org/trunk@39092 git-svn-id: http://core.svn.wordpress.org/trunk@39034 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-users-controller.php | 88 ++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index f23a6cd49b..9a8cc21245 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -95,10 +95,30 @@ class WP_REST_Users_Controller extends WP_REST_Controller { ) ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/me', array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_current_item' ), - 'args' => array( - 'context' => array(), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_current_item' ), + 'args' => array( + 'context' => array(), + ), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'update_current_item' ), + 'permission_callback' => array( $this, 'update_current_item_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + array( + 'methods' => WP_REST_Server::DELETABLE, + 'callback' => array( $this, 'delete_current_item' ), + 'permission_callback' => array( $this, 'delete_current_item_permissions_check' ), + 'args' => array( + 'force' => array( + 'default' => false, + 'description' => __( 'Required to be true, as resource does not support trashing.' ), + ), + 'reassign' => array(), + ), ), 'schema' => array( $this, 'get_public_item_schema' ), )); @@ -567,6 +587,36 @@ class WP_REST_Users_Controller extends WP_REST_Controller { return $response; } + /** + * Checks if a given request has access to update the current user. + * + * @since 4.7.0 + * @access public + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. + */ + public function update_current_item_permissions_check( $request ) { + $request['id'] = get_current_user_id(); + + return $this->update_item_permissions_check( $request ); + } + + /** + * Updates the current user. + * + * @since 4.7.0 + * @access public + * + * @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. + */ + function update_current_item( $request ) { + $request['id'] = get_current_user_id(); + + return $this->update_item( $request ); + } + /** * Checks if a given request has access delete a user. * @@ -645,6 +695,36 @@ class WP_REST_Users_Controller extends WP_REST_Controller { return $response; } + /** + * Checks if a given request has access to delete the current user. + * + * @since 4.7.0 + * @access public + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. + */ + public function delete_current_item_permissions_check( $request ) { + $request['id'] = get_current_user_id(); + + return $this->delete_item_permissions_check( $request ); + } + + /** + * Deletes the current user. + * + * @since 4.7.0 + * @access public + * + * @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. + */ + function delete_current_item( $request ) { + $request['id'] = get_current_user_id(); + + return $this->delete_item( $request ); + } + /** * Prepares a single user output for response. * diff --git a/wp-includes/version.php b/wp-includes/version.php index ec36fca073..19f2875a10 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-beta1-39091'; +$wp_version = '4.7-beta1-39092'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.