Beispiel #1
0
 /**
  * Determine if the user has and active subscription to give them access
  * to the requested resource.
  *
  * @since 1.9.0
  *
  * @param  boolean$user_access_permission
  * @param  integer $user_id
  * @return boolean $user_access_permission
  */
 public static function get_subscription_permission($user_access_permission, $user_id)
 {
     global $post;
     // ignore the current case if the following conditions are met
     if (!class_exists('WC_Subscriptions') || empty($user_id) || !in_array($post->post_type, array('course', 'lesson', 'quiz')) || !wcs_user_has_subscription($user_id)) {
         return $user_access_permission;
     }
     // at this user has a subscription
     // is the subscription on the the current course?
     $course_id = 0;
     if ('course' == $post->post_type) {
         $course_id = $post->ID;
     } elseif ('lesson' == $post->post_type) {
         $course_id = Sensei()->lesson->get_course_id($post->ID);
     } else {
         $lesson_id = Sensei()->quiz->get_lesson_id($post->ID);
         $course_id = Sensei()->lesson->get_course_id($lesson_id);
     }
     // if the course has no subscription WooCommerce product attached to return the permissions as is
     $product_id = Sensei_WC::get_course_product_id($course_id);
     $product = wc_get_product($product_id);
     if (!in_array($product->get_type(), self::get_subscription_types())) {
         return $user_access_permission;
     }
     // give access if user has active subscription on the product otherwise restrict it.
     // also check if the user was added to the course directly after the subscription started.
     if (wcs_user_has_subscription($user_id, $product_id, 'active') || wcs_user_has_subscription($user_id, $product_id, 'pending-cancel') || self::was_user_added_without_subscription($user_id, $product_id, $course_id)) {
         $user_access_permission = true;
     } else {
         $user_access_permission = false;
         // do not show the WC permissions message
         remove_filter('sensei_the_no_permissions_message', array('Sensei_WC', 'alter_no_permissions_message'), 20, 2);
         Sensei()->permissions_message['title'] = __('No active subscription', 'woothemes-sensei');
         Sensei()->permissions_message['message'] = __('Sorry, you do not have an access to this content without an active subscription.', 'woothemes-sensei');
     }
     return $user_access_permission;
 }