ID ) || current_user_can( 'manage_slms' ) ) { return $response; } $user_id = get_current_user_id(); $course_id = (int) self::get_course_id_from_content( $post->ID, 'slms_lesson' ); if ( $user_id && $course_id && Enrollment::has_access( $user_id, $course_id ) ) { return $response; } $data = $response->get_data(); foreach ( array( 'content', 'excerpt' ) as $field ) { if ( isset( $data[ $field ] ) && is_array( $data[ $field ] ) ) { $data[ $field ]['rendered'] = ''; $data[ $field ]['protected'] = true; unset( $data[ $field ]['raw'] ); } } $response->set_data( $data ); return $response; } public static function check_access() { if ( is_admin() ) { return; } $post_type = get_post_type(); if ( ! in_array( $post_type, array( 'slms_lesson', 'slms_quiz' ), true ) ) { return; } if ( ! is_user_logged_in() ) { wp_safe_redirect( wp_login_url( get_permalink() ) ); exit; } $post_id = get_the_ID(); $user_id = get_current_user_id(); // Get the course ID from the content hierarchy $course_id = self::get_course_id_from_content( $post_id, $post_type ); if ( ! $course_id ) { return; } // Enrolled, and access hasn't ended (the course page says when it ended). if ( ! Enrollment::has_access( $user_id, $course_id ) && ! Capabilities::can_preview( $post_id ) ) { wp_safe_redirect( get_permalink( $course_id ) ); exit; } // Check sequential access for lessons if ( 'slms_lesson' === $post_type ) { if ( ! Progress::can_access_lesson( $user_id, $post_id, $course_id ) && ! Capabilities::can_preview( $post_id ) ) { wp_safe_redirect( get_permalink( $course_id ) ); exit; } } } public static function get_course_id_from_content( $post_id, $post_type = '' ) { if ( ! $post_type ) { $post_type = get_post_type( $post_id ); } if ( 'slms_course' === $post_type ) { return $post_id; } if ( 'slms_lesson' === $post_type ) { $unit_id = get_post_meta( $post_id, '_slms_parent_unit', true ); if ( $unit_id ) { return get_post_meta( $unit_id, '_slms_parent_course', true ); } } if ( 'slms_quiz' === $post_type ) { $lesson_id = get_post_meta( $post_id, '_slms_parent_lesson', true ); if ( $lesson_id ) { return self::get_course_id_from_content( $lesson_id, 'slms_lesson' ); } } return 0; } }