/**
 * Return the total number of members for the installation.
 *
 * Note that this is a raw count of non-spam, activated users. It does not
 * account for users who have logged activity (last_active). See
 * {@link bp_core_get_active_member_count()}.
 *
 * @since 1.2.0
 *
 * @return int The total number of members.
 */
function bp_core_get_total_member_count()
{
    global $wpdb;
    $count = wp_cache_get('bp_total_member_count', 'bp');
    if (false === $count) {
        $status_sql = bp_core_get_status_sql();
        $count = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql}");
        wp_cache_set('bp_total_member_count', $count, 'bp');
    }
    /**
     * Filters the total number of members for the installation.
     *
     * @since 1.2.0
     *
     * @param int $count Total number of members.
     */
    return apply_filters('bp_core_get_total_member_count', $count);
}
 /**
  * Prepare the query for user_ids.
  *
  * @since 1.7.0
  */
 public function prepare_user_ids_query()
 {
     global $wpdb;
     $bp = buddypress();
     // Default query variables used here.
     $type = '';
     $per_page = 0;
     $page = 1;
     $user_id = 0;
     $include = false;
     $search_terms = false;
     $exclude = false;
     $meta_key = false;
     $meta_value = false;
     extract($this->query_vars);
     // Setup the main SQL query container.
     $sql = array('select' => '', 'where' => array(), 'orderby' => '', 'order' => '', 'limit' => '');
     /* TYPE **************************************************************/
     // Determines the sort order, which means it also determines where the
     // user IDs are drawn from (the SELECT and WHERE statements).
     switch ($type) {
         // 'online' query happens against the last_activity usermeta key
         // Filter 'bp_user_query_online_interval' to modify the
         // number of minutes used as an interval.
         case 'online':
             $this->uid_name = 'user_id';
             $this->uid_table = $bp->members->table_name_last_activity;
             $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
             $sql['where'][] = $wpdb->prepare("u.component = %s AND u.type = 'last_activity'", buddypress()->members->id);
             /**
              * Filters the threshold for activity timestamp minutes since to indicate online status.
              *
              * @since 1.8.0
              *
              * @param int $value Amount of minutes for threshold. Default 15.
              */
             $sql['where'][] = $wpdb->prepare("u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters('bp_user_query_online_interval', 15));
             $sql['orderby'] = "ORDER BY u.date_recorded";
             $sql['order'] = "DESC";
             break;
             // 'active', 'newest', and 'random' queries
             // all happen against the last_activity usermeta key.
         // 'active', 'newest', and 'random' queries
         // all happen against the last_activity usermeta key.
         case 'active':
         case 'newest':
         case 'random':
             $this->uid_name = 'user_id';
             $this->uid_table = $bp->members->table_name_last_activity;
             $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
             $sql['where'][] = $wpdb->prepare("u.component = %s AND u.type = 'last_activity'", buddypress()->members->id);
             if ('newest' == $type) {
                 $sql['orderby'] = "ORDER BY u.user_id";
                 $sql['order'] = "DESC";
             } elseif ('random' == $type) {
                 $sql['orderby'] = "ORDER BY rand()";
             } else {
                 $sql['orderby'] = "ORDER BY u.date_recorded";
                 $sql['order'] = "DESC";
             }
             break;
             // 'popular' sorts by the 'total_friend_count' usermeta.
         // 'popular' sorts by the 'total_friend_count' usermeta.
         case 'popular':
             $this->uid_name = 'user_id';
             $this->uid_table = $wpdb->usermeta;
             $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
             $sql['where'][] = $wpdb->prepare("u.meta_key = %s", bp_get_user_meta_key('total_friend_count'));
             $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)";
             $sql['order'] = "DESC";
             break;
             // 'alphabetical' sorts depend on the xprofile setup.
         // 'alphabetical' sorts depend on the xprofile setup.
         case 'alphabetical':
             // We prefer to do alphabetical sorts against the display_name field
             // of wp_users, because the table is smaller and better indexed. We
             // can do so if xprofile sync is enabled, or if xprofile is inactive.
             //
             // @todo remove need for bp_is_active() check.
             if (!bp_disable_profile_sync() || !bp_is_active('xprofile')) {
                 $this->uid_name = 'ID';
                 $this->uid_table = $wpdb->users;
                 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
                 $sql['orderby'] = "ORDER BY u.display_name";
                 $sql['order'] = "ASC";
                 // When profile sync is disabled, alphabetical sorts must happen against
                 // the xprofile table.
             } else {
                 $this->uid_name = 'user_id';
                 $this->uid_table = $bp->profile->table_name_data;
                 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
                 $sql['where'][] = $wpdb->prepare("u.field_id = %d", bp_xprofile_fullname_field_id());
                 $sql['orderby'] = "ORDER BY u.value";
                 $sql['order'] = "ASC";
             }
             // Alphabetical queries ignore last_activity, while BP uses last_activity
             // to infer spam/deleted/non-activated users. To ensure that these users
             // are filtered out, we add an appropriate sub-query.
             $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql('') . " )";
             break;
             // Any other 'type' falls through.
         // Any other 'type' falls through.
         default:
             $this->uid_name = 'ID';
             $this->uid_table = $wpdb->users;
             $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
             // In this case, we assume that a plugin is
             // handling order, so we leave those clauses
             // blank.
             break;
     }
     /* WHERE *************************************************************/
     // 'include' - User ids to include in the results.
     $include = false !== $include ? wp_parse_id_list($include) : array();
     $include_ids = $this->get_include_ids($include);
     if (!empty($include_ids)) {
         $include_ids = implode(',', wp_parse_id_list($include_ids));
         $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})";
     }
     // 'exclude' - User ids to exclude from the results.
     if (false !== $exclude) {
         $exclude_ids = implode(',', wp_parse_id_list($exclude));
         $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})";
     }
     // 'user_id' - When a user id is passed, limit to the friends of the user
     // @todo remove need for bp_is_active() check.
     if (!empty($user_id) && bp_is_active('friends')) {
         $friend_ids = friends_get_friend_user_ids($user_id);
         $friend_ids = implode(',', wp_parse_id_list($friend_ids));
         if (!empty($friend_ids)) {
             $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})";
             // If the user has no friends, the query should always
             // return no users.
         } else {
             $sql['where'][] = $this->no_results['where'];
         }
     }
     /* Search Terms ******************************************************/
     // 'search_terms' searches user_login and user_nicename
     // xprofile field matches happen in bp_xprofile_bp_user_query_search().
     if (false !== $search_terms) {
         $search_terms = bp_esc_like(wp_kses_normalize_entities($search_terms));
         if ($search_wildcard === 'left') {
             $search_terms_nospace = '%' . $search_terms;
             $search_terms_space = '%' . $search_terms . ' %';
         } elseif ($search_wildcard === 'right') {
             $search_terms_nospace = $search_terms . '%';
             $search_terms_space = '% ' . $search_terms . '%';
         } else {
             $search_terms_nospace = '%' . $search_terms . '%';
             $search_terms_space = '%' . $search_terms . '%';
         }
         $sql['where']['search'] = $wpdb->prepare("u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )", $search_terms_nospace, $search_terms_space, $search_terms_nospace, $search_terms_space);
     }
     // Only use $member_type__in if $member_type is not set.
     if (empty($member_type) && !empty($member_type__in)) {
         $member_type = $member_type__in;
     }
     // Member types to exclude. Note that this takes precedence over inclusions.
     if (!empty($member_type__not_in)) {
         $member_type_clause = $this->get_sql_clause_for_member_types($member_type__not_in, 'NOT IN');
         // Member types to include.
     } elseif (!empty($member_type)) {
         $member_type_clause = $this->get_sql_clause_for_member_types($member_type, 'IN');
     }
     if (!empty($member_type_clause)) {
         $sql['where']['member_type'] = $member_type_clause;
     }
     // 'meta_key', 'meta_value' allow usermeta search
     // To avoid global joins, do a separate query.
     if (false !== $meta_key) {
         $meta_sql = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key);
         if (false !== $meta_value) {
             $meta_sql .= $wpdb->prepare(" AND meta_value = %s", $meta_value);
         }
         $found_user_ids = $wpdb->get_col($meta_sql);
         if (!empty($found_user_ids)) {
             $sql['where'][] = "u.{$this->uid_name} IN (" . implode(',', wp_parse_id_list($found_user_ids)) . ")";
         } else {
             $sql['where'][] = '1 = 0';
         }
     }
     // 'per_page', 'page' - handles LIMIT.
     if (!empty($per_page) && !empty($page)) {
         $sql['limit'] = $wpdb->prepare("LIMIT %d, %d", intval(($page - 1) * $per_page), intval($per_page));
     } else {
         $sql['limit'] = '';
     }
     /**
      * Filters the clauses for the user query.
      *
      * @since 2.0.0
      *
      * @param array         $sql  Array of SQL clauses to be used in the query.
      * @param BP_User_Query $this Current BP_User_Query instance.
      */
     $sql = apply_filters_ref_array('bp_user_query_uid_clauses', array($sql, &$this));
     // Assemble the query chunks.
     $this->uid_clauses['select'] = $sql['select'];
     $this->uid_clauses['where'] = !empty($sql['where']) ? 'WHERE ' . implode(' AND ', $sql['where']) : '';
     $this->uid_clauses['orderby'] = $sql['orderby'];
     $this->uid_clauses['order'] = $sql['order'];
     $this->uid_clauses['limit'] = $sql['limit'];
     /**
      * Fires before the BP_User_Query query is made.
      *
      * @since 1.7.0
      *
      * @param BP_User_Query $this Current BP_User_Query instance. Passed by reference.
      */
     do_action_ref_array('bp_pre_user_query', array(&$this));
 }
Пример #3
0
/**
 * Returns the total number of members for the installation.
 *
 * @package BuddyPress Core
 * @return int The total number of members.
 */
function bp_core_get_total_member_count()
{
    global $wpdb, $bp;
    if (!($count = wp_cache_get('bp_total_member_count', 'bp'))) {
        $status_sql = bp_core_get_status_sql();
        $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql}"));
        wp_cache_set('bp_total_member_count', $count, 'bp');
    }
    return apply_filters('bp_core_get_total_member_count', $count);
}
Пример #4
0
 /**
  * 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);
 }
Пример #5
0
 /**
  * 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);
 }