Skip to:
Content

BuddyPress.org

Changeset 12273


Ignore:
Timestamp:
11/05/2018 08:20:05 PM (6 years ago)
Author:
boonebgorges
Message:

Groups: Improve treatment of single-member groups on user deletion.

Instead of deleting single-member groups on user deletion, we simply
remove the member from the group. Because BuddyPress expects groups to
have at least one admin member, in these cases we assign a site admin
to be the sole group admin.

This changeset also fixes a related bug whereby the group admin/mod
cache was not properly invalidated on membership deletion.

Props djpaul.
Fixes #7476.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-cache.php

    r11530 r12273  
    213213}
    214214add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
     215
     216/**
     217 * Clear group administrator and moderator cache when a group member is deleted.
     218 *
     219 * @since 4.0.0
     220 *
     221 * @param int $user_id  User ID.
     222 * @param int $group_id Group ID.
     223 */
     224function bp_groups_clear_group_administrator_cache_on_member_delete( $user_id, $group_id ) {
     225    groups_clear_group_administrator_cache( $group_id );
     226}
     227add_action( 'bp_groups_member_after_delete', 'bp_groups_clear_group_administrator_cache_on_member_delete', 10, 2 );
    215228
    216229/**
  • trunk/src/bp-groups/classes/class-bp-groups-member.php

    r12162 r12273  
    14291429     * Delete all group membership information for the specified user.
    14301430     *
     1431     * In cases where the user is the sole member of a group, a site administrator is
     1432     * assigned to be the group's administrator. Unhook `groups_remove_data_for_user()`
     1433     * to modify this behavior.
     1434     *
    14311435     * @since 1.0.0
     1436     * @since 4.0.0 The method behavior was changed so that single-member groups are not deleted.
    14321437     *
    14331438     * @param int $user_id ID of the user.
    1434      * @return mixed
     1439     * @return bool
    14351440     */
    14361441    public static function delete_all_for_user( $user_id ) {
    1437         global $wpdb;
    1438 
    1439         $bp = buddypress();
    1440 
    1441         // Get all the group ids for the current user's groups and update counts.
    14421442        $group_ids = BP_Groups_Member::get_group_ids( $user_id );
     1443
    14431444        foreach ( $group_ids['groups'] as $group_id ) {
    1444             groups_update_groupmeta( $group_id, 'total_member_count', groups_get_total_member_count( $group_id ) - 1 );
    1445 
    1446             // If current user is the creator of a group and is the sole admin, delete that group to avoid counts going out-of-sync.
    1447             if ( groups_is_user_admin( $user_id, $group_id ) && count( groups_get_group_admins( $group_id ) ) < 2 && groups_is_user_creator( $user_id, $group_id ) )
    1448                 groups_delete_group( $group_id );
    1449         }
    1450 
    1451         return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d", $user_id ) );
     1445            if ( groups_is_user_admin( $user_id, $group_id ) ) {
     1446                // If the user is a sole group admin, install a site admin as their replacement.
     1447                if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
     1448                    $admin = get_users( [
     1449                        'blog_id' => bp_get_root_blog_id(),
     1450                        'fields'  => 'id',
     1451                        'number'  => 1,
     1452                        'orderby' => 'ID',
     1453                        'role'    => 'administrator',
     1454                    ] );
     1455
     1456                    if ( ! empty( $admin ) ) {
     1457                        groups_join_group( $group_id, $admin[0] );
     1458
     1459                        $member = new BP_Groups_Member( $admin[0], $group_id );
     1460                        $member->promote( 'admin' );
     1461                    }
     1462                }
     1463            }
     1464
     1465            BP_Groups_Member::delete( $user_id, $group_id );
     1466        }
     1467
     1468        return true;
    14521469    }
    14531470}
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-member.php

    r12162 r12273  
    14551455        $this->assertSame( self::$group_ids[1], $memberships[0]->group_id );
    14561456    }
     1457
     1458    /**
     1459     * @ticket BP7476
     1460     */
     1461    public function test_delete_all_for_user() {
     1462        $new_user = self::factory()->user->create();
     1463
     1464        $admin_users = get_users( [
     1465            'blog_id' => bp_get_root_blog_id(),
     1466            'fields'  => 'id',
     1467            'number'  => 1,
     1468            'orderby' => 'ID',
     1469            'role'    => 'administrator',
     1470        ] );
     1471
     1472        $admin_user = (int) $admin_users[0];
     1473
     1474        // Sole admin of group.
     1475        $new_group = self::factory()->group->create( [
     1476            'creator_id' => $new_user,
     1477        ] );
     1478
     1479        // One of two group admins.
     1480        groups_join_group( self::$group_ids[0], $new_user );
     1481        $m1 = new BP_Groups_Member( $new_user, self::$group_ids[0] );
     1482        $m1->promote( 'admin' );
     1483
     1484        // Not an admin.
     1485        groups_join_group( self::$group_ids[1], $new_user );
     1486        $m2 = new BP_Groups_Member( $new_user, self::$group_ids[1] );
     1487
     1488        BP_Groups_Member::delete_all_for_user( $new_user );
     1489
     1490        $new_group_members = BP_Groups_Member::get_group_administrator_ids( $new_group );
     1491        $this->assertSame( [ $admin_user ], wp_list_pluck( $new_group_members, 'user_id' ) );
     1492
     1493        $g0_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[0] );
     1494        $this->assertSame( [ self::$user_ids[3] ], wp_list_pluck( $g0_members, 'user_id' ) );
     1495
     1496        $g1_members = BP_Groups_Member::get_group_administrator_ids( self::$group_ids[1] );
     1497        $this->assertSame( [ self::$user_ids[3] ], wp_list_pluck( $g1_members, 'user_id' ) );
     1498    }
    14571499}
Note: See TracChangeset for help on using the changeset viewer.