/**
* Get a user's friends, in the order in which they joined the site.
*
* @since 1.0.0
*
* @see BP_Core_User::get_users() for a description of return value.
*
* @param int $user_id ID of the user whose friends are being retrieved.
* @param int $per_page Optional. Number of results to return per page.
* Default: 0 (no pagination; show all results).
* @param int $page Optional. Number of the page of results to return.
* Default: 0 (no pagination; show all results).
* @param string $filter Optional. Limit results to those matching a search
* string.
* @return array See {@link BP_Core_User::get_users()}.
*/
function friends_get_newest($user_id, $per_page = 0, $page = 0, $filter = '')
{
/**
* Filters a user's friends listed from newest to oldest.
*
* @since 1.2.0
*
* @param array {
* @type int $total_users Total number of users matched by query params.
* @type array $paged_users The current page of users matched by query params.
* }
*/
return apply_filters('friends_get_newest', BP_Core_User::get_users('newest', $per_page, $page, $user_id, $filter));
}
/**
* Perform a database query to populate any extra metadata we might need.
*
* Different components will hook into the 'bp_user_query_populate_extras'
* action to loop in the things they want.
*
* @since 1.7.0
*
* @global WPDB $wpdb Global WordPress database access object.
*/
public function populate_extras()
{
global $wpdb;
// Bail if no users.
if (empty($this->user_ids) || empty($this->results)) {
return;
}
// Bail if the populate_extras flag is set to false
// In the case of the 'popular' sort type, we force
// populate_extras to true, because we need the friend counts.
if ('popular' == $this->query_vars['type']) {
$this->query_vars['populate_extras'] = 1;
}
if (!(bool) $this->query_vars['populate_extras']) {
return;
}
// Turn user ID's into a query-usable, comma separated value.
$user_ids_sql = implode(',', wp_parse_id_list($this->user_ids));
/**
* Allows users to independently populate custom extras.
*
* Note that anything you add here should query using $user_ids_sql, to
* avoid running multiple queries per user in the loop.
*
* Two BuddyPress components currently do this:
* - XProfile: To override display names.
* - Friends: To set whether or not a user is the current users friend.
*
* @see bp_xprofile_filter_user_query_populate_extras()
* @see bp_friends_filter_user_query_populate_extras()
*
* @since 1.7.0
*
* @param BP_User_Query $this Current BP_User_Query instance.
* @param string $user_ids_sql Comma-separated string of user IDs.
*/
do_action_ref_array('bp_user_query_populate_extras', array($this, $user_ids_sql));
// Fetch last_active data from the activity table.
$last_activities = BP_Core_User::get_last_activity($this->user_ids);
// Set a last_activity value for each user, even if it's empty.
foreach ($this->results as $user_id => $user) {
$user_last_activity = isset($last_activities[$user_id]) ? $last_activities[$user_id]['date_recorded'] : '';
$this->results[$user_id]->last_activity = $user_last_activity;
}
// Fetch usermeta data
// We want the three following pieces of info from usermeta:
// - friend count
// - latest update.
$total_friend_count_key = bp_get_user_meta_key('total_friend_count');
$bp_latest_update_key = bp_get_user_meta_key('bp_latest_update');
// Total_friend_count must be set for each user, even if its
// value is 0.
foreach ($this->results as $uindex => $user) {
$this->results[$uindex]->total_friend_count = 0;
}
// Create, prepare, and run the separate usermeta query.
$user_metas = $wpdb->get_results($wpdb->prepare("SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key));
// The $members_template global expects the index key to be different
// from the meta_key in some cases, so we rejig things here.
foreach ($user_metas as $user_meta) {
switch ($user_meta->meta_key) {
case $total_friend_count_key:
$key = 'total_friend_count';
break;
case $bp_latest_update_key:
$key = 'latest_update';
break;
}
if (isset($this->results[$user_meta->user_id])) {
$this->results[$user_meta->user_id]->{$key} = $user_meta->meta_value;
}
}
// When meta_key or meta_value have been passed to the query,
// fetch the resulting values for use in the template functions.
if (!empty($this->query_vars['meta_key'])) {
$meta_sql = array('select' => "SELECT user_id, meta_key, meta_value", 'from' => "FROM {$wpdb->usermeta}", 'where' => $wpdb->prepare("WHERE meta_key = %s", $this->query_vars['meta_key']));
if (false !== $this->query_vars['meta_value']) {
$meta_sql['where'] .= $wpdb->prepare(" AND meta_value = %s", $this->query_vars['meta_value']);
}
$metas = $wpdb->get_results("{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}");
if (!empty($metas)) {
foreach ($metas as $meta) {
if (isset($this->results[$meta->user_id])) {
$this->results[$meta->user_id]->meta_key = $meta->meta_key;
if (!empty($meta->meta_value)) {
$this->results[$meta->user_id]->meta_value = $meta->meta_value;
}
}
}
}
//.........这里部分代码省略.........
/**
* Find users who match on the value of an xprofile data.
*
* @global object $bp Global BuddyPress settings object
* @global nxtdb $nxtdb NXTClass database object
* @param string $search_terms The terms to search the profile table value column for.
* @param integer $limit The limit of results we want.
* @param integer $page The page we are on for pagination.
* @param boolean $populate_extras Populate extra user fields?
* @return array Associative array
* @static
*/
function search_users($search_terms, $limit = null, $page = 1, $populate_extras = true)
{
global $bp, $nxtdb;
$pag_sql = $limit && $page ? $nxtdb->prepare(" LIMIT %d, %d", intval(($page - 1) * intval($limit)), intval($limit)) : '';
$search_terms = like_escape($nxtdb->escape($search_terms));
$status_sql = bp_core_get_status_sql('u.');
$total_users_sql = apply_filters('bp_core_search_users_count_sql', "SELECT COUNT(DISTINCT u.ID) as id FROM {$nxtdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE '%%{$search_terms}%%' ORDER BY pd.value ASC", $search_terms);
$paged_users_sql = apply_filters('bp_core_search_users_sql', "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.user_email FROM {$nxtdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE '%%{$search_terms}%%' ORDER BY pd.value ASC{$pag_sql}", $search_terms, $pag_sql);
$total_users = $nxtdb->get_var($total_users_sql);
$paged_users = $nxtdb->get_results($paged_users_sql);
/***
* Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list.
* We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join)
*/
foreach ((array) $paged_users as $user) {
$user_ids[] = $user->id;
}
$user_ids = $nxtdb->escape(join(',', (array) $user_ids));
// Add additional data to the returned results
if ($populate_extras) {
$paged_users = BP_Core_User::get_user_extras($paged_users, $user_ids);
}
return array('users' => $paged_users, 'total' => $total_users);
}
/**
* Fetch everything in the wp_users table for a user, without any usermeta.
*
* @package BuddyPress Core
* @param user_id The ID of the user.
* @uses BP_Core_User::get_core_userdata() Performs the query.
*/
function bp_core_get_core_userdata($user_id)
{
if (empty($user_id)) {
return false;
}
if (!($userdata = wp_cache_get('bp_core_userdata_' . $user_id, 'bp'))) {
$userdata = BP_Core_User::get_core_userdata($user_id);
wp_cache_set('bp_core_userdata_' . $user_id, $userdata, 'bp');
}
return apply_filters('bp_core_get_core_userdata', $userdata);
}
/**
* Find users who match on the value of an xprofile data.
*
* @global wpdb $wpdb WordPress database object.
*
* @param string $search_terms The terms to search the profile table
* value column for.
* @param integer $limit The limit of results we want.
* @param integer $page The page we are on for pagination.
* @param boolean $populate_extras Populate extra user fields?
* @return array Associative array.
*/
public static function search_users($search_terms, $limit = null, $page = 1, $populate_extras = true)
{
global $wpdb;
$bp = buddypress();
$user_ids = array();
$pag_sql = $limit && $page ? $wpdb->prepare(" LIMIT %d, %d", intval(($page - 1) * intval($limit)), intval($limit)) : '';
$search_terms_like = '%' . bp_esc_like($search_terms) . '%';
$status_sql = bp_core_get_status_sql('u.');
/**
* Filters the SQL used to query for searched users count.
*
* @since BuddyPress (1.0.0)
*
* @param string $value SQL statement for the searched users count query.
*/
$total_users_sql = apply_filters('bp_core_search_users_count_sql', $wpdb->prepare("SELECT COUNT(DISTINCT u.ID) as id FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC", $search_terms_like), $search_terms);
/**
* Filters the SQL used to query for searched users.
*
* @since BuddyPress (1.0.0)
*
* @param string $value SQL statement for the searched users query.
*/
$paged_users_sql = apply_filters('bp_core_search_users_sql', $wpdb->prepare("SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.user_email FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC{$pag_sql}", $search_terms_like), $search_terms, $pag_sql);
$total_users = $wpdb->get_var($total_users_sql);
$paged_users = $wpdb->get_results($paged_users_sql);
/***
* Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list.
* We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join)
*/
foreach ((array) $paged_users as $user) {
$user_ids[] = $user->id;
}
// Add additional data to the returned results
if ($populate_extras) {
$paged_users = BP_Core_User::get_user_extras($paged_users, $user_ids);
}
return array('users' => $paged_users, 'total' => $total_users);
}
/**
* bp_core_get_random_member()
*
* Returns the user_id for a user based on their username.
*
* @package BuddyPress Core
* @param $username str Username to check.
* @global $wpdb WordPress DB access object.
* @return false on no match
* @return int the user ID of the matched user.
*/
function bp_core_get_random_member()
{
global $bp, $wpdb;
if ($bp->current_component == BP_MEMBERS_SLUG && isset($_GET['random'])) {
$user = BP_Core_User::get_random_users(1);
$ud = get_userdata($user['users'][0]->user_id);
bp_core_redirect($bp->root_domain . '/' . BP_MEMBERS_SLUG . '/' . $ud->user_login);
}
}
/**
* Get a user's friends, in the order in which they joined the site.
*
* @see BP_Core_User::get_users() for a description of return value.
*
* @param int $user_id ID of the user whose friends are being retreived.
* @param int $per_page Optional. Number of results to return per page.
* Default: 0 (no pagination; show all results).
* @param int $page Optional. Number of the page of results to return.
* Default: 0 (no pagination; show all results).
* @param string $filter Optional. Limit results to those matching a search
* string.
* @return array See {@link BP_Core_User::get_users()}.
*/
function friends_get_newest($user_id, $per_page = 0, $page = 0, $filter = '')
{
return apply_filters('friends_get_newest', BP_Core_User::get_users('newest', $per_page, $page, $user_id, $filter));
}
/**
* Get the last active date of many users at once.
*
* @todo Why is this in the Friends component?
*
* @param array $user_ids IDs of users whose last_active meta is
* being queried.
*
* @return array $retval Array of last_active values + user_ids.
*/
public static function get_bulk_last_active($user_ids)
{
global $wpdb;
$last_activities = BP_Core_User::get_last_activity($user_ids);
// Sort and structure as expected in legacy function
usort($last_activities, create_function('$a, $b', '
if ( $a["date_recorded"] == $b["date_recorded"] ) {
return 0;
}
return ( strtotime( $a["date_recorded"] ) < strtotime( $b["date_recorded"] ) ) ? 1 : -1;
'));
$retval = array();
foreach ($last_activities as $last_activity) {
$u = new stdClass();
$u->last_activity = $last_activity['date_recorded'];
$u->user_id = $last_activity['user_id'];
$retval[] = $u;
}
return $retval;
}
请发表评论