Changeset 7948
- Timestamp:
- 02/21/2014 01:50:30 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-groups/bp-groups-classes.php
r7939 r7948 1437 1437 public function setup_hooks() { 1438 1438 // Take this early opportunity to set the default 'type' param 1439 // to 'last_ modified', which will ensure that BP_User_Query1439 // to 'last_joined', which will ensure that BP_User_Query 1440 1440 // trusts our order and does not try to apply its own 1441 1441 if ( empty( $this->query_vars_raw['type'] ) ) { 1442 $this->query_vars_raw['type'] = 'last_ modified';1442 $this->query_vars_raw['type'] = 'last_joined'; 1443 1443 } 1444 1444 … … 1472 1472 'group_role' => array( 'member' ), 1473 1473 'is_confirmed' => true, 1474 'type' => 'last_joined', 1474 1475 ) ); 1475 1476 … … 1509 1510 'orderby' => '', 1510 1511 'order' => '', 1511 'limit' => '',1512 1512 ); 1513 1513 … … 1572 1572 $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; 1573 1573 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() 1579 1578 $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 ); 1584 1588 1585 1589 return $this->group_member_ids; … … 1589 1593 * Tell BP_User_Query to order by the order of our query results. 1590 1594 * 1591 * This implementation assumes the 'last_modified' sort order1592 * 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. 1593 1597 * 1594 1598 * @param BP_User_Query $query BP_User_Query object. … … 1600 1604 } 1601 1605 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 } 1607 1618 1608 1619 // Prevent this filter from running on future BP_User_Query -
trunk/bp-groups/bp-groups-functions.php
r7932 r7948 385 385 * Default: 1. 386 386 * @type array $group_role Optional. Array of group roles to include. 387 * @type string $search_terms Optional. Filter results by a search string. 388 * @type string $type Optional. Sort the order of results. 'last_joined', 389 * 'first_joined', or any of the $type params available in 390 * {@link BP_User_Query}. Default: 'last_joined'. 387 391 * } 388 * @param int $group_id 389 * @param int $limit Maximum members to return 390 * @param int $page The page of results to return (requires $limit) 391 * @param bool $exclude_admins_mods Whether to exclude admins and mods 392 * @param bool $exclude_banned Whether to exclude banned users 393 * @param array|string $exclude Array or comma-sep list of users to exclude 394 * @return array Multi-d array of 'members' list and 'count' 392 * @return array Multi-d array of 'members' list and 'count'. 395 393 */ 396 394 function groups_get_group_members( $args = array() ) { … … 423 421 'group_role' => array(), 424 422 'search_terms' => false, 423 'type' => 'last_joined', 425 424 ) ); 426 425 … … 456 455 'exclude' => $r['exclude'], 457 456 'search_terms' => $r['search_terms'], 458 'type' => 'last_modified',457 'type' => $r['type'], 459 458 ) ); 460 459 -
trunk/bp-groups/bp-groups-template.php
r7947 r7948 2020 2020 'group_role' => false, 2021 2021 'search_terms' => false, 2022 'type' => 'last_joined', 2022 2023 ) ); 2023 2024 … … 2123 2124 * Default: 1. 2124 2125 * @type array $group_role Optional. Array of group roles to include. 2126 * @type string $type Optional. Sort order of results. 'last_joined', 2127 * 'first_joined', or any of the $type params available in 2128 * {@link BP_User_Query}. Default: 'last_joined'. 2125 2129 * @type string $search_terms Optional. Search terms to match. 2126 2130 * } … … 2139 2143 'group_role' => false, 2140 2144 'search_terms' => false, 2145 'type' => 'last_joined', 2141 2146 ) ); 2142 2147 -
trunk/tests/testcases/groups/class-bp-group-member-query.php
r7505 r7948 335 335 } 336 336 337 /** 338 * @group type 339 */ 340 public function test_get_with_type_last_joined() { 341 $g = $this->factory->group->create(); 342 $u1 = $this->create_user(); 343 $u2 = $this->create_user(); 344 $time = time(); 345 346 $this->add_user_to_group( $u1, $g, array( 347 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ), 348 ) ); 349 350 $this->add_user_to_group( $u2, $g, array( 351 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ), 352 ) ); 353 354 $query_members = new BP_Group_Member_Query( array( 355 'group_id' => $g, 356 'type' => 'last_joined', 357 ) ); 358 359 $ids = wp_parse_id_list( array_keys( $query_members->results ) ); 360 $this->assertEquals( array( $u2, $u1 ), $ids ); 361 } 362 363 /** 364 * @group type 365 */ 366 public function test_get_with_type_first_joined() { 367 $g = $this->factory->group->create(); 368 $u1 = $this->create_user(); 369 $u2 = $this->create_user(); 370 $time = time(); 371 372 $this->add_user_to_group( $u1, $g, array( 373 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 500 ), 374 ) ); 375 376 $this->add_user_to_group( $u2, $g, array( 377 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ), 378 ) ); 379 380 $query_members = new BP_Group_Member_Query( array( 381 'group_id' => $g, 382 'type' => 'first_joined', 383 ) ); 384 385 $ids = wp_parse_id_list( array_keys( $query_members->results ) ); 386 $this->assertEquals( array( $u1, $u2 ), $ids ); 387 } 388 389 /** 390 * @group type 391 */ 392 public function test_get_with_type_alphabetical() { 393 $g = $this->factory->group->create(); 394 $u1 = $this->create_user( array( 395 'display_name' => 'AAA', 396 ) ); 397 $u2 = $this->create_user( array( 398 'display_name' => 'CCC', 399 ) ); 400 $u3 = $this->create_user( array( 401 'display_name' => 'BBB', 402 ) ); 403 $time = time(); 404 405 $this->add_user_to_group( $u1, $g, array( 406 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 100 ), 407 ) ); 408 409 $this->add_user_to_group( $u2, $g, array( 410 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 200 ), 411 ) ); 412 413 $this->add_user_to_group( $u3, $g, array( 414 'date_modified' => gmdate( 'Y-m-d H:i:s', $time - 300 ), 415 ) ); 416 417 $query_members = new BP_Group_Member_Query( array( 418 'group_id' => $g, 419 'type' => 'alphabetical', 420 ) ); 421 422 $ids = wp_parse_id_list( array_keys( $query_members->results ) ); 423 $this->assertEquals( array( $u1, $u3, $u2 ), $ids ); 424 } 337 425 }
Note: See TracChangeset
for help on using the changeset viewer.