LLMS_Student::unenroll( int $product_id, string $trigger = 'any', string $new_status = 'expired' )
Remove a student from a LifterLMS course or membership
Contents
Description Description
See also See also
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.<br> Passing
anywill 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'
Return Return
(bool)
Source Source
File: includes/models/model.llms.student.php
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 ) {
/**
* This filter allows customization of enrollments created prior to version 3.0.0
*
* Prior to 3.0.0 enrollments did not track an enrollment trigger so any unenrollments
* performed on an enrollment in this state will automatically be unenrolled.
*
* Returning `false` will prevent unenrollments against enrollments which don't have
* an enrollment trigger.
*
* @since 3.0.0
*
* @param bool $allow_unenrollment If true, allows unenrollment, otherwise prevents unenrollment.
*/
$update = apply_filters( 'lifterlms_legacy_unenrollment_action', true );
} elseif ( $enrollment_trigger === $trigger ) {
$update = true;
}
}
// Update if we can.
if ( $update ) {
// Update enrollment for the product.
if ( $this->insert_status_postmeta( $product_id, $new_status ) ) {
// Update the cache.
$this->cache_set( sprintf( 'enrollment_status_%d', $product_id ), $new_status );
$this->cache_delete( sprintf( 'date_enrolled_%d', $product_id ) );
$this->cache_delete( sprintf( 'date_updated_%d', $product_id ) );
$post_type = str_replace( 'llms_', '', get_post_type( $product_id ) );
// Run legacy action and trigger cascading unenrollments for membership relationships.
if ( 'membership' === $post_type ) {
// Users should be unenrolled from all courses they accessed through this membership.
$this->remove_membership_level( $product_id, $new_status );
}
/**
* Trigger an action immediately following user unenrollment
*
* The dynamic portion of this hook, `{$post_type}` corresponds to the post type of the
* `$product_id`. Note that any post type prefixed with `llms_` is stripped. For example
* when triggered by a membership (`llms_membership`) the hook will be `llms_user_removed_from_membership`.
*
* @since 3.37.9
*
* @param int $user_id WP_User ID of the student
* @param int $product_id WP_Post ID of the product.
* @param string $trigger Enrollment trigger.
* @param string $new_status New enrollment status of the student after the unenrollment has taken place.
*/
do_action( "llms_user_removed_from_{$post_type}", $this->get_id(), $product_id, $trigger, $new_status );
return true;
}
}
// Update was prevented.
return false;
}
Expand full source code Collapse full source code View on GitHub
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.<br> 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. |