Skip to:
Content

BuddyPress.org

Ticket #7614: 7614-4.diff

File 7614-4.diff, 23.9 KB (added by espellcaste, 7 weeks ago)
  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index f9bb87405..61a732aa6 100644
    function groups_delete_group( $group_id ) { 
    481481function groups_is_valid_status( $status ) {
    482482        $bp = buddypress();
    483483
    484         return in_array( $status, (array) $bp->groups->valid_status );
     484        return in_array( $status, (array) $bp->groups->valid_status, true );
    485485}
    486486
    487487/**
    function groups_check_slug( $slug ) { 
    515515}
    516516
    517517/**
    518  * Get a group slug by its ID.
     518 * Get slug from a group.
    519519 *
    520520 * @since 1.0.0
     521 * @since 10.0.0 Updated to use `bp_get_group`.
    521522 *
    522  * @param int $group_id The numeric ID of the group.
    523  * @return string The group's slug.
     523 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object.
     524 * @return bool|string The group's slug. False if group doesn't exist.
    524525 */
    525 function groups_get_slug( $group_id ) {
    526         $group = groups_get_group( $group_id );
    527         return !empty( $group->slug ) ? $group->slug : '';
     526function groups_get_slug( $group ) {
     527
     528        $group = bp_get_group( $group );
     529
     530        if ( empty( $group->id ) ) {
     531                return false;
     532        }
     533
     534        return ! empty( $group->slug ) ? $group->slug : '';
    528535}
    529536
    530537/**
    function groups_get_id_by_previous_slug( $group_slug ) { 
    557564 * Remove a user from a group.
    558565 *
    559566 * @since 1.0.0
     567 * @since 10.0.0 Updated to use `bp_get_group`.
    560568 *
    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.
     569 * @param int|string|BP_Groups_Group $group   The Group ID, the Group Slug or the Group object.
     570 * @param int                        $user_id Optional. ID of the user. Defaults to the currently
     571 *                                            logged-in user.
    564572 * @return bool True on success, false on failure.
    565573 */
    566 function groups_leave_group( $group_id, $user_id = 0 ) {
     574function groups_leave_group( $group, $user_id = 0 ) {
    567575
    568         if ( empty( $user_id ) )
     576        $current_group = null;
     577
     578        if ( is_numeric( $group ) ) {
     579                $current_group = groups_get_current_group();
     580
     581                if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === (int) $group ) {
     582                        $group = $current_group;
     583                }
     584        }
     585
     586        if ( ! isset( $group->id ) ) {
     587                $group = bp_get_group( $group );
     588        }
     589
     590        if ( empty( $group->id ) ) {
     591                return false;
     592        }
     593
     594        if ( empty( $user_id ) ) {
    569595                $user_id = bp_loggedin_user_id();
     596        }
    570597
    571598        // 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 ) ) {
     599        if ( count( groups_get_group_admins( $group->id ) ) < 2 ) {
     600                if ( groups_is_user_admin( $user_id, $group->id ) ) {
    574601                        bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' );
    575602                        return false;
    576603                }
    577604        }
    578605
    579         if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) {
     606        if ( ! BP_Groups_Member::delete( $user_id, $group->id ) ) {
    580607                return false;
    581608        }
    582609
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    586613         * Fires after a user leaves a group.
    587614         *
    588615         * @since 1.0.0
     616         * @since 10.0.0 Updated to add the `$group` parameter.
    589617         *
    590          * @param int $group_id ID of the group.
    591          * @param int $user_id  ID of the user leaving the group.
     618         * @param int             $group_id ID of the group.
     619         * @param int             $user_id  ID of the user leaving the group.
     620         * @param BP_Groups_Group $group    The group object.
    592621         */
    593         do_action( 'groups_leave_group', $group_id, $user_id );
     622        do_action( 'groups_leave_group', $group->id, $user_id, $group );
    594623
    595624        return true;
    596625}
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    599628 * Add a user to a group.
    600629 *
    601630 * @since 1.0.0
     631 * @since 10.0.0 Updated to use `bp_get_group`.
    602632 *
    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.
     633 * @param int|string|BP_Groups_Group $group   The Group ID, the Group Slug or the Group object.
     634 * @param int                        $user_id Optional. ID of the user. Defaults to the currently
     635 *                                            logged-in user.
    606636 * @return bool True on success, false on failure.
    607637 */
    608 function groups_join_group( $group_id, $user_id = 0 ) {
     638function groups_join_group( $group, $user_id = 0 ) {
    609639
    610         if ( empty( $user_id ) )
     640        $current_group = null;
     641
     642        if ( is_numeric( $group ) ) {
     643                $current_group = groups_get_current_group();
     644
     645                if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === (int) $group ) {
     646                        $group = $current_group;
     647                }
     648        }
     649
     650        if ( ! isset( $group->id ) ) {
     651                $group = bp_get_group( $group );
     652        }
     653
     654        if ( empty( $group->id ) ) {
     655                return false;
     656        }
     657
     658        $group_id = $group->id;
     659
     660        if ( empty( $user_id ) ) {
    611661                $user_id = bp_loggedin_user_id();
     662        }
    612663
    613664        // Check if the user has an outstanding invite. If so, delete it.
    614         if ( groups_check_user_has_invite( $user_id, $group_id ) )
     665        if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
    615666                groups_delete_invite( $user_id, $group_id );
     667        }
    616668
    617669        // Check if the user has an outstanding request. If so, delete it.
    618         if ( groups_check_for_membership_request( $user_id, $group_id ) )
     670        if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
    619671                groups_delete_membership_request( null, $user_id, $group_id );
     672        }
    620673
    621674        // User is already a member, just return true.
    622         if ( groups_is_user_member( $user_id, $group_id ) )
     675        if ( groups_is_user_member( $user_id, $group_id ) ) {
    623676                return true;
     677        }
    624678
    625         $new_member                = new BP_Groups_Member;
     679        $new_member                = new BP_Groups_Member();
    626680        $new_member->group_id      = $group_id;
    627681        $new_member->user_id       = $user_id;
    628682        $new_member->inviter_id    = 0;
    function groups_join_group( $group_id, $user_id = 0 ) { 
    631685        $new_member->date_modified = bp_core_current_time();
    632686        $new_member->is_confirmed  = 1;
    633687
    634         if ( !$new_member->save() )
     688        if ( ! $new_member->save() ) {
    635689                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;
     690        }
    643691
    644692        // Record this in activity streams.
    645693        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                 ) );
     694                groups_record_activity(
     695                        array(
     696                                'type'    => 'joined_group',
     697                                'item_id' => $group_id,
     698                                'user_id' => $user_id,
     699                        )
     700                );
    651701        }
    652702
    653703        /**
    654704         * Fires after a user joins a group.
    655705         *
    656706         * @since 1.0.0
     707         * @since 10.0.0 Added the `$group` parameter.
    657708         *
    658          * @param int $group_id ID of the group.
    659          * @param int $user_id  ID of the user joining the group.
     709         * @param int             $group_id ID of the group.
     710         * @param int             $user_id  ID of the user joining the group.
     711         * @param BP_Groups_Group $group    The group object.
    660712         */
    661         do_action( 'groups_join_group', $group_id, $user_id );
     713        do_action( 'groups_join_group', $group_id, $user_id, $group );
    662714
    663715        return true;
    664716}
    function groups_join_group( $group_id, $user_id = 0 ) { 
    667719 * Update the last_activity meta value for a given group.
    668720 *
    669721 * @since 1.0.0
     722 * @since 10.0.0 Updated to use `bp_get_group`.
    670723 *
    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.
     724 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object.
     725 *                                          Default: the current group's ID.
     726 * @return bool False on failure.
    674727 */
    675 function groups_update_last_activity( $group_id = 0 ) {
     728function groups_update_last_activity( $group = 0 ) {
    676729
    677         if ( empty( $group_id ) ) {
    678                 $group_id = buddypress()->groups->current_group->id;
     730        $current_group = null;
     731
     732        if ( is_numeric( $group ) ) {
     733                $group_id      = (int) $group;
     734                $current_group = groups_get_current_group();
     735
     736                if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === $group_id ) {
     737                        $group = $current_group;
     738                }
    679739        }
    680740
    681         if ( empty( $group_id ) ) {
     741        if ( ! isset( $group->id ) ) {
     742                $group = bp_get_group( $group );
     743        }
     744
     745        if ( empty( $group->id ) ) {
    682746                return false;
    683747        }
    684748
    685         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
     749        groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
    686750}
    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' );
     751add_action( 'groups_join_group', 'groups_update_last_activity' );
     752add_action( 'groups_leave_group', 'groups_update_last_activity' );
     753add_action( 'groups_created_group', 'groups_update_last_activity' );
    690754
    691755/** General Group Functions ***************************************************/
    692756
    function groups_get_group_members( $args = array() ) { 
    823887 * Get the member count for a group.
    824888 *
    825889 * @since 1.2.3
     890 * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count` and `bp_get_group`.
    826891 *
    827  * @param int $group_id Group ID.
    828  * @return int Count of confirmed members for the group.
     892 * @param int|string|BP_Groups_Group $group      The Group ID, the Group Slug or the Group object.
     893 * @param bool                       $skip_cache Optional. Skip grabbing from cache. Defaults to false.
     894 * @return int|bool Count of confirmed members for the group. False if group doesn't exist.
    829895 */
    830 function groups_get_total_member_count( $group_id ) {
    831         return BP_Groups_Group::get_total_member_count( $group_id );
     896function groups_get_total_member_count( $group, $skip_cache = false ) {
     897
     898        $group = bp_get_group( $group );
     899
     900        if ( empty( $group->id ) ) {
     901                return false;
     902        }
     903
     904        return (int) BP_Groups_Group::get_total_member_count( $group->id, (bool) $skip_cache );
    832905}
    833906
    834907/** Group Fetching, Filtering & Searching  ************************************/
    function groups_get_groups( $args = '' ) { 
    918991 * Get the total group count for the site.
    919992 *
    920993 * @since 1.2.0
     994 * @since 10.0.0 Added the `$skip_cache` parameter.
    921995 *
     996 * @param bool $skip_cache Optional. Skip getting count from cache. Defaults to false.
    922997 * @return int
    923998 */
    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;
     999function groups_get_total_group_count( $skip_cache = false ) {
     1000        return (int) BP_Groups_Group::get_total_group_count( $skip_cache );
    9331001}
    9341002
    9351003/**
    function groups_get_total_group_count() { 
    9491017 */
    9501018function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) {
    9511019
    952         if ( empty( $user_id ) )
     1020        if ( empty( $user_id ) ) {
    9531021                $user_id = bp_displayed_user_id();
     1022        }
    9541023
    9551024        return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page );
    9561025}
  • src/bp-groups/classes/class-bp-groups-group.php

    diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
    index 15dc4d592..6ecc21d9d 100644
    class BP_Groups_Group { 
    17231723         * bp_current_user_can( 'bp_moderate' ).
    17241724         *
    17251725         * @since 1.6.0
     1726         * @since 10.0.0 Added the `$skip_cache` parameter.
    17261727         *
    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
    17281733         */
    1729         public static function get_total_group_count() {
     1734        public static function get_total_group_count( $skip_cache = false ) {
    17301735                global $wpdb;
    17311736
    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' );
    17351739
    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                }
    17371751
    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 );
    17391760        }
    17401761
    17411762        /**
    17421763         * Get the member count for a group.
    17431764         *
    17441765         * @since 1.6.0
     1766         * @since 10.0.0 Updated to use the `groups_get_group_members`.
    17451767         *
    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.
    17471770         * @return int Count of confirmed members for the group.
    17481771         */
    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 );
    17511775
    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'] ? $members['count'] : 0;
    17531787
    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 );
    17551800        }
    17561801
    17571802        /**
  • src/bp-groups/classes/class-bp-groups-list-table.php

    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 { 
    701701         * Markup for the Number of Members column.
    702702         *
    703703         * @since 1.7.0
     704         * @since 10.0.0 Updated to use `groups_get_total_member_count`.
    704705         *
    705706         * @param array $item Information about the current row.
    706707         */
    707708        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'] ) );
    709710
    710711                /**
    711712                 * Filters the markup for the number of Members column.
  • src/bp-groups/classes/class-bp-groups-member.php

    diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php
    index 738a8aa8b..d938800fe 100644
    class BP_Groups_Member { 
    465465        /** Static Methods ****************************************************/
    466466
    467467        /**
    468          * Refresh the total_group_count for a user.
     468         * Refresh the `total_group_count` for a user.
    469469         *
    470470         * @since 1.8.0
    471471         *
    472472         * @param int $user_id ID of the user.
    473          * @return bool True on success, false on failure.
    474473         */
    475474        public static function refresh_total_group_count_for_user( $user_id ) {
    476                 return bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
     475                bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
    477476        }
    478477
    479478        /**
    480          * Refresh the total_member_count for a group.
     479         * Refresh the `total_member_count` for a group.
     480         *
     481         * The request skip the current cache so that we always grab the lastest total count.
    481482         *
    482483         * @since 1.8.0
     484         * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count`
    483485         *
    484486         * @param int $group_id ID of the group.
    485          * @return bool|int True on success, false on failure.
    486487         */
    487488        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 ) );
     489                BP_Groups_Group::get_total_member_count( $group_id, true );
    489490        }
    490491
    491492        /**
    class BP_Groups_Member { 
    495496         *
    496497         * @param int $user_id  ID of the user.
    497498         * @param int $group_id ID of the group.
    498          * @return True on success, false on failure.
     499         * @return bool True on success, false on failure.
    499500         */
    500501        public static function delete( $user_id, $group_id ) {
    501502                global $wpdb;
    class BP_Groups_Member { 
    510511                 */
    511512                do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
    512513
    513                 $bp = buddypress();
     514                $bp     = buddypress();
    514515                $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
    515516
    516517                // Update the user's group count.
    class BP_Groups_Member { 
    529530                 */
    530531                do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
    531532
    532                 return $remove;
     533                return (bool) $remove;
    533534        }
    534535
    535536        /**
  • src/bp-groups/screens/single/members.php

    diff --git src/bp-groups/screens/single/members.php src/bp-groups/screens/single/members.php
    index 6174fd604..fccce2902 100644
     
    1414 */
    1515function groups_screen_group_members() {
    1616
    17         if ( !bp_is_single_item() )
     17        if ( ! bp_is_single_item() ) {
    1818                return false;
     19        }
    1920
    2021        $bp = buddypress();
    2122
    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 
    2523        /**
    2624         * Fires before the loading of a group's Members page.
    2725         *
    function groups_screen_group_members() { 
    3937         * @param string $value Path to a group's Members template.
    4038         */
    4139        bp_core_load_template( apply_filters( 'groups_template_group_members', 'groups/single/home' ) );
    42 }
    43  No newline at end of file
     40}
  • tests/phpunit/testcases/groups/cache.php

    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 { 
    259259                // check if function references cache or hits the DB by comparing query count
    260260                $this->assertEquals( $first_query_count, $wpdb->num_queries );
    261261        }
     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        }
    262282}
  • tests/phpunit/testcases/groups/functions.php

    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 { 
    184184                $g = self::factory()->group->create( array( 'creator_id' => $u1 ) );
    185185
    186186                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 ) );
    188197        }
    189198
    190199        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    193202         */
    194203        public function test_total_member_count_groups_leave_group() {
    195204                $u1 = self::factory()->user->create();
     205                $u2 = self::factory()->user->create();
    196206                $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
    197                 groups_join_group( $g1, $u1 );
    198207
    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 ) );
    201215        }
    202216
    203217        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    214228                $this->set_current_user( $u1 );
    215229                buddypress()->is_item_admin = true;
    216230
     231                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
     232
    217233                groups_ban_member( $u2, $g1 );
    218234
    219                 $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     235                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
    220236        }
    221237
    222238        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    235251
    236252                groups_ban_member( $u2, $g1 );
    237253
     254                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
     255
    238256                groups_unban_member( $u2, $g1 );
    239257
    240                 $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) );
     258                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
    241259        }
    242260
    243261        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    255273                        'send_invite' => 1,
    256274                ) );
    257275
     276                $this->assertEquals( 1, groups_get_total_member_count( $g ) );
     277
    258278                groups_accept_invite( $u2, $g );
    259279
    260                 $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     280                $this->assertEquals( 2, groups_get_total_member_count( $g ) );
    261281        }
    262282
    263283        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    275295                ) );
    276296                groups_accept_membership_request( 0, $u2, $g );
    277297
    278                 $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     298                $this->assertEquals( 2, groups_get_total_member_count( $g ) );
    279299        }
    280300
    281301        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    294314
    295315                groups_remove_member( $u2, $g1 );
    296316
    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 ) );
    298343        }
    299344
    300345        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    313358                        'date_created' => bp_core_current_time(),
    314359                ) );
    315360
    316                 $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) );
     361                $this->assertEquals( 1, groups_get_total_member_count( $g ) );
    317362        }
    318363
    319364        /**