Skip to:
Content

BuddyPress.org

Ticket #7614: 7614-1.diff

File 7614-1.diff, 12.8 KB (added by espellcaste, 2 months 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..dbfc81343 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 get count from `groups_get_groupmeta`,
     787 *               with fallback to the `BP_Groups_Group::get_total_member_count`
    770788 *
    771789 * @param int $group_id Group ID.
    772790 * @return int Count of confirmed members for the group.
    773791 */
    774792function groups_get_total_member_count( $group_id ) {
    775         return BP_Groups_Group::get_total_member_count( $group_id );
     793        $cache_key = 'total_member_count';
     794        $count     = groups_get_groupmeta( $group_id, $cache_key );
     795
     796        if ( false === $count ) {
     797                $count = (int) BP_Groups_Group::get_total_member_count( $group_id );
     798
     799                groups_update_groupmeta( $group_id, $cache_key, $count );
     800        }
     801
     802        return (int) $count;
    776803}
    777804
    778805/** 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..321a9994e 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         *
    16831685         * @param int $group_id Group ID.
    16841686         * @return int Count of confirmed members for the group.
    16851687         */
    16861688        public static function get_total_member_count( $group_id ) {
    1687                 global $wpdb;
    1688 
    1689                 $bp = buddypress();
     1689                $members = groups_get_group_members(
     1690                        array(
     1691                                'group_id'            => $group_id,
     1692                                'exclude_banned'      => true,
     1693                                'exclude_admins_mods' => false,
     1694                                'type'                => 'active',
     1695                        )
     1696                );
    16901697
    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 ) );
     1698                return (int) $members['count'] ?? 0;
    16921699        }
    16931700
    16941701        /**
  • 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..81ee219b1 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.
    481481         *
    482482         * @since 1.8.0
    483483         *
    class BP_Groups_Member { 
    495495         *
    496496         * @param int $user_id  ID of the user.
    497497         * @param int $group_id ID of the group.
    498          * @return True on success, false on failure.
     498         * @return bool True on success, false on failure.
    499499         */
    500500        public static function delete( $user_id, $group_id ) {
    501501                global $wpdb;
    class BP_Groups_Member { 
    510510                 */
    511511                do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
    512512
    513                 $bp = buddypress();
     513                $bp     = buddypress();
    514514                $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
    515515
    516516                // Update the user's group count.
    class BP_Groups_Member { 
    529529                 */
    530530                do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
    531531
    532                 return $remove;
     532                return (bool) $remove;
    533533        }
    534534
    535535        /**
  • 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        /**