Skip to:
Content

BuddyPress.org

Ticket #5711: 5711.01.patch

File 5711.01.patch, 2.9 KB (added by r-a-y, 11 years ago)
  • src/bp-groups/bp-groups-cache.php

     
    173173add_action( 'groups_uninvite_user', 'groups_clear_group_user_object_cache', 10, 2 );
    174174add_action( 'groups_remove_member', 'groups_clear_group_user_object_cache', 10, 2 );
    175175
     176/**
     177 * Clear group administrator cache.
     178 *
     179 * @since BuddyPress (2.1.0)
     180 *
     181 * @param int $group_id The group ID.
     182 */
     183function groups_clear_group_administrator_cache( $group_id ) {
     184        wp_cache_delete( $group_id, 'bp_group_admins' );
     185}
     186add_action( 'groups_promote_member', 'groups_clear_group_administrator_cache' );
     187add_action( 'groups_demote_member',  'groups_clear_group_administrator_cache' );
     188add_action( 'groups_delete_group',   'groups_clear_group_administrator_cache' );
     189
    176190/* List actions to clear super cached pages on, if super cache is installed */
    177191add_action( 'groups_join_group',                 'bp_core_clear_cache' );
    178192add_action( 'groups_leave_group',                'bp_core_clear_cache' );
  • src/bp-groups/bp-groups-classes.php

     
    26262626        public static function get_group_administrator_ids( $group_id ) {
    26272627                global $bp, $wpdb;
    26282628
    2629                 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_admin = 1 AND is_banned = 0", $group_id ) );
     2629                $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
     2630
     2631                if ( false === $group_admins ) {
     2632                        $group_admins = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_admin = 1 AND is_banned = 0", $group_id ) );
     2633
     2634                        wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
     2635                }
     2636
     2637                return $group_admins;
    26302638        }
    26312639
    26322640        /**
  • tests/phpunit/testcases/groups/cache.php

     
    154154                $this->assertNotEmpty( wp_cache_get( $g1, 'bp_groups' ) );
    155155                $this->assertNotEmpty( wp_cache_get( $g2, 'bp_groups' ) );
    156156        }
     157
     158        /**
     159         * @group groups_get_group_admins
     160         */
     161        public function test_groups_get_group_admins_cache() {
     162                $u1 = $this->create_user();
     163                $u2 = $this->create_user();
     164                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     165
     166                // prime cache
     167                groups_get_group_admins( $g );
     168
     169                // promote user 2 to an admin
     170                bp_update_is_item_admin( true );
     171                groups_promote_member( $u2, $g, 'admin' );
     172
     173                // assert that cache is invalidated
     174                $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
     175
     176                // assert new cached value
     177                $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
     178        }
    157179}