Skip to content

Instantly share code, notes, and snippets.

@codehooligans codehooligans/PHP
Last active Apr 29, 2016

Embed
What would you like to do?
Learndash override default administrator access.
/**
* Override the default Learndash concept that administrators are auto-enrolled in Courses.
* @param bool $has_access - value passed from learndash true the user has access, false the user does not.
* @param int $post_id - post_id to check access for
* @param int $user_id - user_id to check access for.
* @return bool true the user has access, false the user does not.
*/
function learndash_override_admin_course_access( $has_access, $post_id, $user_id = null ) {
if ( ! empty( $post_id ) && !learndash_is_sample( $post_id ) ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
if ( !empty( $user_id ) ) {
// If the user is administrator then return access value.
if ( user_can( $user_id, 'administrator' ) ) {
// Because the $post_id may be lesson, quiz etc we get the course_id from the post_id to be sure.
$course_id = learndash_get_course_id( $post_id );
if (!empty( $course_id ) ) {
// If the user see the 'take this course' button on the Course page. We don't want to prevent the form submit.
if ((isset($_POST['course_id'])) || ($_POST['course_id'] == $post_id) && (isset($_POST['course_join'])) ) {
return $has_access;
} else {
$user_course_access_from = get_user_meta( $user_id, 'course_'. $course_id .'_access_from', true);
// If the user meta does not contain the course_x_access_from then the user does not already have access.
// As this is an administrator we set the $has_access to false.
if ( empty( $user_course_access_from ) ) {
$has_access = false;
}
}
}
}
}
}
return $has_access;
}
add_filter('sfwd_lms_has_access', 'learndash_override_admin_course_access', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.