Ticket #921: 921.05.query.diff
File 921.05.query.diff, 8.8 KB (added by , 11 years ago) |
---|
-
bp-groups/bp-groups-classes.php
diff --git bp-groups/bp-groups-classes.php bp-groups/bp-groups-classes.php index c540a49..ddad9b2 100644
class BP_Group_Member_Query extends BP_User_Query { 1436 1436 */ 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 1445 1445 // Set the sort order … … class BP_Group_Member_Query extends BP_User_Query { 1471 1471 'group_id' => 0, 1472 1472 'group_role' => array( 'member' ), 1473 1473 'is_confirmed' => true, 1474 'type' => 'last_joined', 1474 1475 ) ); 1475 1476 1476 1477 $group_member_ids = $this->get_group_member_ids(); … … class BP_Group_Member_Query extends BP_User_Query { 1508 1509 'where' => array(), 1509 1510 'orderby' => '', 1510 1511 'order' => '', 1511 'limit' => '',1512 1512 ); 1513 1513 1514 1514 /** WHERE clauses *****************************************************/ … … class BP_Group_Member_Query extends BP_User_Query { 1571 1571 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"; 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']}" ); 1581 1582 1582 /** LIMIT clause ******************************************************/ 1583 $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" ); 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; 1586 1590 } … … class BP_Group_Member_Query extends BP_User_Query { 1588 1592 /** 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. 1595 1599 */ 1596 1600 public function set_orderby( $query ) { 1601 1597 1602 $gm_ids = $this->get_group_member_ids(); 1603 1598 1604 if ( empty( $gm_ids ) ) { 1599 1605 $gm_ids = array( 0 ); 1600 1606 } 1601 1607 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 ); 1608 // For 'last_joined' and 'first_joined' types, we force 1609 // the order according to the query performed in 1610 // BP_Group_Member_Query::get_group_members(). Otherwise, fall 1611 // through and let BP_User_Query do its own ordering. 1612 if ( in_array( $query->query_vars['type'], array( 'last_joined', 'first_joined' ) ) ) { 1613 1614 // The first param in the FIELD() clause is the sort column id 1615 $gm_ids = array_merge( array( 'u.id' ), wp_parse_id_list( $gm_ids ) ); 1616 $gm_ids_sql = implode( ',', $gm_ids ); 1605 1617 1606 $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")"; 1618 $query->uid_clauses['orderby'] = "ORDER BY FIELD(" . $gm_ids_sql . ")"; 1619 } 1607 1620 1608 1621 // Prevent this filter from running on future BP_User_Query 1609 1622 // instances on the same page -
bp-groups/bp-groups-functions.php
diff --git bp-groups/bp-groups-functions.php bp-groups/bp-groups-functions.php index a27a499..d63f980 100644
function groups_get_group_members( $args = array() ) { 422 422 'exclude' => false, 423 423 'group_role' => array(), 424 424 'search_terms' => false, 425 'type' => 'last_joined', 425 426 ) ); 426 427 427 428 // For legacy users. Use of BP_Groups_Member::get_all_for_group() … … function groups_get_group_members( $args = array() ) { 455 456 'group_role' => $r['group_role'], 456 457 'exclude' => $r['exclude'], 457 458 'search_terms' => $r['search_terms'], 458 'type' => 'last_modified',459 'type' => $r['type'], 459 460 ) ); 460 461 461 462 // Structure the return value as expected by the template functions -
bp-groups/bp-groups-template.php
diff --git bp-groups/bp-groups-template.php bp-groups/bp-groups-template.php index 675409e..6850194 100644
class BP_Groups_Group_Members_Template { 1982 1982 'exclude_banned' => 1, 1983 1983 'group_role' => false, 1984 1984 'search_terms' => false, 1985 'type' => 'last_joined', 1985 1986 ) ); 1986 1987 1987 1988 // @todo No … … class BP_Groups_Group_Members_Template { 2085 2086 * @type bool|int True (or 1) to exclude banned users from results. 2086 2087 * Default: 1. 2087 2088 * @type array $group_role Optional. Array of group roles to include. 2089 * @type string $type Optional. Sort order of results. 'last_joined', 2090 * 'first_joined', or any of the $type params available in 2091 * {@link BP_User_Query}. Default: 'last_joined'. 2088 2092 * @type string $search_terms Optional. Search terms to match. 2089 2093 * } 2090 2094 */ … … function bp_group_has_members( $args = '' ) { 2101 2105 'exclude_banned' => 1, 2102 2106 'group_role' => false, 2103 2107 'search_terms' => false, 2108 'type' => 'last_joined', 2104 2109 ) ); 2105 2110 2106 2111 if ( empty( $r['search_terms'] ) && ! empty( $_REQUEST['s'] ) ) -
tests/testcases/groups/class-bp-group-member-query.php
diff --git tests/testcases/groups/class-bp-group-member-query.php tests/testcases/groups/class-bp-group-member-query.php index 46e44f9..b38943f 100644
class BP_Tests_BP_Group_Member_Query_TestCases extends BP_UnitTestCase { 334 334 $this->assertEquals( array( $u2 ), $ids ); 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 }