Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Student::remove_membership_level( int $membership_id, string $status = 'expired', boolean $delete = false )

Remove a student from a membership level.


Parameters Parameters

$membership_id

(int) (Required) WP Post ID of the membership.

$status

(string) (Optional) Status to update the removal to. Default is expired.

Default value: 'expired'

$delete

(boolean) (Optional) Status to update the removal to. Default is false.

Default value: false


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/models/model.llms.student.php

	private function remove_membership_level( $membership_id, $status = 'expired', $delete = false ) {

		// Remove the user from the membership level.
		$membership_levels = $this->get_membership_levels();
		$key               = array_search( $membership_id, $membership_levels );
		if ( false !== $key ) {
			unset( $membership_levels[ $key ] );
		}
		update_user_meta( $this->get_id(), '_llms_restricted_levels', $membership_levels );

		global $wpdb;
		// Locate all enrollments triggered by this membership level.
		$q = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT post_id FROM {$wpdb->prefix}lifterlms_user_postmeta WHERE user_id = %d AND meta_key = '_enrollment_trigger' AND meta_value = %s",
				array( $this->get_id(), 'membership_' . $membership_id )
			),
			'OBJECT_K'
		); // db call ok; no-cache ok.

		$courses = array_keys( $q );

		if ( $courses ) {

			// Loop through all the courses and update the enrollment status.
			foreach ( $courses  as $course_id ) {
				// See if they should continue to have access to this course via another membership's current auto-enroll settings.
				$membership_levels    = $this->get_membership_levels();
				$unenroll_from_course = true;

				foreach ( $membership_levels as $membership_level_id ) {
					if ( $membership_id === $membership_level_id ) {
						continue;
					}

					$membership         = new LLMS_Membership( $membership_level_id );
					$autoenroll_courses = $membership->get_auto_enroll_courses();

					if ( $autoenroll_courses ) {
						if ( in_array( $course_id, $autoenroll_courses ) ) {
							// Update the enrollment trigger to the membership that is keeping them enrolled in the course.
							llms_update_user_postmeta( $this->get_id(), $course_id, '_enrollment_trigger', 'membership_' . $membership_level_id, false );

							$unenroll_from_course = false;
							break;
						}
					}
				}

				if ( ! $unenroll_from_course ) {
					continue;
				}

				if ( ! $delete ) {
					$this->unenroll( $course_id, 'membership_' . $membership_id, $status );
				} else {
					$this->delete_enrollment( $course_id, 'membership_' . $membership_id );
				}
			}
		}
	}


Top ↑

Changelog Changelog

Changelog
Version Description
3.7.5 Unknown.
3.36.2 Added the $delete parameter, that will allow related courses enrollments data deletion.
2.7 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.