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.


Top ↑

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


Top ↑

Return Return

(array) Restriction check result data.


Top ↑

Source Source

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

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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 );
 
}


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' ).
3.16.11 Unknown.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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