diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
index 61a732aa6..2fbd1c79a 100644
|
|
function bp_get_group( $group = false ) { |
114 | 114 | |
115 | 115 | if ( $group instanceof BP_Groups_Group ) { |
116 | 116 | $group_obj = $group; |
117 | | } elseif ( is_string( $group ) ) { |
118 | | $group_obj = bp_get_group_by( 'slug', $group ); |
119 | | } elseif ( is_numeric( $group ) ) { |
120 | | $group_obj = bp_get_group_by( 'id', $group ); |
121 | | } elseif ( isset( $groups_template->group ) && is_object( $groups_template->group ) ) { |
| 117 | |
| 118 | // Nothing requested? Let's use the current Group of the Groups Loop, if available. |
| 119 | } elseif ( ! $group && isset( $groups_template->group ) && is_object( $groups_template->group ) ) { |
122 | 120 | $group_obj = $groups_template->group; |
| 121 | } else { |
| 122 | $current_group = null; |
| 123 | |
| 124 | // Let's get the current group if we can. |
| 125 | if ( did_action( 'bp_groups_set_current_group' ) ) { |
| 126 | $current_group = groups_get_current_group(); |
| 127 | } |
| 128 | |
| 129 | $field = ''; |
| 130 | if ( is_string( $group ) ) { |
| 131 | $field = 'slug'; |
| 132 | } elseif ( is_numeric( $group ) ) { |
| 133 | $field = 'id'; |
| 134 | $group = (int) $group; |
| 135 | } |
| 136 | |
| 137 | // Let's use the current Group if it matches with the requested field value. |
| 138 | if ( isset( $current_group->{$field} ) && $current_group->{$field} === $group ) { |
| 139 | $group_obj = $current_group; |
| 140 | } else { |
| 141 | $group_obj = bp_get_group_by( $field, $group ); |
| 142 | } |
123 | 143 | } |
124 | 144 | |
125 | 145 | return $group_obj; |
… |
… |
function groups_get_id_by_previous_slug( $group_slug ) { |
572 | 592 | * @return bool True on success, false on failure. |
573 | 593 | */ |
574 | 594 | function groups_leave_group( $group, $user_id = 0 ) { |
575 | | |
576 | | $current_group = null; |
577 | | |
578 | | if ( is_numeric( $group ) ) { |
579 | | $current_group = groups_get_current_group(); |
580 | | |
581 | | if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === (int) $group ) { |
582 | | $group = $current_group; |
583 | | } |
584 | | } |
585 | | |
586 | | if ( ! isset( $group->id ) ) { |
587 | | $group = bp_get_group( $group ); |
588 | | } |
| 595 | // Get the group object. |
| 596 | $group = bp_get_group( $group ); |
589 | 597 | |
590 | 598 | if ( empty( $group->id ) ) { |
591 | 599 | return false; |
… |
… |
function groups_leave_group( $group, $user_id = 0 ) { |
636 | 644 | * @return bool True on success, false on failure. |
637 | 645 | */ |
638 | 646 | function groups_join_group( $group, $user_id = 0 ) { |
639 | | |
640 | | $current_group = null; |
641 | | |
642 | | if ( is_numeric( $group ) ) { |
643 | | $current_group = groups_get_current_group(); |
644 | | |
645 | | if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === (int) $group ) { |
646 | | $group = $current_group; |
647 | | } |
648 | | } |
| 647 | // Get the group object. |
| 648 | $group = bp_get_group( $group ); |
649 | 649 | |
650 | 650 | if ( ! isset( $group->id ) ) { |
651 | 651 | $group = bp_get_group( $group ); |
… |
… |
function groups_join_group( $group, $user_id = 0 ) { |
726 | 726 | * @return bool False on failure. |
727 | 727 | */ |
728 | 728 | function groups_update_last_activity( $group = 0 ) { |
729 | | |
730 | | $current_group = null; |
731 | | |
732 | | if ( is_numeric( $group ) ) { |
733 | | $group_id = (int) $group; |
734 | | $current_group = groups_get_current_group(); |
735 | | |
736 | | if ( $current_group instanceof BP_Groups_Group && (int) $current_group->id === $group_id ) { |
737 | | $group = $current_group; |
738 | | } |
739 | | } |
740 | | |
741 | | if ( ! isset( $group->id ) ) { |
742 | | $group = bp_get_group( $group ); |
743 | | } |
| 729 | // Get the group object. |
| 730 | $group = bp_get_group( $group ); |
744 | 731 | |
745 | 732 | if ( empty( $group->id ) ) { |
746 | 733 | return false; |
diff --git src/bp-groups/classes/class-bp-groups-component.php src/bp-groups/classes/class-bp-groups-component.php
index c8c9b7787..8d4d35eb5 100644
|
|
class BP_Groups_Component extends BP_Component { |
265 | 265 | $this->current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) ); |
266 | 266 | } |
267 | 267 | |
| 268 | // Make sure the Group ID is an integer. |
| 269 | $this->current_group->id = (int) $this->current_group->id; |
| 270 | |
268 | 271 | // When in a single group, the first action is bumped down one because of the |
269 | 272 | // group name, so we need to adjust this and set the group name to current_item. |
270 | 273 | $bp->current_item = bp_current_action(); |
… |
… |
class BP_Groups_Component extends BP_Component { |
280 | 283 | |
281 | 284 | // If the user is not an admin, check if they are a moderator. |
282 | 285 | if ( ! bp_is_item_admin() ) { |
283 | | bp_update_is_item_mod ( groups_is_user_mod ( bp_loggedin_user_id(), $this->current_group->id ), 'groups' ); |
| 286 | bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $this->current_group->id ), 'groups' ); |
284 | 287 | } |
285 | 288 | |
286 | 289 | // Check once if the current group has a custom front template. |
287 | 290 | $this->current_group->front_template = bp_groups_get_front_template( $this->current_group ); |
288 | 291 | |
| 292 | /** |
| 293 | * Fires once the `current_group` global is fully set. |
| 294 | * |
| 295 | * @since 10.0.0 |
| 296 | * |
| 297 | * @param BP_Groups_Group|object $current_group The current group object. |
| 298 | */ |
| 299 | do_action_ref_array( 'bp_groups_set_current_group', array( $this->current_group ) ); |
| 300 | |
289 | 301 | // Initialize the nav for the groups component. |
290 | 302 | $this->nav = new BP_Core_Nav( $this->current_group->id ); |
291 | 303 | |
diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
index e82efd86e..208378547 100644
|
|
class BP_Tests_Groups_Functions extends BP_UnitTestCase { |
330 | 330 | groups_join_group( $g1, $u2 ); |
331 | 331 | groups_join_group( $g1, $u3 ); |
332 | 332 | |
333 | | $this->set_current_user( $u1 ); |
334 | | |
335 | 333 | $this->assertEquals( 3, groups_get_total_member_count( $g1 ) ); |
336 | 334 | $this->assertEquals( 3, BP_Groups_Group::get_total_member_count( $g1 ) ); |
337 | 335 | |
| 336 | add_filter( 'bp_remove_user_data_on_delete_user_hook', '__return_true' ); |
| 337 | |
338 | 338 | // Delete user. |
339 | 339 | wp_delete_user( $u2 ); |
340 | 340 | |
| 341 | remove_filter( 'bp_remove_user_data_on_delete_user_hook', '__return_true' ); |
| 342 | |
341 | 343 | $this->assertEquals( 2, groups_get_total_member_count( $g1 ) ); |
342 | 344 | $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) ); |
343 | 345 | } |
diff --git tests/phpunit/testcases/groups/functions/get-group.php tests/phpunit/testcases/groups/functions/get-group.php
index c86240832..0ae4c31d0 100644
|
|
class BP_Tests_Get_Groups_Param extends BP_UnitTestCase { |
22 | 22 | parent::tearDown(); |
23 | 23 | } |
24 | 24 | |
| 25 | /** |
| 26 | * @group bp_get_group |
| 27 | */ |
25 | 28 | public function test_bp_get_group_with_no_group() { |
26 | 29 | $this->assertFalse( bp_get_group() ); |
27 | 30 | $this->assertFalse( bp_get_group_by( 'id', 0 ) ); |
28 | 31 | } |
29 | 32 | |
| 33 | /** |
| 34 | * @group bp_get_group |
| 35 | */ |
30 | 36 | public function test_bp_get_group_with_id() { |
31 | 37 | $g = $this->factory->group->create(); |
32 | 38 | |
… |
… |
class BP_Tests_Get_Groups_Param extends BP_UnitTestCase { |
35 | 41 | $this->assertSame( $g, bp_get_group_by( 'ID', $g )->id ); |
36 | 42 | } |
37 | 43 | |
| 44 | /** |
| 45 | * @group bp_get_group |
| 46 | */ |
38 | 47 | public function test_bp_get_group_with_slug() { |
39 | 48 | $slug = 'test-group'; |
40 | 49 | $g = $this->factory->group->create( array( 'slug' => $slug ) ); |
… |
… |
class BP_Tests_Get_Groups_Param extends BP_UnitTestCase { |
49 | 58 | $this->assertSame( $slug, $g2->slug ); |
50 | 59 | } |
51 | 60 | |
| 61 | /** |
| 62 | * @group bp_get_group |
| 63 | */ |
52 | 64 | public function test_bp_get_group_with_object() { |
53 | 65 | $g = $this->factory->group->create_and_get(); |
54 | 66 | |
55 | 67 | $this->assertSame( $g->id, bp_get_group( $g )->id ); |
56 | 68 | } |
57 | 69 | |
| 70 | /** |
| 71 | * @group bp_get_group |
| 72 | */ |
58 | 73 | public function test_bp_get_group_from_groups_template() { |
59 | 74 | $g = $this->factory->group->create( array( 'status' => 'private' ) ); |
60 | 75 | |
61 | | // Fake the current group. |
62 | | $GLOBALS['groups_template'] = new stdClass; |
63 | | $GLOBALS['groups_template']->group = groups_get_group( $g ); |
| 76 | if ( bp_has_groups( array( 'include' => array( $g ) ) ) ) { |
| 77 | while ( bp_groups() ) { |
| 78 | bp_the_group(); |
| 79 | $group = bp_get_group(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $this->assertSame( $g, $group->id ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @group bp_get_group |
| 88 | */ |
| 89 | public function test_bp_get_group_from_current_group() { |
| 90 | $bp = buddypress(); |
| 91 | $g = $this->factory->group->create_and_get( array( 'name' => 'foo' ) ); |
| 92 | |
| 93 | // Set the current group. |
| 94 | $bp->groups->current_group = $g; |
| 95 | |
| 96 | // Change the name to check the current group was used. |
| 97 | $bp->groups->current_group->name = 'bar'; |
| 98 | |
| 99 | // Override the name |
| 100 | do_action( 'bp_groups_set_current_group' ); |
64 | 101 | |
65 | | $this->assertSame( $g, bp_get_group()->id ); |
| 102 | $this->assertSame( 'bar', bp_get_group( $g->id )->name ); |
66 | 103 | } |
67 | 104 | } |