Skip to:
Content

BuddyPress.org

Ticket #5711: 5711.02.patch

File 5711.02.patch, 5.0 KB (added by r-a-y, 11 years ago)

Updated to fix notice when using static add_user_to_group() method.

  • 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
     190/**
     191 * Clear group administrator cache when a group member is saved.
     192 *
     193 * This accounts for situations where group administrators are added manually
     194 * using {@link BP_Groups_Member::save()}.  Usually via a plugin.
     195 *
     196 * @since BuddyPress (2.1.0)
     197 *
     198 * @param BP_Groups_Member $member
     199 */
     200function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member $member ) {
     201        groups_clear_group_administrator_cache( $member->group_id );
     202}
     203add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
     204
    176205/* List actions to clear super cached pages on, if super cache is installed */
    177206add_action( 'groups_join_group',                 'bp_core_clear_cache' );
    178207add_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/includes/testcase.php

     
    336336        public static function add_user_to_group( $user_id, $group_id, $args = array() ) {
    337337                $r = wp_parse_args( $args, array(
    338338                        'date_modified' => bp_core_current_time(),
    339                         'is_confirmed' => 1,
    340                         'invite_sent' => 0,
    341                         'inviter_id' => 0,
     339                        'is_confirmed'  => 1,
     340                        'is_admin'      => 0,
     341                        'invite_sent'   => 0,
     342                        'inviter_id'    => 0,
    342343                ) );
    343344
    344345                $new_member                = new BP_Groups_Member;
    345346                $new_member->group_id      = $group_id;
    346347                $new_member->user_id       = $user_id;
    347348                $new_member->inviter_id    = 0;
    348                 $new_member->is_admin      = 0;
     349                $new_member->is_admin      = $r['is_admin'];
    349350                $new_member->user_title    = '';
    350351                $new_member->date_modified = $r['date_modified'];
    351352                $new_member->is_confirmed  = $r['is_confirmed'];
  • 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        }
     179
     180        /**
     181         * @group groups_get_group_admins
     182         */
     183        public function test_groups_get_group_admins_cache_on_member_save() {
     184                $u1 = $this->create_user();
     185                $u2 = $this->create_user();
     186                $g = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     187
     188                // prime cache
     189                groups_get_group_admins( $g );
     190
     191                // promote user 2 to an admin via BP_Groups_Member::save()
     192                self::add_user_to_group( $u2, $g, array( 'is_admin' => 1 ) );
     193
     194                // assert that cache is invalidated
     195                $this->assertEmpty( wp_cache_get( $g, 'bp_group_admins' ) );
     196
     197                // assert new cached value
     198                $this->assertEquals( 2, count( groups_get_group_admins( $g ) ) );
     199        }
    157200}