public function test_bp_user_query_sort_by_popular()
 {
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     $u3 = $this->factory->user->create();
     $u4 = $this->factory->user->create();
     bp_update_user_meta($u1, bp_get_user_meta_key('total_friend_count'), '5');
     bp_update_user_meta($u2, bp_get_user_meta_key('total_friend_count'), '90');
     bp_update_user_meta($u3, bp_get_user_meta_key('total_friend_count'), '101');
     bp_update_user_meta($u4, bp_get_user_meta_key('total_friend_count'), '3002');
     $q = new BP_User_Query(array('type' => 'popular'));
     $users = is_array($q->results) ? array_values($q->results) : array();
     $user_ids = wp_parse_id_list(wp_list_pluck($users, 'ID'));
     $expected = array($u4, $u3, $u2, $u1);
     $this->assertEquals($expected, $user_ids);
 }
/** Notifications *************************************************************/
function bp_core_screen_notification_settings()
{
    global $bp;
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    if (isset($_POST['submit'])) {
        check_admin_referer('bp_settings_notifications');
        if (isset($_POST['notifications'])) {
            foreach ((array) $_POST['notifications'] as $key => $value) {
                if ($meta_key = bp_get_user_meta_key($key)) {
                    bp_update_user_meta((int) $bp->displayed_user->id, $meta_key, $value);
                }
            }
        }
        bp_core_add_message(__('Changes saved.', 'buddypress'), 'success');
        do_action('bp_core_notification_settings_after_save');
    }
    bp_core_load_template(apply_filters('bp_core_screen_notification_settings', 'members/single/settings/notifications'));
}
/**
 * Delete a piece of usermeta.
 *
 * This is a wrapper for delete_user_meta() that allows for easy use of
 * bp_get_user_meta_key(), thereby increasing compatibility with non-standard
 * BP setups.
 *
 * @since BuddyPress (1.5.0)
 *
 * @see delete_user_meta() For complete details about parameters and return values.
 * @uses bp_get_user_meta_key() For a filterable version of the meta key.
 *
 * @param int $user_id The ID of the user whose meta you're deleting.
 * @param string $key The meta key to delete.
 * @param mixed $value Optional. Metadata value.
 * @return bool False for failure. True for success.
 */
