Skip to:
Content

BuddyPress.org

Ticket #5414: 5414.group_invite_count.01.patch

File 5414.group_invite_count.01.patch, 4.5 KB (added by r-a-y, 10 years ago)
  • bp-groups/bp-groups-cache.php

    function bp_groups_clear_group_members_caches( $group_obj, $user_ids ) { 
    112112add_action( 'bp_groups_delete_group', 'bp_groups_clear_group_members_caches', 10, 2 );
    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        wp_cache_delete( $user_id, 'bp_group_invite_count' );
     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                wp_cache_delete( $user_id, 'bp_group_invite_count' );
     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 ) {
    121167        wp_cache_delete( 'bp_total_groups_for_user_' . $user_id, 'bp' );
  • bp-groups/bp-groups-classes.php

    class BP_Groups_Member { 
    23322332
    23332333                $bp = buddypress();
    23342334
    2335                 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 {$exclude_sql} ORDER BY date_modified ASC", $user_id ) );
     2335                $count = wp_cache_get( $user_id, 'bp_group_invite_count' );
     2336
     2337                if ( false === $count ) {
     2338                        $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 ) );
     2339
     2340                        wp_cache_set( $user_id, $count, 'bp_group_invite_count' );
     2341                }
     2342
     2343                return $count;
    23362344        }
    23372345
    23382346        /**
  • tests/testcases/groups/functions.php

    Bar!'; 
    572572
    573573                $this->set_current_user( $old_user );
    574574        }
     575
     576        /**
     577         * @group counts
     578         */
     579        public function test_get_invite_count_for_user() {
     580                $u1 = $this->create_user();
     581                $u2 = $this->create_user();
     582                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     583
     584                // create invitation
     585                groups_invite_user( array(
     586                        'user_id'    => $u2,
     587                        'group_id'   => $g,
     588                        'inviter_id' => $u1,
     589                ) );
     590
     591                // send the invite
     592                // this function is imperative to set the 'invite_sent' flag in the DB
     593                // why is this separated from groups_invite_user()?
     594                // @see groups_screen_group_invite()
     595                groups_send_invites( $u1, $g );
     596
     597                // assert invite count
     598                $this->assertEquals( 1, groups_get_invite_count_for_user( $u2 ) );
     599
     600                // accept the invite and reassert
     601                groups_accept_invite( $u2, $g );
     602                $this->assertEquals( 0, groups_get_invite_count_for_user( $u2 ) );
     603        }
    575604}