Skip to:
Content

BuddyPress.org

Changeset 7179


Ignore:
Timestamp:
06/07/2013 01:31:27 AM (12 years ago)
Author:
boonebgorges
Message:

Improvements to refreshing of total_group_count on group membership events

When group membership status is changed for a user, that user's
total_group_count meta must be updated. This changeset introduces a number of
improvements to the way this updating is done:

  • Update using an actual queried group count, rather than incrementing. This ensures that even if the count is off (due to a bug in BP or some other plugin), it will be corrected the next time a membership event is recorded.
  • Move the total_group_count refreshing further down the stack. Previously, the updating was happening at various depths: sometimes in the bp-groups-functions.php functions, sometimes in the database methods, and sometimes not at all. By moving the updates so that they directly follow the database queries, we reduce redundancy and ensure that the group count is updated in all cases

This changeset also introduces integration tests for the relevant groups_*
membership functions and total_group_count.

Fixes #5018

Props mukkundthanki, r-a-y for early patches

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r7147 r7179  
    12231223        $this->id = $wpdb->insert_id;
    12241224
     1225        // Update the user's group count
     1226        self::refresh_total_group_count_for_user( $this->user_id );
     1227
    12251228        do_action_ref_array( 'groups_member_after_save', array( &$this ) );
    12261229
     
    12771280
    12781281        groups_update_groupmeta( $this->group_id, 'total_member_count', ( (int) groups_get_groupmeta( $this->group_id, 'total_member_count' ) + 1 ) );
    1279         bp_update_user_meta( $this->user_id, 'total_group_count', (int) bp_get_user_meta( $this->user_id, 'total_group_count', true ) + 1 );
    12801282
    12811283        return $this->save();
     
    12871289        $this->is_confirmed  = 1;
    12881290        $this->date_modified = bp_core_current_time();
    1289 
    1290         bp_update_user_meta( $this->user_id, 'total_group_count', (int) bp_get_user_meta( $this->user_id, 'total_group_count', true ) + 1 );
    12911291    }
    12921292
     
    12951295        $this->is_confirmed = 1;
    12961296        $this->date_modified = bp_core_current_time();
    1297 
    1298         bp_update_user_meta( $this->user_id, 'total_group_count', (int) bp_get_user_meta( $this->user_id, 'total_group_count', true ) + 1 );
    12991297    }
    13001298
     
    13091307        groups_update_groupmeta( $this->group_id, 'total_member_count', ( (int) groups_get_groupmeta( $this->group_id, 'total_member_count' ) - 1 ) );
    13101308
    1311         $group_count = bp_get_user_meta( $this->user_id, 'total_group_count', true );
    1312         if ( !empty( $group_count ) )
    1313             bp_update_user_meta( $this->user_id, 'total_group_count', (int) $group_count - 1 );
     1309        // Update the user's group count
     1310        self::refresh_total_group_count_for_user( $this->user_id );
    13141311
    13151312        return $result;
     
    13181315    /** Static Methods ********************************************************/
    13191316
     1317    public static function refresh_total_group_count_for_user( $user_id ) {
     1318        bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
     1319    }
     1320
    13201321    function delete( $user_id, $group_id ) {
    13211322        global $wpdb, $bp;
    13221323
    1323         return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
     1324        $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
     1325
     1326        // Update the user's group count
     1327        self::refresh_total_group_count_for_user( $user_id );
     1328
     1329        return $remove;
    13241330    }
    13251331
  • trunk/bp-groups/bp-groups-functions.php

    r7172 r7179  
    302302    // Modify group member count
    303303    groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
    304 
    305     // Modify user's group memberhip count
    306     bp_update_user_meta( $user_id, 'total_group_count', (int) bp_get_user_meta( $user_id, 'total_group_count', true ) - 1 );
    307304
    308305    /**
Note: See TracChangeset for help on using the changeset viewer.