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


Top ↑

Return Return

(false|string) When no enrollment status exists, returns false. Otherwise returns the enrollment status as a string.


Top ↑

Source Source

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

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
        // Get the oldest recorded Enrollment date.
        $res = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT updated_date FROM {$wpdb->prefix}lifterlms_user_postmeta WHERE meta_key = %s AND user_id = %d AND post_id = %d ORDER BY updated_date DESC LIMIT 1",
                array( $key, $this->get_id(), $product_id )
            )
        );
 
        $this->cache_set( $cache_key, $res );
 
    }
 
    return ( $res ) ? date_i18n( $format, strtotime( $res ) ) : false;
 
}
 
/**
 * Get the current enrollment status of a student for a particular product
 *
 * @since 3.0.0
 * @since 3.17.0 Unknown.
 * @since 3.37.9 Added filter `llms_user_enrollment_status_allowed_post_types`.
 * @since 4.4.1 Moved filter `llms_user_enrollment_status_allowed_post_types` to function `llms_get_enrollable_status_check_post_types()`.
 * @since 4.18.0 Added a tie-breaker when there are multiple enrollment statuses with the same date & time.
 * @since 5.7.0 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
 *
 * @param  int  $product_id  WP Post ID of a Course, Section, Lesson, or Membership
 * @param  bool $use_cache   If true, returns cached data if available, if false will run a db query
 * @return false|string      When no enrollment status exists, returns `false`. Otherwise returns the
 *                           enrollment status as a string.
 */
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


Top ↑

Changelog 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.

Top ↑

User Contributed Notes User Contributed Notes

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