llms_page_restricted( int $post_id, int|null $user_id = null )
Determine if content should be restricted.
Description Description
Called during "template_include" to determine if redirects or template overrides are in order.
Parameters Parameters
- $post_id
-
(int) (Required) WordPress Post ID of the content.
- $user_id
-
(int|null) (Optional) WP User ID (will use get_current_user_id() if none supplied). Default
null.Default value: null
Return Return
(array) Restriction check result data.
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 );
} elseif ( is_singular() && $sitewide_membership_id ) {
$restriction_id = $sitewide_membership_id;
$reason = 'sitewide_membership';
} elseif ( is_singular() && $membership_id ) {
$restriction_id = $membership_id;
$reason = 'membership';
} 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;
} else {
$restriction_id = $lesson->get( 'parent_course' );
$reason = 'enrollment_lesson';
}
} elseif ( is_singular() && 'course' === $post_type ) {
$restriction_id = $post_id;
$reason = 'enrollment_course';
} 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.
*
* @since Unknown.
*
* @param array $results Restriction check result data.
* @param int $post_id WordPress Post ID of the content.
*/
$results = apply_filters( 'llms_page_restricted_before_check_access', $results, $post_id );
extract( $results ); // phpcs:ignore
}
/**
* 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;
/**
* Allow filtering of the restricted results.
*
* @since Unknown
*
* @param array $results Restriction check result data.
* @param int $post_id WordPress Post ID of the content.
*/
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;
/* This filter is documented above. */
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;
/* This filter is documented above. */
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'];
/* This filter is documented above. */
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;
/* This filter is documented above. */
return apply_filters( 'llms_page_restricted', $results, $post_id );
}
}
}
/* This filter is documented above. */
return apply_filters( 'llms_page_restricted', $results, $post_id );
}
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' ). |
| 3.16.11 | Unknown. |
| 1.0.0 | Introduced. |