Users: Deprecate update_user_status() in favor of wp_update_user().

Props spacedmonkey, SergeyBiryukov.
Fixes #45747.
Built from https://develop.svn.wordpress.org/trunk@45708


git-svn-id: http://core.svn.wordpress.org/trunk@45519 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-08-01 11:28:54 +00:00
parent 2d574d673f
commit b32cff72c2
5 changed files with 99 additions and 60 deletions

View File

@ -303,59 +303,6 @@ function upload_space_setting( $id ) {
<?php <?php
} }
/**
* Update the status of a user in the database.
*
* Used in core to mark a user as spam or "ham" (not spam) in Multisite.
*
* @since 3.0.0
*
* @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;
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 ) {
/**
* 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. * Cleans the user cache for a specific user.
* *

View File

@ -76,22 +76,33 @@ if ( isset( $_GET['action'] ) ) {
$userfunction = 'all_spam'; $userfunction = 'all_spam';
$blogs = get_blogs_of_user( $user_id, true ); $blogs = get_blogs_of_user( $user_id, true );
foreach ( (array) $blogs as $details ) { foreach ( (array) $blogs as $details ) {
if ( $details->userblog_id != get_network()->site_id ) { // main blog not a spam ! if ( $details->userblog_id != get_network()->site_id ) { // main blog not a spam !
update_blog_status( $details->userblog_id, 'spam', '1' ); 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; break;
case 'notspam': case 'notspam':
$user = get_userdata( $user_id );
$userfunction = 'all_notspam'; $userfunction = 'all_notspam';
$blogs = get_blogs_of_user( $user_id, true ); $blogs = get_blogs_of_user( $user_id, true );
foreach ( (array) $blogs as $details ) { foreach ( (array) $blogs as $details ) {
update_blog_status( $details->userblog_id, 'spam', '0' ); 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; break;
} }
} }

View File

@ -685,3 +685,48 @@ function install_blog_defaults( $blog_id, $user_id ) {
$wpdb->suppress_errors( $suppress ); $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;
}

View File

@ -1468,14 +1468,15 @@ function validate_username( $username ) {
* *
* Most of the `$userdata` array fields have filters associated with the values. Exceptions are * 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', * '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 * 'user_registered', 'spam', and 'role'. The filters have the prefix 'pre_user_' followed by the
* name. An example using 'description' would have the filter called, 'pre_user_description' that * field name. An example using 'description' would have the filter called, 'pre_user_description'
* can be hooked into. * that can be hooked into.
* *
* @since 2.0.0 * @since 2.0.0
* @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact * @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(). * methods for new installations. See wp_get_user_contact_methods().
* @since 4.7.0 The user's locale can be passed to `$userdata`. * @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. * @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 * @type bool $use_ssl Whether the user should always access the admin over
* https. Default false. * https. Default false.
* @type string $user_registered Date the user registered. Format is 'Y-m-d H:i:s'. * @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 * @type string|bool $show_admin_bar_front Whether to display the Admin Bar for the user on the
* site's front end. Default true. * site's front end. Default true.
* @type string $role User's role. * @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!' ) ); 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']; $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']; $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
$meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $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']; $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' ); $data = $data + compact( 'user_login' );
} }
if ( is_multisite() ) {
$data = $data + compact( 'spam' );
}
/** /**
* Filters user data before the record is created or updated. * 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. * @param WP_User $old_user_data Object containing user's data prior to update.
*/ */
do_action( 'profile_update', $user_id, $old_user_data ); 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 { } else {
/** /**
* Fires immediately after a new user is registered. * Fires immediately after a new user is registered.

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.3-alpha-45707'; $wp_version = '5.3-alpha-45708';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.