Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/03/2013 05:12:54 PM (7 years ago)
Author:
boonebgorges
Message:

Introduces BP_Group_Member_Query and refactors bp_group_has_members() to use it

BP_Group_Member_Query extends BP_User_Query, which has a number of notable
benefits:

  • Group member queries no longer JOIN against global user tables
  • Less code duplication, since general logic like 'exclude' is handled by BP_User_Query
  • Future access to the additional parameters of BP_User_Query, such as 'type'

Using the new BP_Group_Member_Query, this changeset also changes the way that
group member queries filter by group roles (member, mod, admin). The new
group_role parameter in the bp_group_has_members() stack accepts an array of
group roles. The legacy argument 'exclude_admins_mods' is still accepted, and
translates to 'group_role' => array( 'member' ) when true. These group_role
enhancements will allow for future enhancements in the Groups Admin section of
the Dashboard, and other places where it might be useful to query for the
members of a group matching a specific role. See #4977.

Fixes #4482

Props trishasalas for early patches and feedback. Props johnjamesjacoby for
review.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-functions.php

    r7087 r7141  
    383383 * Fetch the members of a group
    384384 *
    385  * Procedural wrapper for BP_Groups_Member::get_all_for_group().
     385 * Since BuddyPress 1.8, a procedural wrapper for BP_Group_Member_Query.
     386 * Previously called BP_Groups_Member::get_all_for_group().
     387 *
     388 * To use the legacy query, filter 'bp_use_legacy_group_member_query',
     389 * returning true.
    386390 *
    387391 * @param int $group_id
     
    393397 * @return array Multi-d array of 'members' list and 'count'
    394398 */
    395 function groups_get_group_members( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false ) {
    396     return BP_Groups_Member::get_all_for_group( $group_id, $limit, $page, $exclude_admins_mods, $exclude_banned, $exclude );
     399function groups_get_group_members( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false, $group_role = false ) {
     400
     401    // For legacy users. Use of BP_Groups_Member::get_all_for_group()
     402    // is deprecated.
     403    if ( apply_filters( 'bp_use_legacy_group_member_query', false, __FUNCTION__, func_get_args() ) ) {
     404        $retval = BP_Groups_Member::get_all_for_group( $group_id, $limit, $page, $exclude_admins_mods, $exclude_banned, $exclude );
     405    } else {
     406
     407        // exclude_admins_mods is a legacy argument. Convert to group_role
     408        if ( empty( $group_role ) ) {
     409            $group_role = $exclude_admins_mods ? array( 'member' ) : array( 'member', 'mod', 'admin' );
     410        }
     411
     412        // Perform the group member query (extends BP_User_Query)
     413        $members = new BP_Group_Member_Query( array(
     414            'group_id'       => $group_id,
     415            'per_page'       => $limit,
     416            'page'           => $page,
     417            'group_role'     => $group_role,
     418            'exclude_banned' => $exclude_banned,
     419            'exclude'        => $exclude,
     420            'type'           => 'last_modified',
     421        ) );
     422
     423        // Structure the return value as expected by the template functions
     424        $retval = array(
     425            'members' => array_values( $members->results ),
     426            'count'   => $members->total_users,
     427        );
     428    }
     429
     430    return $retval;
    397431}
    398432
Note: See TracChangeset for help on using the changeset viewer.