From 43eb2619e98e472f1608d0a72afd23eb699b20f8 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 2 Dec 2016 06:59:34 +0000 Subject: [PATCH] REST API: Require the reassign parameter when deleting users. When deleting a user through the WordPress admin, a specific decision is presented - whether to assign all of the user's posts to another user, or to delete all of the posts. This change requires `reassign` as a parameter in the corresponding REST API endpoint, so that content isn't accidentally lost. Merges [39426] to the 4.7 branch. Props jeremyfelt. Fixes #39000. Built from https://develop.svn.wordpress.org/branches/4.7@39427 git-svn-id: http://core.svn.wordpress.org/branches/4.7@39367 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-users-controller.php | 31 ++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 31 insertions(+), 2 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 823e5d571e..c81d092efe 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 @@ -92,6 +92,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller { 'reassign' => array( 'type' => 'integer', 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), + 'required' => true, + 'sanitize_callback' => array( $this, 'check_reassign' ), ), ), ), @@ -125,6 +127,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller { 'reassign' => array( 'type' => 'integer', 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), + 'required' => true, + 'sanitize_callback' => array( $this, 'check_reassign' ), ), ), ), @@ -132,6 +136,31 @@ class WP_REST_Users_Controller extends WP_REST_Controller { )); } + /** + * Checks for a valid value for the reassign parameter when deleting users. + * + * The value can be an integer, 'false', false, or ''. + * + * @since 4.7.0 + * + * @param int|bool $value The value passed to the reassign parameter. + * @param WP_REST_Request $request Full details about the request. + * @param string $param The parameter that is being sanitized. + * + * @return int|bool|WP_Error + */ + public function check_reassign( $value, $request, $param ) { + if ( is_numeric( $value ) ) { + return $value; + } + + if ( empty( $value ) || false === $value || 'false' === $value ) { + return false; + } + + return new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) ); + } + /** * Permissions check for getting all users. * @@ -673,7 +702,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller { */ public function delete_item( $request ) { $id = (int) $request['id']; - $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null; + $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] ); $force = isset( $request['force'] ) ? (bool) $request['force'] : false; // We don't support trashing for users. diff --git a/wp-includes/version.php b/wp-includes/version.php index 5aa39c3706..6880715dd1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-RC1-39425'; +$wp_version = '4.7-RC1-39427'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.