diff --git a/classes/Course.php b/classes/Course.php index d89778ba93..fac8d341bf 100644 --- a/classes/Course.php +++ b/classes/Course.php @@ -299,6 +299,7 @@ public function __construct( $register_hooks = true ) { add_filter( 'template_include', array( $this, 'handle_password_protected' ) ); add_action( 'login_form_postpass', array( $this, 'handle_password_submit' ) ); + add_filter( 'post_password_required', array( $this, 'bypass_password_for_enrolled' ), 10, 2 ); add_filter( 'the_preview', array( $this, 'handle_schedule_courses' ) ); } @@ -342,6 +343,29 @@ public function handle_password_submit() { } } + /** + * Bypass password protection for enrolled users. + * + * @since 3.0.0 + * + * @param bool $required Whether the password is required. + * @param WP_Post $post The post object. + * + * @return bool false if the current user is enrolled, original value otherwise. + */ + public function bypass_password_for_enrolled( $required, $post ) { + if ( ! $required ) { + return $required; + } + + $post_types = array( tutor()->course_post_type, tutor()->bundle_post_type ); + if ( in_array( $post->post_type, $post_types, true ) && tutor_utils()->is_enrolled( $post->ID ) ) { + return false; + } + + return $required; + } + /** * Handle password protected course/bundle. * diff --git a/templates/single/password-protected.php b/templates/single/password-protected.php index 9addac2d6d..b41dc719c2 100644 --- a/templates/single/password-protected.php +++ b/templates/single/password-protected.php @@ -18,7 +18,7 @@ has_video_in_single() ? tutor_course_video() : get_tutor_course_thumbnail(); ?> -