diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
index 9ecddaad7..00f4956eb 100644
|
|
function groups_get_id_by_previous_slug( $group_slug ) { |
500 | 500 | /** |
501 | 501 | * Remove a user from a group. |
502 | 502 | * |
| 503 | * @todo Check if group exists first. |
| 504 | * @todo Get group by other fields: slug/id/object. See #6749. |
| 505 | * |
503 | 506 | * @since 1.0.0 |
504 | 507 | * |
505 | 508 | * @param int $group_id ID of the group. |
… |
… |
function groups_leave_group( $group_id, $user_id = 0 ) { |
542 | 545 | /** |
543 | 546 | * Add a user to a group. |
544 | 547 | * |
| 548 | * @todo Check if group exists first. |
| 549 | * @todo Get group by other fields: slug/id/object. See #6749. |
| 550 | * |
545 | 551 | * @since 1.0.0 |
546 | 552 | * |
547 | 553 | * @param int $group_id ID of the group. |
… |
… |
function groups_leave_group( $group_id, $user_id = 0 ) { |
551 | 557 | */ |
552 | 558 | function groups_join_group( $group_id, $user_id = 0 ) { |
553 | 559 | |
554 | | if ( empty( $user_id ) ) |
| 560 | if ( empty( $user_id ) ) { |
555 | 561 | $user_id = bp_loggedin_user_id(); |
| 562 | } |
556 | 563 | |
557 | 564 | // Check if the user has an outstanding invite. If so, delete it. |
558 | | if ( groups_check_user_has_invite( $user_id, $group_id ) ) |
| 565 | if ( groups_check_user_has_invite( $user_id, $group_id ) ) { |
559 | 566 | groups_delete_invite( $user_id, $group_id ); |
| 567 | } |
560 | 568 | |
561 | 569 | // Check if the user has an outstanding request. If so, delete it. |
562 | | if ( groups_check_for_membership_request( $user_id, $group_id ) ) |
| 570 | if ( groups_check_for_membership_request( $user_id, $group_id ) ) { |
563 | 571 | groups_delete_membership_request( null, $user_id, $group_id ); |
| 572 | } |
564 | 573 | |
565 | 574 | // User is already a member, just return true. |
566 | | if ( groups_is_user_member( $user_id, $group_id ) ) |
| 575 | if ( groups_is_user_member( $user_id, $group_id ) ) { |
567 | 576 | return true; |
| 577 | } |
568 | 578 | |
569 | | $new_member = new BP_Groups_Member; |
| 579 | $new_member = new BP_Groups_Member(); |
570 | 580 | $new_member->group_id = $group_id; |
571 | 581 | $new_member->user_id = $user_id; |
572 | 582 | $new_member->inviter_id = 0; |
… |
… |
function groups_join_group( $group_id, $user_id = 0 ) { |
575 | 585 | $new_member->date_modified = bp_core_current_time(); |
576 | 586 | $new_member->is_confirmed = 1; |
577 | 587 | |
578 | | if ( !$new_member->save() ) |
| 588 | if ( ! $new_member->save() ) { |
579 | 589 | return false; |
| 590 | } |
580 | 591 | |
581 | 592 | $bp = buddypress(); |
582 | 593 | |
583 | | if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id ) |
| 594 | if ( ! isset( $bp->groups->current_group ) || ! $bp->groups->current_group || $group_id !== $bp->groups->current_group->id ) { |
584 | 595 | $group = groups_get_group( $group_id ); |
585 | | else |
| 596 | } else { |
586 | 597 | $group = $bp->groups->current_group; |
| 598 | } |
587 | 599 | |
588 | 600 | // Record this in activity streams. |
589 | 601 | if ( bp_is_active( 'activity' ) ) { |
590 | | groups_record_activity( array( |
591 | | 'type' => 'joined_group', |
592 | | 'item_id' => $group_id, |
593 | | 'user_id' => $user_id, |
594 | | ) ); |
| 602 | groups_record_activity( |
| 603 | array( |
| 604 | 'type' => 'joined_group', |
| 605 | 'item_id' => $group_id, |
| 606 | 'user_id' => $user_id, |
| 607 | ) |
| 608 | ); |
595 | 609 | } |
596 | 610 | |
597 | 611 | /** |
598 | 612 | * Fires after a user joins a group. |
599 | 613 | * |
600 | 614 | * @since 1.0.0 |
| 615 | * @since 10.0.0 Added the `$group` parameter. |
601 | 616 | * |
602 | | * @param int $group_id ID of the group. |
603 | | * @param int $user_id ID of the user joining the group. |
| 617 | * @param int $group_id ID of the group. |
| 618 | * @param int $user_id ID of the user joining the group. |
| 619 | * @param BP_Groups_Group $group The group object. |
604 | 620 | */ |
605 | | do_action( 'groups_join_group', $group_id, $user_id ); |
| 621 | do_action( 'groups_join_group', $group_id, $user_id, $group ); |
606 | 622 | |
607 | 623 | return true; |
608 | 624 | } |
… |
… |
function groups_get_group_members( $args = array() ) { |
767 | 783 | * Get the member count for a group. |
768 | 784 | * |
769 | 785 | * @since 1.2.3 |
| 786 | * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count` |
770 | 787 | * |
771 | | * @param int $group_id Group ID. |
| 788 | * @param int $group_id Group ID. |
| 789 | * @param bool $skip_cache Skip grabbing from cache. |
772 | 790 | * @return int Count of confirmed members for the group. |
773 | 791 | */ |
774 | | function groups_get_total_member_count( $group_id ) { |
775 | | return BP_Groups_Group::get_total_member_count( $group_id ); |
| 792 | function groups_get_total_member_count( $group_id, $skip_cache = false ) { |
| 793 | return (int) BP_Groups_Group::get_total_member_count( $group_id, $skip_cache ); |
776 | 794 | } |
777 | 795 | |
778 | 796 | /** Group Fetching, Filtering & Searching ************************************/ |
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
index f06ec8055..57fdd5e7a 100644
|
|
class BP_Groups_Group { |
1667 | 1667 | global $wpdb; |
1668 | 1668 | |
1669 | 1669 | $hidden_sql = ''; |
1670 | | if ( !bp_current_user_can( 'bp_moderate' ) ) |
| 1670 | if ( ! bp_current_user_can( 'bp_moderate' ) ) { |
1671 | 1671 | $hidden_sql = "WHERE status != 'hidden'"; |
| 1672 | } |
1672 | 1673 | |
1673 | 1674 | $bp = buddypress(); |
1674 | 1675 | |
… |
… |
class BP_Groups_Group { |
1679 | 1680 | * Get the member count for a group. |
1680 | 1681 | * |
1681 | 1682 | * @since 1.6.0 |
| 1683 | * @since 10.0.0 Updated to use the `groups_get_group_members`. |
1682 | 1684 | * |
1683 | | * @param int $group_id Group ID. |
| 1685 | * @param int $group_id Group ID. |
| 1686 | * @param bool $skip_cache Skip getting count from cache. |
1684 | 1687 | * @return int Count of confirmed members for the group. |
1685 | 1688 | */ |
1686 | | public static function get_total_member_count( $group_id ) { |
1687 | | global $wpdb; |
| 1689 | public static function get_total_member_count( $group_id, $skip_cache = false ) { |
| 1690 | $cache_key = 'total_member_count'; |
| 1691 | $count = groups_get_groupmeta( $group_id, $cache_key ); |
1688 | 1692 | |
1689 | | $bp = buddypress(); |
| 1693 | if ( false === $count || true === $skip_cache ) { |
| 1694 | $members = groups_get_group_members( |
| 1695 | array( |
| 1696 | 'group_id' => $group_id, |
| 1697 | 'exclude_banned' => true, |
| 1698 | 'exclude_admins_mods' => false, |
| 1699 | 'type' => 'active', |
| 1700 | ) |
| 1701 | ); |
| 1702 | |
| 1703 | $count = $members['count'] ?? 0; |
1690 | 1704 | |
1691 | | return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ); |
| 1705 | groups_update_groupmeta( $group_id, $cache_key, (int) $count ); |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * Filters the total member count for a group. |
| 1710 | * |
| 1711 | * @since 10.0.0 |
| 1712 | * |
| 1713 | * @param int $count Total member count for group. |
| 1714 | * @param int $group_id The ID of the group. |
| 1715 | */ |
| 1716 | return apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id ); |
1692 | 1717 | } |
1693 | 1718 | |
1694 | 1719 | /** |
diff --git src/bp-groups/classes/class-bp-groups-list-table.php src/bp-groups/classes/class-bp-groups-list-table.php
index be3864b32..4a102b3fd 100644
|
|
class BP_Groups_List_Table extends WP_List_Table { |
701 | 701 | * Markup for the Number of Members column. |
702 | 702 | * |
703 | 703 | * @since 1.7.0 |
| 704 | * @since 10.0.0 Updated to use `groups_get_total_member_count`. |
704 | 705 | * |
705 | 706 | * @param array $item Information about the current row. |
706 | 707 | */ |
707 | 708 | public function column_members( $item = array() ) { |
708 | | $count = groups_get_groupmeta( $item['id'], 'total_member_count' ); |
| 709 | $count = groups_get_total_member_count( absint( $item['id'] ) ); |
709 | 710 | |
710 | 711 | /** |
711 | 712 | * Filters the markup for the number of Members column. |
diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php
index a442ce932..d26de99ea 100644
|
|
class BP_Groups_Member { |
465 | 465 | /** Static Methods ****************************************************/ |
466 | 466 | |
467 | 467 | /** |
468 | | * Refresh the total_group_count for a user. |
| 468 | * Refresh the `total_group_count` for a user. |
469 | 469 | * |
470 | 470 | * @since 1.8.0 |
471 | 471 | * |
… |
… |
class BP_Groups_Member { |
477 | 477 | } |
478 | 478 | |
479 | 479 | /** |
480 | | * Refresh the total_member_count for a group. |
| 480 | * Refresh the `total_member_count` for a group. |
| 481 | * |
| 482 | * The request skip the current cache so that we always grab the lastest total count. |
481 | 483 | * |
482 | 484 | * @since 1.8.0 |
| 485 | * @since 10.0.0 Updated to use `BP_Groups_Group::get_total_member_count` |
483 | 486 | * |
484 | 487 | * @param int $group_id ID of the group. |
485 | | * @return bool|int True on success, false on failure. |
| 488 | * @return int Count of confirmed members for the group. |
486 | 489 | */ |
487 | 490 | public static function refresh_total_member_count_for_group( $group_id ) { |
488 | | return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) ); |
| 491 | return (int) BP_Groups_Group::get_total_member_count( $group_id, true ); |
489 | 492 | } |
490 | 493 | |
491 | 494 | /** |
… |
… |
class BP_Groups_Member { |
495 | 498 | * |
496 | 499 | * @param int $user_id ID of the user. |
497 | 500 | * @param int $group_id ID of the group. |
498 | | * @return True on success, false on failure. |
| 501 | * @return bool True on success, false on failure. |
499 | 502 | */ |
500 | 503 | public static function delete( $user_id, $group_id ) { |
501 | 504 | global $wpdb; |
… |
… |
class BP_Groups_Member { |
510 | 513 | */ |
511 | 514 | do_action( 'bp_groups_member_before_delete', $user_id, $group_id ); |
512 | 515 | |
513 | | $bp = buddypress(); |
| 516 | $bp = buddypress(); |
514 | 517 | $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) ); |
515 | 518 | |
516 | 519 | // Update the user's group count. |
… |
… |
class BP_Groups_Member { |
529 | 532 | */ |
530 | 533 | do_action( 'bp_groups_member_after_delete', $user_id, $group_id ); |
531 | 534 | |
532 | | return $remove; |
| 535 | return (bool) $remove; |
533 | 536 | } |
534 | 537 | |
535 | 538 | /** |
diff --git src/bp-groups/screens/single/members.php src/bp-groups/screens/single/members.php
index 6174fd604..fccce2902 100644
|
|
|
14 | 14 | */ |
15 | 15 | function groups_screen_group_members() { |
16 | 16 | |
17 | | if ( !bp_is_single_item() ) |
| 17 | if ( ! bp_is_single_item() ) { |
18 | 18 | return false; |
| 19 | } |
19 | 20 | |
20 | 21 | $bp = buddypress(); |
21 | 22 | |
22 | | // Refresh the group member count meta. |
23 | | groups_update_groupmeta( $bp->groups->current_group->id, 'total_member_count', groups_get_total_member_count( $bp->groups->current_group->id ) ); |
24 | | |
25 | 23 | /** |
26 | 24 | * Fires before the loading of a group's Members page. |
27 | 25 | * |
… |
… |
function groups_screen_group_members() { |
39 | 37 | * @param string $value Path to a group's Members template. |
40 | 38 | */ |
41 | 39 | bp_core_load_template( apply_filters( 'groups_template_group_members', 'groups/single/home' ) ); |
42 | | } |
43 | | No newline at end of file |
| 40 | } |
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
index 4b7533f55..8a852a9ff 100644
|
|
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
184 | 184 | $g = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
185 | 185 | |
186 | 186 | groups_join_group( $g, $u2 ); |
187 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 187 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
193 | 193 | */ |
194 | 194 | public function test_total_member_count_groups_leave_group() { |
195 | 195 | $u1 = self::factory()->user->create(); |
| 196 | $u2 = self::factory()->user->create(); |
196 | 197 | $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
197 | | groups_join_group( $g1, $u1 ); |
198 | 198 | |
199 | | groups_leave_group( $g1, $u1 ); |
200 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 199 | groups_join_group( $g1, $u2 ); |
| 200 | |
| 201 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 202 | |
| 203 | groups_leave_group( $g1, $u2 ); |
| 204 | |
| 205 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
201 | 206 | } |
202 | 207 | |
203 | 208 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
214 | 219 | $this->set_current_user( $u1 ); |
215 | 220 | buddypress()->is_item_admin = true; |
216 | 221 | |
| 222 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 223 | |
217 | 224 | groups_ban_member( $u2, $g1 ); |
218 | 225 | |
219 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 226 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
220 | 227 | } |
221 | 228 | |
222 | 229 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
235 | 242 | |
236 | 243 | groups_ban_member( $u2, $g1 ); |
237 | 244 | |
| 245 | $this->assertEquals( 1, groups_get_total_member_count( $g1 ) ); |
| 246 | |
238 | 247 | groups_unban_member( $u2, $g1 ); |
239 | 248 | |
240 | | $this->assertEquals( 2, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 249 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
241 | 250 | } |
242 | 251 | |
243 | 252 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
255 | 264 | 'send_invite' => 1, |
256 | 265 | ) ); |
257 | 266 | |
| 267 | $this->assertEquals( 1, groups_get_total_member_count( $g ) ); |
| 268 | |
258 | 269 | groups_accept_invite( $u2, $g ); |
259 | 270 | |
260 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 271 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
261 | 272 | } |
262 | 273 | |
263 | 274 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
275 | 286 | ) ); |
276 | 287 | groups_accept_membership_request( 0, $u2, $g ); |
277 | 288 | |
278 | | $this->assertEquals( 2, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 289 | $this->assertEquals( 2, groups_get_total_member_count( $g ) ); |
279 | 290 | } |
280 | 291 | |
281 | 292 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
294 | 305 | |
295 | 306 | groups_remove_member( $u2, $g1 ); |
296 | 307 | |
297 | | $this->assertEquals( 1, groups_get_groupmeta( $g1, 'total_member_count' ) ); |
| 308 | $this->assertEquals( 1, groups_get_total_member_count( $g1 )); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @group total_member_count |
| 313 | * @group groups_remove_member |
| 314 | */ |
| 315 | public function test_total_member_count_groups_delete_member() { |
| 316 | $u1 = self::factory()->user->create(); |
| 317 | $u2 = self::factory()->user->create(); |
| 318 | $u3 = self::factory()->user->create(); |
| 319 | $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) ); |
| 320 | |
| 321 | groups_join_group( $g1, $u2 ); |
| 322 | groups_join_group( $g1, $u3 ); |
| 323 | |
| 324 | $this->set_current_user( $u1 ); |
| 325 | |
| 326 | $this->assertEquals( 3, groups_get_total_member_count( $g1 ) ); |
| 327 | $this->assertEquals( 3, BP_Groups_Group::get_total_member_count( $g1 ) ); |
| 328 | |
| 329 | // Delete user. |
| 330 | wp_delete_user( $u2 ); |
| 331 | |
| 332 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
| 333 | $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) ); |
298 | 334 | } |
299 | 335 | |
300 | 336 | /** |
… |
… |
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
313 | 349 | 'date_created' => bp_core_current_time(), |
314 | 350 | ) ); |
315 | 351 | |
316 | | $this->assertEquals( 1, groups_get_groupmeta( $g, 'total_member_count' ) ); |
| 352 | $this->assertEquals( 1, groups_get_total_member_count( $g ) ); |
317 | 353 | } |
318 | 354 | |
319 | 355 | /** |