diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
index b23780ae4..51a87d723 100644
|
|
function groups_delete_group( $group_id ) { |
481 | 481 | function groups_is_valid_status( $status ) { |
482 | 482 | $bp = buddypress(); |
483 | 483 | |
484 | | return in_array( $status, (array) $bp->groups->valid_status ); |
| 484 | return in_array( $status, (array) $bp->groups->valid_status, true ); |
485 | 485 | } |
486 | 486 | |
487 | 487 | /** |
… |
… |
function groups_check_slug( $slug ) { |
524 | 524 | */ |
525 | 525 | function groups_get_slug( $group_id ) { |
526 | 526 | $group = groups_get_group( $group_id ); |
527 | | return !empty( $group->slug ) ? $group->slug : ''; |
| 527 | return ! empty( $group->slug ) ? $group->slug : ''; |
528 | 528 | } |
529 | 529 | |
530 | 530 | /** |
… |
… |
function groups_get_id_by_previous_slug( $group_slug ) { |
557 | 557 | * Remove a user from a group. |
558 | 558 | * |
559 | 559 | * @since 1.0.0 |
| 560 | * @since 10.0.0 Updated to use `bp_get_group`. |
560 | 561 | * |
561 | | * @param int $group_id ID of the group. |
562 | | * @param int $user_id Optional. ID of the user. Defaults to the currently |
563 | | * logged-in user. |
| 562 | * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. |
| 563 | * @param int $user_id Optional. ID of the user. Defaults to the currently |
| 564 | * logged-in user. |
564 | 565 | * @return bool True on success, false on failure. |
565 | 566 | */ |
566 | | function groups_leave_group( $group_id, $user_id = 0 ) { |
| 567 | function groups_leave_group( $group, $user_id = 0 ) { |
567 | 568 | |
568 | | if ( empty( $user_id ) ) |
| 569 | $bp = buddypress(); |
| 570 | $group = bp_get_group( $group ); |
| 571 | |
| 572 | if ( |
| 573 | empty( $group->id ) |
| 574 | && ( |
| 575 | ! empty( $bp->groups->current_group ) |
| 576 | && is_numeric( $group ) |
| 577 | && $group === $bp->groups->current_group->id |
| 578 | ) |
| 579 | ) { |
| 580 | $group = $bp->groups->current_group; |
| 581 | } |
| 582 | |
| 583 | if ( empty( $group->id ) ) { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | if ( empty( $user_id ) ) { |
569 | 588 | $user_id = bp_loggedin_user_id(); |
| 589 | } |
570 | 590 | |
571 | 591 | // Don't let single admins leave the group. |
572 | | if ( count( groups_get_group_admins( $group_id ) ) < 2 ) { |
573 | | if ( groups_is_user_admin( $user_id, $group_id ) ) { |
| 592 | if ( count( groups_get_group_admins( $group->id ) ) < 2 ) { |
| 593 | if ( groups_is_user_admin( $user_id, $group->id ) ) { |
574 | 594 | bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' ); |
575 | 595 | return false; |
576 | 596 | } |
577 | 597 | } |
578 | 598 | |
579 | | if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) { |
| 599 | if ( ! BP_Groups_Member::delete( $user_id, $group->id ) ) { |
580 | 600 | return false; |
581 | 601 | } |
582 | 602 | |
… |
… |
function groups_leave_group( $group_id, $user_id = 0 ) { |
586 | 606 | * Fires after a user leaves a group. |
587 | 607 | * |
588 | 608 | * @since 1.0.0 |
| 609 | * @since 10.0.0 Updated to add the `$group` parameter. |
589 | 610 | * |
590 | | * @param int $group_id ID of the group. |
591 | | * @param int $user_id ID of the user leaving the group. |
| 611 | * @param int $group_id ID of the group. |
| 612 | * @param int $user_id ID of the user leaving the group. |
| 613 | * @param BP_Groups_Group $group The group object. |
592 | 614 | */ |
593 | | do_action( 'groups_leave_group', $group_id, $user_id ); |
| 615 | do_action( 'groups_leave_group', $group->id, $user_id, $group ); |
594 | 616 | |
595 | 617 | return true; |
596 | 618 | } |
… |
… |
function groups_leave_group( $group_id, $user_id = 0 ) { |
599 | 621 | * Add a user to a group. |
600 | 622 | * |
601 | 623 | * @since 1.0.0 |
| 624 | * @since 10.0.0 Updated to use `bp_get_group`. |
602 | 625 | * |
603 | | * @param int $group_id ID of the group. |
604 | | * @param int $user_id Optional. ID of the user. Defaults to the currently |
605 | | * logged-in user. |
| 626 | * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. |
| 627 | * @param int $user_id Optional. ID of the user. Defaults to the currently |
| 628 | * logged-in user. |
606 | 629 | * @return bool True on success, false on failure. |
607 | 630 | */ |
608 | | function groups_join_group( $group_id, $user_id = 0 ) { |
| 631 | function groups_join_group( $group, $user_id = 0 ) { |
609 | 632 | |
610 | | if ( empty( $user_id ) ) |
| 633 | $bp = buddypress(); |
| 634 | $group = bp_get_group( $group ); |
| 635 | |
| 636 | if ( |
| 637 | empty( $group->id ) |
| 638 | && ( |
| 639 | ! empty( $bp->groups->current_group ) |
| 640 | && is_numeric( $group ) |
| 641 | && $group === $bp->groups->current_group->id |
| 642 | ) |
| 643 | ) { |
| 644 | $group = $bp->groups->current_group; |
| 645 | } |
| 646 | |
| 647 | if ( empty( $group->id ) ) { |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | $group_id = $group->id; |
| 652 | |
| 653 | if ( empty( $user_id ) ) { |
611 | 654 | $user_id = bp_loggedin_user_id(); |
| 655 | } |
612 | 656 | |
613 | 657 | // Check if the user has an outstanding invite. If so, delete it. |
614 | | if ( groups_check_user_has_invite( $user_id, $group_id ) ) |
| 658 | if ( groups_check_user_has_invite( $user_id, $group_id ) ) { |
615 | 659 | groups_delete_invite( $user_id, $group_id ); |
| 660 | } |
616 | 661 | |
617 | 662 | // Check if the user has an outstanding request. If so, delete it. |
618 | | if ( groups_check_for_membership_request( $user_id, $group_id ) ) |
| 663 | if ( groups_check_for_membership_request( $user_id, $group_id ) ) { |
619 | 664 | groups_delete_membership_request( null, $user_id, $group_id ); |
| 665 | } |
620 | 666 | |
621 | 667 | // User is already a member, just return true. |
622 | | if ( groups_is_user_member( $user_id, $group_id ) ) |
| 668 | if ( groups_is_user_member( $user_id, $group_id ) ) { |
623 | 669 | return true; |
| 670 | } |
624 | 671 | |
625 | | $new_member = new BP_Groups_Member; |
| 672 | $new_member = new BP_Groups_Member(); |
626 | 673 | $new_member->group_id = $group_id; |
627 | 674 | $new_member->user_id = $user_id; |
628 | 675 | $new_member->inviter_id = 0; |
… |
… |
function groups_join_group( $group_id, $user_id = 0 ) { |
631 | 678 | $new_member->date_modified = bp_core_current_time(); |
632 | 679 | $new_member->is_confirmed = 1; |
633 | 680 | |
634 | | if ( !$new_member->save() ) |
| 681 | if ( ! $new_member->save() ) { |
635 | 682 | return false; |
636 | | |
637 | | $bp = buddypress(); |
638 | | |
639 | | if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id ) |
640 | | $group = groups_get_group( $group_id ); |
641 | | else |
642 | | $group = $bp->groups->current_group; |
| 683 | } |
643 | 684 | |
644 | 685 | // Record this in activity streams. |
645 | 686 | if ( bp_is_active( 'activity' ) ) { |
646 | | groups_record_activity( array( |
647 | | 'type' => 'joined_group', |
648 | | 'item_id' => $group_id, |
649 | | 'user_id' => $user_id, |
650 | | ) ); |
| 687 | groups_record_activity( |
| 688 | array( |
| 689 | 'type' => 'joined_group', |
| 690 | 'item_id' => $group_id, |
| 691 | 'user_id' => $user_id, |
| 692 | ) |
| 693 | ); |
651 | 694 | } |
652 | 695 | |
653 | 696 | /** |
654 | 697 | * Fires after a user joins a group. |
655 | 698 | * |
656 | 699 | * @since 1.0.0 |
| 700 | * @since 10.0.0 Added the `$group` parameter. |
657 | 701 | * |
658 | | * @param int $group_id ID of the group. |
659 | | * @param int $user_id ID of the user joining the group. |
| 702 | * @param int $group_id ID of the group. |
| 703 | * @param int $user_id ID of the user joining the group. |
| 704 | * @param BP_Groups_Group $group The group object. |
660 | 705 | */ |
661 | | do_action( 'groups_join_group', $group_id, $user_id ); |
| 706 | do_action( 'groups_join_group', $group_id, $user_id, $group ); |
662 | 707 | |
663 | 708 | return true; |
664 | 709 | } |
… |
… |
function groups_join_group( $group_id, $user_id = 0 ) { |
667 | 712 | * Update the last_activity meta value for a given group. |
668 | 713 | * |
669 | 714 | * @since 1.0.0 |
| 715 | * @since 10.0.0 Updated to use `bp_get_group`. |
670 | 716 | * |
671 | | * @param int $group_id Optional. The ID of the group whose last_activity is |
672 | | * being updated. Default: the current group's ID. |
673 | | * @return false|null False on failure. |
| 717 | * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. |
| 718 | * Default: the current group's ID. |
| 719 | * @return bool False on failure. |
674 | 720 | */ |
675 | | function groups_update_last_activity( $group_id = 0 ) { |
| 721 | function groups_update_last_activity( $group = 0 ) { |
676 | 722 | |
677 | | if ( empty( $group_id ) ) { |
678 | | $group_id = buddypress()->groups->current_group->id; |
| 723 | $bp = buddypress(); |
| 724 | $group = bp_get_group( $group ); |
| 725 | |
| 726 | if ( |
| 727 | empty( $group->id ) |
| 728 | && ( |
| 729 | ! empty( $bp->groups->current_group ) |
| 730 | && is_numeric( $group ) |
| 731 | && $group === $bp->groups->current_group->id |
| 732 | ) |
| 733 | ) { |
| 734 | $group = $bp->groups->current_group; |
679 | 735 | } |
680 | 736 | |
681 | | if ( empty( $group_id ) ) { |
| 737 | if ( empty( $group->id ) ) { |
682 | 738 | return false; |
683 | 739 | } |
684 | 740 | |
685 | | groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() ); |
| 741 | groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() ); |
686 | 742 | } |
687 | | add_action( 'groups_join_group', 'groups_update_last_activity' ); |
688 | | add_action( 'groups_leave_group', 'groups_update_last_activity' ); |
689 | | add_action( 'groups_created_group', 'groups_update_last_activity' ); |
| 743 | add_action( 'groups_join_group', 'groups_update_last_activity' ); |
| 744 | add_action( 'groups_leave_group', 'groups_update_last_activity' ); |
| 745 | add_action( 'groups_created_group', 'groups_update_last_activity' ); |
690 | 746 | |
691 | 747 | /** General Group Functions ***************************************************/ |
692 | 748 | |
… |
… |
function groups_get_group_members( $args = array() ) { |
823 | 879 | * Get the member count for a group. |
824 | 880 | * |
825 | 881 | * @since 1.2.3 |
| 882 | * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count` and `bp_get_group`. |
826 | 883 | * |
827 | | * @param int $group_id Group ID. |
828 | | * @return int Count of confirmed members for the group. |
| 884 | * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object. |
| 885 | * @param bool $skip_cache Optional. Skip grabbing from cache. Defaults to false. |
| 886 | * @return int|bool Count of confirmed members for the group. False if group doesn't exist. |
829 | 887 | */ |
830 | | function groups_get_total_member_count( $group_id ) { |
831 | | return BP_Groups_Group::get_total_member_count( $group_id ); |
| 888 | function groups_get_total_member_count( $group, $skip_cache = false ) { |
| 889 | |
| 890 | $group = bp_get_group( $group ); |
| 891 | |
| 892 | if ( empty( $group->id ) ) { |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | return (int) BP_Groups_Group::get_total_member_count( $group->id, (bool) $skip_cache ); |
832 | 897 | } |
833 | 898 | |
834 | 899 | /** Group Fetching, Filtering & Searching ************************************/ |
… |
… |
function groups_get_groups( $args = '' ) { |
918 | 983 | * Get the total group count for the site. |
919 | 984 | * |
920 | 985 | * @since 1.2.0 |
| 986 | * @since 10.0.0 Added the `$skip_cache` parameter. |
921 | 987 | * |
| 988 | * @param bool $skip_cache Optional. Skip getting count from cache. Defaults to false. |
922 | 989 | * @return int |
923 | 990 | */ |
924 | | function groups_get_total_group_count() { |
925 | | $count = wp_cache_get( 'bp_total_group_count', 'bp' ); |
926 | | |
927 | | if ( false === $count ) { |
928 | | $count = BP_Groups_Group::get_total_group_count(); |
929 | | wp_cache_set( 'bp_total_group_count', $count, 'bp' ); |
930 | | } |
931 | | |
932 | | return $count; |
| 991 | function groups_get_total_group_count( $skip_cache = false ) { |
| 992 | return (int) BP_Groups_Group::get_total_group_count( $skip_cache ); |
933 | 993 | } |
934 | 994 | |
935 | 995 | /** |
… |
… |
function groups_get_total_group_count() { |
949 | 1009 | */ |
950 | 1010 | function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) { |
951 | 1011 | |
952 | | if ( empty( $user_id ) ) |
| 1012 | if ( empty( $user_id ) ) { |
953 | 1013 | $user_id = bp_displayed_user_id(); |
| 1014 | } |
954 | 1015 | |
955 | 1016 | return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page ); |
956 | 1017 | } |
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
index 15dc4d592..b2ad43fdb 100644
|
|
class BP_Groups_Group { |
1723 | 1723 | * bp_current_user_can( 'bp_moderate' ). |
1724 | 1724 | * |
1725 | 1725 | * @since 1.6.0 |
| 1726 | * @since 10.0.0 Added the `$skip_cache` parameter. |
1726 | 1727 | * |
1727 | | * @return int Group count. |
| 1728 | * @global BuddyPress $bp The one true BuddyPress instance. |
| 1729 | * @global wpdb $wpdb WordPress database object. |
| 1730 | * |
| 1731 | * @param bool $skip_cache Optional. Skip getting count from cache. Defaults to false. |
| 1732 | * @return int |
1728 | 1733 | */ |
1729 | | public static function get_total_group_count() { |
| 1734 | public static function get_total_group_count( $skip_cache = false ) { |
1730 | 1735 | global $wpdb; |
1731 | 1736 | |
1732 | | $hidden_sql = ''; |
1733 | | if ( !bp_current_user_can( 'bp_moderate' ) ) |
1734 | | $hidden_sql = "WHERE status != 'hidden'"; |
| 1737 | $cache_key = 'bp_total_group_count'; |
| 1738 | $count = wp_cache_get( $cache_key, 'bp' ); |
1735 | 1739 | |
1736 | | $bp = buddypress(); |
| 1740 | if ( false === $count || true === $skip_cache ) { |
| 1741 | $hidden_sql = ''; |
| 1742 | if ( ! bp_current_user_can( 'bp_moderate' ) ) { |
| 1743 | $hidden_sql = "WHERE status != 'hidden'"; |
| 1744 | } |
| 1745 | |
| 1746 | $bp = buddypress(); |
| 1747 | $count = $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" ); |
| 1748 | |
| 1749 | wp_cache_set( $cache_key, (int) $count, 'bp' ); |
| 1750 | } |
1737 | 1751 | |
1738 | | return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" ); |
| 1752 | /** |
| 1753 | * Filters the total group count. |
| 1754 | * |
| 1755 | * @since 10.0.0 |
| 1756 | * |
| 1757 | * @param int $count Total group count. |
| 1758 | */ |
| 1759 | return (int) apply_filters( 'bp_groups_total_group_count', (int) $count ); |
1739 | 1760 | } |
1740 | 1761 | |
1741 | 1762 | /** |
1742 | 1763 | * Get the member count for a group. |
1743 | 1764 | * |
1744 | 1765 | * @since 1.6.0 |
| 1766 | * @since 10.0.0 Updated to use the `groups_get_group_members`. |
1745 | 1767 | * |
1746 | | * @param int $group_id Group ID. |
| 1768 | * @param int $group_id Group ID. |
| 1769 | * @param bool $skip_cache Optional. Skip getting count from cache. Defaults to false. |
1747 | 1770 | * @return int Count of confirmed members for the group. |
1748 | 1771 | */ |
1749 | | public static function get_total_member_count( $group_id ) { |
1750 | | global $wpdb; |
| 1772 | public static function get_total_member_count( $group_id, $skip_cache = false ) { |
| 1773 | $cache_key = 'total_member_count'; |
| 1774 | $count = groups_get_groupmeta( $group_id, $cache_key ); |
1751 | 1775 | |
1752 | | $bp = buddypress(); |
| 1776 | if ( false === $count || true === $skip_cache ) { |
| 1777 | $members = groups_get_group_members( |
| 1778 | array( |
| 1779 | 'group_id' => $group_id, |
| 1780 | 'exclude_banned' => true, |
| 1781 | 'exclude_admins_mods' => false, |
| 1782 | 'type' => 'active', |
| 1783 | ) |
| 1784 | ); |
| 1785 | |
| 1786 | $count = $members['count'] ?? 0; |
1753 | 1787 | |
1754 | | return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ); |
| 1788 | groups_update_groupmeta( $group_id, $cache_key, (int) $count ); |
| 1789 | } |
| 1790 | |
| 1791 | /** |
| 1792 | * Filters the total member count for a group. |
| 1793 | * |
| 1794 | * @since 10.0.0 |
| 1795 | * |
| 1796 | * @param int $count Total member count for group. |
| 1797 | * @param int $group_id The ID of the group. |
| 1798 | */ |
| 1799 | return (int) apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id ); |
1755 | 1800 | } |
1756 | 1801 | |
1757 | 1802 | /** |
diff --git src/bp-groups/classes/class-bp-groups-list-table.php src/bp-groups/classes/class-bp-groups-list-table.php
index be3864b32..4a102b3fd 100644
|
|
class BP_Groups_List_Table extends WP_List_Table { |
701 | 701 | * Markup for the Number of Members column. |
702 | 702 | * |
703 | 703 | * @since 1.7.0 |
| 704 | * @since 10.0.0 Updated to use `groups_get_total_member_count`. |
704 | 705 | * |
705 | 706 | * @param array $item Information about the current row. |
706 | 707 | */ |
707 | 708 | public function column_members( $item = array() ) { |
708 | | $count = groups_get_groupmeta( $item['id'], 'total_member_count' ); |
| 709 | $count = groups_get_total_member_count( absint( $item['id'] ) ); |
709 | 710 | |
710 | 711 | /** |
711 | 712 | * Filters the markup for the number of Members column. |
diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php
index 738a8aa8b..ada3a1541 100644
|
|
class BP_Groups_Member { |
465 | 465 | /** Static Methods ****************************************************/ |
466 | 466 | |
467 | 467 | /** |
468 | | * Refresh the total_group_count for a user. |
| 468 | * Refresh the `total_group_count` for a user. |
469 | 469 | * |
470 | 470 | * @since 1.8.0 |
471 | 471 | * |
… |
… |
class BP_Groups_Member { |
477 | 477 | } |
478 | 478 | |
479 | 479 | /** |
480 | | * Refresh the total_member_count for a group. |
| 480 | * Refresh the `total_member_count` for a group. |
| 481 | * |
| 482 | * The request skip the current cache so that we always grab the lastest total count. |
481 | 483 | * |
482 | 484 | * @since 1.8.0 |
| 485 | * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count` |
483 | 486 | * |
484 | 487 | * @param int $group_id ID of the group. |
485 | | * @return bool|int True on success, false on failure. |
| 488 | * @return int Count of confirmed members for the group. |
486 | 489 | */ |
487 | 490 | public static function refresh_total_member_count_for_group( $group_id ) { |
488 | | return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) ); |
| 491 | return (int) BP_Groups_Group::get_total_member_count( $group_id, true ); |
489 | 492 | } |
490 | 493 | |
491 | 494 | /** |
… |
… |
class BP_Groups_Member { |
495 | 498 | * |
496 | 499 | * @param int $user_id ID of the user. |
497 | 500 | * @param int $group_id ID of the group. |
498 | | * @return True on success, false on failure. |
| 501 | * @return bool True on success, false on failure. |
499 | 502 | */ |
500 | 503 | public static function delete( $user_id, $group_id ) { |
501 | 504 | global $wpdb; |
… |
… |
class BP_Groups_Member { |
510 | 513 | */ |
511 | 514 | do_action( 'bp_groups_member_before_delete', $user_id, $group_id ); |
512 | 515 | |
513 | | $bp = buddypress(); |
| 516 | $bp = buddypress(); |
514 | 517 | $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) ); |
515 | 518 | |
516 | 519 | // Update the user's group count. |
… |
… |
class BP_Groups_Member { |
529 | 532 | */ |
530 | 533 | do_action( 'bp_groups_member_after_delete', $user_id, $group_id ); |
531 | 534 | |
532 | | return $remove; |
| 535 | return (bool) $remove; |
533 | 536 | } |
534 | 537 | |
535 | 538 | /** |
diff --git src/bp-groups/screens/single/members.php src/bp-groups/screens/single/members.php
index 6174fd604..fccce2902 100644
|
|
|
14 | 14 | */ |
15 | 15 | function groups_screen_group_members() { |
16 | 16 | |
17 | | if ( !bp_is_single_item() ) |
| 17 | if ( ! bp_is_single_item() ) { |
18 | 18 | return false; |
| 19 | } |
19 | 20 | |
20 | 21 | $bp = buddypress(); |
21 | 22 | |
22 | | // Refresh the group member count meta. |
23 | | groups_update_groupmeta( $bp->groups->current_group->id, 'total_member_count', groups_get_total_member_count( $bp->groups->current_group->id ) ); |
24 | | |
25 | 23 | /** |
26 | 24 | * Fires before the loading of a group's Members page. |
27 | 25 | * |
… |
… |
function groups_screen_group_members() { |
39 | 37 | * @param string $value Path to a group's Members template. |
40 | 38 | */ |
41 | 39 | bp_core_load_template( apply_filters( 'groups_template_group_members', 'groups/single/home' ) ); |
42 | | } |
43 | | No newline at end of file |
| 40 | } |
diff --git tests/phpunit/testcases/groups/cache.php tests/phpunit/testcases/groups/cache.php
index 42187775b..002fd16b0 100644
|
|
class BP_Tests_Group_Cache extends BP_UnitTestCase { |
259 | 259 | // check if function references cache or hits the DB by comparing query count |
260 | 260 | $this->assertEquals( $first_query_count, $wpdb->num_queries ); |
261 | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @group groups_get_total_group_count |
| 265 | * @group counts |
| 266 | */ |
| 267 | public function test_total_groups_count() { |
| 268 | $u1 = self::factory()->user->create(); |
| 269 | $u2 = self::factory()->user->create(); |
| 270 | $u3 = self::factory()->user->create(); |
| 271 | self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
| 272 | self::factory()->group->create( array( 'creator_id' => $u2 ) ); |
| 273 | |
| 274 | $this->assertEquals( 2, groups_get_total_group_count() ); |
| 275 | $this->assertEquals( 2, BP_Groups_Group::get_total_group_count() ); |
| 276 | |
| 277 | self::factory()->group->create( array( 'creator_id' => $u3 ) ); |
| 278 | |
| 279 | $this->assertEquals( 3, groups_get_total_group_count( true ) ); |
| 280 | $this->assertEquals( 3, BP_Groups_Group::get_total_group_count() ); |
| 281 | } |
262 | 282 | } |
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
index 4b7533f55..e82efd86e 100644
|
|
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
184 | 184 | $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
185 | 185 | |
186 | 186 | groups_join_group( $g, $u2 ); |
187 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 187 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @group total_member_count |
| 192 | */ |
| 193 | public function test_total_member_count_with_invalid_group() { |
| 194 | $this->assertFalse( groups_get_total_member_count( 'invalid-group' ) ); |
| 195 | $this->assertFalse( groups_get_total_member_count( '' ) ); |
| 196 | $this->assertFalse( groups_get_total_member_count( 123456789 ) ); |
188 | 197 | } |
189 | 198 | |
190 | 199 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
193 | 202 | */ |
194 | 203 | public function test_total_member_count_groups_leave_group() { |
195 | 204 | $u1 = self::factory()->user->create(); |
| 205 | $u2 = self::factory()->user->create(); |
196 | 206 | $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
197 | | groups_join_group( $g1, $u1 ); |
198 | 207 | |
199 | | groups_leave_group( $g1, $u1 ); |
200 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 208 | groups_join_group( $g1, $u2 ); |
| 209 | |
| 210 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 211 | |
| 212 | groups_leave_group( $g1, $u2 ); |
| 213 | |
| 214 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
201 | 215 | } |
202 | 216 | |
203 | 217 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
214 | 228 | $this->set_current_user( $u1 ); |
215 | 229 | buddypress()->is_item_admin = true; |
216 | 230 | |
| 231 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 232 | |
217 | 233 | groups_ban_member( $u2, $g1 ); |
218 | 234 | |
219 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 235 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
220 | 236 | } |
221 | 237 | |
222 | 238 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
235 | 251 | |
236 | 252 | groups_ban_member( $u2, $g1 ); |
237 | 253 | |
| 254 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
| 255 | |
238 | 256 | groups_unban_member( $u2, $g1 ); |
239 | 257 | |
240 | | $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 258 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
241 | 259 | } |
242 | 260 | |
243 | 261 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
255 | 273 | 'send_invite' => 1, |
256 | 274 | ) ); |
257 | 275 | |
| 276 | $this->assertEquals( 1, groups_get_total_member_count( $g ) ); |
| 277 | |
258 | 278 | groups_accept_invite( $u2, $g ); |
259 | 279 | |
260 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 280 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
261 | 281 | } |
262 | 282 | |
263 | 283 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
275 | 295 | ) ); |
276 | 296 | groups_accept_membership_request( 0, $u2, $g ); |
277 | 297 | |
278 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 298 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
279 | 299 | } |
280 | 300 | |
281 | 301 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
294 | 314 | |
295 | 315 | groups_remove_member( $u2, $g1 ); |
296 | 316 | |
297 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 317 | $this->assertEquals( 1, groups_get_total_member_count( $g1 )); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @group total_member_count |
| 322 | * @group groups_remove_member |
| 323 | */ |
| 324 | public function test_total_member_count_groups_delete_member() { |
| 325 | $u1 = self::factory()->user->create(); |
| 326 | $u2 = self::factory()->user->create(); |
| 327 | $u3 = self::factory()->user->create(); |
| 328 | $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
| 329 | |
| 330 | groups_join_group( $g1, $u2 ); |
| 331 | groups_join_group( $g1, $u3 ); |
| 332 | |
| 333 | $this->set_current_user( $u1 ); |
| 334 | |
| 335 | $this->assertEquals( 3, groups_get_total_member_count( $g1 ) ); |
| 336 | $this->assertEquals( 3, BP_Groups_Group::get_total_member_count( $g1 ) ); |
| 337 | |
| 338 | // Delete user. |
| 339 | wp_delete_user( $u2 ); |
| 340 | |
| 341 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 342 | $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) ); |
298 | 343 | } |
299 | 344 | |
300 | 345 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
313 | 358 | 'date_created' => bp_core_current_time(), |
314 | 359 | ) ); |
315 | 360 | |
316 | | $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 361 | $this->assertEquals( 1, groups_get_total_member_count( $g ) ); |
317 | 362 | } |
318 | 363 | |
319 | 364 | /** |