Skip to:
Content

BuddyPress.org

Changeset 8200


Ignore:
Timestamp:
03/29/2014 12:35:30 AM (11 years ago)
Author:
r-a-y
Message:

Cache the group invite count for a user.

This commit:

  • Caches calls to BP_Groups_Member::get_invite_count_for_user()
  • Invalidates the group invite count cache on various actions
  • Adds a unit test

See #5414

Location:
trunk
Files:
3 edited

Legend:

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

    r7956 r8200  
    113113
    114114/**
     115 * Clear a user's cached total group invite count.
     116 *
     117 * Count is cleared when an invite is accepted, rejected or deleted.
     118 *
     119 * @since BuddyPress (2.0.0)
     120 *
     121 * @param int $user_id The user ID.
     122 */
     123function bp_groups_clear_invite_count_for_user( $user_id ) {
     124    wp_cache_delete( $user_id, 'bp_group_invite_count' );
     125}
     126add_action( 'groups_accept_invite', 'bp_groups_clear_invite_count_for_user' );
     127add_action( 'groups_reject_invite', 'bp_groups_clear_invite_count_for_user' );
     128add_action( 'groups_delete_invite', 'bp_groups_clear_invite_count_for_user' );
     129
     130/**
     131 * Clear a user's cached total group invite count when a user is uninvited.
     132 *
     133 * Groan. Our API functions are not consistent.
     134 *
     135 * @since BuddyPress (2.0.0)
     136 *
     137 * @param int $group_id The group ID. Not used in this function.
     138 * @param int $user_id The user ID.
     139 */
     140function bp_groups_clear_invite_count_on_uninvite( $group_id, $user_id ) {
     141    bp_groups_clear_invite_count_for_user( $user_id );
     142}
     143add_action( 'groups_uninvite_user', 'bp_groups_clear_invite_count_on_uninvite', 10, 2 );
     144
     145/**
     146 * Clear a user's cached total group invite count when a new invite is sent.
     147 *
     148 * @since BuddyPress (2.0.0)
     149 *
     150 * @param int $group_id The group ID. Not used in this function.
     151 * @param array $invited_users Array of invited user IDs.
     152 */
     153function bp_groups_clear_invite_count_on_send( $group_id, $invited_users ) {
     154    foreach ( $invited_users as $user_id ) {
     155        bp_groups_clear_invite_count_for_user( $user_id );
     156    }
     157}
     158add_action( 'groups_send_invites', 'bp_groups_clear_invite_count_on_send', 10, 2 );
     159
     160/**
    115161 * Clear a user's cached group count.
    116162 *
    117  * @param int $group_id ID of the group. Not used in this function.
    118  * @param int $user_id ID of the user whose group count is being changed.
     163 * @param int $group_id The group ID. Not used in this function.
     164 * @param int $user_id The user ID.
    119165 */
    120166function groups_clear_group_user_object_cache( $group_id, $user_id ) {
  • trunk/bp-groups/bp-groups-classes.php

    r8168 r8200  
    23632363        $bp = buddypress();
    23642364
    2365         return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d ORDER BY date_modified ASC", $user_id ) );
     2365        $count = wp_cache_get( $user_id, 'bp_group_invite_count' );
     2366
     2367        if ( false === $count ) {
     2368            $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d", $user_id ) );
     2369            wp_cache_set( $user_id, $count, 'bp_group_invite_count' );
     2370        }
     2371
     2372        return $count;
    23662373    }
    23672374
  • trunk/tests/testcases/groups/functions.php

    r8133 r8200  
    578578        $this->set_current_user( $old_user );
    579579    }
     580
     581    /**
     582     * @group counts
     583     */
     584    public function test_get_invite_count_for_user() {
     585        $u1 = $this->create_user();
     586        $u2 = $this->create_user();
     587        $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     588
     589        // create invitation
     590        groups_invite_user( array(
     591            'user_id'    => $u2,
     592            'group_id'   => $g,
     593            'inviter_id' => $u1,
     594        ) );
     595
     596        // send the invite
     597        // this function is imperative to set the 'invite_sent' flag in the DB
     598        // why is this separated from groups_invite_user()?
     599        // @see groups_screen_group_invite()
     600        groups_send_invites( $u1, $g );
     601
     602        // assert invite count
     603        $this->assertEquals( 1, groups_get_invite_count_for_user( $u2 ) );
     604
     605        // accept the invite and reassert
     606        groups_accept_invite( $u2, $g );
     607        $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) );
     608    }
    580609}
Note: See TracChangeset for help on using the changeset viewer.