/** * Generates a random password drawn from the defined set of characters * @return string the password */ function bb_generate_password($length = 12, $special_chars = true) { return WP_Pass::generate_password($length, $special_chars); }
function _put_user($args = null) { $defaults = array('ID' => false, 'user_login' => '', 'user_nicename' => '', 'user_email' => '', 'user_url' => '', 'user_pass' => false, 'user_registered' => time(), 'display_name' => '', 'user_status' => 0, 'strict_user_login' => true); $fields = array_keys(wp_parse_args($args)); $args = wp_parse_args($args, $defaults); unset($defaults['strict_user_login']); if (isset($args['ID']) && $args['ID']) { unset($defaults['ID']); $fields = array_intersect($fields, array_keys($defaults)); } else { $fields = array_keys($defaults); } extract($args, EXTR_SKIP); $ID = (int) $ID; if (!$ID || in_array('user_login', $fields)) { $user_login = $this->sanitize_user($user_login, $strict_user_login); if (!$user_login) { return new WP_Error('user_login', __('Invalid login name')); } if (!$ID && $this->get_user($user_login, array('by' => 'login'))) { return new WP_Error('user_login', __('Name already exists')); } } if (!$ID || in_array('user_nicename', $fields)) { if (!($user_nicename = $this->sanitize_nicename($user_nicename ? $user_nicename : $user_login))) { return new WP_Error('user_nicename', __('Invalid nicename')); } if (!$ID && $this->get_user($user_nicename, array('by' => 'nicename'))) { return new WP_Error('user_nicename', __('Nicename already exists')); } } if (!$ID || in_array('user_email', $fields)) { if (!$this->is_email($user_email)) { return new WP_Error('user_email', __('Invalid email address')); } if ($already_email = $this->get_user($user_email, array('by' => 'email'))) { // if new user, or if multiple users with that email, or if only one user with that email, but it's not the user being updated if (!$ID || is_wp_error($already_email) || $already_email->ID != $ID) { return new WP_Error('user_email', __('Email already exists')); } } } if (!$ID || in_array('user_url', $fields)) { $user_url = esc_url($user_url); } if (!$ID || in_array('user_pass', $fields)) { if (!$user_pass) { $user_pass = WP_Pass::generate_password(); } $plain_pass = $user_pass; $user_pass = WP_Pass::hash_password($user_pass); } if (!$ID || in_array('user_registered', $fields)) { if (!is_numeric($user_registered)) { $user_registered = backpress_gmt_strtotime($user_registered); } if (!$user_registered || $user_registered < 0) { return new WP_Error('user_registered', __('Invalid registration time')); } if (!($user_registered = @gmdate('Y-m-d H:i:s', $user_registered))) { return new WP_Error('user_registered', __('Invalid registration timestamp')); } } if (!$ID || in_array('user_display', $fields)) { if (!$display_name) { $display_name = $user_login; } } $db_return = NULL; if ($ID) { $db_return = $this->db->update($this->db->users, compact($fields), compact('ID')); } else { $db_return = $this->db->insert($this->db->users, compact($fields)); $ID = $this->db->insert_id; } if (!$db_return) { return new WP_Error('WP_Users::_put_user', __('Query failed')); } // Cache the result if ($ID) { wp_cache_delete($ID, 'users'); $this->get_user($ID, array('from_cache' => false)); } $args = compact(array_keys($args)); $args['plain_pass'] = $plain_pass; do_action(__CLASS__ . '::' . __FUNCTION__, $args); return $args; }