Skip to:
Content

BuddyPress.org

Ticket #4060: 4060.03.patch

File 4060.03.patch, 25.4 KB (added by boonebgorges, 13 years ago)
  • bp-core/bp-core-classes.php

    diff --git bp-core/bp-core-classes.php bp-core/bp-core-classes.php
    index 32fd518..cb1965b 100644
     
    11<?php
     2
    23// Exit if accessed directly
    34if ( !defined( 'ABSPATH' ) ) exit;
    45
    56/**
     7 * BuddyPress User Query class
     8 *
     9 * Used for querying users in a BuddyPress context, in situations where
     10 * WP_User_Query won't do the trick: Member directories, the Friends component,
     11 * etc.
     12 *
     13 * @since BuddyPress (1.7)
     14 */
     15class BP_User_Query {
     16
     17        /** Variables *************************************************************/
     18
     19        /**
     20         * Array of variables to query with
     21         *
     22         * @since BuddyPress (1.7)
     23         * @var array
     24         */
     25        public $query_vars = array();
     26
     27        /**
     28         * List of found users and their respective data
     29         *
     30         * @since BuddyPress (1.7)
     31         * @access public To allow components to manipulate them
     32         * @var array
     33         */
     34        public $results = array();
     35
     36        /**
     37         * Total number of found users for the current query
     38         *
     39         * @since BuddyPress (1.7)
     40         * @access public To allow components to manipulate it
     41         * @var int
     42         */
     43        public $total_users = 0;
     44
     45        /**
     46         * List of found user ID's
     47         *
     48         * @since BuddyPress (1.7)
     49         * @access private To disallow components from manipulating them
     50         * @var array
     51         */
     52        private $user_ids = array();
     53
     54        /**
     55         * SQL clauses for the user ID query
     56         *
     57         * @since BuddyPress (1.7)
     58         * @access protected To disallow
     59         * @var array()
     60         */
     61        private $uid_clauses = array();
     62
     63        /**
     64         * SQL database column name to order by
     65         *
     66         * @since BuddyPress (1.7)
     67         * @var string
     68         */
     69        private $uid_name = '';
     70
     71        /** Methods ***************************************************************/
     72
     73        /**
     74         * Constructor
     75         *
     76         * @since 1.7
     77         *
     78         * @param string|array $query The query variables
     79         */
     80        public function __construct( $query = null ) {
     81                if ( ! empty( $query ) ) {
     82                        $this->query_vars = wp_parse_args( $query, array(
     83                                'type'            => 'newest',
     84                                'per_page'        => 0,
     85                                'page'            => 1,
     86                                'user_id'         => 0,
     87                                'include'         => false,
     88                                'search_terms'    => false,
     89                                'exclude'         => false,
     90                                'meta_key'        => false,
     91                                'meta_value'      => false,
     92                                'populate_extras' => true,
     93                                'count_total'     => 'count_query'
     94                        ) );
     95
     96                        // Get user ids
     97                        $this->prepare_user_ids_query();
     98                        $this->do_user_ids_query();
     99                }
     100
     101                // Bail if no user IDs were found
     102                if ( empty( $this->user_ids ) ) {
     103                        return;
     104                }
     105
     106                // Fetch additional data. First, using WP_User_Query
     107                $this->do_wp_user_query();
     108
     109                // Get BuddyPress specific user data
     110                $this->populate_extras();
     111        }
     112
     113        /**
     114         * Prepare the query for user_ids
     115         *
     116         * @since BuddyPress (1.7)
     117         */
     118        protected function prepare_user_ids_query() {
     119                global $wpdb, $bp;
     120
     121                // Default query variables used here
     122                $type         = '';
     123                $per_page     = 0;
     124                $page         = 1;
     125                $user_id      = 0;
     126                $include      = false;
     127                $search_terms = false;
     128                $exclude      = false;
     129                $meta_key     = false;
     130                $meta_value   = false;
     131
     132                extract( $this->query_vars );
     133
     134                // Setup the main SQL query container
     135                $sql = array(
     136                        'select'  => '',
     137                        'where'   => array(),
     138                        'orderby' => '',
     139                        'order'   => '',
     140                        'limit'   => ''
     141                );
     142
     143                /** TYPE **************************************************************/
     144
     145                // Determines the sort order, which means it also determines where the
     146                // user IDs are drawn from (the SELECT and WHERE statements)
     147                switch ( $type ) {
     148
     149                        // 'active', 'online', 'newest', and 'random' queries
     150                        // all happen against the last_activity usermeta key
     151                        case 'active' :
     152                        case 'online' :
     153                        case 'newest' :
     154                        case 'random' :
     155                                $this->uid_name = 'user_id';
     156                                $sql['select']  = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->usermeta} u";
     157                                $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'last_activity' ) );
     158
     159                                if ( 'newest' == $type ) {
     160                                        $sql['orderby'] = "ORDER BY u.user_id";
     161                                        $sql['order'] = "DESC";
     162                                } else if ( 'random' == $type ) {
     163                                        $sql['orderby'] = "ORDER BY rand()";
     164                                } else {
     165                                        $sql['orderby'] = "ORDER BY u.meta_value";
     166                                        $sql['order'] = "DESC";
     167                                }
     168
     169                                break;
     170
     171                        // 'popular' sorts by the 'total_friend_count' usermeta
     172                        case 'popular' :
     173                                $this->uid_name = 'user_id';
     174                                $sql['select']  = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->usermeta} u";
     175                                $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) );
     176                                $sql['orderby'] = "ORDER BY u.meta_value";
     177                                $sql['order']   = "DESC";
     178
     179                                break;
     180
     181                        // 'alphabetical' sorts depend on the xprofile setup
     182                        case 'alphabetical' :
     183
     184                                // We prefer to do alphabetical sorts against the display_name field
     185                                // of wp_users, because the table is smaller and better indexed. We
     186                                // can do so if xprofile sync is enabled, or if xprofile is inactive.
     187                                //
     188                                // @todo remove need for bp_is_active() check
     189                                if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) {
     190                                        $this->uid_name = 'ID';
     191                                        $sql['select']  = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->users} u";
     192                                        $sql['orderby'] = "ORDER BY u.display_name";
     193                                        $sql['order']   = "ASC";
     194
     195                                // When profile sync is disabled, alphabetical sorts must happen against
     196                                // the xprofile table
     197                                } else {
     198                                        $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) );
     199
     200                                        $this->uid_name = 'user_id';
     201                                        $sql['select']  = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$bp->profile->table_name_data} u";
     202                                        $sql['where'][] = "u.field_id = {$fullname_field_id}";
     203                                        $sql['orderby'] = "ORDER BY u.value";
     204                                        $sql['order']   = "ASC";
     205                                }
     206
     207                                break;
     208                }
     209
     210                /** WHERE *************************************************************/
     211
     212                // 'include' - User ids to include in the results
     213                if ( false !== $include ) {
     214                        $include        = wp_parse_id_list( $include );
     215                        $include_ids    = $wpdb->escape( implode( ',', (array) $include ) );
     216                        $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})";
     217                }
     218
     219                // 'exclude' - User ids to exclude from the results
     220                if ( false !== $exclude ) {
     221                        $exclude        = wp_parse_id_list( $exclude );
     222                        $exclude_ids    = $wpdb->escape( implode( ',', (array) $exclude ) );
     223                        $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})";
     224                }
     225
     226                // 'user_id' - When a user id is passed, limit to the friends of the user
     227                // @todo remove need for bp_is_active() check
     228                if ( !empty( $user_id ) && bp_is_active( 'friends' ) ) {
     229                        $friend_ids = friends_get_friend_user_ids( $user_id );
     230                        $friend_ids = $wpdb->escape( implode( ',', (array) $friend_ids ) );
     231
     232                        if ( !empty( $friend_ids ) ) {
     233                                $sql['where'][] = "u.{$this->uid_name} NOT IN ({$friend_ids})";
     234                        } else {
     235                                // If the user has no friends, make sure the query returns null
     236                                $sql['where'][] = "0 = 1";
     237                        }
     238                }
     239
     240                /** Search Terms ******************************************************/
     241
     242                // 'search_terms' searches the xprofile fields
     243                // To avoid global joins, do a separate query
     244                // @todo remove need for bp_is_active() check
     245                if ( false !== $search_terms && bp_is_active( 'xprofile' ) ) {
     246                        $found_user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s", '%%' . like_escape( $search_terms ) . '%%' ), ARRAY_N );
     247
     248                        if ( ! empty( $found_user_ids ) ) {
     249                                $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")";
     250                        }
     251                }
     252
     253                // 'meta_key', 'meta_value' allow usermeta search
     254                // To avoid global joins, do a separate query
     255                if ( false !== $meta_key ) {
     256                        $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key );
     257
     258                        if ( false !== $meta_value ) {
     259                                $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value );
     260                        }
     261
     262                        $found_user_ids = $wpdb->get_col( $meta_sql );
     263
     264                        if ( ! empty( $found_user_ids ) ) {
     265                                $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")";
     266                        }
     267                }
     268
     269                // 'per_page', 'page' - handles LIMIT
     270                if ( !empty( $per_page ) && !empty( $page ) ) {
     271                        $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) );
     272                } else {
     273                        $sql['limit'] = '';
     274                }
     275
     276                // Assemble the query chunks
     277                $this->uid_clauses['select']  = $sql['select'];
     278                $this->uid_clauses['where']   = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : '';
     279                $this->uid_clauses['orderby'] = $sql['orderby'];
     280                $this->uid_clauses['order']   = $sql['order'];
     281                $this->uid_clauses['limit']   = $sql['limit'];
     282
     283                do_action_ref_array( 'bp_pre_user_query', array( &$this ) );
     284        }
     285
     286        /**
     287         * Perform a database query to specifically get only user IDs, using
     288         * existing query variables set previously in the constructor.
     289         *
     290         * Also used to quickly perform user total counts.
     291         *
     292         * @since BuddyPress (1.7)
     293         */
     294        protected function do_user_ids_query() {
     295                global $wpdb;
     296
     297                // If counting using SQL_CALC_FOUND_ROWS, set it up here
     298                if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {
     299                        $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] );
     300                }
     301
     302                // Get the specific user ids
     303                $this->user_ids = $wpdb->get_col( $wpdb->prepare( "{$this->uid_clauses['select']} {$this->uid_clauses['where']} {$this->uid_clauses['orderby']} {$this->uid_clauses['order']} {$this->uid_clauses['limit']}" ) );
     304
     305                // Get the total user count
     306                if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {
     307                        $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "SELECT FOUND_ROWS()", $this ) );
     308                } elseif ( 'count_query' == $this->query_vars['count_total'] ) {
     309                        $count_select      = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(DISTINCT u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] );
     310                        $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "{$count_select} {$this->uid_clauses['where']}", $this ) );
     311                }
     312        }
     313
     314        /**
     315         * Perform a database query using the WP_User_Query() object, using existing
     316         * fields, variables, and user ID's set previously in this class.
     317         *
     318         * @since BuddyPress (1.7)
     319         */
     320        protected function do_wp_user_query() {
     321                $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array(
     322
     323                        // Relevant
     324                        'fields'      => array( 'ID', 'user_registered', 'user_login', 'user_nicename', 'display_name', 'user_email' ),
     325                        'include'     => $this->user_ids,
     326
     327                        // Overrides
     328                        'blog_id'     => 0,    // BP does not require blog roles
     329                        'count_total' => false // We already have a count
     330
     331                ), $this ) );
     332
     333                // Reindex for easier matching
     334                $r = array();
     335                foreach ( $wp_user_query->results as $u ) {
     336                        $r[ $u->ID ] = $u;
     337                }
     338
     339                // Match up to the user ids from the main query
     340                foreach ( $this->user_ids as $uid ) {
     341                        if ( isset( $r[ $uid ] ) ) {
     342                                $this->results[ $uid ] = $r[ $uid ];
     343
     344                                // The BP template functions expect an 'id'
     345                                // (as opposed to 'ID') property
     346                                $this->results[ $uid ]->id = $uid;
     347                        }
     348                }
     349        }
     350
     351        /**
     352         * Perform a database query to populate any extra metadata we might need.
     353         * Different components will hook into the 'bp_user_query_populate_extras'
     354         * action to loop in the things they want.
     355         *
     356         * @since BuddyPress (1.7)
     357         *
     358         * @global BuddyPress $bp
     359         * @global WPDB $wpdb
     360         * @return
     361         */
     362        protected function populate_extras() {
     363                global $wpdb;
     364
     365                // Bail if no users
     366                if ( empty( $this->user_ids ) || empty( $this->results ) ) {
     367                        return;
     368                }
     369
     370                // Bail if the populate_extras flag is set to false
     371                // In the case of the 'popular' sort type, we force
     372                // populate_extras to true, because we need the friend counts
     373                if ( 'popular' == $this->query_vars['type'] ) {
     374                        $this->query_vars['populate_extras'] = 1;
     375                }
     376
     377                if ( ! (bool) $this->query_vars['populate_extras'] ) {
     378                        return;
     379                }
     380
     381                // Turn user ID's into a query-usable, comma separated value
     382                $user_ids_sql = implode( ',', wp_parse_id_list( $this->user_ids ) );
     383
     384                /**
     385                 * Use this action to independently populate your own custom extras.
     386                 *
     387                 * Note that anything you add here should query using $user_ids_sql, to
     388                 * avoid running multiple queries per user in the loop.
     389                 *
     390                 * Two BuddyPress components currently do this:
     391                 * - XProfile: To override display names
     392                 * - Friends:  To set whether or not a user is the current users friend
     393                 *
     394                 * @see bp_xprofile_filter_user_query_populate_extras()
     395                 * @see bp_friends_filter_user_query_populate_extras()
     396                 */
     397                do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) );
     398
     399                // Fetch usermeta data
     400                // We want the three following pieces of info from usermeta:
     401                // - friend count
     402                // - last activity
     403                // - latest update
     404                $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' );
     405                $last_activity_key      = bp_get_user_meta_key( 'last_activity'      );
     406                $bp_latest_update_key   = bp_get_user_meta_key( 'bp_latest_update'   );
     407
     408                // total_friend_count must be set for each user, even if its
     409                // value is 0
     410                foreach ( $this->results as $uindex => $user ) {
     411                        $this->results[$uindex]->total_friend_count = 0;
     412                }
     413
     414                // Create, prepare, and run the seperate usermeta query
     415                $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $last_activity_key, $bp_latest_update_key ) );
     416
     417                // The $members_template global expects the index key to be different
     418                // from the meta_key in some cases, so we rejig things here.
     419                foreach ( $user_metas as $user_meta ) {
     420                        switch ( $user_meta->meta_key ) {
     421                                case $total_friend_count_key :
     422                                        $key = 'total_friend_count';
     423                                        break;
     424
     425                                case $last_activity_key :
     426                                        $key = 'last_activity';
     427                                        break;
     428
     429                                case $bp_latest_update_key :
     430                                        $key = 'latest_update';
     431                                        break;
     432                        }
     433
     434                        if ( isset( $this->results[ $user_meta->user_id ] ) ) {
     435                                $this->results[ $user_meta->user_id ]->{$key} = $user_meta->meta_value;
     436                        }
     437                }
     438
     439                // When meta_key or meta_value have been passed to the query,
     440                // fetch the resulting values for use in the template functions
     441                if ( ! empty( $this->query_vars['meta_key'] ) ) {
     442                        $meta_sql = array(
     443                                'select' => "SELECT user_id, meta_key, meta_value",
     444                                'from'   => "FROM $wpdb->usermeta",
     445                                'where'  => $wpdb->prepare( "WHERE meta_key = %s", $this->query_vars['meta_key'] )
     446                        );
     447
     448                        if ( false !== $this->query_vars['meta_value'] ) {
     449                                $meta_sql['where'] .= $wpdb->prepare( " AND meta_value = %s", $this->query_vars['meta_value'] );
     450                        }
     451
     452                        $metas = $wpdb->get_results( $wpdb->prepare( "{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}" ) );
     453
     454                        if ( ! empty( $metas ) ) {
     455                                foreach ( $metas as $meta ) {
     456                                        if ( isset( $this->results[ $meta->user_id ] ) ) {
     457                                                $this->results[ $meta->user_id ]->meta_key = $meta->meta_key;
     458
     459                                                if ( ! empty( $meta->meta_value ) ) {
     460                                                        $this->results[ $meta->user_id ]->meta_value = $meta->meta_value;
     461                                                }
     462                                        }
     463                                }
     464                        }
     465                }
     466        }
     467}
     468
     469/**
    6470 * BP_Core_User class can be used by any component. It will fetch useful
    7471 * details for any user when provided with a user_id.
    8472 *
    class BP_Core_User { 
    201665        function get_users( $type, $limit = 0, $page = 1, $user_id = 0, $include = false, $search_terms = false, $populate_extras = true, $exclude = false, $meta_key = false, $meta_value = false ) {
    202666                global $wpdb, $bp;
    203667
     668                _deprecated_function( __METHOD__, '1.7', 'BP_User_Query' );
     669
    204670                $sql = array();
    205671
    206672                $sql['select_main'] = "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.display_name, u.user_email";
  • bp-core/bp-core-widgets.php

    diff --git bp-core/bp-core-widgets.php bp-core/bp-core-widgets.php
    index fae5d68..0da4947 100644
    class BP_Core_Members_Widget extends WP_Widget { 
    4242                   . $title
    4343                   . $after_title; ?>
    4444
    45                 <?php if ( bp_has_members( 'user_id=0&type=' . $instance['member_default'] . '&max=' . $instance['max_members'] . '&populate_extras=0' ) ) : ?>
     45                <?php if ( bp_has_members( 'user_id=0&type=' . $instance['member_default'] . '&max=' . $instance['max_members'] . '&populate_extras=1' ) ) : ?>
    4646                        <div class="item-options" id="members-list-options">
    4747                                <a href="<?php echo site_url( bp_get_members_root_slug() ); ?>" id="newest-members" <?php if ( $instance['member_default'] == 'newest' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></a>
    4848                                |  <a href="<?php echo site_url( bp_get_members_root_slug() ); ?>" id="recently-active-members" <?php if ( $instance['member_default'] == 'active' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></a>
    class BP_Core_Whos_Online_Widget extends WP_Widget { 
    159159                   . $instance['title']
    160160                   . $after_title; ?>
    161161
    162                 <?php if ( bp_has_members( 'user_id=0&type=online&per_page=' . $instance['max_members'] . '&max=' . $instance['max_members'] . '&populate_extras=0' ) ) : ?>
     162                <?php if ( bp_has_members( 'user_id=0&type=online&per_page=' . $instance['max_members'] . '&max=' . $instance['max_members'] . '&populate_extras=1' ) ) : ?>
    163163                        <div class="avatar-block">
    164164                                <?php while ( bp_members() ) : bp_the_member(); ?>
    165165                                        <div class="item-avatar">
    class BP_Core_Recently_Active_Widget extends WP_Widget { 
    223223                   . $instance['title']
    224224                   . $after_title; ?>
    225225
    226                 <?php if ( bp_has_members( 'user_id=0&type=active&per_page=' . $instance['max_members'] . '&max=' . $instance['max_members'] . '&populate_extras=0' ) ) : ?>
     226                <?php if ( bp_has_members( 'user_id=0&type=active&per_page=' . $instance['max_members'] . '&max=' . $instance['max_members'] . '&populate_extras=1' ) ) : ?>
    227227                        <div class="avatar-block">
    228228                                <?php while ( bp_members() ) : bp_the_member(); ?>
    229229                                        <div class="item-avatar">
    function bp_core_ajax_widget_members() { 
    293293                        break;
    294294        }
    295295
    296         if ( bp_has_members( 'user_id=0&type=' . $type . '&per_page=' . $_POST['max-members'] . '&max=' . $_POST['max-members'] . '&populate_extras=0' ) ) : ?>
     296        if ( bp_has_members( 'user_id=0&type=' . $type . '&per_page=' . $_POST['max-members'] . '&max=' . $_POST['max-members'] . '&populate_extras=1' ) ) : ?>
    297297                <?php echo '0[[SPLIT]]'; // return valid result. TODO: remove this. ?>
    298298                <div class="avatar-block">
    299299                        <?php while ( bp_members() ) : bp_the_member(); ?>
    function bp_core_ajax_widget_members() { 
    326326add_action( 'wp_ajax_widget_members', 'bp_core_ajax_widget_members' );
    327327add_action( 'wp_ajax_nopriv_widget_members', 'bp_core_ajax_widget_members' );
    328328
    329 ?>
    330  No newline at end of file
     329?>
  • new file p-friends/bp-friends-filters.php

    diff --git bp-friends/bp-friends-filters.php bp-friends/bp-friends-filters.php
    new file mode 100644
    index 0000000..93251cb
    - +  
     1<?php
     2
     3/**
     4 * BuddyPress Friend Filters
     5 *
     6 * @package BuddyPress
     7 * @subpackage FriendsFilters
     8 */
     9
     10/**
     11 * Filter BP_User_Query::populate_extras to override each queried users fullname
     12 *
     13 * @since BuddyPress (1.7)
     14 *
     15 * @global BuddyPress $bp
     16 * @global WPDB $wpdb
     17 * @param BP_User_Query $user_query
     18 * @param string $user_ids_sql
     19 */
     20function bp_friends_filter_user_query_populate_extras( BP_User_Query $user_query, $user_ids_sql ) {
     21        global $bp, $wpdb;
     22
     23        // Fetch whether or not the user is a friend of the current user
     24        $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids_sql} ) ) OR (initiator_user_id IN ( {$user_ids_sql} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id() ) );
     25
     26        // The "friend" is the user ID in the pair who is *not* the logged in user
     27        foreach ( (array) $friend_status as $fs ) {
     28                $friend_id = bp_loggedin_user_id() == $fs->initiator_user_id ? $fs->friend_user_id : $fs->initiator_user_id;
     29
     30                if ( isset( $user_query->results[ $friend_id ] ) ) {
     31                        $user_query->results[ $friend_id ]->is_friend = $fs->is_confirmed;
     32                }
     33        }
     34}
     35add_filter( 'bp_user_query_populate_extras', 'bp_friends_filter_user_query_populate_extras', 4, 2 );
  • bp-members/bp-members-functions.php

    diff --git bp-members/bp-members-functions.php bp-members/bp-members-functions.php
    index fa17fb1..c206fda 100644
    add_action( 'bp_setup_globals', 'bp_core_define_slugs', 11 ); 
    7171/**
    7272 * Return an array of users IDs based on the parameters passed.
    7373 *
     74 * Since BuddyPress 1.7, bp_core_get_users() uses BP_User_Query. If you
     75 * need backward compatibility with BP_Core_User::get_users(), filter the
     76 * bp_use_legacy_user_query value, returning true.
     77 *
    7478 * @package BuddyPress Core
    7579 */
    7680function bp_core_get_users( $args = '' ) {
    7781
    78         $defaults = array(
    79                 'type'            => 'active', // active, newest, alphabetical, random or popular
    80                 'user_id'         => false,    // Pass a user_id to limit to only friend connections for this user
    81                 'exclude'         => false,    // Users to exclude from results
    82                 'search_terms'    => false,    // Limit to users that match these search terms
    83                 'meta_key'        => false,    // Limit to users who have this piece of usermeta
    84                 'meta_value'      => false,    // With meta_key, limit to users where usermeta matches this value
    85 
    86                 'include'         => false,    // Pass comma separated list of user_ids to limit to only these users
    87                 'per_page'        => 20,       // The number of results to return per page
    88                 'page'            => 1,        // The page to return if limiting per page
    89                 'populate_extras' => true,     // Fetch the last active, where the user is a friend, total friend count, latest update
    90         );
     82        // Parse the user query arguments
     83        $params = wp_parse_args( $args, array(
     84                'type'            => 'active',     // active, newest, alphabetical, random or popular
     85                'user_id'         => false,        // Pass a user_id to limit to only friend connections for this user
     86                'exclude'         => false,        // Users to exclude from results
     87                'search_terms'    => false,        // Limit to users that match these search terms
     88                'meta_key'        => false,        // Limit to users who have this piece of usermeta
     89                'meta_value'      => false,        // With meta_key, limit to users where usermeta matches this value
     90                'include'         => false,        // Pass comma separated list of user_ids to limit to only these users
     91                'per_page'        => 20,           // The number of results to return per page
     92                'page'            => 1,            // The page to return if limiting per page
     93                'populate_extras' => true,         // Fetch the last active, where the user is a friend, total friend count, latest update
     94                'count_total'     => 'count_query' // What kind of total user count to do, if any. 'count_query', 'sql_calc_found_rows', or false
     95        ) );
     96
     97        // For legacy users. Use of BP_Core_User::get_users() is deprecated.
     98        if ( apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, $params ) ) {
     99                extract( $params, EXTR_SKIP );
     100                $retval = BP_Core_User::get_users( $type, $per_page, $page, $user_id, $include, $search_terms, $populate_extras, $exclude, $meta_key, $meta_value );
    91101
    92         $params = wp_parse_args( $args, $defaults );
    93         extract( $params, EXTR_SKIP );
     102        // Default behavior as of BuddyPress 1.7
     103        } else {               
     104
     105                // Get users like we were asked to do...
     106                $users = new BP_User_Query( $params );
     107
     108                // ...but reformat the results to match bp_core_get_users() behavior.
     109                $retval = array(
     110                        'users' => array_values( $users->results ),
     111                        'total' => $users->total_users
     112                );
     113        }
    94114
    95         return apply_filters( 'bp_core_get_users', BP_Core_User::get_users( $type, $per_page, $page, $user_id, $include, $search_terms, $populate_extras, $exclude, $meta_key, $meta_value ), $params );
     115        return apply_filters( 'bp_core_get_users', $retval, $params );
    96116}
    97117
    98118/**
  • bp-xprofile/bp-xprofile-filters.php

    diff --git bp-xprofile/bp-xprofile-filters.php bp-xprofile/bp-xprofile-filters.php
    index fe8bd70..9fb4dba 100644
    function xprofile_filter_comments( $comments, $post_id ) { 
    219219}
    220220add_filter( 'comments_array', 'xprofile_filter_comments', 10, 2 );
    221221
     222/**
     223 * Filter BP_User_Query::populate_extras to override each queries users fullname
     224 *
     225 * @since BuddyPress (1.7)
     226 *
     227 * @global BuddyPress $bp
     228 * @global WPDB $wpdb
     229 * @param BP_User_Query $user_query
     230 * @param string $user_ids_sql
     231 */
     232function bp_xprofile_filter_user_query_populate_extras( BP_User_Query $user_query, $user_ids_sql ) {
     233        global $bp, $wpdb;
     234
     235        if ( bp_is_active( 'xprofile' ) ) {
     236                $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) );
     237                $user_id_names     = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, value as fullname FROM {$bp->profile->table_name_data} WHERE user_id IN ({$user_ids_sql}) AND field_id = {$fullname_field_id}" ) );
    222238
     239                // Loop through names and override each user's fullname
     240                foreach ( $user_id_names as $user ) {
     241                        if ( isset( $user_query->results[ $user->user_id ] ) ) {
     242                                $user_query->results[ $user->user_id ]->fullname = $user->fullname;
     243                        }
     244                }
     245        }
     246}
     247add_filter( 'bp_user_query_populate_extras', 'bp_xprofile_filter_user_query_populate_extras', 2, 2 );
    223248
    224249?>