apply_filters( 'sfwd_lms_has_access', boolean $has_access, int $post_id, int $user_id )
Filters whether a user has access to the course.
Description #
Parameters #
- $has_access
-
(boolean) Whether the user has access to the course or not.
- $post_id
-
(int) Post ID.
- $user_id
-
(int) User ID.
Source #
Examples #
Note: Extended code example below not guaranteed, you may need to consult with a developer
<?php
/**
* Example usage for sfwd_lms_has_access filter.
*/
add_filter(
'sfwd_lms_has_access',
function( $has_access = false, $step_id = 0, $user_id = 0 ) {
// Only override if current access is false.
if ( true !== $has_access ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
if ( ( ! empty( $user_id ) ) && ( ! empty( $step_id ) ) ) {
$course_id = learndash_get_course_id( $step_id );
if ( ! empty( $course_id ) ) {
$user_meta_course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );
if ( ( is_array( $user_meta_course_progress ) ) && ( isset( $user_meta_course_progress[ $course_id ] ) ) ) {
// If here the user does not have access but had access to the course at some point.
$step_post_type = get_post_type( $step_id );
if ( 'sfwd-courses' == $step_post_type ) {
$has_access = true;
} elseif ( 'sfwd-lessons' == $step_post_type ) {
// If the user has previously completed the course > lesson then allow access.
if ( ( isset( $user_meta_course_progress[ $course_id ]['lessons'][ $step_id ] ) ) && ( $user_meta_course_progress[ $course_id ]['lessons'][ $step_id ] ) ) {
$has_access = true;
}
} elseif ( 'sfwd-topic' == $step_post_type ) {
$lesson_id = learndash_get_lesson_id( $step_id, $course_id );
if ( ! empty( $lesson_id ) ) {
// If the user has previously completed the course > lesson > topic then allow access.
if ( ( isset( $user_meta_course_progress[ $course_id ]['topics'][ $lesson_id ][ $step_id ] ) ) && ( $user_meta_course_progress[ $course_id ]['topics'][ $lesson_id ][ $step_id ] ) ) {
$has_access = true;
}
}
}
}
}
}
}
// Always return $has_access
return $has_access;
},
1001,
3
);
add_filter(
'sfwd_lms_has_access',
function( $return, $post_id, $user_id ) {
if ( empty( $user_id ) ) {
if ( ! is_user_logged_in() ) {
return $return;
} else {
$user_id = get_current_user_id();
}
}
$course_id = learndash_get_course_id( $post_id );
if ( empty( $course_id ) ) {
return $return;
} else {
$allowed_course_ids = array( 949 );
if ( ! in_array( $course_id, $allowed_course_ids ) ) {
return $return;
}
// User must have specified role in order to access the courses listed in the $allowed_course_ids array
if ( user_can( $user_id, 'subscriber' ) ) {
$return = true;
}
/*
Example 2: User must have specified capability in order to access the courses listed in the $allowed_course_ids array
if(user_can($user_id, "3a_textbook"))
$return = true; */
}
return $return;
},
10,
3
);
add_filter(
'sfwd_lms_has_access',
function( $access, $post_id = 0, $user_id = 0 ) {
// Fill in detailed logic checking user_id and $post_id.
// Always return $access;
return $access;
},
99,
3
);
add_filter(
'sfwd_lms_has_access',
function( $has_access = false, $post_id = 0, $user_id = 0 ) {
if ( empty( $user_id ) ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
} else {
$user_id = 0;
}
}
if ( ( ! empty( $post_id ) ) && ( ! empty( $user_id ) ) ) {
$lesson_id = 0;
$post_type = get_post_type( $post_id );
if ( $post_type == 'sfwd-lessons' ) {
$lesson_id = $post_id;
} elseif ( in_array( $post_type, array( 'sfwd-topic', 'sfwd-quiz' ) ) ) {
$lesson_id = learndash_get_setting( $post_id, 'lesson' );
}
if ( ! empty( $lesson_id ) ) {
$lesson_access_from = ld_lesson_access_from( $lesson_id, get_current_user_id() );
if ( ! empty( $lesson_access_from ) ) {
// If here then the lesson is drip and the user does not yet have access.
// So we redirect to course
$course_id = $course_id = learndash_get_course_id( $post_id );
if ( ! empty( $course_id ) ) {
wp_redirect( get_permalink( $course_id ) );
}
}
}
}
// Default is always return $has_access
return $has_access;
},
1,
3
);
Changelog #
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |