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/tests/testcases/groups/template.php

    r7125 r7141  
    119119
    120120    /**
     121     * Switching from BP_Groups_Member to BP_Group_Member_Query meant a
     122     * change in the format of the values returned from the query. For
     123     * backward compatibility, we translate some of the return values
     124     * of BP_Group_Member_Query to the older format. This test makes sure
     125     * that the translation happens properly.
     126     *
     127     * @group bp_group_has_members
     128     */
     129    public function test_bp_group_has_members_backpat_retval_format() {
     130        $g = $this->factory->group->create();
     131        $u1 = $this->create_user();
     132        $u2 = $this->create_user();
     133
     134        $date_modified = gmdate( 'Y-m-d H:i:s', time() - 100 );
     135
     136        $new_member                = new BP_Groups_Member;
     137        $new_member->group_id      = $g;
     138        $new_member->user_id       = $u1;
     139        $new_member->inviter_id    = $u2;
     140        $new_member->is_admin      = 0;
     141        $new_member->user_title    = '';
     142        $new_member->date_modified = $date_modified;
     143        $new_member->is_confirmed  = 1;
     144        $new_member->save();
     145
     146        global $members_template;
     147        bp_group_has_members( array(
     148            'group_id' => $g,
     149        ) );
     150
     151        $u1_object = new WP_User( $u1 );
     152
     153        $expected = new stdClass;
     154        $expected->user_id = $u1;
     155        $expected->date_modified = $date_modified;
     156        $expected->is_banned = 0;
     157        $expected->user_login = $u1_object->user_login;
     158        $expected->user_nicename = $u1_object->user_nicename;
     159        $expected->user_email = $u1_object->user_email;
     160        $expected->display_name = $u1_object->display_name;
     161
     162        // In order to use assertEquals, we need to discard the
     163        // irrelevant properties of the found object. Hack alert
     164        $found = new stdClass;
     165        foreach ( array( 'user_id', 'date_modified', 'is_banned', 'user_login', 'user_nicename', 'user_email', 'display_name' ) as $key ) {
     166            if ( isset( $members_template->members[0]->{$key} ) ) {
     167                $found->{$key} = $members_template->members[0]->{$key};
     168            }
     169        }
     170
     171        $this->assertEquals( $expected, $found );
     172    }
     173
     174    /**
    121175     * @group bp_group_has_members
    122176     */
Note: See TracChangeset for help on using the changeset viewer.