BuddyDev

Search

[Resolved] Auto join groups – cron job

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

    Hi Darshan,
    The plugin works fine with the Hidden Groups. If you are using the plugin, are you having any issue adding the hidden group to the list?

    Regards
    Brajesh

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

    Hi Brajesh,

    It auto-subscribes but it doesn’t robe the user from the group assigned before if they no longer belong to a group.

    Thanks

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

    Hi Darshan,
    It seems you are referring to the custom code section. Please allow me to check and get back to you by tomorrow.

    Regards
    Brajesh

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

    Hi Darshan,
    You were right. The code from Ravi used an API function that did not return hidden group if the user is context was not logged.

    Here is the updated code. Please make sure to update the profile field id with your own.

    
    
    /**
     * Retrieves all group ids of which the user is a member or has requested membership or is banned.
     *
     * @param int $user_id user id.
     * @param int $limit per page.
     * @param int $page page number.
     *
     * @return array
     */
    function bd_get_user_group_ids( $user_id, $limit = false, $page = false ) {
    	global $wpdb;
    
    	$pag_sql = '';
    
    	if ( $limit && $page ) {
    		$pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
    	}
    
    	$bp = buddypress();
    
    	$group_sql = $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d {$pag_sql}", $user_id );
    
    	return $wpdb->get_col( $group_sql );
    }
    
    /**
     * 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 || ! function_exists( 'groups_leave_group' ) ) {
    		return;
    	}
    
    	$existing_user_group_ids = bd_get_user_group_ids($user_id );
    
    	$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 );
    } );
    
    

    This code does not need the the Auto group join plugin. You can still use the plugin for other purpose.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved