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

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