Skip to:
Content

BuddyPress.org

Changeset 6100


Ignore:
Timestamp:
06/14/2012 08:12:59 PM (12 years ago)
Author:
djpaul
Message:

When a group is created or deleted, clear each group member's "total groups count" cache.
Addresses a bug where the counts never changed in an environment with a persistent object cache.

  • See #4226
  • Specify cache group for to wp_cache_delete() in groups_clear_group_user_object_cache().
  • Add $user_ids parameter to the 'bp_groups_delete_group' action to save a repeat DB query later.
  • Add $group object parameter to the 'groups_created_group' action.
Location:
trunk/bp-groups
Files:
3 edited

Legend:

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

    r6093 r6100  
    5050add_action( 'groups_create_group_step_complete', 'groups_clear_group_object_cache' );
    5151
     52/**
     53 * Clears caches for the group creator when a group is created
     54 *
     55 * @param int $group_id
     56 * @param BP_Groups_Group $group_obj
     57 * @since BuddyPress (1.6)
     58 */
     59function bp_groups_clear_group_creator_cache( $group_id, $group_obj ) {
     60    // Clears the 'total groups' for this user
     61    groups_clear_group_user_object_cache( $group_obj->id, $group_obj->creator_id );
     62}
     63add_action( 'groups_created_group', 'bp_groups_clear_group_creator_cache', 10, 2 );
     64
     65/**
     66 * Clears caches for all members in a group when a group is deleted
     67 *
     68 * @param BP_Groups_Group $group_obj
     69 * @param array User IDs who were in this group
     70 * @since BuddyPress (1.6)
     71 */
     72function bp_groups_clear_group_members_caches( $group_obj, $user_ids ) {
     73    // Clears the 'total groups' cache for each member in a group
     74    foreach ( (array) $user_ids as $user_id )
     75        groups_clear_group_user_object_cache( $group_obj->id, $user_id );
     76}
     77add_action( 'bp_groups_delete_group', 'bp_groups_clear_group_members_caches', 10, 2 );
     78
    5279function groups_clear_group_user_object_cache( $group_id, $user_id ) {
    53     wp_cache_delete( 'bp_total_groups_for_user_' . $user_id );
     80    wp_cache_delete( 'bp_total_groups_for_user_' . $user_id, 'bp' );
    5481}
    5582add_action( 'groups_join_group',   'groups_clear_group_user_object_cache', 10, 2 );
  • trunk/bp-groups/bp-groups-classes.php

    r5999 r6100  
    173173
    174174        // Fetch the user IDs of all the members of the group
    175         $user_ids = BP_Groups_Member::get_group_member_ids( $this->id );
    176         $user_ids = implode( ',', (array) $user_ids );
     175        $user_ids    = BP_Groups_Member::get_group_member_ids( $this->id );
     176        $user_id_str = implode( ',', (array) $user_ids );
    177177
    178178        // Modify group count usermeta for members
    179         $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->usermeta} SET meta_value = meta_value - 1 WHERE meta_key = 'total_group_count' AND user_id IN ( {$user_ids} )" ) );
     179        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->usermeta} SET meta_value = meta_value - 1 WHERE meta_key = 'total_group_count' AND user_id IN ( {$user_id_str} )" ) );
    180180
    181181        // Now delete all group member entries
    182182        BP_Groups_Member::delete_all( $this->id );
    183183
    184         do_action_ref_array( 'bp_groups_delete_group', array( &$this ) );
     184        do_action_ref_array( 'bp_groups_delete_group', array( &$this, $user_ids ) );
    185185
    186186        wp_cache_delete( 'bp_groups_group_' . $this->id, 'bp' );
  • trunk/bp-groups/bp-groups-functions.php

    r6093 r6100  
    130130    }
    131131
    132     do_action( 'groups_created_group', $group->id );
     132    do_action( 'groups_created_group', $group->id, $group );
    133133
    134134    return $group->id;
Note: See TracChangeset for help on using the changeset viewer.