function wp_update_user($userdata) {
global $wpdb, $current_user;
$ID = (int) $userdata['ID'];
// First, get all of the original fields
$user = get_userdata($ID);
// Escape data pulled from DB.
$user = add_magic_quotes(get_object_vars($user));
// If password is changing, hash it now.
if ( ! empty($userdata['user_pass']) ) {
$plaintext_pass = $userdata['user_pass'];
$userdata['user_pass'] = md5($userdata['user_pass']);
}
// Merge old and new fields with new fields overwriting old ones.
$userdata = array_merge($user, $userdata);
$user_id = wp_insert_user($userdata);
// Update the cookies if the password changed.
if( $current_user->id == $ID ) {
if ( isset($plaintext_pass) ) {
wp_clearcookie();
wp_setcookie($userdata['user_login'], $plaintext_pass);
}
}
return $user_id;
}
/**
* Creates a new user with args passed through an array or string of arguments.
*
* wp_nonce_field( 'register' ) must be used on the register form
*
* @param: username [string] - The desired username for the new user
* @param: email [string] - The desired email address for the new user
* @param: use_password [bool] [default: false] - Whether to specify a password on registration
* @param: password [string] - If use_password is true, the desired password for the new user
* @param: use_tos [bool] [default: true] - Whether the user needs to accept Terms of Service
* @param: tos [string] - If use_tos is true, the value to the accept Terms of Service checkbox
* @param: unique_email [bool] [default: false] - Set to true if only one username is allowed per email address
* @param: do_redirect [bool] [default: true] Whether to redirect the user after registration is complete
* @param: redirect [string] [default: User Profile Page] - The url to redirect the user to after successful login
* @param: send_email [bool] [default: true] Whether to send an email containing the username and password of the newly registered user
* @param: profile_info [array] [dafault: false] An array containing values to be used in wp_update_user() such as first_name, last_name
* @param: validate [bool] [default: true]
* @param: require_verify_email [bool] [default: false] Sends the user an email with a Activate Account link to activate their account
* @param: override_nonce [bool] [default: false] Bypasses the nonce check, not recommended in most situations
*
* @return: Int ID, the ID of the newly registered user [on error returns error string] or WP_Error
*/
function hma_new_user($args)
{
if (is_user_logged_in()) {
hm_error_message('You are already logged in', 'register');
return new WP_Error('already-logged-in');
}
$checks = array('use_password' => false, 'tos' => '', 'use_tos' => true, 'unique_email' => false, 'do_redirect' => true, 'do_login' => false, 'redirect' => '', 'send_email' => false, 'override_nonce' => false);
$defaults = array('user_login' => '', 'user_email' => '', 'user_pass' => false, 'role' => 'subscriber', 'validate' => true);
$original_args = $args;
$default_args = array_merge($defaults, $checks);
$args = wp_parse_args($args, $default_args);
extract($args, EXTR_SKIP);
$validation = apply_filters('hma_registration_info', $args);
unset($args['user_pass2']);
unset($original_args['user_pass2']);
unset($user_pass2);
if (is_wp_error($validation) && $validate == true) {
return $validation;
}
// Merge arrays overwritting defaults, remove any non-standard keys keys with empty values.
$user_vars = array_filter(array('user_login' => $user_login, 'user_pass' => $user_pass, 'user_email' => $user_email, 'display_name' => $display_name));
// Check for require_verify_email, send email and store temp data
if ($require_verify_email) {
$original_args['require_verify_email'] = false;
$unverified_users = (array) get_option('unverified_users');
$unverified_users[time()] = $original_args;
update_option('unverified_users', $unverified_users);
$message = "Please click the link below to activate your account for " . get_bloginfo() . "\n \n";
$message .= '<a href="' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '">' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '</a>';
$headers = 'From: ' . get_bloginfo() . ' <noreply@' . get_bloginfo('url') . '>' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n\r\n";
wp_mail($user_vars['user_email'], 'Please activate your account for ' . get_bloginfo(), $message, $headers);
return hm_return_success('sent-email-activation', '<p class="message success">You have been sent an activation email, please follow the link in the email sent to ' . $user_vars['user_email'] . '</p>');
}
$user_id = wp_insert_user($user_vars);
if (!$user_id || is_wp_error($user_id)) {
return $user_id;
}
// Setup the users role
if ($role) {
$user = new WP_User($user_id);
$user->set_role($role);
}
// Get any remaining variable that were passed
$meta_vars = array_diff_key($original_args, $defaults, $checks, $user_vars);
foreach ((array) $meta_vars as $key => $value) {
if (hma_is_profile_field($key) || !hma_custom_profile_fields()) {
update_user_meta($user_id, $key, $value);
}
}
$user = get_userdata($user_id);
// Send Notifcation email if specified
if ($send_email) {
$email = hma_email_registration_success($user, $user_pass);
}
// If they chose a password, login them in
if (($use_password == 'true' || $do_login == true) && !empty($user->ID)) {
wp_login($user->user_login, $user_pass);
wp_clearcookie();
wp_setcookie($user->user_login, $user_pass, false);
do_action('wp_login', $user->user_login);
wp_set_current_user($user->ID);
}
// Redirect the user if is set
if ($redirect !== '' && !empty($user->ID) && $do_redirect == true) {
wp_redirect($redirect);
exit;
}
do_action('hma_registered_user', $user);
return $user_id;
}
/**
* Logout the user by removing their WRAP cookies
*
*/
public function logout()
{
foreach (array_keys($_COOKIE) as $name) {
if (preg_match('/^WRAP.*/',$name)) {
// set the expiration date to one hour ago
setcookie($name, "", time() - 3600, "/", "ncsu.edu");
}
}
wp_clearcookie();
nocache_headers();
header('Location:' . get_option('siteurl'));
exit();
}
/**
* Create a new WordPress user with the specified identity URL and user data.
*
* @param string $identity_url OpenID to associate with the newly
* created account
* @param array $user_data array of user data
*/
function openid_create_new_user($identity_url, &$user_data) {
global $wpdb;
// Identity URL is new, so create a user
@include_once( ABSPATH . 'wp-admin/upgrade-functions.php'); // 2.1
@include_once( ABSPATH . WPINC . '/registration-functions.php'); // 2.0.4
// otherwise, try to use preferred username
if ( empty($username) && array_key_exists('nickname', $user_data) ) {
$username = openid_generate_new_username($user_data['nickname'], false);
}
// finally, build username from OpenID URL
if (empty($username)) {
$username = openid_generate_new_username($identity_url);
}
$user_data['user_login'] = $username;
$user_data['user_pass'] = substr( md5( uniqid( microtime() ) ), 0, 7);
$user_id = wp_insert_user( $user_data );
if( $user_id ) { // created ok
$user_data['ID'] = $user_id;
// XXX this all looks redundant, see openid_set_current_user
$user = new WP_User( $user_id );
if( ! wp_login( $user->user_login, $user_data['user_pass'] ) ) {
openid_message(__('User was created fine, but wp_login() for the new user failed. This is probably a bug.', 'openid'));
openid_status('error');
openid_error(openid_message());
return;
}
// notify of user creation
wp_new_user_notification( $user->user_login );
wp_clearcookie();
wp_setcookie( $user->user_login, md5($user->user_pass), true, '', '', true );
// Bind the provided identity to the just-created user
openid_add_user_identity($user_id, $identity_url);
openid_status('redirect');
if ( !$user->has_cap('edit_posts') ) $redirect_to = '/wp-admin/profile.php';
} else {
// failed to create user for some reason.
openid_message(__('OpenID authentication successful, but failed to create WordPress user. This is probably a bug.', 'openid'));
openid_status('error');
openid_error(openid_message());
}
}
/**
* TT New User
* Creates a new user with args passed through an array or string of arguments. Passing arguments works the same
* as functions such as query_posts(). Params are show as variable names which you must use when passing args
* NOTE: wp_nonce_field( 'register' ) must be used on the register form
*
* @Param: username [string] - The desired username for the new user
* @Param: email [string] - The desired email address for the new user
* @Param: use_password [bool] [default: false] - Whether to specify a password on registration
* @Param: password [string] - If use_password is true, the desired password for the new user
* @Param: use_tos [bool] [default: true] - Whether the user needs to accept Terms of Service
* @Param: tos [string] - If use_tos is true, the value to the accept Terms of Service checkbox
* @Param: unique_email [bool] [default: false] - Set to true if only one username is allowed per email address
* @Param: do_redirect [bool] [default: true] Whether to redirect the user after registration is complete
* @Param: redirect [string] [default: User Profile Page] - The url to redirect the user to after successful login
* @Param: send_email [bool] [default: true] Whether to send an email containing the username and password of the newly registered user
* @Param: profile_info [array] [dafault: false] An array containing values to be used in wp_update_user() such as first_name, last_name
* @Param: validate [bool] [default: true]
* @param: require_verify_email [bool] [default: false] Sends the user an email with a Activate Account link to activate their account
* @param: override_nonce [bool] [default: false] Bypasses the nonce check, not recommended in most situations
* @return: The ID of the newly registered user [on error returns error string]
* @author: Joe Hoyle
* @version 1.0
**/
function tja_new_user($args)
{
//Check the nonce field
if ($args['override_nonce'] !== true) {
check_admin_referer('register');
}
if (is_user_logged_in()) {
tj_error_message('You are already logged in', 'register');
return new WP_Error('already-logged-in');
}
include_once ABSPATH . '/wp-includes/registration.php';
$checks = array('use_password' => false, 'tos' => '', 'use_tos' => true, 'unique_email' => false, 'do_redirect' => true, 'redirect' => '', 'send_email' => false);
$defaults = array('user_login' => '', 'user_email' => '', 'user_pass' => false, 'role' => 'subscriber', 'validate' => true);
$original_args = $args;
$default_args = array_merge($defaults, $checks);
//Strip any tags then may have been put into the array
strip_tags((string) $args);
$args = wp_parse_args($args, $default_args);
extract($args, EXTR_SKIP);
$validation = apply_filters('tja_registration_info', $args);
unset($args['user_pass2']);
unset($user_pass2);
if ($validation['status'] === 'error' && $validate == true) {
return $validation;
}
// Merge arrays overwritting defaults, remove any non-standard keys keys with empty values.
$user_vars = array_filter(array_intersect_key(array_merge($defaults, $args), $defaults));
//Check for require_verify_email, send email and store temp data
if ($require_verify_email) {
$original_args['require_verify_email'] = false;
$unverified_users = (array) get_option('unverified_users');
$unverified_users[time()] = $original_args;
update_option('unverified_users', $unverified_users);
$message = "Please click the link below to activate your account for " . get_bloginfo() . "\n \n";
$message .= '<a href="' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '">' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '</a>';
$headers = 'From: ' . get_bloginfo() . ' <noreply@' . get_bloginfo('url') . '>' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n\r\n";
wp_mail($user_vars['user_email'], 'Please activate your account for ' . get_bloginfo(), $message, $headers);
return tj_return_success('sent-email-activation', '<p class="message success">You have been sent an activation email, please follow the link in the email sent to ' . $user_vars['user_email'] . '</p>');
}
$user_id = wp_insert_user($user_vars);
if ($role) {
$user = new WP_User($user_id);
$user->set_role($role);
}
// Get any remaining variable that were passed
$meta_vars = array_diff_key($original_args, $defaults, $checks);
foreach ((array) $meta_vars as $key => $value) {
update_usermeta($user_id, $key, $value);
}
$user = get_userdata($user_id);
//Send Notifcation email if specified
if ($send_email == true) {
$email = tja_email_registration_success($user, $user_pass);
}
//If they chose a password, login them in
if ($use_password == 'true' && $user->ID > 0) {
wp_login($user->user_login, $user_pass);
wp_clearcookie();
wp_setcookie($user->user_login, $user_pass, false);
}
//Redirect the user if is set
if ($redirect !== '' && $user->ID && $do_redirect == true) {
wp_redirect($redirect);
}
return $user_id;
}
function do_wp_logout()
{
//
// LOG OUT
// ------------------------------
// used by login.php
// No need to check for newly-created vars after this one, or use any globals, it's all too simple.
if ($this->wpVersion >= 2.5) {
wp_logout();
unset($_COOKIE[AUTH_COOKIE]);
unset($_COOKIE[SECURE_AUTH_COOKIE]);
unset($_COOKIE[LOGGED_IN_COOKIE]);
} else {
wp_clearcookie();
}
do_action('wp_logout');
wp_set_current_user(0, 0);
nocache_headers();
unset($_COOKIE[USER_COOKIE]);
unset($_COOKIE[PASS_COOKIE]);
}
/**
* Create a new WordPress user with the specified identity URL and user data.
*
* @param string $identity_url OpenID to associate with the newly
* created account
* @param array $user_data array of user data
*/
function create_new_user($identity_url, &$user_data)
{
global $wpdb, $openid;
// Identity URL is new, so create a user
@(include_once ABSPATH . 'wp-admin/upgrade-functions.php');
// 2.1
@(include_once ABSPATH . WPINC . '/registration-functions.php');
// 2.0.4
// use email address for username if URL is from emailtoid.net
$username = $identity_url;
if (null != $_SESSION['openid_login_email'] and strpos($username, 'http://emailtoid.net/') == 0) {
if ($user_data['user_email'] == NULL) {
$user_data['user_email'] = $_SESSION['openid_login_email'];
}
$username = $_SESSION['openid_login_email'];
unset($_SESSION['openid_login_email']);
}
$user_data['user_login'] = $wpdb->escape(WordPressOpenID_Logic::generate_new_username($username));
$user_data['user_pass'] = substr(md5(uniqid(microtime())), 0, 7);
$user_id = wp_insert_user($user_data);
$openid->log->debug("wp_create_user( {$user_data} ) returned {$user_id} ");
if ($user_id) {
// created ok
$user_data['ID'] = $user_id;
// XXX this all looks redundant, see WordPressOpenID_Logic::set_current_user
$openid->log->debug("OpenIDConsumer: Created new user {$user_id} : " . $user_data['user_login'] . " and metadata: " . var_export($user_data, true));
$user = new WP_User($user_id);
if (!wp_login($user->user_login, $user_data['user_pass'])) {
$openid->message = 'User was created fine, but wp_login() for the new user failed. ' . 'This is probably a bug.';
$openid->action = 'error';
$openid->log->err($openid->message);
return;
}
// notify of user creation
wp_new_user_notification($user->user_login);
wp_clearcookie();
wp_setcookie($user->user_login, md5($user->user_pass), true, '', '', true);
// Bind the provided identity to the just-created user
global $userdata;
$userdata = get_userdata($user_id);
$store = WordPressOpenID_Logic::getStore();
$store->insert_identity($user_id, $identity_url);
$openid->action = 'redirect';
if (!$user->has_cap('edit_posts')) {
$redirect_to = '/wp-admin/profile.php';
}
} else {
// failed to create user for some reason.
$openid->message = 'OpenID authentication successful, but failed to create WordPress user. ' . 'This is probably a bug.';
$openid->action = 'error';
$openid->log->error($openid->message);
}
}
function update_home_siteurl( $old_value, $value ) {
global $wp_rewrite, $user_login, $user_pass_md5;
if ( defined( "WP_INSTALLING" ) )
return;
// If home changed, write rewrite rules to new location.
$wp_rewrite->flush_rules();
// Clear cookies for old paths.
wp_clearcookie();
// Set cookies for new paths.
wp_setcookie( $user_login, $user_pass_md5, true, get_option( 'home' ), get_option( 'siteurl' ));
}
/**
* Create a new WordPress user with the specified identity URL and user data.
*
* @param string $identity_url OpenID to associate with the newly
* created account
* @param array $user_data array of user data
*/
function openid_create_new_user($identity_url, &$user_data)
{
global $wpdb;
// Identity URL is new, so create a user
@(include_once ABSPATH . 'wp-admin/upgrade-functions.php');
// 2.1
@(include_once ABSPATH . WPINC . '/registration-functions.php');
// 2.0.4
// use email address for username if URL is from emailtoid.net
$username = $identity_url;
if (null != $_SESSION['openid_login_email'] and strpos($username, 'http://emailtoid.net/') == 0) {
if ($user_data['user_email'] == NULL) {
$user_data['user_email'] = $_SESSION['openid_login_email'];
}
$username = $_SESSION['openid_login_email'];
unset($_SESSION['openid_login_email']);
}
$user_data['user_login'] = $wpdb->escape(openid_generate_new_username($username));
$user_data['user_pass'] = substr(md5(uniqid(microtime())), 0, 7);
$user_id = wp_insert_user($user_data);
if ($user_id) {
// created ok
$user_data['ID'] = $user_id;
// XXX this all looks redundant, see openid_set_current_user
$user = new WP_User($user_id);
if (!wp_login($user->user_login, $user_data['user_pass'])) {
openid_message(__('User was created fine, but wp_login() for the new user failed. This is probably a bug.', 'openid'));
openid_action('error');
openid_error(openid_message());
return;
}
// notify of user creation
wp_new_user_notification($user->user_login);
wp_clearcookie();
wp_setcookie($user->user_login, md5($user->user_pass), true, '', '', true);
// Bind the provided identity to the just-created user
openid_add_user_identity($user_id, $identity_url);
openid_status('redirect');
if (!$user->has_cap('edit_posts')) {
$redirect_to = '/wp-admin/profile.php';
}
} else {
// failed to create user for some reason.
openid_message(__('OpenID authentication successful, but failed to create WordPress user. This is probably a bug.', 'openid'));
openid_status('error');
openid_error(openid_message());
}
}
开发者ID:alx,项目名称:pressid,代码行数:55,代码来源:common.php
示例17: wp_update_user
function wp_update_user($userdata)
{
global $wpdb, $wp_version;
$ID = (int) $userdata['ID'];
// First, get all of the original fields
$user = get_userdata($ID);
// Escape data pulled from DB.
$user = add_magic_quotes(get_object_vars($user));
// If password is changing, hash it now.
if (!empty($userdata['user_pass'])) {
$plaintext_pass = $userdata['user_pass'];
// NOTE BY JOHN WELLS -- IN WINTERMUTE VERSION BELOW IS UNCOMMENTED, BUT PHPBB WILL NOT BE PROVIDING
// A PLAINTEXT PASSWORD HERE, SO THIS WILL NOT WORK || TODO: 20: TO CHECK HOW TO RECONCILE PASSWORDS, IF AT ALL
//$userdata['user_pass'] = wp_hash_password($userdata['user_pass']); //[WP-UNITED CHANGED]
}
// Merge old and new fields with new fields overwriting old ones.
$userdata = array_merge($user, $userdata);
$user_id = wp_insert_user($userdata);
// Update the cookies if the password changed.
$current_user = wp_get_current_user();
if ($current_user->id == $ID) {
if (isset($plaintext_pass)) {
if ((double) $wp_version >= 2.5) {
//new additions
wp_clear_auth_cookie();
wp_set_auth_cookie($ID);
} else {
//old WP
wp_clearcookie();
wp_setcookie($userdata['user_login'], $userdata['user_pass'], true, '', '', false);
// wp_setcookie($userdata['user_login'], $plaintext_pass); [WP-UNITED CHANGED]
}
}
}
return $user_id;
}
请发表评论