Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/user.php

    r17435 r15235  
    4444        $secure_cookie = is_ssl();
    4545
    46     $secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, $credentials);
    47 
    4846    global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
    4947    $auth_secure_cookie = $secure_cookie;
     
    109107
    110108    if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
    111         return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
    112         $username, site_url( 'wp-login.php?action=lostpassword', 'login' ) ) );
     109        return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
    113110
    114111    $user =  new WP_User($userdata->ID);
     
    166163 *
    167164 * @since 3.0.0
    168  * @param array $users User ID number list.
     165 * @param array $userid User ID number list.
    169166 * @return array Amount of posts each user has written.
    170167 */
     
    330327
    331328/**
    332  * WordPress User Query class.
    333  *
    334  * @since 3.1.0
    335  */
    336 class WP_User_Query {
    337 
    338     /**
    339      * List of found user ids
    340      *
    341      * @since 3.1.0
    342      * @access private
    343      * @var array
    344      */
    345     var $results;
    346 
    347     /**
    348      * Total number of found users for the current query
    349      *
    350      * @since 3.1.0
    351      * @access private
    352      * @var int
    353      */
    354     var $total_users = 0;
    355 
    356     // SQL clauses
    357     var $query_fields;
    358     var $query_from;
    359     var $query_where;
    360     var $query_orderby;
    361     var $query_limit;
    362 
    363     /**
    364      * PHP4 constructor
    365      */
    366     function WP_User_Query( $query = null ) {
    367         $this->__construct( $query );
    368     }
    369 
    370     /**
    371      * PHP5 constructor
    372      *
    373      * @since 3.1.0
    374      *
    375      * @param string|array $args The query variables
    376      * @return WP_User_Query
    377      */
    378     function __construct( $query = null ) {
    379         if ( !empty( $query ) ) {
    380             $this->query_vars = wp_parse_args( $query, array(
    381                 'blog_id' => $GLOBALS['blog_id'],
    382                 'role' => '',
    383                 'meta_key' => '',
    384                 'meta_value' => '',
    385                 'meta_compare' => '',
    386                 'include' => array(),
    387                 'exclude' => array(),
    388                 'search' => '',
    389                 'orderby' => 'login',
    390                 'order' => 'ASC',
    391                 'offset' => '', 'number' => '',
    392                 'count_total' => true,
    393                 'fields' => 'all',
    394                 'who' => ''
    395             ) );
    396 
    397             $this->prepare_query();
    398             $this->query();
    399         }
    400     }
    401 
    402     /**
    403      * Prepare the query variables
    404      *
    405      * @since 3.1.0
    406      * @access private
    407      */
    408     function prepare_query() {
    409         global $wpdb;
    410 
    411         $qv = &$this->query_vars;
    412 
    413         if ( is_array( $qv['fields'] ) ) {
    414             $qv['fields'] = array_unique( $qv['fields'] );
    415 
    416             $this->query_fields = array();
    417             foreach ( $qv['fields'] as $field )
    418                 $this->query_fields[] = $wpdb->users . '.' . esc_sql( $field );
    419             $this->query_fields = implode( ',', $this->query_fields );
    420         } elseif ( 'all' == $qv['fields'] ) {
    421             $this->query_fields = "$wpdb->users.*";
    422         } else {
    423             $this->query_fields = "$wpdb->users.ID";
    424         }
    425 
    426         $this->query_from = "FROM $wpdb->users";
    427         $this->query_where = "WHERE 1=1";
    428 
    429         // sorting
    430         if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) {
    431             $orderby = 'user_' . $qv['orderby'];
    432         } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) {
    433             $orderby = $qv['orderby'];
    434         } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) {
    435             $orderby = 'display_name';
    436         } elseif ( 'post_count' == $qv['orderby'] ) {
    437             // todo: avoid the JOIN
    438             $where = get_posts_by_author_sql('post');
    439             $this->query_from .= " LEFT OUTER JOIN (
    440                 SELECT post_author, COUNT(*) as post_count
    441                 FROM wp_posts
    442                 $where
    443                 GROUP BY post_author
    444             ) p ON ({$wpdb->users}.ID = p.post_author)
    445             ";
    446             $orderby = 'post_count';
    447         } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) {
    448             $orderby = 'ID';
    449         } else {
    450             $orderby = 'user_login';
    451         }
    452 
    453         $qv['order'] = strtoupper( $qv['order'] );
    454         if ( 'ASC' == $qv['order'] )
    455             $order = 'ASC';
    456         else
    457             $order = 'DESC';
    458         $this->query_orderby = "ORDER BY $orderby $order";
    459 
    460         // limit
    461         if ( $qv['number'] ) {
    462             if ( $qv['offset'] )
    463                 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
    464             else
    465                 $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
    466         }
    467 
    468         $search = trim( $qv['search'] );
    469         if ( $search ) {
    470             $leading_wild = ( ltrim($search, '*') != $search );
    471             $trailing_wild = ( rtrim($search, '*') != $search );
    472             if ( $leading_wild && $trailing_wild )
    473                 $wild = 'both';
    474             elseif ( $leading_wild )
    475                 $wild = 'leading';
    476             elseif ( $trailing_wild )
    477                 $wild = 'trailing';
    478             else
    479                 $wild = false;
    480             if ( $wild )
    481                 $search = trim($search, '*');
    482 
    483             if ( false !== strpos( $search, '@') )
    484                 $search_columns = array('user_email');
    485             elseif ( is_numeric($search) )
    486                 $search_columns = array('user_login', 'ID');
    487             elseif ( preg_match('|^https?://|', $search) )
    488                 $search_columns = array('user_url');
    489             else
    490                 $search_columns = array('user_login', 'user_nicename');
    491 
    492             $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
    493         }
    494 
    495         $blog_id = absint( $qv['blog_id'] );
    496 
    497         if ( 'authors' == $qv['who'] && $blog_id ) {
    498             $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
    499             $qv['meta_value'] = '_wp_zero_value'; // Hack to pass '0'
    500             $qv['meta_compare'] = '!=';
    501             $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
    502         }
    503 
    504         _parse_meta_query( $qv );
    505 
    506         $role = trim( $qv['role'] );
    507 
    508         if ( $blog_id && ( $role || is_multisite() ) ) {
    509             $cap_meta_query = array();
    510             $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    511 
    512             if ( $role ) {
    513                 $cap_meta_query['value'] = '"' . $role . '"';
    514                 $cap_meta_query['compare'] = 'like';
    515             }
    516 
    517             $qv['meta_query'][] = $cap_meta_query;
    518         }
    519 
    520         if ( !empty( $qv['meta_query'] ) ) {
    521             $clauses = call_user_func_array( '_get_meta_sql', array( $qv['meta_query'], 'user', $wpdb->users, 'ID', &$this ) );
    522             $this->query_from .= $clauses['join'];
    523             $this->query_where .= $clauses['where'];
    524         }
    525 
    526         if ( !empty( $qv['include'] ) ) {
    527             $ids = implode( ',', wp_parse_id_list( $qv['include'] ) );
    528             $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
    529         } elseif ( !empty($qv['exclude']) ) {
    530             $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
    531             $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
    532         }
    533 
    534         do_action_ref_array( 'pre_user_query', array( &$this ) );
    535     }
    536 
    537     /**
    538      * Execute the query, with the current variables
    539      *
    540      * @since 3.1.0
    541      * @access private
    542      */
    543     function query() {
    544         global $wpdb;
    545 
    546         if ( is_array( $this->query_vars['fields'] ) || 'all' == $this->query_vars['fields'] ) {
    547             $this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
    548         } else {
    549             $this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
    550         }
    551 
    552         if ( $this->query_vars['count_total'] )
    553             $this->total_users = $wpdb->get_var("SELECT COUNT(*) $this->query_from $this->query_where");
    554 
    555         if ( !$this->results )
    556             return;
    557 
    558         if ( 'all_with_meta' == $this->query_vars['fields'] ) {
    559             cache_users( $this->results );
    560 
    561             $r = array();
    562             foreach ( $this->results as $userid )
    563                 $r[ $userid ] = new WP_User( $userid, '', $this->query_vars['blog_id'] );
    564 
    565             $this->results = $r;
    566         }
    567     }
    568 
    569     /*
    570      * Used internally to generate an SQL string for searching across multiple columns
    571      *
    572      * @access protected
    573      * @since 3.1.0
    574      *
    575      * @param string $string
    576      * @param array $cols
    577      * @param bool $wild Whether to allow wildcard searches. Default is false for Network Admin, true for
    578      *  single site. Single site allows leading and trailing wildcards, Network Admin only trailing.
    579      * @return string
    580      */
    581     function get_search_sql( $string, $cols, $wild = false ) {
    582         $string = esc_sql( $string );
    583 
    584         $searches = array();
    585         $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
    586         $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
    587         foreach ( $cols as $col ) {
    588             if ( 'ID' == $col )
    589                 $searches[] = "$col = '$string'";
    590             else
    591                 $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'";
    592         }
    593 
    594         return ' AND (' . implode(' OR ', $searches) . ')';
    595     }
    596 
    597     /**
    598      * Return the list of users
    599      *
    600      * @since 3.1.0
    601      * @access public
    602      *
    603      * @return array
    604      */
    605     function get_results() {
    606         return $this->results;
    607     }
    608 
    609     /**
    610      * Return the total number of users for the current query
    611      *
    612      * @since 3.1.0
    613      * @access public
    614      *
    615      * @return array
    616      */
    617     function get_total() {
    618         return $this->total_users;
    619     }
    620 }
    621 
    622 /**
    623  * Retrieve list of users matching criteria.
    624  *
    625  * @since 3.1.0
    626  * @uses $wpdb
    627  * @uses WP_User_Query See for default arguments and information.
    628  *
    629  * @param array $args Optional.
    630  * @return array List of users.
    631  */
    632 function get_users( $args = array() ) {
    633 
    634     $args = wp_parse_args( $args );
    635     $args['count_total'] = false;
    636 
    637     $user_search = new WP_User_Query($args);
    638 
    639     return (array) $user_search->get_results();
    640 }
    641 
    642 /**
    643  * Get the blogs a user belongs to.
    644  *
    645  * @since 3.0.0
    646  *
    647  * @param int $id User Id
    648  * @param bool $all Whether to retrieve all blogs or only blogs that are not marked as deleted, archived, or spam.
    649  * @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.
    650  */
    651 function get_blogs_of_user( $id, $all = false ) {
    652     global $wpdb;
    653 
    654     if ( !is_multisite() ) {
    655         $blog_id = get_current_blog_id();
    656         $blogs = array();
    657         $blogs[ $blog_id ]->userblog_id = $blog_id;
    658         $blogs[ $blog_id ]->blogname = get_option('blogname');
    659         $blogs[ $blog_id ]->domain = '';
    660         $blogs[ $blog_id ]->path = '';
    661         $blogs[ $blog_id ]->site_id = 1;
    662         $blogs[ $blog_id ]->siteurl = get_option('siteurl');
    663         return $blogs;
    664     }
    665 
    666     $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' );
    667 
    668     // Try priming the new cache from the old cache
    669     if ( false === $blogs ) {
    670         $cache_suffix = $all ? '_all' : '_short';
    671         $blogs = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
    672         if ( is_array( $blogs ) ) {
    673             $blogs = array_keys( $blogs );
    674             if ( $all )
    675                 wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    676         }
    677     }
    678 
    679     if ( false === $blogs ) {
    680         $user = get_userdata( (int) $id );
    681         if ( !$user )
    682             return false;
    683 
    684         $blogs = $match = array();
    685         $prefix_length = strlen($wpdb->base_prefix);
    686         foreach ( (array) $user as $key => $value ) {
    687             if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    688                 continue;
    689             if ( substr($key, -12, 12) != 'capabilities' )
    690                 continue;
    691             if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
    692                 if ( count( $match ) > 2 )
    693                     $blogs[] = (int) $match[ 2 ];
    694                 else
    695                     $blogs[] = 1;
    696             }
    697         }
    698         wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    699     }
    700 
    701     $blog_deets = array();
    702     foreach ( (array) $blogs as $blog_id ) {
    703         $blog = get_blog_details( $blog_id );
    704         if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
    705             $blog_deets[ $blog_id ]->userblog_id    = $blog_id;
    706             $blog_deets[ $blog_id ]->blogname       = $blog->blogname;
    707             $blog_deets[ $blog_id ]->domain     = $blog->domain;
    708             $blog_deets[ $blog_id ]->path           = $blog->path;
    709             $blog_deets[ $blog_id ]->site_id        = $blog->site_id;
    710             $blog_deets[ $blog_id ]->siteurl        = $blog->siteurl;
    711         }
    712     }
    713 
    714     return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all );
    715 }
    716 
    717 /**
    718  * Checks if the current user belong to a given blog.
    719  *
    720  * @since 3.0.0
    721  *
    722  * @param int $blog_id Blog ID
    723  * @return bool True if the current users belong to $blog_id, false if not.
    724  */
    725 function is_blog_user( $blog_id = 0 ) {
    726     global $wpdb;
    727 
    728     $current_user = wp_get_current_user();
    729     if ( !$blog_id )
    730         $blog_id = $wpdb->blogid;
    731 
    732     $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
    733 
    734     if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
    735         return true;
    736 
    737     return false;
     329 * Get users for the blog.
     330 *
     331 * For setups that use the multi-blog feature. Can be used outside of the
     332 * multi-blog feature.
     333 *
     334 * @since 2.2.0
     335 * @uses $wpdb WordPress database object for queries
     336 * @uses $blog_id The Blog id of the blog for those that use more than one blog
     337 *
     338 * @param int $id Blog ID.
     339 * @return array List of users that are part of that Blog ID
     340 */
     341function get_users_of_blog( $id = '' ) {
     342    global $wpdb, $blog_id;
     343    if ( empty($id) )
     344        $id = (int) $blog_id;
     345    $blog_prefix = $wpdb->get_blog_prefix($id);
     346    $users = $wpdb->get_results( "SELECT user_id, user_id AS ID, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$blog_prefix}capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
     347    return $users;
    738348}
    739349
     
    748358 *
    749359 * @param int $user_id Post ID.
    750  * @param string $meta_key Metadata name.
    751  * @param mixed $meta_value Metadata value.
     360 * @param string $key Metadata name.
     361 * @param mixed $value Metadata value.
    752362 * @param bool $unique Optional, default is false. Whether the same key should not be added.
    753363 * @return bool False for failure. True for success.
     
    807417 *
    808418 * @param int $user_id Post ID.
    809  * @param string $meta_key Metadata key.
    810  * @param mixed $meta_value Metadata value.
     419 * @param string $key Metadata key.
     420 * @param mixed $value Metadata value.
    811421 * @param mixed $prev_value Optional. Previous value to check before removing.
    812422 * @return bool False on failure, true if success.
     
    820430 *
    821431 * Assumes there are neither duplicated nor orphaned capabilities meta_values.
    822  * Assumes role names are unique phrases.  Same assumption made by WP_User_Query::prepare_query()
     432 * Assumes role names are unique phrases.  Same assumption made by WP_User_Search::prepare_query()
    823433 * Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users.
    824434 * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.
     
    829439 */
    830440function count_users($strategy = 'time') {
    831     global $wpdb, $wp_roles;
     441    global $wpdb, $blog_id, $wp_roles;
    832442
    833443    // Initialize
    834     $id = get_current_blog_id();
     444    $id = (int) $blog_id;
    835445    $blog_prefix = $wpdb->get_blog_prefix($id);
    836446    $result = array();
     
    950560 * <ol>
    951561 * <li>show_option_all - Text to show all and whether HTML option exists.</li>
    952  * <li>show_option_none - Text for show none and whether HTML option exists.</li>
    953  * <li>hide_if_only_one_author - Don't create the dropdown if there is only one user.</li>
    954  * <li>orderby - SQL order by clause for what order the users appear. Default is 'display_name'.</li>
     562 * <li>show_option_none - Text for show none and whether HTML option exists.
     563 *     </li>
     564 * <li>orderby - SQL order by clause for what order the users appear. Default is
     565 * 'display_name'.</li>
    955566 * <li>order - Default is 'ASC'. Can also be 'DESC'.</li>
    956567 * <li>include - User IDs to include.</li>
    957568 * <li>exclude - User IDs to exclude.</li>
    958569 * <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element. A 'true' value is overridden when id argument is set.</li>
    959  * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentheses</li>
     570 * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentesis</li>
    960571 * <li>echo - Default is '1'. Whether to display or retrieve content.</li>
    961572 * <li>selected - Which User ID is selected.</li>
    962  * <li>include_selected - Always include the selected user ID in the dropdown. Default is false.</li>
    963573 * <li>name - Default is 'user'. Name attribute of select element.</li>
    964574 * <li>id - Default is the value of the 'name' parameter. ID attribute of select element.</li>
    965575 * <li>class - Class attribute of select element.</li>
    966576 * <li>blog_id - ID of blog (Multisite only). Defaults to ID of current blog.</li>
    967  * <li>who - Which users to query.  Currently only 'authors' is supported. Default is all users.</li>
    968577 * </ol>
    969578 *
     
    975584 */
    976585function wp_dropdown_users( $args = '' ) {
     586    global $wpdb;
    977587    $defaults = array(
    978         'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
     588        'show_option_all' => '', 'show_option_none' => '',
    979589        'orderby' => 'display_name', 'order' => 'ASC',
    980590        'include' => '', 'exclude' => '', 'multi' => 0,
    981591        'show' => 'display_name', 'echo' => 1,
    982         'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
    983         'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false
     592        'selected' => 0, 'name' => 'user', 'class' => '', 'blog_id' => $GLOBALS['blog_id'],
     593        'id' => '',
    984594    );
    985595
     
    989599    extract( $r, EXTR_SKIP );
    990600
    991     $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
    992     $query_args['fields'] = array( 'ID', $show );
    993     $users = get_users( $query_args );
     601    $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
     602    $query = "SELECT {$wpdb->users}.* FROM $wpdb->users, $wpdb->usermeta WHERE {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$blog_prefix}capabilities'";
     603
     604    $query_where = array();
     605
     606    if ( is_array($include) )
     607        $include = join(',', $include);
     608    $include = preg_replace('/[^0-9,]/', '', $include); // (int)
     609    if ( $include )
     610        $query_where[] = "ID IN ($include)";
     611
     612    if ( is_array($exclude) )
     613        $exclude = join(',', $exclude);
     614    $exclude = preg_replace('/[^0-9,]/', '', $exclude); // (int)
     615    if ( $exclude )
     616        $query_where[] = "ID NOT IN ($exclude)";
     617
     618    if ( $query_where )
     619        $query .= " AND " . join(' AND', $query_where);
     620
     621    $query .= " ORDER BY $orderby $order";
     622
     623    $users = $wpdb->get_results( $query );
    994624
    995625    $output = '';
    996     if ( !empty($users) && ( empty($hide_if_only_one_author) || count($users) > 1 ) ) {
     626    if ( !empty($users) ) {
    997627        $name = esc_attr( $name );
    998628        if ( $multi && ! $id )
     
    1011641        }
    1012642
    1013         $found_selected = false;
    1014643        foreach ( (array) $users as $user ) {
    1015644            $user->ID = (int) $user->ID;
    1016             $_selected = selected( $user->ID, $selected, false );
    1017             if ( $_selected )
    1018                 $found_selected = true;
    1019             $display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
    1020             $output .= "\t<option value='$user->ID'$_selected>" . esc_html($display) . "</option>\n";
    1021         }
    1022 
    1023         if ( $include_selected && ! $found_selected && ( $selected > 0 ) ) {
    1024             $user = get_userdata( $selected );
    1025645            $_selected = selected( $user->ID, $selected, false );
    1026646            $display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
     
    1066686 */
    1067687function get_user_metavalues($ids) {
     688    global $wpdb;
     689
     690    $clean = array_map('intval', $ids);
     691    if ( 0 == count($clean) )
     692        return $objects;
     693
     694    $list = implode(',', $clean);
     695
     696    $show = $wpdb->hide_errors();
     697    $metavalues = $wpdb->get_results("SELECT user_id, meta_key, meta_value FROM $wpdb->usermeta WHERE user_id IN ($list)");
     698    $wpdb->show_errors($show);
     699
    1068700    $objects = array();
    1069 
    1070     $ids = array_map('intval', $ids);
    1071     foreach ( $ids as $id )
     701    foreach($clean as $id) {
    1072702        $objects[$id] = array();
    1073 
    1074     $metas = update_meta_cache('user', $ids);
    1075 
    1076     foreach ( $metas as $id => $meta ) {
    1077         foreach ( $meta as $key => $metavalues ) {
    1078             foreach ( $metavalues as $value ) {
    1079                 $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);
    1080             }
    1081         }
     703    }
     704    foreach($metavalues as $meta_object) {
     705        $objects[$meta_object->user_id][] = $meta_object;
    1082706    }
    1083707
     
    1125749function _fill_many_users( &$users ) {
    1126750    $ids = array();
    1127     foreach( $users as $user_object ) {
     751    foreach($users as $user_object) {
    1128752        $ids[] = $user_object->ID;
    1129753    }
    1130754
    1131     $metas = get_user_metavalues($ids);
    1132 
    1133     foreach ( $users as $user_object ) {
    1134         if ( isset($metas[$user_object->ID]) ) {
    1135             _fill_single_user($user_object, $metas[$user_object->ID]);
     755    $metas = get_user_metavalues($ids);
     756
     757    foreach($users as $user_object) {
     758        if (isset($metas[$user_object->ID])) {
     759            _fill_single_user($user_object, $metas[$user_object->ID]);
    1136760        }
    1137761    }
     
    1182806 *
    1183807 * @since 2.3.0
    1184  * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and
     808 * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
    1185809 *  $user_id if $context == 'edit' and field name prefix == 'user_'.
    1186810 *
    1187811 * @uses apply_filters() Calls 'edit_user_$field' passing $value and $user_id if $context == 'db'.
    1188812 * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'user_'.
    1189  * @uses apply_filters() Calls '{$field}_pre' passing $value if $context == 'db' and field name prefix != 'user_'.
     813 * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'user_'.
    1190814 *
    1191815 * @uses apply_filters() Calls '$field' passing $value, $user_id and $context if $context == anything
     
    1220844    if ( 'edit' == $context ) {
    1221845        if ( $prefixed ) {
    1222             $value = apply_filters("edit_{$field}", $value, $user_id);
     846            $value = apply_filters("edit_$field", $value, $user_id);
    1223847        } else {
    1224             $value = apply_filters("edit_user_{$field}", $value, $user_id);
     848            $value = apply_filters("edit_user_$field", $value, $user_id);
    1225849        }
    1226850
    1227851        if ( 'description' == $field )
    1228             $value = esc_html( $value ); // textarea_escaped?
     852            $value = esc_html($value);
    1229853        else
    1230854            $value = esc_attr($value);
    1231855    } else if ( 'db' == $context ) {
    1232856        if ( $prefixed ) {
    1233             $value = apply_filters("pre_{$field}", $value);
     857            $value = apply_filters("pre_$field", $value);
    1234858        } else {
    1235             $value = apply_filters("pre_user_{$field}", $value);
     859            $value = apply_filters("pre_user_$field", $value);
    1236860        }
    1237861    } else {
     
    1240864            $value = apply_filters($field, $value, $user_id, $context);
    1241865        else
    1242             $value = apply_filters("user_{$field}", $value, $user_id, $context);
     866            $value = apply_filters("user_$field", $value, $user_id, $context);
    1243867    }
    1244868
     
    1282906    wp_cache_delete($user->user_email, 'useremail');
    1283907    wp_cache_delete($user->user_nicename, 'userslugs');
    1284     wp_cache_delete('blogs_of_user-' . $id, 'users');
    1285 }
    1286 
    1287 /**
    1288  * Checks whether the given username exists.
    1289  *
    1290  * @since 2.0.0
    1291  *
    1292  * @param string $username Username.
    1293  * @return null|int The user's ID on success, and null on failure.
    1294  */
    1295 function username_exists( $username ) {
    1296     if ( $user = get_userdatabylogin( $username ) ) {
    1297         return $user->ID;
    1298     } else {
    1299         return null;
    1300     }
    1301 }
    1302 
    1303 /**
    1304  * Checks whether the given email exists.
    1305  *
    1306  * @since 2.1.0
    1307  * @uses $wpdb
    1308  *
    1309  * @param string $email Email.
    1310  * @return bool|int The user's ID on success, and false on failure.
    1311  */
    1312 function email_exists( $email ) {
    1313     if ( $user = get_user_by_email($email) )
    1314         return $user->ID;
    1315 
    1316     return false;
    1317 }
    1318 
    1319 /**
    1320  * Checks whether an username is valid.
    1321  *
    1322  * @since 2.0.1
    1323  * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
    1324  *
    1325  * @param string $username Username.
    1326  * @return bool Whether username given is valid
    1327  */
    1328 function validate_username( $username ) {
    1329     $sanitized = sanitize_user( $username, true );
    1330     $valid = ( $sanitized == $username );
    1331     return apply_filters( 'validate_username', $valid, $username );
    1332 }
    1333 
    1334 /**
    1335  * Insert an user into the database.
    1336  *
    1337  * Can update a current user or insert a new user based on whether the user's ID
    1338  * is present.
    1339  *
    1340  * Can be used to update the user's info (see below), set the user's role, and
    1341  * set the user's preference on whether they want the rich editor on.
    1342  *
    1343  * Most of the $userdata array fields have filters associated with the values.
    1344  * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
    1345  * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
    1346  * by the field name. An example using 'description' would have the filter
    1347  * called, 'pre_user_description' that can be hooked into.
    1348  *
    1349  * The $userdata array can contain the following fields:
    1350  * 'ID' - An integer that will be used for updating an existing user.
    1351  * 'user_pass' - A string that contains the plain text password for the user.
    1352  * 'user_login' - A string that contains the user's username for logging in.
    1353  * 'user_nicename' - A string that contains a nicer looking name for the user.
    1354  *      The default is the user's username.
    1355  * 'user_url' - A string containing the user's URL for the user's web site.
    1356  * 'user_email' - A string containing the user's email address.
    1357  * 'display_name' - A string that will be shown on the site. Defaults to user's
    1358  *      username. It is likely that you will want to change this, for both
    1359  *      appearance and security through obscurity (that is if you don't use and
    1360  *      delete the default 'admin' user).
    1361  * 'nickname' - The user's nickname, defaults to the user's username.
    1362  * 'first_name' - The user's first name.
    1363  * 'last_name' - The user's last name.
    1364  * 'description' - A string containing content about the user.
    1365  * 'rich_editing' - A string for whether to enable the rich editor. False
    1366  *      if not empty.
    1367  * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
    1368  * 'role' - A string used to set the user's role.
    1369  * 'jabber' - User's Jabber account.
    1370  * 'aim' - User's AOL IM account.
    1371  * 'yim' - User's Yahoo IM account.
    1372  *
    1373  * @since 2.0.0
    1374  * @uses $wpdb WordPress database layer.
    1375  * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
    1376  * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
    1377  * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
    1378  *
    1379  * @param array $userdata An array of user data.
    1380  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
    1381  */
    1382 function wp_insert_user($userdata) {
    1383     global $wpdb;
    1384 
    1385     extract($userdata, EXTR_SKIP);
    1386 
    1387     // Are we updating or creating?
    1388     if ( !empty($ID) ) {
    1389         $ID = (int) $ID;
    1390         $update = true;
    1391         $old_user_data = get_userdata($ID);
    1392     } else {
    1393         $update = false;
    1394         // Hash the password
    1395         $user_pass = wp_hash_password($user_pass);
    1396     }
    1397 
    1398     $user_login = sanitize_user($user_login, true);
    1399     $user_login = apply_filters('pre_user_login', $user_login);
    1400 
    1401     //Remove any non-printable chars from the login string to see if we have ended up with an empty username
    1402     $user_login = trim($user_login);
    1403 
    1404     if ( empty($user_login) )
    1405         return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
    1406 
    1407     if ( !$update && username_exists( $user_login ) )
    1408         return new WP_Error('existing_user_login', __('This username is already registered.') );
    1409 
    1410     if ( empty($user_nicename) )
    1411         $user_nicename = sanitize_title( $user_login );
    1412     $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
    1413 
    1414     if ( empty($user_url) )
    1415         $user_url = '';
    1416     $user_url = apply_filters('pre_user_url', $user_url);
    1417 
    1418     if ( empty($user_email) )
    1419         $user_email = '';
    1420     $user_email = apply_filters('pre_user_email', $user_email);
    1421 
    1422     if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
    1423         return new WP_Error('existing_user_email', __('This email address is already registered.') );
    1424 
    1425     if ( empty($display_name) )
    1426         $display_name = $user_login;
    1427     $display_name = apply_filters('pre_user_display_name', $display_name);
    1428 
    1429     if ( empty($nickname) )
    1430         $nickname = $user_login;
    1431     $nickname = apply_filters('pre_user_nickname', $nickname);
    1432 
    1433     if ( empty($first_name) )
    1434         $first_name = '';
    1435     $first_name = apply_filters('pre_user_first_name', $first_name);
    1436 
    1437     if ( empty($last_name) )
    1438         $last_name = '';
    1439     $last_name = apply_filters('pre_user_last_name', $last_name);
    1440 
    1441     if ( empty($description) )
    1442         $description = '';
    1443     $description = apply_filters('pre_user_description', $description);
    1444 
    1445     if ( empty($rich_editing) )
    1446         $rich_editing = 'true';
    1447 
    1448     if ( empty($comment_shortcuts) )
    1449         $comment_shortcuts = 'false';
    1450 
    1451     if ( empty($admin_color) )
    1452         $admin_color = 'fresh';
    1453     $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
    1454 
    1455     if ( empty($use_ssl) )
    1456         $use_ssl = 0;
    1457 
    1458     if ( empty($user_registered) )
    1459         $user_registered = gmdate('Y-m-d H:i:s');
    1460 
    1461     if ( empty($show_admin_bar_front) )
    1462         $show_admin_bar_front = 'true';
    1463 
    1464     if ( empty($show_admin_bar_admin) )
    1465         $show_admin_bar_admin = is_multisite() ? 'true' : 'false';
    1466 
    1467     $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
    1468 
    1469     if ( $user_nicename_check ) {
    1470         $suffix = 2;
    1471         while ($user_nicename_check) {
    1472             $alt_user_nicename = $user_nicename . "-$suffix";
    1473             $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
    1474             $suffix++;
    1475         }
    1476         $user_nicename = $alt_user_nicename;
    1477     }
    1478 
    1479     $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
    1480     $data = stripslashes_deep( $data );
    1481 
    1482     if ( $update ) {
    1483         $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
    1484         $user_id = (int) $ID;
    1485     } else {
    1486         $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
    1487         $user_id = (int) $wpdb->insert_id;
    1488     }
    1489 
    1490     update_user_meta( $user_id, 'first_name', $first_name );
    1491     update_user_meta( $user_id, 'last_name', $last_name );
    1492     update_user_meta( $user_id, 'nickname', $nickname );
    1493     update_user_meta( $user_id, 'description', $description );
    1494     update_user_meta( $user_id, 'rich_editing', $rich_editing );
    1495     update_user_meta( $user_id, 'comment_shortcuts', $comment_shortcuts );
    1496     update_user_meta( $user_id, 'admin_color', $admin_color );
    1497     update_user_meta( $user_id, 'use_ssl', $use_ssl );
    1498     update_user_meta( $user_id, 'show_admin_bar_front', $show_admin_bar_front );
    1499     update_user_meta( $user_id, 'show_admin_bar_admin', $show_admin_bar_admin );
    1500 
    1501     $user = new WP_User($user_id);
    1502 
    1503     foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
    1504         if ( empty($$method) )
    1505             $$method = '';
    1506 
    1507         update_user_meta( $user_id, $method, $$method );
    1508     }
    1509 
    1510     if ( isset($role) )
    1511         $user->set_role($role);
    1512     elseif ( !$update )
    1513         $user->set_role(get_option('default_role'));
    1514 
    1515     wp_cache_delete($user_id, 'users');
    1516     wp_cache_delete($user_login, 'userlogins');
    1517 
    1518     if ( $update )
    1519         do_action('profile_update', $user_id, $old_user_data);
    1520     else
    1521         do_action('user_register', $user_id);
    1522 
    1523     return $user_id;
    1524 }
    1525 
    1526 /**
    1527  * Update an user in the database.
    1528  *
    1529  * It is possible to update a user's password by specifying the 'user_pass'
    1530  * value in the $userdata parameter array.
    1531  *
    1532  * If $userdata does not contain an 'ID' key, then a new user will be created
    1533  * and the new user's ID will be returned.
    1534  *
    1535  * If current user's password is being updated, then the cookies will be
    1536  * cleared.
    1537  *
    1538  * @since 2.0.0
    1539  * @see wp_insert_user() For what fields can be set in $userdata
    1540  * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
    1541  *
    1542  * @param array $userdata An array of user data.
    1543  * @return int The updated user's ID.
    1544  */
    1545 function wp_update_user($userdata) {
    1546     $ID = (int) $userdata['ID'];
    1547 
    1548     // First, get all of the original fields
    1549     $user = get_userdata($ID);
    1550 
    1551     // Escape data pulled from DB.
    1552     $user = add_magic_quotes(get_object_vars($user));
    1553 
    1554     // If password is changing, hash it now.
    1555     if ( ! empty($userdata['user_pass']) ) {
    1556         $plaintext_pass = $userdata['user_pass'];
    1557         $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
    1558     }
    1559 
    1560     wp_cache_delete($user[ 'user_email' ], 'useremail');
    1561 
    1562     // Merge old and new fields with new fields overwriting old ones.
    1563     $userdata = array_merge($user, $userdata);
    1564     $user_id = wp_insert_user($userdata);
    1565 
    1566     // Update the cookies if the password changed.
    1567     $current_user = wp_get_current_user();
    1568     if ( $current_user->id == $ID ) {
    1569         if ( isset($plaintext_pass) ) {
    1570             wp_clear_auth_cookie();
    1571             wp_set_auth_cookie($ID);
    1572         }
    1573     }
    1574 
    1575     return $user_id;
    1576 }
    1577 
    1578 /**
    1579  * A simpler way of inserting an user into the database.
    1580  *
    1581  * Creates a new user with just the username, password, and email. For a more
    1582  * detail creation of a user, use wp_insert_user() to specify more infomation.
    1583  *
    1584  * @since 2.0.0
    1585  * @see wp_insert_user() More complete way to create a new user
    1586  *
    1587  * @param string $username The user's username.
    1588  * @param string $password The user's password.
    1589  * @param string $email The user's email (optional).
    1590  * @return int The new user's ID.
    1591  */
    1592 function wp_create_user($username, $password, $email = '') {
    1593     $user_login = esc_sql( $username );
    1594     $user_email = esc_sql( $email    );
    1595     $user_pass = $password;
    1596 
    1597     $userdata = compact('user_login', 'user_email', 'user_pass');
    1598     return wp_insert_user($userdata);
    1599 }
    1600 
    1601 
    1602 /**
    1603  * Set up the default contact methods
    1604  *
    1605  * @access private
    1606  * @since
    1607  *
    1608  * @param object $user User data object (optional)
    1609  * @return array $user_contactmethods Array of contact methods and their labels.
    1610  */
    1611 function _wp_get_user_contactmethods( $user = null ) {
    1612     $user_contactmethods = array(
    1613         'aim' => __('AIM'),
    1614         'yim' => __('Yahoo IM'),
    1615         'jabber' => __('Jabber / Google Talk')
    1616     );
    1617     return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
    1618908}
    1619909
Note: See TracChangeset for help on using the changeset viewer.