Skip to:
Content

BuddyPress.org

Ticket #7614: 7614-2.diff

File 7614-2.diff, 14.0 KB (added by espellcaste, 3 years 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 9ecddaad7..00f4956eb 100644
    function groups_get_id_by_previous_slug( $group_slug ) { 
    500500/**
    501501 * Remove a user from a group.
    502502 *
     503 * @todo Check if group exists first.
     504 * @todo Get group by other fields: slug/id/object. See #6749.
     505 *
    503506 * @since 1.0.0
    504507 *
    505508 * @param int $group_id ID of the group.
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    542545/**
    543546 * Add a user to a group.
    544547 *
     548 * @todo Check if group exists first.
     549 * @todo Get group by other fields: slug/id/object. See #6749.
     550 *
    545551 * @since 1.0.0
    546552 *
    547553 * @param int $group_id ID of the group.
    function groups_leave_group( $group_id, $user_id = 0 ) { 
    551557 */
    552558function groups_join_group( $group_id, $user_id = 0 ) {
    553559
    554         if ( empty( $user_id ) )
     560        if ( empty( $user_id ) ) {
    555561                $user_id = bp_loggedin_user_id();
     562        }
    556563
    557564        // Check if the user has an outstanding invite. If so, delete it.
    558         if ( groups_check_user_has_invite( $user_id, $group_id ) )
     565        if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
    559566                groups_delete_invite( $user_id, $group_id );
     567        }
    560568
    561569        // Check if the user has an outstanding request. If so, delete it.
    562         if ( groups_check_for_membership_request( $user_id, $group_id ) )
     570        if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
    563571                groups_delete_membership_request( null, $user_id, $group_id );
     572        }
    564573
    565574        // User is already a member, just return true.
    566         if ( groups_is_user_member( $user_id, $group_id ) )
     575        if ( groups_is_user_member( $user_id, $group_id ) ) {
    567576                return true;
     577        }
    568578
    569         $new_member                = new BP_Groups_Member;
     579        $new_member                = new BP_Groups_Member();
    570580        $new_member->group_id      = $group_id;
    571581        $new_member->user_id       = $user_id;
    572582        $new_member->inviter_id    = 0;
    function groups_join_group( $group_id, $user_id = 0 ) { 
    575585        $new_member->date_modified = bp_core_current_time();
    576586        $new_member->is_confirmed  = 1;
    577587
    578         if ( !$new_member->save() )
     588        if ( ! $new_member->save() ) {
    579589                return false;
     590        }
    580591
    581592        $bp = buddypress();
    582593
    583         if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id )
     594        if ( ! isset( $bp->groups->current_group ) || ! $bp->groups->current_group || $group_id !== $bp->groups->current_group->id ) {
    584595                $group = groups_get_group( $group_id );
    585         else
     596        } else {
    586597                $group = $bp->groups->current_group;
     598        }
    587599
    588600        // Record this in activity streams.
    589601        if ( bp_is_active( 'activity' ) ) {
    590                 groups_record_activity( array(
    591                         'type'    => 'joined_group',
    592                         'item_id' => $group_id,
    593                         'user_id' => $user_id,
    594                 ) );
     602                groups_record_activity(
     603                        array(
     604                                'type'    => 'joined_group',
     605                                'item_id' => $group_id,
     606                                'user_id' => $user_id,
     607                        )
     608                );
    595609        }
    596610
    597611        /**
    598612         * Fires after a user joins a group.
    599613         *
    600614         * @since 1.0.0
     615         * @since 10.0.0 Added the `$group` parameter.
    601616         *
    602          * @param int $group_id ID of the group.
    603          * @param int $user_id  ID of the user joining the group.
     617         * @param int             $group_id ID of the group.
     618         * @param int             $user_id  ID of the user joining the group.
     619         * @param BP_Groups_Group $group    The group object.
    604620         */
    605         do_action( 'groups_join_group', $group_id, $user_id );
     621        do_action( 'groups_join_group', $group_id, $user_id, $group );
    606622
    607623        return true;
    608624}
    function groups_get_group_members( $args = array() ) { 
    767783 * Get the member count for a group.
    768784 *
    769785 * @since 1.2.3
     786 * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count`
    770787 *
    771  * @param int $group_id Group ID.
     788 * @param int  $group_id   Group ID.
     789 * @param bool $skip_cache Skip grabbing from cache.
    772790 * @return int Count of confirmed members for the group.
    773791 */
    774 function groups_get_total_member_count( $group_id ) {
    775         return BP_Groups_Group::get_total_member_count( $group_id );
     792function groups_get_total_member_count( $group_id, $skip_cache = false ) {
     793        return (int) BP_Groups_Group::get_total_member_count( $group_id, $skip_cache );
    776794}
    777795
    778796/** Group Fetching, Filtering & Searching  ************************************/
  • 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 f06ec8055..57fdd5e7a 100644
    class BP_Groups_Group { 
    16671667                global $wpdb;
    16681668
    16691669                $hidden_sql = '';
    1670                 if ( !bp_current_user_can( 'bp_moderate' ) )
     1670                if ( ! bp_current_user_can( 'bp_moderate' ) ) {
    16711671                        $hidden_sql = "WHERE status != 'hidden'";
     1672                }
    16721673
    16731674                $bp = buddypress();
    16741675
    class BP_Groups_Group { 
    16791680         * Get the member count for a group.
    16801681         *
    16811682         * @since 1.6.0
     1683         * @since 10.0.0 Updated to use the `groups_get_group_members`.
    16821684         *
    1683          * @param int $group_id Group ID.
     1685         * @param int  $group_id   Group ID.
     1686         * @param bool $skip_cache Skip getting count from cache.
    16841687         * @return int Count of confirmed members for the group.
    16851688         */
    1686         public static function get_total_member_count( $group_id ) {
    1687                 global $wpdb;
     1689        public static function get_total_member_count( $group_id, $skip_cache = false ) {
     1690                $cache_key = 'total_member_count';
     1691                $count     = groups_get_groupmeta( $group_id, $cache_key );
    16881692
    1689                 $bp = buddypress();
     1693                if ( false === $count || true === $skip_cache ) {
     1694                        $members = groups_get_group_members(
     1695                                array(
     1696                                        'group_id'            => $group_id,
     1697                                        'exclude_banned'      => true,
     1698                                        'exclude_admins_mods' => false,
     1699                                        'type'                => 'active',
     1700                                )
     1701                        );
     1702
     1703                        $count = $members['count'] ?? 0;
    16901704
    1691                 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 ) );
     1705                        groups_update_groupmeta( $group_id, $cache_key, (int) $count );
     1706                }
     1707
     1708                /**
     1709                 * Filters the total member count for a group.
     1710                 *
     1711                 * @since 10.0.0
     1712                 *
     1713                 * @param int $count    Total member count for group.
     1714                 * @param int $group_id The ID of the group.
     1715                 */
     1716                return apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id );
    16921717        }
    16931718
    16941719        /**
  • 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 a442ce932..d26de99ea 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         *
    class BP_Groups_Member { 
    477477        }
    478478
    479479        /**
    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.
    481483         *
    482484         * @since 1.8.0
     485         * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count`
    483486         *
    484487         * @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.
    486489         */
    487490        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 );
    489492        }
    490493
    491494        /**
    class BP_Groups_Member { 
    495498         *
    496499         * @param int $user_id  ID of the user.
    497500         * @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.
    499502         */
    500503        public static function delete( $user_id, $group_id ) {
    501504                global $wpdb;
    class BP_Groups_Member { 
    510513                 */
    511514                do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
    512515
    513                 $bp = buddypress();
     516                $bp     = buddypress();
    514517                $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
    515518
    516519                // Update the user's group count.
    class BP_Groups_Member { 
    529532                 */
    530533                do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
    531534
    532                 return $remove;
     535                return (bool) $remove;
    533536        }
    534537
    535538        /**
  • 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/functions.php

    diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
    index 4b7533f55..8a852a9ff 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 ) );
    188188        }
    189189
    190190        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    193193         */
    194194        public function test_total_member_count_groups_leave_group() {
    195195                $u1 = self::factory()->user->create();
     196                $u2 = self::factory()->user->create();
    196197                $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
    197                 groups_join_group( $g1, $u1 );
    198198
    199                 groups_leave_group( $g1, $u1 );
    200                 $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     199                groups_join_group( $g1, $u2 );
     200
     201                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
     202
     203                groups_leave_group( $g1, $u2 );
     204
     205                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
    201206        }
    202207
    203208        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    214219                $this->set_current_user( $u1 );
    215220                buddypress()->is_item_admin = true;
    216221
     222                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
     223
    217224                groups_ban_member( $u2, $g1 );
    218225
    219                 $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     226                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
    220227        }
    221228
    222229        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    235242
    236243                groups_ban_member( $u2, $g1 );
    237244
     245                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
     246
    238247                groups_unban_member( $u2, $g1 );
    239248
    240                 $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) );
     249                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
    241250        }
    242251
    243252        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    255264                        'send_invite' => 1,
    256265                ) );
    257266
     267                $this->assertEquals( 1, groups_get_total_member_count( $g ) );
     268
    258269                groups_accept_invite( $u2, $g );
    259270
    260                 $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     271                $this->assertEquals( 2, groups_get_total_member_count( $g ) );
    261272        }
    262273
    263274        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    275286                ) );
    276287                groups_accept_membership_request( 0, $u2, $g );
    277288
    278                 $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) );
     289                $this->assertEquals( 2, groups_get_total_member_count( $g ) );
    279290        }
    280291
    281292        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    294305
    295306                groups_remove_member( $u2, $g1 );
    296307
    297                 $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) );
     308                $this->assertEquals( 1, groups_get_total_member_count( $g1 ));
     309        }
     310
     311        /**
     312         * @group total_member_count
     313         * @group groups_remove_member
     314         */
     315        public function test_total_member_count_groups_delete_member() {
     316                $u1 = self::factory()->user->create();
     317                $u2 = self::factory()->user->create();
     318                $u3 = self::factory()->user->create();
     319                $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
     320
     321                groups_join_group( $g1, $u2 );
     322                groups_join_group( $g1, $u3 );
     323
     324                $this->set_current_user( $u1 );
     325
     326                $this->assertEquals( 3, groups_get_total_member_count( $g1 ) );
     327                $this->assertEquals( 3, BP_Groups_Group::get_total_member_count( $g1 ) );
     328
     329                // Delete user.
     330                wp_delete_user( $u2 );
     331
     332                $this->assertEquals( 2, groups_get_total_member_count( $g1 ) );
     333                $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) );
    298334        }
    299335
    300336        /**
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    313349                        'date_created' => bp_core_current_time(),
    314350                ) );
    315351
    316                 $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) );
     352                $this->assertEquals( 1, groups_get_total_member_count( $g ) );
    317353        }
    318354
    319355        /**