Skip to:
Content

BuddyPress.org

Ticket #7614: 7614.activate-users.diff

File 7614.activate-users.diff, 2.2 KB (added by dcavins, 16 months ago)

Update group member counts for groups that a user belongs to when that user logs in for the first time.

  • src/bp-groups/bp-groups-cache.php

    diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php
    index 7678418c1..f104cf191 100644
    function bp_groups_reset_cache_incrementor_on_group_term_remove( $object_id, $te 
    378378}
    379379add_action( 'bp_remove_object_terms', 'bp_groups_reset_cache_incrementor_on_group_term_remove', 10, 3 );
    380380
     381/**
     382 * When a user is already a member of a group, then becomes active (logs in for the first time),
     383 * the group member count needs to be updated because they were not counted previously.
     384 *
     385 * @since 11.0.0
     386 *
     387 * @param int $user_id ID of the user whose activity is recorded.
     388 */
     389function enqueue_update_group_member_totals_for_newly_active_user( $user_id ) {
     390        // The hook is called before the activity is recorded, so we need to defer the group count updates.
     391        add_action( 'shutdown', 'update_group_member_totals_for_newly_active_user' );
     392
     393        // Store the user ID as a transient, else we won't know which user was updated.
     394        set_transient( 'bp_first_activity_for_member_' . wp_get_session_token(), $user_id, 30 );
     395}
     396        /**
     397         * At shutdown, we refresh the group counts for the newly active member.
     398         *
     399         * @since 11.0.0
     400         */
     401        function update_group_member_totals_for_newly_active_user() {
     402                $transient_id = 'bp_first_activity_for_member_' . wp_get_session_token();
     403
     404                // Get the user ID from the transient.
     405                $user_id = get_transient( $transient_id );
     406
     407                if ( false === $user_id ) {
     408                        return;
     409                } else {
     410                        $user_id = (int) $user_id;
     411                }
     412
     413                // Check for group memberships
     414                $memberships = bp_get_user_groups( $user_id );
     415
     416                // Update the group member count for groups that will be affected.
     417                if ( $memberships ) {
     418                        foreach ( $memberships as $group_id => $group_membership ) {
     419                                groups_get_total_member_count( $group_id, true );
     420                        }
     421                }
     422
     423                // Remove the transient.
     424                delete_transient( $transient_id );
     425        }
     426add_action( 'bp_first_activity_for_member', 'enqueue_update_group_member_totals_for_newly_active_user' );
     427
    381428/* List actions to clear super cached pages on, if super cache is installed */
    382429add_action( 'groups_join_group',                 'bp_core_clear_cache' );
    383430add_action( 'groups_leave_group',                'bp_core_clear_cache' );