X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/38ca813a0e312e2768e5b9519f0415cd0aa84781..refs/tags/wordpress-3.4.2:/wp-includes/user.php diff --git a/wp-includes/user.php b/wp-includes/user.php index af7f5de5..6b342c85 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -61,11 +61,10 @@ function wp_signon( $credentials = '', $secure_cookie = '' ) { } wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie); - do_action('wp_login', $credentials['user_login']); + do_action('wp_login', $user->user_login, $user); return $user; } - /** * Authenticate the user using the username and password. */ @@ -88,7 +87,7 @@ function wp_authenticate_username_password($user, $username, $password) { $userdata = get_user_by('login', $username); if ( !$userdata ) - return new WP_Error('invalid_username', sprintf(__('ERROR: Invalid username. Lost your password?'), site_url('wp-login.php?action=lostpassword', 'login'))); + return new WP_Error('invalid_username', sprintf(__('ERROR: Invalid username. Lost your password?'), wp_lostpassword_url())); if ( is_multisite() ) { // Is user marked as spam? @@ -109,7 +108,7 @@ function wp_authenticate_username_password($user, $username, $password) { if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) return new WP_Error( 'incorrect_password', sprintf( __( 'ERROR: The password you entered for the username %1$s is incorrect. Lost your password?' ), - $username, site_url( 'wp-login.php?action=lostpassword', 'login' ) ) ); + $username, wp_lostpassword_url() ) ); $user = new WP_User($userdata->ID); return $user; @@ -154,7 +153,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 +164,12 @@ 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 array $users Array of user IDs. * @param string|array $post_type Optional. Post type to check. Defaults to post. * @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' ) { global $wpdb; $count = array(); @@ -254,20 +254,18 @@ function get_user_option( $option, $user = 0, $deprecated = '' ) { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '3.0' ); - if ( empty($user) ) { + if ( empty( $user ) ) $user = wp_get_current_user(); - $user = $user->ID; - } - - $user = get_userdata($user); + else + $user = new WP_User( $user ); - // Keys used as object vars cannot have dashes. - $key = str_replace('-', '', $option); + if ( ! $user->exists() ) + return false; - if ( isset( $user->{$wpdb->prefix . $key} ) ) // Blog specific - $result = $user->{$wpdb->prefix . $key}; - elseif ( isset( $user->{$key} ) ) // User specific and cross-blog - $result = $user->{$key}; + if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific + $result = $user->get( $wpdb->prefix . $option ); + elseif ( $user->has_prop( $option ) ) // User specific and cross-blog + $result = $user->get( $option ); else $result = false; @@ -361,7 +359,6 @@ class WP_User_Query { var $query_orderby; var $query_limit; - /** * PHP5 constructor * @@ -381,6 +378,7 @@ class WP_User_Query { 'include' => array(), 'exclude' => array(), 'search' => '', + 'search_columns' => array(), 'orderby' => 'login', 'order' => 'ASC', 'offset' => '', @@ -479,14 +477,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) && ! 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 ); } @@ -647,16 +650,26 @@ function get_users( $args = array() ) { * * @since 3.0.0 * - * @param int $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. + * @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. An empty array if the user doesn't exist or belongs to no blogs. */ -function get_blogs_of_user( $id, $all = false ) { +function get_blogs_of_user( $user_id, $all = false ) { global $wpdb; - if ( !is_multisite() ) { + $user_id = (int) $user_id; + + // Logged out users can't have blogs + if ( empty( $user_id ) ) + return array(); + + $keys = get_user_meta( $user_id ); + if ( empty( $keys ) ) + return array(); + + if ( ! is_multisite() ) { $blog_id = get_current_blog_id(); - $blogs = array(); + $blogs = array( $blog_id => new stdClass ); $blogs[ $blog_id ]->userblog_id = $blog_id; $blogs[ $blog_id ]->blogname = get_option('blogname'); $blogs[ $blog_id ]->domain = ''; @@ -666,78 +679,73 @@ function get_blogs_of_user( $id, $all = false ) { return $blogs; } - $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' ); - - // Try priming the new cache from the old cache - if ( false === $blogs ) { - $cache_suffix = $all ? '_all' : '_short'; - $blogs = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' ); - if ( is_array( $blogs ) ) { - $blogs = array_keys( $blogs ); - if ( $all ) - wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' ); + $blogs = array(); + + if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) { + $blog = get_blog_details( 1 ); + if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) { + $blogs[ 1 ] = (object) array( + 'userblog_id' => 1, + 'blogname' => $blog->blogname, + 'domain' => $blog->domain, + 'path' => $blog->path, + 'site_id' => $blog->site_id, + 'siteurl' => $blog->siteurl, + ); } + unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); } - if ( false === $blogs ) { - $user = get_userdata( (int) $id ); - if ( !$user ) - return false; + $keys = array_keys( $keys ); - $blogs = $match = array(); - $prefix_length = strlen($wpdb->base_prefix); - foreach ( (array) $user as $key => $value ) { - if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix ) - continue; - if ( substr($key, -12, 12) != 'capabilities' ) - continue; - if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) { - if ( count( $match ) > 2 ) - $blogs[] = (int) $match[ 2 ]; - else - $blogs[] = 1; - } - } - wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' ); - } + foreach ( $keys as $key ) { + if ( 'capabilities' !== substr( $key, -12 ) ) + continue; + if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) + continue; + $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); + if ( ! is_numeric( $blog_id ) ) + continue; - $blog_deets = array(); - foreach ( (array) $blogs as $blog_id ) { + $blog_id = (int) $blog_id; $blog = get_blog_details( $blog_id ); - if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) { - $blog_deets[ $blog_id ]->userblog_id = $blog_id; - $blog_deets[ $blog_id ]->blogname = $blog->blogname; - $blog_deets[ $blog_id ]->domain = $blog->domain; - $blog_deets[ $blog_id ]->path = $blog->path; - $blog_deets[ $blog_id ]->site_id = $blog->site_id; - $blog_deets[ $blog_id ]->siteurl = $blog->siteurl; + if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) { + $blogs[ $blog_id ] = (object) array( + 'userblog_id' => $blog_id, + 'blogname' => $blog->blogname, + 'domain' => $blog->domain, + 'path' => $blog->path, + 'site_id' => $blog->site_id, + 'siteurl' => $blog->siteurl, + ); } } - return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all ); + return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all ); } /** - * Checks if the current user belong to a given blog. + * Find out whether a user is a member of a given blog. * - * @since 3.0.0 + * @since MU 1.1 + * @uses get_blogs_of_user() * - * @param int $blog_id Blog ID - * @return bool True if the current users belong to $blog_id, false if not. + * @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_blog_user( $blog_id = 0 ) { - global $wpdb; - - $current_user = wp_get_current_user(); - if ( !$blog_id ) - $blog_id = $wpdb->blogid; +function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { + $user_id = (int) $user_id; + $blog_id = (int) $blog_id; - $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities'; + if ( empty( $user_id ) ) + $user_id = get_current_user_id(); - if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) ) - return true; + if ( empty( $blog_id ) ) + $blog_id = get_current_blog_id(); - return false; + $blogs = get_blogs_of_user( $user_id ); + return array_key_exists( $blog_id, $blogs ); } /** @@ -787,12 +795,12 @@ 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. */ -function get_user_meta($user_id, $key, $single = false) { +function get_user_meta($user_id, $key = '', $single = false) { return get_metadata('user', $user_id, $key, $single); } @@ -822,7 +830,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. * @@ -849,7 +857,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); @@ -877,14 +885,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; } } } @@ -925,20 +933,21 @@ function setup_userdata($for_user_id = '') { else $user = new WP_User($for_user_id); - $userdata = $user->data; + $userdata = null; $user_ID = (int) $user->ID; $user_level = (int) isset($user->user_level) ? $user->user_level : 0; - if ( 0 == $user->ID ) { + if ( ! $user->exists() ) { $user_login = $user_email = $user_url = $user_pass_md5 = $user_identity = ''; return; } - $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; + $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; } /** @@ -967,7 +976,7 @@ function setup_userdata($for_user_id = '') { *