Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/21/2014 01:50:30 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce support for a 'type' sorting parameter in BP_Group_Member_Query

The 'type' parameter supports any of the arguments ('newest', 'active', etc)
supported by the parent class BP_User_Query. In addition, it's possible to
sort by type 'last_joined' and 'first_joined'.

This changeset also introduces the 'type' parameter through the
bp_group_has_members() function stack.

See #921

Props imath, boonebgorges

File:
1 edited

Legend:

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

    r7939 r7948  
    14371437    public function setup_hooks() {
    14381438        // Take this early opportunity to set the default 'type' param
    1439         // to 'last_modified', which will ensure that BP_User_Query
     1439        // to 'last_joined', which will ensure that BP_User_Query
    14401440        // trusts our order and does not try to apply its own
    14411441        if ( empty( $this->query_vars_raw['type'] ) ) {
    1442             $this->query_vars_raw['type'] = 'last_modified';
     1442            $this->query_vars_raw['type'] = 'last_joined';
    14431443        }
    14441444
     
    14721472            'group_role'   => array( 'member' ),
    14731473            'is_confirmed' => true,
     1474            'type'         => 'last_joined',
    14741475        ) );
    14751476
     
    15091510            'orderby' => '',
    15101511            'order'   => '',
    1511             'limit'   => '',
    15121512        );
    15131513
     
    15721572        $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : '';
    15731573
    1574         /** ORDER BY clause ***************************************************/
    1575 
    1576         // @todo For now, mimicking legacy behavior of
    1577         // bp_group_has_members(), which has us order by date_modified
    1578         // only. Should abstract it in the future
     1574        // We fetch group members in order of last_joined, regardless
     1575        // of 'type'. If the 'type' value is not 'last_joined' or
     1576        // 'first_joined', the order will be overridden in
     1577        // BP_Group_Member_Query::set_orderby()
    15791578        $sql['orderby'] = "ORDER BY date_modified";
    1580         $sql['order']   = "DESC";
    1581 
    1582         /** LIMIT clause ******************************************************/
    1583         $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
     1579        $sql['order']   = 'first_joined' === $this->query_vars['type'] ? 'ASC' : 'DESC';
     1580
     1581        $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']}" );
     1582
     1583        /**
     1584         * Use this filter to build a custom query (such as when you've
     1585         * defined a custom 'type').
     1586         */
     1587        $this->group_member_ids = apply_filters( 'bp_group_member_query_group_member_ids', $this->group_member_ids, $this );
    15841588
    15851589        return $this->group_member_ids;
     
    15891593     * Tell BP_User_Query to order by the order of our query results.
    15901594     *
    1591      * This implementation assumes the 'last_modified' sort order
    1592      * hardcoded in BP_Group_Member_Query::get_group_member_ids().
     1595     * We only override BP_User_Query's native ordering in case of the
     1596     * 'last_joined' and 'first_joined' $type parameters.
    15931597     *
    15941598     * @param BP_User_Query $query BP_User_Query object.
     
    16001604        }
    16011605
    1602         // The first param in the FIELD() clause is the sort column id
    1603         $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
    1604         $gm_ids_sql = implode( ',', $gm_ids );
    1605 
    1606         $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
     1606        // For 'last_joined' and 'first_joined' types, we force
     1607        // the order according to the query performed in
     1608        // BP_Group_Member_Query::get_group_members(). Otherwise, fall
     1609        // through and let BP_User_Query do its own ordering.
     1610        if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined' ) ) ) {
     1611
     1612            // The first param in the FIELD() clause is the sort column id
     1613            $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
     1614            $gm_ids_sql = implode( ',', $gm_ids );
     1615
     1616            $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
     1617        }
    16071618
    16081619        // Prevent this filter from running on future BP_User_Query
Note: See TracChangeset for help on using the changeset viewer.