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 9b25cf7974cbc..7060d806a6106 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 @@ -622,9 +622,6 @@ public function create_item( $request ) { } return $error; } - } - - if ( is_multisite() ) { $user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email ); if ( ! $user_id ) { @@ -637,21 +634,35 @@ public function create_item( $request ) { $user->ID = $user_id; $user_id = wp_update_user( wp_slash( (array) $user ) ); + } else { + $user_id = wp_insert_user( wp_slash( (array) $user ) ); + } - if ( is_wp_error( $user_id ) ) { - return $user_id; + if ( is_wp_error( $user_id ) ) { + if ( in_array( $user_id->get_error_code(), array( 'existing_user_login' ) ) ) { + return new WP_Error( + 'rest_existing_user_login', + __( 'Sorry, that username already exists!' ), + array( 'status' => 409 ) + ); } + if ( in_array( $user_id->get_error_code(), array( 'existing_user_email' ) ) ) { + return new WP_Error( + 'rest_user_existing_user_email', + __( 'Sorry, that email address is already used!",' ), + array( 'status' => 409 ) + ); + } + + return $user_id; + } + + if ( is_multisite() ) { $result = add_user_to_blog( get_site()->id, $user_id, '' ); if ( is_wp_error( $result ) ) { return $result; } - } else { - $user_id = wp_insert_user( wp_slash( (array) $user ) ); - - if ( is_wp_error( $user_id ) ) { - return $user_id; - } } $user = get_user_by( 'id', $user_id ); diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index b78e95b95f48d..931027666bc27 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -1689,6 +1689,48 @@ public function test_create_user_invalid_role() { $this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 ); } + /** + * @ticket 41672 + */ + public function test_create_user_with_existing_username_or_email() { + $this->allow_user_to_manage_multisite(); + wp_set_current_user( self::$user ); + + // Create User + $params = array( + 'username' => 'testjsonuser', + 'password' => 'testjsonpassword', + 'email' => 'testjson@example.com', + ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); + $request->add_header( 'content-type', 'application/json' ); + $request->set_body( wp_json_encode( $params ) ); + $response = rest_get_server()->dispatch( $request ); + $this->check_add_edit_user_response( $response ); + + // Make request again, expecting existing_user_login response + $params = array( + 'username' => 'testjsonuser', + 'password' => 'testjsonpassword', + 'email' => 'testjson1@example.com', + ); + + $request->set_body( wp_json_encode( $params ) ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_existing_user_login', $response, 409 ); + + // Make request again, expecting existing_user_email response + $params = array( + 'username' => 'testjsonuser1', + 'password' => 'testjsonpassword', + 'email' => 'testjson@example.com', + ); + $request->set_body( wp_json_encode( $params ) ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_user_existing_user_email', $response, 409 ); + } + public function test_update_item() { $user_id = self::factory()->user->create( array(