From 03122ef7e1023af353ca9a6d42e1167bec5e519a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 1 Aug 2019 11:27:28 +0000 Subject: [PATCH] Users: Deprecate `update_user_status()` in favor of `wp_update_user()`. Props spacedmonkey, SergeyBiryukov. Fixes #45747. git-svn-id: https://develop.svn.wordpress.org/trunk@45708 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ms.php | 53 ------------------- src/wp-admin/network/users.php | 15 +++++- src/wp-includes/ms-deprecated.php | 45 ++++++++++++++++ src/wp-includes/user.php | 44 +++++++++++++-- tests/phpunit/tests/user/multisite.php | 2 +- .../tests/user/wpAuthenticateSpamCheck.php | 2 +- 6 files changed, 100 insertions(+), 61 deletions(-) diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index df39d492b1..a0a3697cb0 100644 --- a/src/wp-admin/includes/ms.php +++ b/src/wp-admin/includes/ms.php @@ -303,59 +303,6 @@ function upload_space_setting( $id ) { update( $wpdb->users, array( sanitize_key( $pref ) => $value ), array( 'ID' => $id ) ); - - $user = new WP_User( $id ); - clean_user_cache( $user ); - - if ( $pref == 'spam' ) { - if ( $value == 1 ) { - /** - * Fires after the user is marked as a SPAM user. - * - * @since 3.0.0 - * - * @param int $id ID of the user marked as SPAM. - */ - do_action( 'make_spam_user', $id ); - } else { - /** - * Fires after the user is marked as a HAM user. Opposite of SPAM. - * - * @since 3.0.0 - * - * @param int $id ID of the user marked as HAM. - */ - do_action( 'make_ham_user', $id ); - } - } - - return $value; -} - /** * Cleans the user cache for a specific user. * diff --git a/src/wp-admin/network/users.php b/src/wp-admin/network/users.php index aa5bace85f..4eac1e3f56 100644 --- a/src/wp-admin/network/users.php +++ b/src/wp-admin/network/users.php @@ -76,22 +76,33 @@ if ( isset( $_GET['action'] ) ) { $userfunction = 'all_spam'; $blogs = get_blogs_of_user( $user_id, true ); + foreach ( (array) $blogs as $details ) { if ( $details->userblog_id != get_network()->site_id ) { // main blog not a spam ! update_blog_status( $details->userblog_id, 'spam', '1' ); } } - update_user_status( $user_id, 'spam', '1' ); + + $user_data = $user->to_array(); + $user_data['spam'] = '1'; + + wp_update_user( $user_data ); break; case 'notspam': + $user = get_userdata( $user_id ); + $userfunction = 'all_notspam'; $blogs = get_blogs_of_user( $user_id, true ); + foreach ( (array) $blogs as $details ) { update_blog_status( $details->userblog_id, 'spam', '0' ); } - update_user_status( $user_id, 'spam', '0' ); + $user_data = $user->to_array(); + $user_data['spam'] = '0'; + + wp_update_user( $user_data ); break; } } diff --git a/src/wp-includes/ms-deprecated.php b/src/wp-includes/ms-deprecated.php index 6763fcb384..7e2cedd5a3 100644 --- a/src/wp-includes/ms-deprecated.php +++ b/src/wp-includes/ms-deprecated.php @@ -685,3 +685,48 @@ function install_blog_defaults( $blog_id, $user_id ) { $wpdb->suppress_errors( $suppress ); } + +/** + * Update the status of a user in the database. + * + * Previously used in core to mark a user as spam or "ham" (not spam) in Multisite. + * + * @since 3.0.0 + * @deprecated 5.3.0 Use wp_update_user() + * @see wp_update_user() + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param int $id The user ID. + * @param string $pref The column in the wp_users table to update the user's status + * in (presumably user_status, spam, or deleted). + * @param int $value The new status for the user. + * @param null $deprecated Deprecated as of 3.0.2 and should not be used. + * @return int The initially passed $value. + */ +function update_user_status( $id, $pref, $value, $deprecated = null ) { + global $wpdb; + + _deprecated_function( __FUNCTION__, '5.3.0', 'wp_update_user()' ); + + if ( null !== $deprecated ) { + _deprecated_argument( __FUNCTION__, '3.0.2' ); + } + + $wpdb->update( $wpdb->users, array( sanitize_key( $pref ) => $value ), array( 'ID' => $id ) ); + + $user = new WP_User( $id ); + clean_user_cache( $user ); + + if ( $pref == 'spam' ) { + if ( $value == 1 ) { + /** This filter is documented in wp-includes/user.php */ + do_action( 'make_spam_user', $id ); + } else { + /** This filter is documented in wp-includes/user.php */ + do_action( 'make_ham_user', $id ); + } + } + + return $value; +} diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 89edb3b315..c32745adee 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1468,14 +1468,15 @@ function validate_username( $username ) { * * Most of the `$userdata` array fields have filters associated with the values. Exceptions are * 'ID', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', 'use_ssl', - * 'user_registered', and 'role'. The filters have the prefix 'pre_user_' followed by the field - * name. An example using 'description' would have the filter called, 'pre_user_description' that - * can be hooked into. + * 'user_registered', 'spam', and 'role'. The filters have the prefix 'pre_user_' followed by the + * field name. An example using 'description' would have the filter called, 'pre_user_description' + * that can be hooked into. * * @since 2.0.0 * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact * methods for new installations. See wp_get_user_contact_methods(). * @since 4.7.0 The user's locale can be passed to `$userdata`. + * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only). * * @global wpdb $wpdb WordPress database abstraction object. * @@ -1509,6 +1510,8 @@ function validate_username( $username ) { * @type bool $use_ssl Whether the user should always access the admin over * https. Default false. * @type string $user_registered Date the user registered. Format is 'Y-m-d H:i:s'. + * @type bool $spam Multisite only. Whether the user is marked as spam. + * Default false. * @type string|bool $show_admin_bar_front Whether to display the Admin Bar for the user on the * site's front end. Default true. * @type string $role User's role. @@ -1644,6 +1647,13 @@ function wp_insert_user( $userdata ) { ) { return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); } + + if ( isset( $userdata['spam'] ) && ! is_multisite() ) { + return new WP_Error( 'no_spam', __( 'Sorry, marking a user as spam is only supported on Multisite.' ) ); + } + + $spam = empty( $userdata['spam'] ) ? 0 : (bool) $userdata['spam']; + $nickname = empty( $userdata['nickname'] ) ? $user_login : $userdata['nickname']; /** @@ -1723,7 +1733,7 @@ function wp_insert_user( $userdata ) { $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color']; $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color ); - $meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : $userdata['use_ssl']; + $meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : (bool) $userdata['use_ssl']; $user_registered = empty( $userdata['user_registered'] ) ? gmdate( 'Y-m-d H:i:s' ) : $userdata['user_registered']; @@ -1752,6 +1762,10 @@ function wp_insert_user( $userdata ) { $data = $data + compact( 'user_login' ); } + if ( is_multisite() ) { + $data = $data + compact( 'spam' ); + } + /** * Filters user data before the record is created or updated. * @@ -1848,6 +1862,28 @@ function wp_insert_user( $userdata ) { * @param WP_User $old_user_data Object containing user's data prior to update. */ do_action( 'profile_update', $user_id, $old_user_data ); + + if ( isset( $userdata['spam'] ) && $userdata['spam'] != $old_user_data->spam ) { + if ( $userdata['spam'] == 1 ) { + /** + * Fires after the user is marked as a SPAM user. + * + * @since 3.0.0 + * + * @param int $user_id ID of the user marked as SPAM. + */ + do_action( 'make_spam_user', $user_id ); + } else { + /** + * Fires after the user is marked as a HAM user. Opposite of SPAM. + * + * @since 3.0.0 + * + * @param int $user_id ID of the user marked as HAM. + */ + do_action( 'make_ham_user', $user_id ); + } + } } else { /** * Fires immediately after a new user is registered. diff --git a/tests/phpunit/tests/user/multisite.php b/tests/phpunit/tests/user/multisite.php index 6cb8df74da..b5942eeb79 100644 --- a/tests/phpunit/tests/user/multisite.php +++ b/tests/phpunit/tests/user/multisite.php @@ -212,7 +212,7 @@ if ( is_multisite() ) : 'user_login' => $spam_username, ) ); - update_user_status( $spam_user_id, 'spam', '1' ); + wp_update_user( array( 'ID' => $spam_user_id, 'spam' => '1' ) ); $this->assertTrue( is_user_spammy( $spam_username ) ); $this->assertFalse( is_user_spammy( 'testuser1' ) ); diff --git a/tests/phpunit/tests/user/wpAuthenticateSpamCheck.php b/tests/phpunit/tests/user/wpAuthenticateSpamCheck.php index 8ff969949a..9860fb97a5 100644 --- a/tests/phpunit/tests/user/wpAuthenticateSpamCheck.php +++ b/tests/phpunit/tests/user/wpAuthenticateSpamCheck.php @@ -34,7 +34,7 @@ class Tests_User_WpAuthenticateSpamCheck extends WP_UnitTestCase { */ function test_wp_authenticate_spam_check_returns_wp_error_when_flagged() { $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); - update_user_status( $user_id, 'spam', 1 ); + wp_update_user( array( 'ID' => $user_id, 'spam' => '1' ) ); $user = new WP_User( $user_id ); $actual_user = wp_authenticate_spam_check( $user ); wpmu_delete_user( $user_id );