LLMS_Student::unenroll( int $product_id, string $trigger = 'any', string $new_status = 'expired' )

Remove a student from a LifterLMS course or membership


Description Description

See also See also


Top ↑

Parameters Parameters

$product_id

(int) (Required) WordPress Post ID of the course or membership.

$trigger

(string) (Optional) Only remove the student if the original enrollment trigger matches the submitted value. Passing any will remove regardless of enrollment trigger.

Default value: 'any'

$new_status

(string) (Optional) the value to update the new status with after removal is complete.

Default value: 'expired'


Top ↑

Return Return

(boolean)


Top ↑

Source Source

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

	/**
	 * Remove a student from a membership level.
	 *
	 * @since 2.7
	 * @since 3.7.5 Unknown.
	 * @since 3.36.2 Added the $delete parameter, that will allow related courses enrollments data deletion.
	 *
	 * @param  int     $membership_id WP Post ID of the membership.
	 * @param  string  $status        Optional. Status to update the removal to. Default is `expired`.
	 * @param  boolean $delete        Optional. Status to update the removal to. Default is `false`.
	 * @return void
	 */
	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 ) {
				if ( ! $delete ) {
					$this->unenroll( $course_id, 'membership_' . $membership_id, $status );
				} else {
					$this->delete_enrollment( $course_id, 'membership_' . $membership_id );
				}
			}
		}

	}

	/**
	 * Remove a student from a LifterLMS course or membership
	 *
	 * @since 3.0.0
	 * @since 3.26.0 Unknown.
	 * @since 3.37.9 Update to accommodate custom post type enrollments added through new filters.
	 *               Marked action `llms_user_removed_from_membership_level` as deprecated, use `llms_user_removed_from_membership` instead.
	 * @since 6.0.0 Removed the deprecated `llms_user_removed_from_membership_level` action hook
	 *              and moved the call to `LLMS_Student::remove_membership_level()` to be before triggering the
	 *              `llms_user_removed_from_{$post_type}` action hook.
	 *
	 * @see llms_unenroll_student()
	 *
	 * @param  int    $product_id WordPress Post ID of the course or membership.
	 * @param  string $trigger    Only remove the student if the original enrollment trigger matches the submitted value.
	 *                            Passing `any` will remove regardless of enrollment trigger.
	 * @param  string $new_status the value to update the new status with after removal is complete.
	 * @return bool
	 */
	public function unenroll( $product_id, $trigger = 'any', $new_status = 'expired' ) {

		// Can only unenroll those that are a currently enrolled.
		if ( ! $this->is_enrolled( $product_id, 'all', false ) ) {
			return false;
		}

		// Assume we can't unenroll.
		$update = false;

		// If trigger is "any" we'll unenroll regardless of the trigger.
		if ( 'any' === $trigger ) {

			$update = true;

		} else {

			$enrollment_trigger = $this->get_enrollment_trigger( $product_id );

			// No enrollment trigger exists b/c pre 3.0.0 enrollment, unenroll the user as if it was an 'any' trigger.
			if ( ! $enrollment_trigger ) {



Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Removed the deprecated llms_user_removed_from_membership_level action hook and moved the call to LLMS_Student::remove_membership_level() to be before triggering the llms_user_removed_from_{$post_type} action hook.
3.37.9 Update to accommodate custom post type enrollments added through new filters. Marked action llms_user_removed_from_membership_level as deprecated, use llms_user_removed_from_membership instead.
3.26.0 Unknown.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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