Skip to:
Content

BuddyPress.org

Changeset 7294


Ignore:
Timestamp:
07/23/2013 03:32:47 PM (8 years ago)
Author:
boonebgorges
Message:

Tell BP_Group_Member_Query queries to order by FIELD()

BP_Group_Member_Query works by first querying for a list of group members
that match the query params, sorted by date_modified. Then, it feeds those ids
to the BP_User_Query parent class. In order to maintain the proper
date_modified sort, we have to override BP_User_Query's ORDER BY param, and
replace it with ORDER BY FIELD(), using the user ids from the first query.

This fixes a regression in BP 1.8, where the default sort order for
bp_group_has_members() was inadvertantly changed to user_id DESC, rather than
the legacy date_modified that existed before the introduction of
BP_Group_Member_Query.

Fixes #5106

Location:
trunk
Files:
2 edited

Legend:

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

    r7289 r7294  
    967967class BP_Group_Member_Query extends BP_User_Query {
    968968    /**
     969     * Array of group member ids, cached to prevent redundant lookups
     970     *
     971     * @var null|array Null if not yet defined, otherwise an array of ints
     972     * @since BuddyPress (1.8.1)
     973     */
     974    protected $group_member_ids;
     975
     976    /**
    969977     * Set up action hooks
    970978     *
     
    978986            $this->query_vars_raw['type'] = 'last_modified';
    979987        }
     988
     989        // Set the sort order
     990        add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
    980991
    981992        // Set up our populate_extras method
     
    10291040        global $wpdb;
    10301041
     1042        if ( is_array( $this->group_member_ids ) ) {
     1043            return $this->group_member_ids;
     1044        }
     1045
    10311046        $bp  = buddypress();
    10321047        $sql = array(
     
    11071122
    11081123        /** LIMIT clause ******************************************************/
    1109 
    1110         $ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
    1111 
    1112         return $ids;
     1124        $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
     1125
     1126        return $this->group_member_ids;
     1127    }
     1128
     1129    /**
     1130     * Tell BP_User_Query to order by the order of our query results
     1131     *
     1132     * This implementation assumes the 'last_modified' sort order
     1133     * hardcoded in BP_Group_Member_Query::get_group_member_ids().
     1134     *
     1135     * @param object $query BP_User_Query object
     1136     */
     1137    public function set_orderby( $query ) {
     1138        $gm_ids = $this->get_group_member_ids();
     1139        if ( empty( $gm_ids ) ) {
     1140            $gm_ids = array( 0 );
     1141        }
     1142
     1143        // The first param in the FIELD() clause is the sort column id
     1144        $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) );
     1145        $gm_ids_sql = implode( ',', $gm_ids );
     1146
     1147        $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")";
     1148
     1149        // Prevent this filter from running on future BP_User_Query
     1150        // instances on the same page
     1151        remove_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
    11131152    }
    11141153
  • trunk/tests/testcases/groups/template.php

    r7185 r7294  
    352352    }
    353353
     354    /**
     355     * Default sort order should be the joined date
     356     *
     357     * @tickett BP5106
     358     * @group bp_group_has_members
     359     */
     360    public function test_bp_group_has_members_default_order() {
     361        $u1 = $this->create_user( array(
     362            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 60 ),
     363        ) );
     364        $u2 = $this->create_user( array(
     365            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 600 ),
     366        ) );
     367        $u3 = $this->create_user( array(
     368            'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 6000 ),
     369        ) );
     370
     371
     372        $g = $this->factory->group->create( array(
     373            'creator_id' => $u1,
     374        ) );
     375
     376        $this->add_user_to_group( $u2, $g, array(
     377            'date_modified' => gmdate( 'Y-m-d H:i:s', time() - 60*60*24 ),
     378        ) );
     379
     380        $this->add_user_to_group( $u3, $g, array(
     381            'date_modified' => gmdate( 'Y-m-d H:i:s', time() - 60*60*12 ),
     382        ) );
     383
     384        global $members_template;
     385        bp_group_has_members( array(
     386            'group_id' => $g,
     387            'exclude_banned' => 0,
     388            'exclude_admins_mods' => false,
     389        ) );
     390
     391        $ids = wp_parse_id_list( wp_list_pluck( $members_template->members, 'user_id' ) );
     392        $this->assertEquals( array( $u1, $u3, $u2, ), $ids );
     393    }
    354394}
Note: See TracChangeset for help on using the changeset viewer.