Skip to:
Content

BuddyPress.org

Changeset 12162


Ignore:
Timestamp:
06/04/2018 08:43:08 PM (6 years ago)
Author:
boonebgorges
Message:

Introduce BP_Groups_Member::get_user_memberships().

This is a new low-level query method that allows a single syntax for fetching
a paginated set of results from the membership table, for different
values of is_pending, etc. Primarily for use in data exporters; see #7820.

Fixes #7859.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-member.php

    r11609 r12162  
    827827
    828828    /**
     829     * Gets memberships of a user for purposes of a personal data export.
     830     *
     831     * @since 4.0.0
     832     *
     833     * @param int $user_id ID of the user.
     834     * @param array $args {
     835     *    Array of optional arguments.
     836     *    @type int    $page     Page of memberships being requested. Default 1.
     837     *    @type int    $per_page Memberships to return per page. Default 20.
     838     *    @type string $type     Membership type being requested. Accepts 'membership',
     839     *                           'pending_request', 'pending_received_invitation',
     840     *                           'pending_sent_invitation'. Default 'membership'.
     841     * }
     842     *
     843     * @return array
     844     */
     845    public static function get_user_memberships( $user_id, $args = array() ) {
     846        global $wpdb;
     847
     848        $bp = buddypress();
     849
     850        $r = array_merge( array(
     851            'page'     => 1,
     852            'per_page' => 20,
     853            'type'     => 'membership',
     854        ), $args );
     855
     856        $sql = array(
     857            'select' => 'SELECT *',
     858            'from'   => "FROM {$bp->groups->table_name_members}",
     859            'where'  => '',
     860            'limits' => '',
     861        );
     862
     863        switch ( $r['type'] ) {
     864            case 'pending_request' :
     865                $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id = 0", $user_id );
     866            break;
     867
     868            case 'pending_received_invitation' :
     869                $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 0 AND inviter_id != 0", $user_id );
     870            break;
     871
     872            case 'pending_sent_invitation' :
     873                $sql['where'] = $wpdb->prepare( "inviter_id = %d AND is_confirmed = 0", $user_id );
     874            break;
     875
     876            case 'membership' :
     877            default :
     878                $sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 1", $user_id );
     879            break;
     880        }
     881
     882        if ( $r['page'] && $r['per_page'] ) {
     883            $sql['limits'] = $wpdb->prepare( "LIMIT %d, %d", ( $r['page'] - 1 ) * $r['per_page'], $r['per_page'] );
     884        }
     885
     886        $memberships = $wpdb->get_results( "{$sql['select']} {$sql['from']} WHERE {$sql['where']} {$sql['limits']}" );
     887
     888        foreach ( $memberships as &$membership ) {
     889            $membership->id           = (int) $membership->id;
     890            $membership->group_id     = (int) $membership->group_id;
     891            $membership->user_id      = (int) $membership->user_id;
     892            $membership->inviter_id   = (int) $membership->inviter_id;
     893            $membership->is_admin     = (int) $membership->is_admin;
     894            $membership->is_mod       = (int) $membership->is_mod;
     895            $membership->is_banned    = (int) $membership->is_banned;
     896            $membership->is_confirmed = (int) $membership->is_confirmed;
     897            $membership->invite_sent  = (int) $membership->invite_sent;
     898        }
     899
     900        return $memberships;
     901    }
     902
     903    /**
    829904     * Check whether a user has an outstanding invitation to a given group.
    830905     *
  • trunk/tests/phpunit/includes/testcase.php

    r11882 r12162  
    120120            bp_activity_remove_all_user_data( $user_id );
    121121        }
     122
     123        bp_core_remove_data( $user_id );
    122124
    123125        return $deleted;
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-member.php

    r11737 r12162  
    55 */
    66class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {
     7    static public $user_ids;
     8    static public $group_ids;
     9
     10    public static function wpSetUpBeforeClass( $factory ) {
     11        global $wpdb, $bp;
     12        self::$user_ids  = $factory->user->create_many( 4 );
     13        self::$group_ids = $factory->group->create_many( 3, array(
     14            'creator_id' => self::$user_ids[3],
     15        ) );
     16    }
     17
     18    public static function wpTearDownAfterClass() {
     19        array_map( array( __CLASS__, 'delete_user' ), self::$user_ids );
     20        array_map( 'groups_delete_group', self::$group_ids );
     21    }
     22
    723    public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {
    824        $invite                = new BP_Groups_Member;
     
    13721388        $this->assertTrue( is_array( $admins ) && empty( $admins ) );
    13731389    }
     1390
     1391    /**
     1392     * @ticket BP7859
     1393     */
     1394    public function test_get_user_memberships_type_membership() {
     1395        groups_join_group( self::$group_ids[0], self::$user_ids[0] );
     1396
     1397        $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(
     1398            'type' => 'membership',
     1399        ) );
     1400
     1401        $this->assertCount( 1, $memberships );
     1402        $this->assertSame( self::$group_ids[0], $memberships[0]->group_id );
     1403    }
     1404
     1405    /**
     1406     * @ticket BP7859
     1407     */
     1408    public function test_get_user_memberships_type_pending_request() {
     1409        groups_join_group( self::$group_ids[0], self::$user_ids[0] );
     1410        groups_send_membership_request( self::$user_ids[0], self::$group_ids[1] );
     1411
     1412        $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(
     1413            'type' => 'pending_request',
     1414        ) );
     1415
     1416        $this->assertCount( 1, $memberships );
     1417        $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );
     1418    }
     1419
     1420    /**
     1421     * @ticket BP7859
     1422     */
     1423    public function test_get_user_memberships_type_pending_received_invitation() {
     1424        groups_join_group( self::$group_ids[0], self::$user_ids[0] );
     1425        groups_invite_user( array(
     1426            'user_id'    => self::$user_ids[0],
     1427            'group_id'   => self::$group_ids[1],
     1428            'inviter_id' => self::$user_ids[1],
     1429        ) );
     1430
     1431        $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(
     1432            'type' => 'pending_received_invitation',
     1433        ) );
     1434
     1435        $this->assertCount( 1, $memberships );
     1436        $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );
     1437    }
     1438
     1439    /**
     1440     * @ticket BP7859
     1441     */
     1442    public function test_get_user_memberships_type_pending_sent_invitation() {
     1443        groups_join_group( self::$group_ids[0], self::$user_ids[0] );
     1444        groups_invite_user( array(
     1445            'user_id'    => self::$user_ids[1],
     1446            'group_id'   => self::$group_ids[1],
     1447            'inviter_id' => self::$user_ids[0],
     1448        ) );
     1449
     1450        $memberships = BP_Groups_Member::get_user_memberships( self::$user_ids[0], array(
     1451            'type' => 'pending_sent_invitation',
     1452        ) );
     1453
     1454        $this->assertCount( 1, $memberships );
     1455        $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );
     1456    }
    13741457}
Note: See TracChangeset for help on using the changeset viewer.