BuddyDev

Search

[Resolved] Auto join groups – cron job

  • Participant
    Level: Initiated
    Posts: 2
    Darshan Makadia on #39472

    Hi Brajesh, any updates on the code example? Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #39478

    Hi Darshan,

    Thank you for the patience.

    1. Please upgrade to BuddyPress Auto Join Group 1.0.4
    2. After that, Please download and activate this https://github.com/buddydev/buddypress-auto-join-groups-sync

    That will allow you synchronising groups on daily basis.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 19
    Darshan on #39483

    Thanks Brajesh

    I have upgraded the plugin and also installed the sync plugin. I can now see the cron job too. One more question re the conditions for the auto lists. I have a group id field in user profile which will contain comma separated id’s. Let’s assume a user belongs to group 1,2 and I have an auto-list created. So, under conditions I selected the group id field and operator as ‘In’ and in the 3rd box I enter 1 for the auto-list of group 1 and 2 for the auto-list of group 2. Is this the correct way to setup? Trying that but it’s not working

  • Participant
    Level: Initiated
    Posts: 19
    Darshan on #39484

    Other question – is the sync suppose to remove user from a group if they don’t match the condition?

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #39497

    Hi Darshan,
    Thank you.

    1. Yes,sync removes users from groups too. It will not remove if the matched groups list is empty.

    2. We don’t have any condition that allows you to use id as group id. So, that will not work.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 19
    Darshan on #39546

    Hi Brajesh,

    I meant using the ‘IN’ condition. I have a xprofile field called group which will be populated in identity manager for each user. So, a user can be part of group 1 and 2. How do I use the IN condition?

    See below screenshot
    https://www.coopappdev.com/wp-content/uploads/2021/07/sync_in_condition.jpg

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #39557

    Hi Darshan
    Thank you for the reply.

    This is not how this plugin works. There are ways to make this condition though.

    Conditions:- These are used to match user profile. If a user matches these conditions, they will get added to the predefined list of groups you have selected.

    In your case, if you want to use the condition like above, you will need to create multiple list. For each list, you will select relevant groups.

    or example, a list containing condition employee id IN 1,2, you will select group 1 and 2.

    This is different from what you are expecting. I believe your expectation is to use the group ids from profile field and sync to it.

    It can be achieved much easier with custom code. Please allow us to post the code(Ravi or I will share the code in next 24 hours).

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 19
    Darshan on #39565

    Thanks

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2933
    Ravi on #39610

    Hello Darshan,

    Please use the following code. This code will sync the user’s group based on the group field id. Make sure to replace the group field with your field id.

    
    
    /**
     * Sync user's groups
     *
     * @param int   $user_id       User id.
     * @param array $new_group_ids New group ids.
     */
    function buddydev_sync_user_groups( $user_id, $new_group_ids ) {
    
    	if ( ! $user_id || ! $new_group_ids ) {
    		return;
    	}
    
    	$user_group              = groups_get_user_groups( $user_id );
    	$existing_user_group_ids = (array) $user_group['groups'];
    
    	$removable_group_ids = array_diff( $existing_user_group_ids, $new_group_ids );
    	$new_join_group_ids  = array_diff( $new_group_ids, $existing_user_group_ids );
    
    	foreach ( $removable_group_ids as $group_id ) {
    		groups_leave_group( $group_id, $user_id );
    	}
    
    	foreach ( $new_join_group_ids as $group_id ) {
    		groups_join_group( $group_id, $user_id );
    	}
    }
    
    /**
     * Sync user's groups on field save.
     */
    add_action( 'xprofile_data_after_save', function ( BP_XProfile_ProfileData $object ) {
    	$field_id = $object->field_id;
    
    	// Replace field id with yours field id.
    	$group_field_id = 5;
    
    	// If not our field id return.
    	if ( $group_field_id != $field_id ) {
    		return;
    	}
    
    	$new_group_ids = wp_parse_id_list( $object->value );
    
    	if ( empty( $new_group_ids ) ) {
    		// should we remove existing groups?
    		// let us avoid that for now.
    		return;
    	}
    
    	buddydev_sync_user_groups( $object->user_id, $new_group_ids );
    } );
    
    

    Please do let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 19
    Darshan on #39634

    Hi Ravi,

    Thanks for the code. I have done some test and it’s working. There is one scenario which is not working. When a user is part of 2 groups then the user group id field will have 1,2 (comma separated values for group id’s). It does work when the user is part of 1 group only.

    Any thoughts?

    Thanks

You must be logged in to reply to this topic.

This topic is: resolved