Browse: Home / Snippets /

Unenroll the User from All common Groups when the Course access is expired

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.


/**
 * Unenroll the User from All common Groups when the Course access is expired.
 *
 * @param int $user_id   This User.
 * @param int $course_id This Course ID.
 */

add_action( 'learndash_user_course_access_expired', function( $user_id = 0, $course_id = 0 ) {
$user_id   = absint( $user_id );

$course_id = absint( $course_id );
if ( ( ! empty( $user_id ) ) && ( ! empty( $course_id ) ) ) {
    // Get all the Groups the User is enrolled in.
    $user_group_ids = learndash_get_users_group_ids( $user_id );
    $user_group_ids = array_map( 'absint', $user_group_ids );

    // Get all the Groups the Course is enrolled in.
    $course_group_ids = learndash_get_course_groups( $course_id );
    $course_group_ids = array_map( 'absint', $course_group_ids );

    if ( ( ! empty( $user_group_ids ) ) && ( ! empty( $course_group_ids ) ) ) {
        // Get the common Groups that both the User and Course are enrolld in.
        $common_group_ids = array_intersect( $user_group_ids, $course_group_ids );
            
        if ( ! empty( $common_group_ids ) ) {
            foreach ( $common_group_ids as $group_id ) {
                // For each common Group...

                // First check if the Group checkbox "Enable automatic group enrollment when a user enrolls into any associated group course" is set.
                $group_auto_enroll_all_courses = get_post_meta( $group_id, 'ld_auto_enroll_group_courses', true );
                
                if ( 'yes' === $group_auto_enroll_all_courses ) {
                    // For each comment Group unenroll the User.
                    ld_update_group_access( $user_id, $group_id, true );
                } else {
                    // Else check if the Course is one of the auto-enroll courses.
                    $group_auto_enroll_courses_ids = get_post_meta( $group_id, 'ld_auto_enroll_group_course_ids', true );
                    $group_auto_enroll_courses_ids = array_map( 'absint', $group_auto_enroll_courses_ids );

                    if ( ( ! empty( $group_auto_enroll_courses_ids ) ) && ( in_array( $course_id, $group_auto_enroll_courses_ids, true ) ) ) {
                
                        // For each comment Group unenroll the User.
                        ld_update_group_access( $user_id, $group_id, true );
                    }
                }
            }
        }
    }
}
}, 20, 2 );