Skip to content

Commit

Permalink
Merge pull request #65 from ipokkel/add-to-groups-fix
Browse files Browse the repository at this point in the history
Add to groups fix
  • Loading branch information
ideadude authored Apr 7, 2020
2 parents 9287ede + 56d9358 commit 763dce7
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions includes/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,60 @@
function pmpro_bp_set_member_groups( $level_id, $user_id, $cancel_level ) {

// Make sure Groups are activated.
if(!function_exists('groups_accept_invite')) return;

if ( ! function_exists( 'groups_create_group' ) ) {
return;
}

$pmpro_bp_options = pmpro_bp_get_user_options( $user_id );

if( !empty( $cancel_level ) ) {
if ( ! empty( $cancel_level ) ) {
$pmpro_bp_old_level_options = pmpro_bp_get_level_options( $cancel_level );
} else {
$pmpro_bp_old_level_options = pmpro_bp_get_user_old_level_options( $user_id );
}

// Add to groups
$old_groups = $pmpro_bp_old_level_options['pmpro_bp_group_automatic_add'];
$new_groups = $pmpro_bp_options['pmpro_bp_group_automatic_add'];

if( !empty( $old_groups ) ) {
foreach($old_groups as $group_id) {
if ( ! empty( $old_groups ) ) {
foreach ( $old_groups as $group_id ) {
groups_leave_group( $group_id, $user_id );
}
}

if( !empty( $new_groups ) ) {
foreach($new_groups as $group_id) {
groups_accept_invite( $user_id, $group_id );
if ( ! empty( $new_groups ) ) {
foreach ( $new_groups as $group_id ) {
groups_join_group( $group_id, $user_id );
}
}

// Invite to groups
$old_groups_invite = $pmpro_bp_old_level_options['pmpro_bp_group_can_request_invite'];
$new_groups_invite = $pmpro_bp_options['pmpro_bp_group_can_request_invite'];

if ( ! empty( $old_groups_invite ) ) {
foreach ( $old_groups_invite as $group_id ) {
groups_uninvite_user( $user_id, $group_id );
// remove user from group as well
groups_leave_group( $group_id, $user_id );
}
}

if ( ! empty( $new_groups_invite ) ) {
foreach ( $new_groups_invite as $group_id ) {
$group = groups_get_group( array( 'group_id' => $group_id ) );
groups_invite_user(
array(
'user_id' => $user_id,
'group_id' => $group_id,
'inviter_id' => $group->creator_id,
'date_modified' => bp_core_current_time(),
'send_invite' => 1,
)
);
}
}

}
add_action( 'pmpro_after_change_membership_level', 'pmpro_bp_set_member_groups', 10, 3 );

0 comments on commit 763dce7

Please sign in to comment.