| 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 | */ |
| 389 | function 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 | } |
| 426 | add_action( 'bp_first_activity_for_member', 'enqueue_update_group_member_totals_for_newly_active_user' ); |
| 427 | |