X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/bf5c68485ef07868ad0a91168ecd0092af7661ae..baca9ce86a38dc54c4574890ee2d352fd81f78b2:/wp-includes/user.php diff --git a/wp-includes/user.php b/wp-includes/user.php index cdec57e2..57f1ec94 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -65,7 +65,6 @@ function wp_signon( $credentials = '', $secure_cookie = '' ) { return $user; } - /** * Authenticate the user using the username and password. */ @@ -85,33 +84,32 @@ function wp_authenticate_username_password($user, $username, $password) { return $error; } - $userdata = get_user_by('login', $username); + $user = get_user_by('login', $username); - if ( !$userdata ) + if ( !$user ) return new WP_Error('invalid_username', sprintf(__('ERROR: Invalid username. Lost your password?'), wp_lostpassword_url())); if ( is_multisite() ) { // Is user marked as spam? - if ( 1 == $userdata->spam) + if ( 1 == $user->spam) return new WP_Error('invalid_username', __('ERROR: Your account has been marked as a spammer.')); // Is a user's blog marked as spam? - if ( !is_super_admin( $userdata->ID ) && isset($userdata->primary_blog) ) { - $details = get_blog_details( $userdata->primary_blog ); + if ( !is_super_admin( $user->ID ) && isset($user->primary_blog) ) { + $details = get_blog_details( $user->primary_blog ); if ( is_object( $details ) && $details->spam == 1 ) return new WP_Error('blog_suspended', __('Site Suspended.')); } } - $userdata = apply_filters('wp_authenticate_user', $userdata, $password); - if ( is_wp_error($userdata) ) - return $userdata; + $user = apply_filters('wp_authenticate_user', $user, $password); + if ( is_wp_error($user) ) + return $user; - if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) + if ( !wp_check_password($password, $user->user_pass, $user->ID) ) return new WP_Error( 'incorrect_password', sprintf( __( 'ERROR: The password you entered for the username %1$s is incorrect. Lost your password?' ), $username, wp_lostpassword_url() ) ); - $user = new WP_User($userdata->ID); return $user; } @@ -154,7 +152,7 @@ function wp_authenticate_cookie($user, $username, $password) { function count_user_posts($userid) { global $wpdb; - $where = get_posts_by_author_sql('post', TRUE, $userid); + $where = get_posts_by_author_sql('post', true, $userid); $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); @@ -165,11 +163,13 @@ function count_user_posts($userid) { * Number of posts written by a list of users. * * @since 3.0.0 - * @param array $user_ids Array of user IDs. - * @param string|array $post_type Optional. Post type to check. Defaults to post. + * + * @param array $users Array of user IDs. + * @param string $post_type Optional. Post type to check. Defaults to post. + * @param bool $public_only Optional. Only return counts for public posts. Defaults to false. * @return array Amount of posts each user has written. */ -function count_many_users_posts($users, $post_type = 'post' ) { +function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { global $wpdb; $count = array(); @@ -177,7 +177,7 @@ function count_many_users_posts($users, $post_type = 'post' ) { return $count; $userlist = implode( ',', array_map( 'absint', $users ) ); - $where = get_posts_by_author_sql( $post_type ); + $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); foreach ( $result as $row ) { @@ -192,24 +192,6 @@ function count_many_users_posts($users, $post_type = 'post' ) { return $count; } -/** - * Check that the user login name and password is correct. - * - * @since 0.71 - * @todo xmlrpc only. Maybe move to xmlrpc.php. - * - * @param string $user_login User name. - * @param string $user_pass User password. - * @return bool False if does not authenticate, true if username and password authenticates. - */ -function user_pass_ok($user_login, $user_pass) { - $user = wp_authenticate($user_login, $user_pass); - if ( is_wp_error($user) ) - return false; - - return true; -} - // // User option functions // @@ -255,11 +237,9 @@ function get_user_option( $option, $user = 0, $deprecated = '' ) { _deprecated_argument( __FUNCTION__, '3.0' ); if ( empty( $user ) ) - $user = wp_get_current_user(); - else - $user = new WP_User( $user ); + $user = get_current_user_id(); - if ( ! isset( $user->ID ) ) + if ( ! $user = get_userdata( $user ) ) return false; if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific @@ -334,6 +314,15 @@ function delete_user_option( $user_id, $option_name, $global = false ) { */ class WP_User_Query { + /** + * Query vars, after parsing + * + * @since 3.5.0 + * @access public + * @var array + */ + var $query_vars = array(); + /** * List of found user ids * @@ -359,7 +348,6 @@ class WP_User_Query { var $query_orderby; var $query_limit; - /** * PHP5 constructor * @@ -379,6 +367,7 @@ class WP_User_Query { 'include' => array(), 'exclude' => array(), 'search' => '', + 'search_columns' => array(), 'orderby' => 'login', 'order' => 'ASC', 'offset' => '', @@ -402,7 +391,7 @@ class WP_User_Query { function prepare_query() { global $wpdb; - $qv = &$this->query_vars; + $qv =& $this->query_vars; if ( is_array( $qv['fields'] ) ) { $qv['fields'] = array_unique( $qv['fields'] ); @@ -417,7 +406,7 @@ class WP_User_Query { $this->query_fields = "$wpdb->users.ID"; } - if ( $this->query_vars['count_total'] ) + if ( $qv['count_total'] ) $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; $this->query_from = "FROM $wpdb->users"; @@ -477,14 +466,19 @@ class WP_User_Query { if ( $wild ) $search = trim($search, '*'); - if ( false !== strpos( $search, '@') ) - $search_columns = array('user_email'); - elseif ( is_numeric($search) ) - $search_columns = array('user_login', 'ID'); - elseif ( preg_match('|^https?://|', $search) ) - $search_columns = array('user_url'); - else - $search_columns = array('user_login', 'user_nicename'); + $search_columns = array(); + if ( $qv['search_columns'] ) + $search_columns = array_intersect( $qv['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename' ) ); + if ( ! $search_columns ) { + if ( false !== strpos( $search, '@') ) + $search_columns = array('user_email'); + elseif ( is_numeric($search) ) + $search_columns = array('user_login', 'ID'); + elseif ( preg_match('|^https?://|', $search) && ! ( is_multisite() && function_exists( 'wp_is_large_network' ) && wp_is_large_network( 'users' ) ) ) + $search_columns = array('user_url'); + else + $search_columns = array('user_login', 'user_nicename'); + } $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); } @@ -544,29 +538,64 @@ class WP_User_Query { function query() { global $wpdb; - if ( is_array( $this->query_vars['fields'] ) || 'all' == $this->query_vars['fields'] ) { + $qv =& $this->query_vars; + + if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) { $this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"); } else { $this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"); } - if ( $this->query_vars['count_total'] ) + if ( $qv['count_total'] ) $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); if ( !$this->results ) return; - if ( 'all_with_meta' == $this->query_vars['fields'] ) { + if ( 'all_with_meta' == $qv['fields'] ) { cache_users( $this->results ); $r = array(); foreach ( $this->results as $userid ) - $r[ $userid ] = new WP_User( $userid, '', $this->query_vars['blog_id'] ); + $r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] ); $this->results = $r; + } elseif ( 'all' == $qv['fields'] ) { + foreach ( $this->results as $key => $user ) { + $this->results[ $key ] = new WP_User( $user ); + } } } + /** + * Retrieve query variable. + * + * @since 3.5.0 + * @access public + * + * @param string $query_var Query variable key. + * @return mixed + */ + function get( $query_var ) { + if ( isset( $this->query_vars[$query_var] ) ) + return $this->query_vars[$query_var]; + + return null; + } + + /** + * Set query variable. + * + * @since 3.5.0 + * @access public + * + * @param string $query_var Query variable key. + * @param mixed $value Query variable value. + */ + function set( $query_var, $value ) { + $this->query_vars[$query_var] = $value; + } + /* * Used internally to generate an SQL string for searching across multiple columns * @@ -647,7 +676,7 @@ function get_users( $args = array() ) { * * @param int $user_id User ID * @param bool $all Whether to retrieve all blogs, or only blogs that are not marked as deleted, archived, or spam. - * @return array A list of the user's blogs. False if the user was not found or an empty array if the user has no blogs. + * @return array A list of the user's blogs. An empty array if the user doesn't exist or belongs to no blogs. */ function get_blogs_of_user( $user_id, $all = false ) { global $wpdb; @@ -656,11 +685,11 @@ function get_blogs_of_user( $user_id, $all = false ) { // Logged out users can't have blogs if ( empty( $user_id ) ) - return false; + return array(); $keys = get_user_meta( $user_id ); if ( empty( $keys ) ) - return false; + return array(); if ( ! is_multisite() ) { $blog_id = get_current_blog_id(); @@ -671,6 +700,9 @@ function get_blogs_of_user( $user_id, $all = false ) { $blogs[ $blog_id ]->path = ''; $blogs[ $blog_id ]->site_id = 1; $blogs[ $blog_id ]->siteurl = get_option('siteurl'); + $blogs[ $blog_id ]->archived = 0; + $blogs[ $blog_id ]->spam = 0; + $blogs[ $blog_id ]->deleted = 0; return $blogs; } @@ -686,6 +718,9 @@ function get_blogs_of_user( $user_id, $all = false ) { 'path' => $blog->path, 'site_id' => $blog->site_id, 'siteurl' => $blog->siteurl, + 'archived' => 0, + 'spam' => 0, + 'deleted' => 0 ); } unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); @@ -712,6 +747,9 @@ function get_blogs_of_user( $user_id, $all = false ) { 'path' => $blog->path, 'site_id' => $blog->site_id, 'siteurl' => $blog->siteurl, + 'archived' => 0, + 'spam' => 0, + 'deleted' => 0 ); } } @@ -725,8 +763,8 @@ function get_blogs_of_user( $user_id, $all = false ) { * @since MU 1.1 * @uses get_blogs_of_user() * - * @param int $user_id The unique ID of the user - * @param int $blog Optional. If no blog_id is provided, current site is used + * @param int $user_id Optional. The unique ID of the user. Defaults to the current user. + * @param int $blog_id Optional. ID of the blog to check. Defaults to the current site. * @return bool */ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { @@ -740,10 +778,7 @@ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { $blog_id = get_current_blog_id(); $blogs = get_blogs_of_user( $user_id ); - if ( is_array( $blogs ) ) - return array_key_exists( $blog_id, $blogs ); - else - return false; + return array_key_exists( $blog_id, $blogs ); } /** @@ -793,7 +828,7 @@ function delete_user_meta($user_id, $meta_key, $meta_value = '') { * @link http://codex.wordpress.org/Function_Reference/get_user_meta * * @param int $user_id Post ID. - * @param string $key The meta key to retrieve. + * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. * @param bool $single Whether to return a single value. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single * is true. @@ -828,7 +863,7 @@ function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') { * Count number of users who have each of the user roles. * * Assumes there are neither duplicated nor orphaned capabilities meta_values. - * Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query() + * Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query() * Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users. * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257. * @@ -855,7 +890,7 @@ function count_users($strategy = 'time') { // Build a CPU-intensive query that will return concise information. $select_count = array(); foreach ( $avail_roles as $this_role => $name ) { - $select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%" . like_escape($this_role) . "%', FALSE))"; + $select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%\"" . like_escape( $this_role ) . "\"%', false))"; } $select_count = implode(', ', $select_count); @@ -883,14 +918,14 @@ function count_users($strategy = 'time') { $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" ); foreach ( $users_of_blog as $caps_meta ) { - $b_roles = unserialize($caps_meta); - if ( is_array($b_roles) ) { - foreach ( $b_roles as $b_role => $val ) { - if ( isset($avail_roles[$b_role]) ) { - $avail_roles[$b_role]++; - } else { - $avail_roles[$b_role] = 1; - } + $b_roles = maybe_unserialize($caps_meta); + if ( ! is_array( $b_roles ) ) + continue; + foreach ( $b_roles as $b_role => $val ) { + if ( isset($avail_roles[$b_role]) ) { + $avail_roles[$b_role]++; + } else { + $avail_roles[$b_role] = 1; } } } @@ -918,34 +953,32 @@ function count_users($strategy = 'time') { * @global int $user_ID The ID of the user * @global string $user_email The email address of the user * @global string $user_url The url in the user's profile - * @global string $user_pass_md5 MD5 of the user's password * @global string $user_identity The display name of the user * * @param int $for_user_id Optional. User ID to set up global data. */ function setup_userdata($for_user_id = '') { - global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity; + global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity; if ( '' == $for_user_id ) - $user = wp_get_current_user(); - else - $user = new WP_User($for_user_id); - - $userdata = null; - $user_ID = (int) $user->ID; - $user_level = (int) isset($user->user_level) ? $user->user_level : 0; - - if ( 0 == $user->ID ) { - $user_login = $user_email = $user_url = $user_pass_md5 = $user_identity = ''; + $for_user_id = get_current_user_id(); + $user = get_userdata( $for_user_id ); + + if ( ! $user ) { + $user_ID = 0; + $user_level = 0; + $userdata = null; + $user_login = $user_email = $user_url = $user_identity = ''; return; } - $userdata = $user; - $user_login = $user->user_login; - $user_email = $user->user_email; - $user_url = $user->user_url; - $user_pass_md5 = md5($user->user_pass); - $user_identity = $user->display_name; + $user_ID = (int) $user->ID; + $user_level = (int) $user->user_level; + $userdata = $user; + $user_login = $user->user_login; + $user_email = $user->user_email; + $user_url = $user->user_url; + $user_identity = $user->display_name; } /** @@ -974,7 +1007,7 @@ function setup_userdata($for_user_id = '') { *
  • id - Default is the value of the 'name' parameter. ID attribute of select element.
  • *
  • class - Class attribute of select element.
  • *
  • blog_id - ID of blog (Multisite only). Defaults to ID of current blog.
  • - *
  • who - Which users to query. Currently only 'authors' is supported. Default is all users.
  • + *
  • who - Which users to query. Currently only 'authors' is supported. Default is all users.
  • * * * @since 2.3.0 @@ -1056,17 +1089,12 @@ function wp_dropdown_users( $args = '' ) { * when calling filters. * * @since 2.3.0 - * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and - * $user_id if $context == 'edit' and field name prefix == 'user_'. - * - * @uses apply_filters() Calls 'edit_user_$field' passing $value and $user_id if $context == 'db'. - * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'user_'. - * @uses apply_filters() Calls '{$field}_pre' passing $value if $context == 'db' and field name prefix != 'user_'. - * + * @uses apply_filters() Calls 'edit_$field' passing $value and $user_id if $context == 'edit'. + * $field is prefixed with 'user_' if it isn't already. + * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db'. $field is prefixed with + * 'user_' if it isn't already. * @uses apply_filters() Calls '$field' passing $value, $user_id and $context if $context == anything - * other than 'raw', 'edit' and 'db' and field name prefix == 'user_'. - * @uses apply_filters() Calls 'user_$field' passing $value if $context == anything other than 'raw', - * 'edit' and 'db' and field name prefix != 'user_'. + * other than 'raw', 'edit' and 'db'. $field is prefixed with 'user_' if it isn't already. * * @param string $field The user Object field name. * @param mixed $value The user Object value. @@ -1086,11 +1114,7 @@ function sanitize_user_field($field, $value, $user_id, $context) { if ( !is_string($value) && !is_numeric($value) ) return $value; - $prefixed = false; - if ( false !== strpos($field, 'user_') ) { - $prefixed = true; - $field_no_prefix = str_replace('user_', '', $field); - } + $prefixed = false !== strpos( $field, 'user_' ); if ( 'edit' == $context ) { if ( $prefixed ) { @@ -1147,15 +1171,19 @@ function update_user_caches($user) { * * @since 3.0.0 * - * @param int $id User ID + * @param WP_User|int $user User object or ID to be cleaned from the cache */ -function clean_user_cache($id) { - $user = WP_User::get_data_by( 'id', $id ); +function clean_user_cache( $user ) { + if ( is_numeric( $user ) ) + $user = new WP_User( $user ); - wp_cache_delete($id, 'users'); - wp_cache_delete($user->user_login, 'userlogins'); - wp_cache_delete($user->user_email, 'useremail'); - wp_cache_delete($user->user_nicename, 'userslugs'); + if ( ! $user->exists() ) + return; + + wp_cache_delete( $user->ID, 'users' ); + wp_cache_delete( $user->user_login, 'userlogins' ); + wp_cache_delete( $user->user_email, 'useremail' ); + wp_cache_delete( $user->user_nicename, 'userslugs' ); } /** @@ -1224,7 +1252,7 @@ function validate_username( $username ) { * 'ID' - An integer that will be used for updating an existing user. * 'user_pass' - A string that contains the plain text password for the user. * 'user_login' - A string that contains the user's username for logging in. - * 'user_nicename' - A string that contains a nicer looking name for the user. + * 'user_nicename' - A string that contains a URL-friendly name for the user. * The default is the user's username. * 'user_url' - A string containing the user's URL for the user's web site. * 'user_email' - A string containing the user's email address. @@ -1248,13 +1276,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) ) { @@ -1277,7 +1310,7 @@ function wp_insert_user($userdata) { return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') ); if ( !$update && username_exists( $user_login ) ) - return new WP_Error('existing_user_login', __('This username is already registered.') ); + return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) ); if ( empty($user_nicename) ) $user_nicename = sanitize_title( $user_login ); @@ -1292,11 +1325,7 @@ function wp_insert_user($userdata) { $user_email = apply_filters('pre_user_email', $user_email); if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) ) - return new WP_Error('existing_user_email', __('This email address is already registered.') ); - - if ( empty($display_name) ) - $display_name = $user_login; - $display_name = apply_filters('pre_user_display_name', $display_name); + return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); if ( empty($nickname) ) $nickname = $user_login; @@ -1310,6 +1339,21 @@ function wp_insert_user($userdata) { $last_name = ''; $last_name = apply_filters('pre_user_last_name', $last_name); + if ( empty( $display_name ) ) { + if ( $update ) + $display_name = $user_login; + elseif ( $first_name && $last_name ) + /* translators: 1: first name, 2: last name */ + $display_name = sprintf( _x( '%1$s %2$s', 'Display name based on first name and last name' ), $first_name, $last_name ); + elseif ( $first_name ) + $display_name = $first_name; + elseif ( $last_name ) + $display_name = $last_name; + else + $display_name = $user_login; + } + $display_name = apply_filters( 'pre_user_display_name', $display_name ); + if ( empty($description) ) $description = ''; $description = apply_filters('pre_user_description', $description); @@ -1395,16 +1439,23 @@ 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. - * @return int The updated user's ID. + * @param mixed $userdata An array of user data or a user object of type stdClass or WP_User. + * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated. */ 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 ); + if ( ! $user_obj ) + return new WP_Error( 'invalid_user_id', __( 'Invalid user 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 ) { @@ -1461,14 +1512,13 @@ function wp_create_user($username, $password, $email = '') { return wp_insert_user($userdata); } - /** * Return a list of meta keys that wp_insert_user() is supposed to set. * - * @access private * @since 3.3.0 + * @access private * - * @param object $user WP_User instance + * @param object $user WP_User instance. * @return array */ function _get_additional_user_keys( $user ) { @@ -1477,12 +1527,12 @@ function _get_additional_user_keys( $user ) { } /** - * Set up the default contact methods + * Set up the default contact methods. * + * @since 2.9.0 * @access private - * @since * - * @param object $user User data object (optional) + * @param object $user User data object (optional). * @return array $user_contactmethods Array of contact methods and their labels. */ function _wp_get_user_contactmethods( $user = null ) { @@ -1493,5 +1543,3 @@ function _wp_get_user_contactmethods( $user = null ) { ); return apply_filters( 'user_contactmethods', $user_contactmethods, $user ); } - -?>