Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/04/2018 08:43:08 PM (7 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.

File:
1 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     *
Note: See TracChangeset for help on using the changeset viewer.