Allow passing stdClass and WP_User to wp_insert_user() and wp_update_user(). Introduce WP_User::to_array(). Eliminate uses of get_object_vars() when passing to wp_*_user(). fixes #21429

git-svn-id: http://core.svn.wordpress.org/trunk@21496 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan Boren 2012-08-10 15:36:54 +00:00
parent ed1698e29b
commit cbd6a8becd
4 changed files with 30 additions and 8 deletions

View File

@ -156,9 +156,9 @@ function edit_user( $user_id = 0 ) {
return $errors;
if ( $update ) {
$user_id = wp_update_user( get_object_vars( $user ) );
$user_id = wp_update_user( $user );
} else {
$user_id = wp_insert_user( get_object_vars( $user ) );
$user_id = wp_insert_user( $user );
wp_new_user_notification( $user_id, isset($_POST['send_password']) ? $pass1 : '' );
}
return $user_id;

View File

@ -82,11 +82,12 @@ if ( is_multisite() && ! current_user_can( 'manage_network_users' ) && $user_id
if ( is_multisite() && IS_PROFILE_PAGE && isset( $_GET[ 'newuseremail' ] ) && $current_user->ID ) {
$new_email = get_option( $current_user->ID . '_new_email' );
if ( $new_email[ 'hash' ] == $_GET[ 'newuseremail' ] ) {
$user = new stdClass;
$user->ID = $current_user->ID;
$user->user_email = esc_html( trim( $new_email[ 'newemail' ] ) );
if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $current_user->user_login ) ) )
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
wp_update_user( get_object_vars( $user ) );
wp_update_user( $user );
delete_option( $current_user->ID . '_new_email' );
wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
die();

View File

@ -665,6 +665,17 @@ class WP_User {
return $this->__isset( $key );
}
/*
* Return an array representation.
*
* @since 3.5.0
*
* @return array Array representation.
*/
function to_array() {
return get_object_vars( $this->data );
}
/**
* Set up capability object properties.
*

View File

@ -1240,13 +1240,18 @@ function validate_username( $username ) {
* @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
* @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
*
* @param array $userdata An array of user data.
* @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
* @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
*/
function wp_insert_user($userdata) {
function wp_insert_user( $userdata ) {
global $wpdb;
extract($userdata, EXTR_SKIP);
if ( is_a( $userdata, 'stdClass' ) )
$userdata = get_object_vars( $userdata );
elseif ( is_a( $userdata, 'WP_User' ) )
$userdata = $userdata->to_array();
extract( $userdata, EXTR_SKIP );
// Are we updating or creating?
if ( !empty($ID) ) {
@ -1387,16 +1392,21 @@ function wp_insert_user($userdata) {
* @see wp_insert_user() For what fields can be set in $userdata
* @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
*
* @param array $userdata An array of user data.
* @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
* @return int The updated user's ID.
*/
function wp_update_user($userdata) {
if ( is_a( $userdata, 'stdClass' ) )
$userdata = get_object_vars( $userdata );
elseif ( is_a( $userdata, 'WP_User' ) )
$userdata = $userdata->to_array();
$ID = (int) $userdata['ID'];
// First, get all of the original fields
$user_obj = get_userdata( $ID );
$user = get_object_vars( $user_obj->data );
$user = $user_obj->to_array();
// Add additional custom fields
foreach ( _get_additional_user_keys( $user_obj ) as $key ) {