llms_page_restricted( int $post_id,  $user_id = null )

Determine if content should be restricted Called during “template_include” to determine if redirects or template overrides are in order


Description Description


Parameters Parameters

$post_id

(int) (Required) WordPress Post ID of the


Top ↑

Return Return

(array) restriction check result data


Top ↑

Source Source

File: includes/functions/llms.functions.access.php

function llms_page_restricted( $post_id, $user_id = null ) {

	$results = array(
		'content_id' => $post_id,
		'is_restricted' => false,
		'reason' => 'accessible',
		'restriction_id' => 0,
	);

	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	$student = false;
	if ( $user_id ) {
		$student = new LLMS_Student( $user_id );
	}

	$post_type = get_post_type( $post_id );

	/**
	 * Do checks to determine if the content should be restricted
	 */
	$sitewide_membership_id = llms_is_post_restricted_by_sitewide_membership( $post_id, $user_id );
	$membership_id = llms_is_post_restricted_by_membership( $post_id, $user_id );

	if ( is_home() && $sitewide_membership_id ) {
		$restriction_id = $sitewide_membership_id;
		$reason = 'sitewide_membership';
		// if it's a search page and the site isn't restricted to a membership bypass restrictions
	} elseif ( ( is_search() ) && ! get_option( 'lifterlms_membership_required', '' ) ) {
		return apply_filters( 'llms_page_restricted', $results, $post_id );
	} // End if().
	elseif ( is_singular() && $sitewide_membership_id ) {
		$restriction_id = $sitewide_membership_id;
		$reason = 'sitewide_membership';
	} // content is restricted by a membership
	elseif ( is_singular() && $membership_id ) {
		$restriction_id = $membership_id;
		$reason = 'membership';
	} // checks for lessons
	elseif ( is_singular() && 'lesson' === $post_type ) {
		$lesson = new LLMS_Lesson( $post_id );
		// if lesson is free, return accessible results and skip the rest of this function
		if ( $lesson->is_free() ) {
			return $results;
		} // End if().
		else {
			$restriction_id = $lesson->get_parent_course();
			$reason = 'enrollment_lesson';
		}
	} // checks for course
	elseif ( is_singular() && 'course' === $post_type ) {
		$restriction_id = $post_id;
		$reason = 'enrollment_course';
	} // checks for memberships
	elseif ( is_singular() && 'llms_membership' === $post_type ) {
		$restriction_id = $post_id;
		$reason = 'enrollment_membership';
	} else {

		/**
		 * Allow filtering of results before checking if the student has access
		 */
		$results = apply_filters( 'llms_page_restricted_before_check_access', $results, $post_id );
		extract( $results );

	}

	/**
	 * Content should be restricted, so we'll do the restriction checks
	 * and return restricted results
	 *
	 * this is run if we have a restriction and a reason for restriction
	 * and we either don't have a logged in student or the logged in student doesn't have access
	 */
	if ( ! empty( $restriction_id ) && ! empty( $reason ) && ( ! $student || ! $student->is_enrolled( $restriction_id ) ) ) {

		$results['is_restricted'] = true;
		$results['reason'] = $reason;
		$results['restriction_id'] = $restriction_id;

		return apply_filters( 'llms_page_restricted', $results, $post_id );

	}

	/**
	 * At this point student has access or the content isn't supposed to be restricted
	 * we need to do some additional checks for specific post types
	 */

	if ( is_singular() ) {

		if ( 'llms_quiz' === $post_type ) {

			$quiz_id = llms_is_quiz_accessible( $post_id, $user_id );
			if ( $quiz_id ) {

				$results['is_restricted'] = true;
				$results['reason'] = 'quiz';
				$results['restriction_id'] = $post_id;
				return apply_filters( 'llms_page_restricted', $results, $post_id );

			}
		}

		if ( 'lesson' === $post_type || 'llms_quiz' === $post_type ) {

			$course_id = llms_is_post_restricted_by_time_period( $post_id, $user_id );
			if ( $course_id ) {

				$results['is_restricted'] = true;
				$results['reason'] = 'course_time_period';
				$results['restriction_id'] = $course_id;
				return apply_filters( 'llms_page_restricted', $results, $post_id );

			}

			$prereq_data = llms_is_post_restricted_by_prerequisite( $post_id, $user_id );
			if ( $prereq_data ) {

				$results['is_restricted'] = true;
				$results['reason'] = sprintf( '%s_prerequisite', $prereq_data['type'] );
				$results['restriction_id'] = $prereq_data['id'];
				return apply_filters( 'llms_page_restricted', $results, $post_id );

			}

			$lesson_id = llms_is_post_restricted_by_drip_settings( $post_id, $user_id );
			if ( $lesson_id ) {

				$results['is_restricted'] = true;
				$results['reason'] = 'lesson_drip';
				$results['restriction_id'] = $lesson_id;
				return apply_filters( 'llms_page_restricted', $results, $post_id );

			}
		}
	}// End if().

	return apply_filters( 'llms_page_restricted', $results, $post_id );

}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: