From 31a63099028d88f9f306d61d62e0d61a70d70136 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 20 Aug 2019 17:25:02 +0000 Subject: [PATCH] Users: In `wp_insert_user()`, account for the `wp_pre_insert_user_data` filter returning empty data. Props juliobox, SergeyBiryukov. Fixes #47902. git-svn-id: https://develop.svn.wordpress.org/trunk@45858 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 4 ++++ tests/phpunit/tests/user.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 9c8d789768..f43f5e9a94 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1794,6 +1794,10 @@ function wp_insert_user( $userdata ) { */ $data = apply_filters( 'wp_pre_insert_user_data', $data, $update, $update ? (int) $ID : null ); + if ( empty( $data ) || ! is_array( $data ) ) { + return new WP_Error( 'empty_data', __( 'Not enough data to create this user.' ) ); + } + if ( $update ) { if ( $user_email !== $old_user_data->user_email ) { $data['user_activation_key'] = ''; diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 13824009b6..720fb137b7 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -961,6 +961,23 @@ class Tests_User extends WP_UnitTestCase { ); $this->assertWPError( $u ); + $this->assertSame( 'invalid_user_id', $u->get_error_code() ); + } + + /** + * @ticket 47902 + */ + public function test_wp_insert_user_with_empty_data() { + global $wpdb; + + add_filter( 'wp_pre_insert_user_data', '__return_empty_array' ); + + $u = self::factory()->user->create(); + + remove_filter( 'wp_pre_insert_user_data', '__return_empty_array' ); + + $this->assertWPError( $u ); + $this->assertSame( 'empty_data', $u->get_error_code() ); } /**