Changeset 11384
- Timestamp:
- 01/14/2017 01:37:35 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/classes/class-bp-groups-group.php
r11383 r11384 678 678 * Default: null (no limit). 679 679 * @return false|array { 680 * @type array $groups Array of matched and paginated group objects.680 * @type array $groups Array of matched and paginated group IDs. 681 681 * @type int $total Total count of groups matching the query. 682 682 * } 683 683 */ 684 684 public static function filter_user_groups( $filter, $user_id = 0, $order = false, $limit = null, $page = null ) { 685 global $wpdb; 686 687 if ( empty( $user_id ) ) 685 if ( empty( $user_id ) ) { 688 686 $user_id = bp_displayed_user_id(); 689 690 $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 691 692 $pag_sql = $order_sql = $hidden_sql = '';693 694 if ( !empty( $limit ) && !empty( $page ) )695 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );696 697 // Get all the group ids for the current user's groups.698 $gids = BP_Groups_Member::get_group_ids( $user_id ); 699 700 if ( empty( $gids['groups'] ) ) 701 return false;702 703 $ bp = buddypress();704 705 $gids = esc_sql( implode( ',', wp_parse_id_list( $gids['groups'] ) ) );706 707 $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT id as group_id FROM {$bp->groups->table_name} WHERE ( name LIKE %s OR description LIKE %s ) AND id IN ({$gids}) {$pag_sql}", $search_terms_like, $search_terms_like ) );708 $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name} WHERE ( name LIKE %s OR description LIKE %s ) AND id IN ({$gids})", $search_terms_like, $search_terms_like ) );709 710 return array( 'groups' => $paged_groups, 'total' => $ total_groups);687 } 688 689 $args = array( 690 'search_terms' => $filter, 691 'user_id' => $user_id, 692 'per_page' => $limit, 693 'page' => $page, 694 'order' => $order, 695 ); 696 697 $groups = BP_Groups_Group::get( $args ); 698 699 // Modify the results to match the old format. 700 $paged_groups = array(); 701 $i = 0; 702 foreach ( $groups['groups'] as $group ) { 703 $paged_groups[ $i ] = new stdClass; 704 $paged_groups[ $i ]->group_id = $group->id; 705 $i++; 706 } 707 708 return array( 'groups' => $paged_groups, 'total' => $groups['total'] ); 711 709 } 712 710 … … 726 724 * @param string|bool $order ASC or DESC. Default: false (default sort). 727 725 * @return array { 728 * @type array $groups Array of matched and paginated group objects.726 * @type array $groups Array of matched and paginated group IDs. 729 727 * @type int $total Total count of groups matching the query. 730 728 * } 731 729 */ 732 730 public static function search_groups( $filter, $limit = null, $page = null, $sort_by = false, $order = false ) { 733 global $wpdb; 734 735 $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 736 737 $pag_sql = $order_sql = $hidden_sql = ''; 738 739 if ( !empty( $limit ) && !empty( $page ) ) 740 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 741 742 if ( !empty( $sort_by ) && !empty( $order ) ) { 743 $sort_by = esc_sql( $sort_by ); 744 $order = esc_sql( $order ); 745 $order_sql = "ORDER BY {$sort_by} {$order}"; 746 } 747 748 if ( !bp_current_user_can( 'bp_moderate' ) ) 749 $hidden_sql = "AND status != 'hidden'"; 750 751 $bp = buddypress(); 752 753 $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT id as group_id FROM {$bp->groups->table_name} WHERE ( name LIKE %s OR description LIKE %s ) {$hidden_sql} {$order_sql} {$pag_sql}", $search_terms_like, $search_terms_like ) ); 754 $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name} WHERE ( name LIKE %s OR description LIKE %s ) {$hidden_sql}", $search_terms_like, $search_terms_like ) ); 755 756 return array( 'groups' => $paged_groups, 'total' => $total_groups ); 731 $args = array( 732 'search_terms' => $filter, 733 'per_page' => $limit, 734 'page' => $page, 735 'orderby' => $sort_by, 736 'order' => $order, 737 ); 738 739 $groups = BP_Groups_Group::get( $args ); 740 741 // Modify the results to match the old format. 742 $paged_groups = array(); 743 $i = 0; 744 foreach ( $groups['groups'] as $group ) { 745 $paged_groups[ $i ] = new stdClass; 746 $paged_groups[ $i ]->group_id = $group->id; 747 $i++; 748 } 749 750 return array( 'groups' => $paged_groups, 'total' => $groups['total'] ); 757 751 } 758 752 … … 1493 1487 * @param int|null $page Optional. The page offset of results to return. 1494 1488 * Default: null (no limit). 1495 * @param bool $populate_extras Optional. Whether to fetch extra 1496 * information about the groups. Default: true. 1489 * @param bool $populate_extras Deprecated. 1497 1490 * @param string|array|bool $exclude Optional. Array or comma-separated list of group 1498 1491 * IDs to exclude from results. … … 1520 1513 } 1521 1514 1522 $bp = buddypress(); 1523 1524 if ( !empty( $exclude ) ) { 1525 $exclude = implode( ',', wp_parse_id_list( $exclude ) ); 1526 $exclude_sql = " AND g.id NOT IN ({$exclude})"; 1527 } 1528 1529 if ( !bp_current_user_can( 'bp_moderate' ) ) 1530 $hidden_sql = " AND status != 'hidden'"; 1531 1532 $letter_like = bp_esc_like( $letter ) . '%'; 1533 1534 if ( !empty( $limit ) && !empty( $page ) ) { 1535 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 1536 } 1537 1538 $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND g.name LIKE %s {$hidden_sql} {$exclude_sql}", $letter_like ) ); 1539 1540 $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND g.name LIKE %s {$hidden_sql} {$exclude_sql} ORDER BY g.name ASC {$pag_sql}", $letter_like ) ); 1541 1542 if ( !empty( $populate_extras ) ) { 1543 foreach ( (array) $paged_groups as $group ) { 1544 $group_ids[] = $group->id; 1545 } 1546 $paged_groups = BP_Groups_Group::get_group_extras( $paged_groups, $group_ids, 'newest' ); 1547 } 1548 1549 return array( 'groups' => $paged_groups, 'total' => $total_groups ); 1515 $args = array( 1516 'per_page' => $limit, 1517 'page' => $page, 1518 'search_terms' => $letter . '*', 1519 'search_columns' => array( 'name' ), 1520 'exclude' => $exclude, 1521 ); 1522 1523 return BP_Groups_Group::get( $args ); 1550 1524 } 1551 1525 … … 1577 1551 */ 1578 1552 public static function get_random( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) { 1579 global $wpdb; 1580 1581 $pag_sql = $hidden_sql = $search_sql = $exclude_sql = ''; 1582 1583 if ( !empty( $limit ) && !empty( $page ) ) 1584 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 1585 1586 if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 1587 $hidden_sql = "AND g.status != 'hidden'"; 1588 1589 if ( !empty( $search_terms ) ) { 1590 $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 1591 $search_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like ); 1592 } 1593 1594 if ( !empty( $exclude ) ) { 1595 $exclude = wp_parse_id_list( $exclude ); 1596 $exclude = esc_sql( implode( ',', $exclude ) ); 1597 $exclude_sql = " AND g.id NOT IN ({$exclude})"; 1598 } 1599 1600 $bp = buddypress(); 1601 1602 if ( !empty( $user_id ) ) { 1603 $user_id = esc_sql( $user_id ); 1604 $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' {$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql} ORDER BY rand() {$pag_sql}" ); 1605 $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m LEFT JOIN {$bp->groups->table_name_groupmeta} gm ON m.group_id = gm.group_id INNER JOIN {$bp->groups->table_name} g ON m.group_id = g.id WHERE gm.meta_key = 'last_activity'{$hidden_sql} {$search_sql} AND m.user_id = {$user_id} AND m.is_confirmed = 1 AND m.is_banned = 0 {$exclude_sql}" ); 1606 } else { 1607 $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' {$hidden_sql} {$search_sql} {$exclude_sql} ORDER BY rand() {$pag_sql}" ); 1608 $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm INNER JOIN {$bp->groups->table_name} g ON gm.group_id = g.id WHERE gm.meta_key = 'last_activity'{$hidden_sql} {$search_sql} {$exclude_sql}" ); 1609 } 1610 1611 if ( !empty( $populate_extras ) ) { 1612 foreach ( (array) $paged_groups as $group ) { 1613 $group_ids[] = $group->id; 1614 } 1615 $paged_groups = BP_Groups_Group::get_group_extras( $paged_groups, $group_ids, 'newest' ); 1616 } 1617 1618 return array( 'groups' => $paged_groups, 'total' => $total_groups ); 1553 $args = array( 1554 'type' => 'random', 1555 'per_page' => $limit, 1556 'page' => $page, 1557 'user_id' => $user_id, 1558 'search_terms' => $search_terms, 1559 'exclude' => $exclude, 1560 ); 1561 1562 return BP_Groups_Group::get( $args ); 1619 1563 } 1620 1564 -
trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php
r11383 r11384 1093 1093 } 1094 1094 1095 public function test_get_by_letter_typical_use() { 1096 $g1 = $this->factory->group->create( array( 1097 'name' => 'Awesome Cool Group', 1098 'description' => 'Neat', 1099 ) ); 1100 $g2 = $this->factory->group->create( array( 1101 'name' => 'Babylon Kong', 1102 'description' => 'Awesome', 1103 ) ); 1104 1105 $groups = BP_Groups_Group::get_by_letter( 'A' ); 1106 1107 $found = wp_list_pluck( $groups['groups'], 'id' ); 1108 1109 $this->assertEquals( array( $g1 ), $found ); 1110 1111 } 1112 1095 1113 public function test_get_by_letter_with_exclude() { 1096 1114 $g1 = $this->factory->group->create( array(
Note: See TracChangeset
for help on using the changeset viewer.