Skip to content

Commit

Permalink
Show notice when trying to access membership-restricted course #1501
Browse files Browse the repository at this point in the history
  • Loading branch information
pgk committed Dec 18, 2016
1 parent 0894b16 commit 76b1e53
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
29 changes: 20 additions & 9 deletions includes/class-sensei-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,21 @@ public function __construct () {
if ( (int) get_option( 'page_on_front' ) > 0 ) {
add_action( 'pre_get_posts', array( $this, 'allow_course_archive_on_front_page' ) );
}
} // End __construct()
}

/**
* @param $message
*/
private static function add_course_access_permission_message( $message )
{
global $post;
if ( Sensei()->settings->get('access_permission') ) {
$message = apply_filters( 'sensei_couse_access_permission_message', $message, $post->ID );
if (!empty($message)) {
Sensei()->notices->add_notice($message, 'info');
}
}
}

/**
* Fires when a quiz has been graded to check if the Course status needs changing
Expand Down Expand Up @@ -2604,9 +2618,7 @@ public static function single_course_content( $content ){
return $content;

} else {

return '<p class="course-excerpt">' . get_post( get_the_ID() )->post_excerpt . '</p>';

}

}// end single_course_content
Expand Down Expand Up @@ -2801,6 +2813,7 @@ public static function the_course_enrolment_actions(){
<?php
global $post, $current_user;
$is_user_taking_course = Sensei_Utils::user_started_course( $post->ID, $current_user->ID );
$is_course_content_restricted = (bool) apply_filters( 'sensei_is_course_content_restricted', false, $post->ID );

if ( is_user_logged_in() && ! $is_user_taking_course ) {

Expand All @@ -2812,7 +2825,9 @@ public static function the_course_enrolment_actions(){

} else {
$should_display_start_course_form = (bool) apply_filters( 'sensei_display_start_course_form', true, $post->ID );

if ( $is_course_content_restricted && false == $should_display_start_course_form ) {
self::add_course_access_permission_message( '' );
}
if ( $should_display_start_course_form ) {
sensei_start_course_form( $post->ID );
}
Expand Down Expand Up @@ -2869,11 +2884,7 @@ public static function the_course_enrolment_actions(){
$anchor_after
);

// register the notice to display
if( Sensei()->settings->get( 'access_permission' ) ){
Sensei()->notices->add_notice( apply_filters( 'sensei_couse_access_permission_message', $notice, $post->ID ) , 'info' ) ;
}

self::add_course_access_permission_message( $notice );

$my_courses_page_id = '';

Expand Down
25 changes: 23 additions & 2 deletions includes/class-sensei-wc-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public static function load_wc_memberships_integration_hooks() {
return;
}

add_filter( 'sensei_is_course_content_restricted', array( __CLASS__, 'is_course_access_restricted' ), 10, 2 );
add_filter( 'sensei_couse_access_permission_message', array( __CLASS__, 'add_wc_memberships_notice' ), 10, 2 );

This comment has been minimized.

Copy link
@lkraav

lkraav Nov 12, 2017

Contributor

@pkg please note the couse typo in sensei_couse_access_permission_message

This comment has been minimized.

Copy link
@lkraav

lkraav Nov 12, 2017

Contributor

Fixing this also requires an adjustment in Sensei_Course::add_course_access_permission_message()

add_filter( 'sensei_display_start_course_form', array( __CLASS__, 'display_start_course_form_to_members_only' ), 10, 2 );
add_filter( 'sensei_user_can_register_for_course', array( __CLASS__, 'display_start_course_form_to_members_only' ), 10, 2 );

add_action( 'wc_memberships_user_membership_status_changed', array( __CLASS__, 'start_courses_associated_with_membership' ) );
add_action( 'wc_memberships_user_membership_saved', array( __CLASS__, 'on_wc_memberships_user_membership_saved' ), 10, 2 );
}
Expand All @@ -25,12 +28,30 @@ public static function is_course_access_restricted( $access_restricted, $course_
return $access_restricted;
}

return self::is_content_restricted( $course_id );
}

private static function is_content_restricted( $object_id ) {
if ( get_current_user_id() > 0 ) {
$access_restricted = !current_user_can( self::WC_MEMBERSHIPS_VIEW_RESTRICTED_POST_CONTENT, $course_id );
$access_restricted = !current_user_can( self::WC_MEMBERSHIPS_VIEW_RESTRICTED_POST_CONTENT, $object_id );
return $access_restricted;
}

return wc_memberships_is_post_content_restricted( $course_id );
return wc_memberships_is_post_content_restricted( $object_id );
}

public static function add_wc_memberships_notice( $content = '' ) {
global $post;
if ( false === self::is_wc_memberships_active() ) {
return $content;
}

if ( isset( $post->ID ) && !in_array( get_post_type( $post->ID ), array( 'course', 'lesson', 'quiz' ), true ) ||
!self::is_content_restricted( $post->ID ) ) {
return $content;
}
$message = wc_memberships()->get_frontend_instance()->get_content_restricted_message( $post->ID );
return $message;
}

/**
Expand Down

0 comments on commit 76b1e53

Please sign in to comment.