Ticket #6210: 6210.bp_get_user_groups.diff
File 6210.bp_get_user_groups.diff, 14.1 KB (added by , 6 years ago) |
---|
-
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 ) { 169 169 return $uncached; 170 170 } 171 171 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 */ 182 function 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 172 194 /** 173 195 * Update the metadata cache for the specified objects. 174 196 * -
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 $ 254 254 add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' ); 255 255 add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' ); 256 256 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 */ 264 function 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 } 268 add_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 */ 278 function 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 } 282 add_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 */ 292 function 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 } 296 add_action( 'groups_accept_invite', 'bp_groups_clear_user_group_cache_on_invitation_change', 10, 2 ); 297 add_action( 'groups_reject_invite', 'bp_groups_clear_user_group_cache_on_invitation_change', 10, 2 ); 298 add_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 */ 308 function 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 } 314 add_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 */ 325 function 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 } 329 add_action( 'groups_membership_requested', 'bp_groups_clear_user_group_cache_on_membership_request', 10, 3 ); 330 257 331 /** 258 332 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook. 259 333 * … … function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) 269 343 wp_cache_delete( $membership->id, 'bp_groups_memberships' ); 270 344 } 271 345 add_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 );274 346 275 347 /** 276 348 * 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() ) { 948 948 ), 'get_user_groups' ); 949 949 950 950 $user_id = intval( $user_id ); 951 $invites_prefix = BP_Invitation_Manager::get_table_name() . '_'; 951 952 952 // @TODO-6210: This is tied to the groups membership table. How to include invites/requests from a separate table?953 953 $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 954 954 if ( false === $membership_ids ) { 955 955 $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 ); 956 966 wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' ); 957 967 } 958 968 959 969 // 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' ); 961 971 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 } 963 980 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 } 966 988 } 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 967 1016 } 968 1017 969 1018 // 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 { 65 65 * 66 66 * @since 2.7.0 67 67 * 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. 69 70 * @return bool True on success, false on failure. 70 71 */ 71 72 public function run_acceptance_action( $type = 'invite', $r ) { … … class BP_Groups_Invitation_Manager extends BP_Invitation_Manager { 88 89 return false; 89 90 } 90 91 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 91 117 // Modify group meta. 92 118 groups_update_groupmeta( $r['item_id'], 'last_activity', bp_core_current_time() ); 93 119 -
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 { 26 26 ) ); 27 27 self::$groups = $f->group->create_many( 4, array( 28 28 'creator_id' => self::$admin_user, 29 'status' => 'private' 29 30 ) ); 30 31 31 32 $now = time(); … … class BP_Tests_Groups_Functions_BpGetUserGroups extends BP_UnitTestCase { 516 517 $server_name = isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : null; 517 518 $_SERVER['SERVER_NAME'] = ''; 518 519 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 ) ); 520 524 521 525 // For `wp_mail()`. 522 526 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 { 16 16 $this->set_current_user( self::factory()->user->create() ); 17 17 18 18 $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' ) ); 20 20 $this->filter_fired = ''; 21 21 } 22 22 … … class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 232 232 'is_admin' => 1, 233 233 ) ); 234 234 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 ) ); 236 239 237 240 // Both admins should get a notification. 238 241 $get_args = array( … … class BP_Tests_Groups_Notifications extends BP_UnitTestCase { 247 250 $this->assertNotEmpty( $u0_notifications ); 248 251 $this->assertNotEmpty( $u1_notifications ); 249 252 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( 251 291 'user_id' => $users[2], 252 292 'group_id' => $this->group, 253 ) ) ); 293 'send_invite' => true 294 ) ); 254 295 255 296 $u0_notifications = BP_Notifications_Notification::get( $get_args ); 256 297 $u1_notifications = BP_Notifications_Notification::get( $get_args );