LLMS_Student::get_enrollment_status( int $product_id, bool $use_cache = true )
Get the current enrollment status of a student for a particular product
Parameters Parameters
- $product_id
-
(int) (Required) WP Post ID of a Course, Section, Lesson, or Membership
- $use_cache
-
(bool) (Optional) If true, returns cached data if available, if false will run a db query
Default value: true
Return Return
(false|string) When no enrollment status exists, returns false. Otherwise returns the enrollment status as a string.
Source Source
File: includes/models/model.llms.student.php
public function get_enrollment_status( $product_id, $use_cache = true ) {
$status = false;
$product_type = get_post_type( $product_id );
if ( ! in_array( $product_type, llms_get_enrollable_status_check_post_types(), true ) ) {
/* This filter is documented at the end of this method. */
return apply_filters( 'llms_get_enrollment_status', $status, $this->get_id(), $product_id, $use_cache );
}
// Get course ID if we're looking at a lesson or section.
if ( in_array( $product_type, array( 'section', 'lesson' ), true ) ) {
$llms_post = llms_get_post( $product_id );
if ( $llms_post ) {
$product_id = $llms_post->get( 'parent_course' );
}
}
if ( $use_cache ) {
$status = $this->cache_get( sprintf( 'enrollment_status_%d', $product_id ) );
}
/**
* After checking the cache, $status will be:
* + `false` if there was nothing in the cache or the function was instructed to not use the cache: Query the database to get the status.
* + a string if there was a status: No need to query the database.
* + `null` if there's no status: No need to query the database.
*/
if ( false === $status ) {
global $wpdb;
// Get the most recent recorded status.
$status = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value FROM {$wpdb->prefix}lifterlms_user_postmeta
WHERE meta_key = '_status' AND user_id = %d AND post_id = %d
ORDER BY updated_date DESC, meta_id DESC LIMIT 1;",
array( $this->get_id(), $product_id )
)
);
// Cache the data: `null` will be stored if the student has no status.
$this->cache_set( sprintf( 'enrollment_status_%d', $product_id ), $status );
}
// Don't return `null` values from the database.
$status = $status ? $status : false;
/**
* Filter a user's enrollment status for a specific post.
*
* Note that if a value is modified by this filter the modified value is *not* cached. Therefore you should
* consider implementing caching of your modified value which matches the caching implemented by this method
* so that the modified value obeys the default caching behavior.
*
* @since Unknown
*
* @param false|string $status When no enrollment status exists, returns `false`. Otherwise returns the
* enrollment status as a string.
* @param int $user_id WP_User ID of the student
* @param int $product_id WP_Post ID of the post used to check the enrollment status.
* @param boolean $use_cache Whether or not to use the local cache.
*/
return apply_filters( 'llms_get_enrollment_status', $status, $this->get_id(), $product_id, $use_cache );
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 5.7.0 | Replaced the call to the deprecated LLMS_Lesson::get_parent_course() method with LLMS_Lesson::get( 'parent_course' ). |
| 4.4.1 | Moved filter llms_user_enrollment_status_allowed_post_types to function llms_get_enrollable_status_check_post_types(). |
| 4.18.0 | Added a tie-breaker when there are multiple enrollment statuses with the same date & time. |
| 3.37.9 | Added filter llms_user_enrollment_status_allowed_post_types. |
| 3.17.0 | Unknown. |
| 3.0.0 | Introduced. |