Skip to:
Content

BuddyPress.org

Ticket #6210: 6210.bp_get_user_groups.diff

File 6210.bp_get_user_groups.diff, 14.1 KB (added by dcavins, 6 years ago)

Snapshot of changes required to make bp_get_user_groups() work with new table.

  • src/bp-core/bp-core-cache.php

    diff --git src/bp-core/bp-core-cache.php src/bp-core/bp-core-cache.php
    index 5cf0d9c07..448b0ce0e 100644
    function bp_get_non_cached_ids( $item_ids, $cache_group ) { 
    169169        return $uncached;
    170170}
    171171
     172/**
     173 * Determine which items from a list do not have cached values,
     174 * when the cache ID is not an integer.
     175 *
     176 * @since 5.0.0
     177 *
     178 * @param array  $item_keys   Key list.
     179 * @param string $cache_group The cache group to check against.
     180 * @return array
     181 */
     182function bp_get_non_cached_keys( $item_keys, $cache_group ) {
     183        $uncached = array();
     184
     185        foreach ( $item_keys as $item_key ) {
     186                if ( false === wp_cache_get( $item_key, $cache_group ) ) {
     187                        $uncached[] = $item_key;
     188                }
     189        }
     190
     191        return $uncached;
     192}
     193
    172194/**
    173195 * Update the metadata cache for the specified objects.
    174196 *
  • src/bp-groups/bp-groups-cache.php

    diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php
    index b44ea09f6..ef6188601 100644
    function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $ 
    254254add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' );
    255255add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' );
    256256
     257/**
     258 * Clear caches on invitation.
     259 *
     260 * @since 5.0.0
     261 *
     262 * @param array $args Array of parsed arguments for the group invite.
     263 */
     264function bp_groups_clear_user_group_cache_on_invitation( $args ) {
     265        wp_cache_delete( $args['user_id'], 'bp_groups_memberships_for_user' );
     266        wp_cache_delete( $args['group_id'], 'bp_groups_memberships' );
     267}
     268add_action( 'groups_invite_user', 'bp_groups_clear_user_group_cache_on_invitation' );
     269
     270/**
     271 * Clear caches on uninvitation.
     272 *
     273 * @since 5.0.0
     274 *
     275 * @param int $group_id    ID of the group being uninvited from.
     276 * @param int $user_id     ID of the user being uninvited.
     277 */
     278function bp_groups_clear_user_group_cache_on_invitation_uninvite( $group_id, $user_id ) {
     279        wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' );
     280        wp_cache_delete( $group_id, 'bp_groups_memberships' );
     281}
     282add_action( 'groups_uninvite_user', 'bp_groups_clear_user_group_cache_on_invitation_uninvite', 10, 2 );
     283
     284/**
     285 * Clear caches on acceptance, rejection or deletion of invitation.
     286 *
     287 * @since 5.0.0
     288 *
     289 * @param int $group_id    ID of the group being uninvited from.
     290 * @param int $user_id     ID of the user being uninvited.
     291 */
     292function bp_groups_clear_user_group_cache_on_invitation_change( $user_id, $group_id ) {
     293        wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' );
     294        wp_cache_delete( $group_id, 'bp_groups_memberships' );
     295}
     296add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_invitation_change', 10, 2 );
     297add_action( 'groups_reject_invite', 'bp_groups_clear_user_group_cache_on_invitation_change', 10, 2 );
     298add_action( 'groups_delete_invite', 'bp_groups_clear_user_group_cache_on_invitation_change', 10, 2 );
     299
     300/**
     301 * Clear caches on sending of invitations.
     302 *
     303 * @since 5.0.0
     304 *
     305 * @param int   $group_id      ID of the group who's being invited to.
     306 * @param array $invited_users Array of users being invited to the group.
     307 */
     308function bp_groups_clear_user_group_cache_on_invitation_send( $group_id, $invited_users ) {
     309        foreach ( $invited_users as $invited_user_id ) {
     310                wp_cache_delete( $invited_user_id, 'bp_groups_memberships_for_user' );
     311        }
     312        wp_cache_delete( $group_id, 'bp_groups_memberships' );
     313}
     314add_action( 'groups_send_invites', 'bp_groups_clear_user_group_cache_on_invitation_send', 10, 2 );
     315
     316/**
     317 * Clear caches on sending of membership request.
     318 *
     319 * @since 5.0.0
     320 *
     321 * @param int   $requesting_user_id  ID of the user requesting membership.
     322 * @param array $admins              Array of group admins.
     323 * @param int   $group_id            ID of the group being requested to
     324 */
     325function bp_groups_clear_user_group_cache_on_membership_request( $requesting_user_id, $admins, $group_id ) {
     326        wp_cache_delete( $requesting_user_id, 'bp_groups_memberships_for_user' );
     327        wp_cache_delete( $group_id, 'bp_groups_memberships' );
     328}
     329add_action( 'groups_membership_requested', 'bp_groups_clear_user_group_cache_on_membership_request', 10, 3 );
     330
    257331/**
    258332 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook.
    259333 *
    function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) 
    269343        wp_cache_delete( $membership->id, 'bp_groups_memberships' );
    270344}
    271345add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    272 add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    273 add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
    274346
    275347/**
    276348 * Reset cache incrementor for the Groups component.
  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index e6ac64465..3fe7501f6 100644
    function bp_get_user_groups( $user_id, $args = array() ) { 
    948948        ), 'get_user_groups' );
    949949
    950950        $user_id = intval( $user_id );
     951        $invites_prefix = BP_Invitation_Manager::get_table_name() . '_';
    951952
    952         // @TODO-6210: This is tied to the groups membership table. How to include invites/requests from a separate table?
    953953        $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' );
    954954        if ( false === $membership_ids ) {
    955955                $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id );
     956                // Fetch invitations and requests.
     957                $invitation_ids = groups_get_invites( array(
     958                        'user_id'     => $user_id,
     959                        'invite_sent' => 'all',
     960                        'fields'      => 'ids',
     961                        'type'        => 'all'
     962                ) );
     963                // Prepend invitation table name.
     964                array_walk( $invitation_ids, function ( &$value, $key, $invites_prefix ) { $value = $invites_prefix . $value; }, $invites_prefix );
     965                $membership_ids = array_merge( $membership_ids, $invitation_ids );
    956966                wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' );
    957967        }
    958968
    959969        // Prime the membership cache.
    960         $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' );
     970        $uncached_membership_ids = bp_get_non_cached_keys( $membership_ids, 'bp_groups_memberships' );
    961971        if ( ! empty( $uncached_membership_ids ) ) {
    962                 $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids );
     972        // Sort the uncached items into regular memberships and invitations.
     973                $uncached_invitation_ids = array();
     974                foreach ( $uncached_membership_ids as $key => $membership_id ) {
     975                        if ( strpos( $membership_id, $invites_prefix ) !== false) {
     976                                $uncached_invitation_ids[] = substr( $membership_id, strlen( $invites_prefix ) );
     977                                unset( $uncached_membership_ids[$key] );
     978                        }
     979                }
    963980
    964                 foreach ( $uncached_memberships as $uncached_membership ) {
    965                         wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' );
     981                // Set memberships.
     982                if ( $uncached_membership_ids ) {
     983                        $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids );
     984
     985                        foreach ( $uncached_memberships as $uncached_membership ) {
     986                                wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' );
     987                        }
    966988                }
     989
     990                // Set invitations/requests.
     991                if ( $uncached_invitation_ids ) {
     992                        $uncached_invitations = groups_get_invites( array(
     993                                'ids'         => $uncached_invitation_ids,
     994                                'invite_sent' => 'all',
     995                                'type'        => 'all'
     996                        ) );
     997                        foreach ( $uncached_invitations as $uncached_invitation ) {
     998                                // Reshape the result as a membership db entry.
     999                                $invitation = new StdClass;
     1000                                $invitation->id            = $uncached_invitation->id;
     1001                                $invitation->group_id      = $uncached_invitation->item_id;
     1002                                $invitation->user_id       = $uncached_invitation->user_id;
     1003                                $invitation->inviter_id    = $uncached_invitation->inviter_id;
     1004                                $invitation->is_admin      = false;
     1005                                $invitation->is_mod            = false;
     1006                                $invitation->user_title    = '';
     1007                                $invitation->date_modified = $uncached_invitation->date_modified;
     1008                                $invitation->comments      = $uncached_invitation->content;
     1009                                $invitation->is_confirmed  = false;
     1010                                $invitation->is_banned     = false;
     1011                                $invitation->invite_sent   = $uncached_invitation->invite_sent;
     1012                                wp_cache_set( $invites_prefix . $invitation->id, $invitation, 'bp_groups_memberships' );
     1013                        }
     1014                }
     1015
    9671016        }
    9681017
    9691018        // Assemble filter array for use in `wp_list_filter()`.
  • src/bp-groups/classes/class-bp-groups-invitation-manager.php

    diff --git src/bp-groups/classes/class-bp-groups-invitation-manager.php src/bp-groups/classes/class-bp-groups-invitation-manager.php
    index 84e528f18..1e6f30a4d 100644
    class BP_Groups_Invitation_Manager extends BP_Invitation_Manager { 
    6565         *
    6666         * @since 2.7.0
    6767         *
    68          * @param int $id The ID of the invitation to mark as sent.
     68         * @param string $type Are we accepting an invitation or request?
     69         * @param array  $r    Parameters that describe the invitation being accepted.
    6970         * @return bool True on success, false on failure.
    7071         */
    7172        public function run_acceptance_action( $type = 'invite', $r  ) {
    class BP_Groups_Invitation_Manager extends BP_Invitation_Manager { 
    8889                        return false;
    8990                }
    9091
     92                if ( 'request' === $type ) {
     93                        /**
     94                         * Fires after a group membership request has been accepted.
     95                         *
     96                         * @since 1.0.0
     97                         *
     98                         * @param int  $user_id  ID of the user who accepted membership.
     99                         * @param int  $group_id ID of the group that was accepted membership to.
     100                         * @param bool $value    If membership was accepted.
     101                         */
     102                        do_action( 'groups_membership_accepted', $r['user_id'], $r['item_id'], true );
     103                } else {
     104                        /**
     105                         * Fires after a user has accepted a group invite.
     106                         *
     107                         * @since 1.0.0
     108                         * @since 2.8.0 The $inviter_id arg was added.
     109                         *
     110                         * @param int $user_id    ID of the user who accepted the group invite.
     111                         * @param int $group_id   ID of the group being accepted to.
     112                         * @param int $inviter_id ID of the user who invited this user to the group.
     113                         */
     114                        do_action( 'groups_accept_invite', $r['user_id'], $r['item_id'], $r['inviter_id'] );
     115                }
     116
    91117                // Modify group meta.
    92118                groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() );
    93119
  • tests/phpunit/testcases/groups/functions/bpGetUserGroups.php

    diff --git tests/phpunit/testcases/groups/functions/bpGetUserGroups.php tests/phpunit/testcases/groups/functions/bpGetUserGroups.php
    index ccdb888ba..8e4c79ae8 100644
    class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 
    2626                ) );
    2727                self::$groups = $f->group->create_many( 4, array(
    2828                        'creator_id' => self::$admin_user,
     29                        'status'     => 'private'
    2930                ) );
    3031
    3132                $now = time();
    class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 
    516517                $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null;
    517518                $_SERVER['SERVER_NAME'] = '';
    518519
    519                 groups_send_membership_request( self::$user, self::$groups[2] );
     520                groups_send_membership_request( array(
     521                        'user_id' => self::$user,
     522                        'group_id' => self::$groups[2]
     523                ) );
    520524
    521525                // For `wp_mail()`.
    522526                if ( is_null( $server_name ) ) {
  • tests/phpunit/testcases/groups/notifications.php

    diff --git tests/phpunit/testcases/groups/notifications.php tests/phpunit/testcases/groups/notifications.php
    index c844c91ef..2aafb30e0 100644
    class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 
    1616                $this->set_current_user( self::factory()->user->create() );
    1717
    1818                $this->requesting_user_id = self::factory()->user->create();
    19                 $this->group = self::factory()->group->create();
     19                $this->group = self::factory()->group->create( array( 'status' =>  'private' ) );
    2020                $this->filter_fired = '';
    2121        }
    2222
    class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 
    232232                        'is_admin' => 1,
    233233                ) );
    234234
    235                 groups_send_membership_request( $users[2], $this->group );
     235                groups_send_membership_request( array(
     236                        'user_id' => $users[2],
     237                        'group_id' => $this->group
     238                ) );
    236239
    237240                // Both admins should get a notification.
    238241                $get_args = array(
    class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 
    247250                $this->assertNotEmpty( $u0_notifications );
    248251                $this->assertNotEmpty( $u1_notifications );
    249252
    250                 $this->assertTrue( groups_invite_user( array(
     253                groups_accept_membership_request( false, $users[2], $this->group );
     254
     255                $u0_notifications = BP_Notifications_Notification::get( $get_args );
     256                $u1_notifications = BP_Notifications_Notification::get( $get_args );
     257                $this->assertEmpty( $u0_notifications );
     258                $this->assertEmpty( $u1_notifications );
     259        }
     260
     261        public function test_membership_request_notifications_should_be_cleared_when_request_is_accepted_via_invite() {
     262                $users = self::factory()->user->create_many( 3 );
     263
     264                $this->add_user_to_group( $users[0], $this->group, array(
     265                        'is_admin' => 1,
     266                ) );
     267                $this->add_user_to_group( $users[1], $this->group, array(
     268                        'is_admin' => 1,
     269                ) );
     270
     271                groups_send_membership_request( array(
     272                        'user_id' => $users[2],
     273                        'group_id' => $this->group
     274                ) );
     275
     276                // Both admins should get a notification.
     277                $get_args = array(
     278                        'user_id' => $users[0],
     279                        'item_id' => $this->group,
     280                        'secondary_item_id' => $users[2],
     281                        'component_action' => 'new_membership_request',
     282                        'is_new' => true,
     283                );
     284                $u0_notifications = BP_Notifications_Notification::get( $get_args );
     285                $u1_notifications = BP_Notifications_Notification::get( $get_args );
     286                $this->assertNotEmpty( $u0_notifications );
     287                $this->assertNotEmpty( $u1_notifications );
     288
     289                // 'Accept' the request by sending an invite.
     290                groups_invite_user( array(
    251291                        'user_id' => $users[2],
    252292                        'group_id' => $this->group,
    253                 ) ) );
     293                        'send_invite' => true
     294                ) );
    254295
    255296                $u0_notifications = BP_Notifications_Notification::get( $get_args );
    256297                $u1_notifications = BP_Notifications_Notification::get( $get_args );