function bp_delete_user_meta($user_id, $key, $value = '')
{
    return delete_user_meta($user_id, bp_get_user_meta_key($key), $value);
}
 /**
  * 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;
                     }
                 }
             }
         }
     }
 }
 function get_bulk_last_active($user_ids)
 {
     global $nxtdb, $bp;
     return $nxtdb->get_results($nxtdb->prepare("SELECT meta_value as last_activity, user_id FROM {$nxtdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key('last_activity')));
 }
 /**
  * Fetch extra user information, such as friend count and last profile update message.
  *
  * Accepts multiple user IDs to fetch data for.
  *
  * @global wpdb $wpdb WordPress database object.
  *
  * @param array $paged_users An array of stdClass containing the users.
  * @param string $user_ids The user ids to select information about.
  * @param string $type The type of fields we wish to get.
  * @return mixed False on error, otherwise associative array of results.
  */
 public static function get_user_extras(&$paged_users, &$user_ids, $type = false)
 {
     global $wpdb;
     $bp = buddypress();
     if (empty($user_ids)) {
         return $paged_users;
     }
     // Sanitize user IDs
     $user_ids = implode(',', wp_parse_id_list($user_ids));
     // Fetch the user's full name
     if (bp_is_active('xprofile') && 'alphabetical' != $type) {
         $names = $wpdb->get_results($wpdb->prepare("SELECT pd.user_id as id, pd.value as fullname FROM {$bp->profile->table_name_fields} pf, {$bp->profile->table_name_data} pd WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} )", bp_xprofile_fullname_field_name()));
         for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
             foreach ((array) $names as $name) {
                 if ($name->id == $paged_users[$i]->id) {
                     $paged_users[$i]->fullname = $name->fullname;
                 }
             }
         }
     }
     // Fetch the user's total friend count
     if ('popular' != $type) {
         $friend_count = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as total_friend_count FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('total_friend_count')));
         for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
             foreach ((array) $friend_count as $fcount) {
                 if ($fcount->id == $paged_users[$i]->id) {
                     $paged_users[$i]->total_friend_count = (int) $fcount->total_friend_count;
                 }
             }
         }
     }
     // Fetch whether or not the user is a friend
     if (bp_is_active('friends')) {
         $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} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id()));
         for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
             foreach ((array) $friend_status as $status) {
                 if ($status->initiator_user_id == $paged_users[$i]->id || $status->friend_user_id == $paged_users[$i]->id) {
                     $paged_users[$i]->is_friend = $status->is_confirmed;
                 }
             }
         }
     }
     if ('active' != $type) {
         $user_activity = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('last_activity')));
         for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
             foreach ((array) $user_activity as $activity) {
                 if ($activity->id == $paged_users[$i]->id) {
                     $paged_users[$i]->last_activity = $activity->last_activity;
                 }
             }
         }
     }
     // Fetch the user's last_activity
     if ('active' != $type) {
         $user_activity = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('last_activity')));
         for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
             foreach ((array) $user_activity as $activity) {
                 if ($activity->id == $paged_users[$i]->id) {
                     $paged_users[$i]->last_activity = $activity->last_activity;
                 }
             }
         }
     }
     // Fetch the user's latest update
     $user_update = $wpdb->get_results($wpdb->prepare("SELECT user_id as id, meta_value as latest_update FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key('bp_latest_update')));
     for ($i = 0, $count = count($paged_users); $i < $count; ++$i) {
         foreach ((array) $user_update as $update) {
             if ($update->id == $paged_users[$i]->id) {
                 $paged_users[$i]->latest_update = $update->latest_update;
             }
         }
     }
     return $paged_users;
 }
/**
 * Returns the total number of members, limited to those members with last_activity
 *
 * @return int The number of active members
 */
function bp_core_get_active_member_count()
{
    global $wpdb;
    if (!($count = get_transient('bp_active_member_count'))) {
        // Avoid a costly join by splitting the lookup
        if (is_multisite()) {
            $sql = "SELECT ID FROM {$wpdb->users} WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)";
        } else {
            $sql = "SELECT ID FROM {$wpdb->users} WHERE user_status != 0";
        }
        $exclude_users = $wpdb->get_col($sql);
        $exclude_users_sql = !empty($exclude_users) ? "AND user_id NOT IN (" . implode(',', wp_parse_id_list($exclude_users)) . ")" : '';
        $count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(user_id) FROM {$wpdb->usermeta} WHERE meta_key = %s {$exclude_users_sql}", bp_get_user_meta_key('last_activity')));
        set_transient('bp_active_member_count', $count);
    }
    return apply_filters('bp_core_get_active_member_count', $count);
}
Example #8
0
/**
 * Handles the changing and saving of user notification settings
 *
 * @return If no reason to proceed
 */
function bp_settings_action_notifications()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if not in settings
    if (!bp_is_settings_component() || !bp_is_current_action('notifications')) {
        return false;
    }
    // 404 if there are any additional action variables attached
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    if (isset($_POST['submit'])) {
        check_admin_referer('bp_settings_notifications');
        if (isset($_POST['notifications'])) {
            foreach ((array) $_POST['notifications'] as $key => $value) {
                if ($meta_key = bp_get_user_meta_key($key)) {
                    bp_update_user_meta((int) bp_displayed_user_id(), $meta_key, $value);
                }
            }
        }
        // Switch feedback for super admins
        if (bp_is_my_profile()) {
            bp_core_add_message(__('Your notification settings have been saved.', 'buddypress'), 'success');
        } else {
            bp_core_add_message(__("This user's notification settings have been saved.", 'buddypress'), 'success');
        }
        do_action('bp_core_notification_settings_after_save');
        bp_core_redirect(bp_displayed_user_domain() . bp_get_settings_slug() . '/notifications/');
    }
}
 function bp_update_user_meta($user_id, $key, $value, $prev_value = '')
 {
     return update_user_meta($user_id, bp_get_user_meta_key($key), $value, $prev_value);
 }
 function get_bulk_last_active($user_ids)
 {
     global $wpdb;
     $user_ids = implode(',', wp_parse_id_list($user_ids));
     return $wpdb->get_results($wpdb->prepare("SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key('last_activity')));
 }