/** * Outputs the lessons course signup lingk * * This hook runs inside the single lesson page. * * @since 1.9.0 */ public static function course_signup_link() { $course_id = Sensei()->lesson->get_course_id(get_the_ID()); if (empty($course_id) || 'course' != get_post_type($course_id) || sensei_all_access()) { return; } ?> <section class="course-signup lesson-meta"> <?php $wc_post_id = (int) get_post_meta($course_id, '_course_woocommerce_product', true); if (Sensei_WC::is_woocommerce_active() && 0 < $wc_post_id) { global $current_user; if (is_user_logged_in()) { wp_get_current_user(); $course_purchased = Sensei_Utils::sensei_customer_bought_product($current_user->user_email, $current_user->ID, $wc_post_id); if ($course_purchased) { $prereq_course_id = get_post_meta($course_id, '_course_prerequisite', true); $course_link = '<a href="' . esc_url(get_permalink($prereq_course_id)) . '" title="' . esc_attr(get_the_title($prereq_course_id)) . '">' . __('the previous course', 'woothemes-sensei') . '</a>'; ?> <div class="sensei-message info"> <?php echo sprintf(__('Please complete %1$s before starting the lesson.', 'woothemes-sensei'), $course_link); ?> </div> <?php } else { ?> <div class="sensei-message info"> <?php $course_link = '<a href="' . esc_url(get_permalink($course_id)) . '"title="' . __('Sign Up', 'woothemes-sensei') . '">' . __('course', 'woothemes-sensei') . '</a>'; echo sprintf(__('Please purchase the %1$s before starting the lesson.', 'woothemes-sensei'), $course_link); ?> </div> <?php } ?> <?php } else { ?> <div class="sensei-message info"><?php echo sprintf(__('Please purchase the %1$s before starting the lesson.', 'woothemes-sensei'), '<a href="' . esc_url(get_permalink($course_id)) . '" title="' . __('Sign Up', 'woothemes-sensei') . '">' . __('course', 'woothemes-sensei') . '</a>'); ?> </div> <?php } ?> <?php } else { ?> <?php if (!Sensei_Utils::user_started_course($course_id, get_current_user_id()) && sensei_is_login_required()) { ?> <div class="sensei-message alert"> <?php $course_link = '<a href="' . esc_url(get_permalink($course_id)) . '" title="' . __('Sign Up', 'woothemes-sensei') . '">' . __('course', 'woothemes-sensei') . '</a>'; if (Sensei_Utils::is_preview_lesson(get_the_ID())) { echo sprintf(__('This is a preview lesson. Please sign up for the %1$s to access all lessons.', 'woothemes-sensei'), $course_link); } else { echo sprintf(__('Please sign up for the %1$s before starting the lesson.', 'woothemes-sensei'), $course_link); } ?> </div> <?php } ?> <?php } // End If Statement ?> </section> <?php }
public function sensei_course_preview_titles($title, $lesson_id) { global $post, $current_user; $course_id = $post->ID; $title_text = ''; if (method_exists('Sensei_Utils', 'is_preview_lesson') && Sensei_Utils::is_preview_lesson($lesson_id)) { $is_user_taking_course = Sensei_Utils::sensei_check_for_activity(array('post_id' => $course_id, 'user_id' => $current_user->ID, 'type' => 'sensei_course_status')); if (!$is_user_taking_course) { if (method_exists('WooThemes_Sensei_Frontend', 'sensei_lesson_preview_title_text')) { $title_text = Sensei()->frontend->sensei_lesson_preview_title_text($course_id); // Remove brackets for display here $title_text = str_replace('(', '', $title_text); $title_text = str_replace(')', '', $title_text); $title_text = '<span class="preview-label">' . $title_text . '</span>'; } $title .= ' ' . $title_text; } } return $title; }
/** * Ouput the single lesson meta * * The function should only be called on the single lesson * */ function sensei_the_single_lesson_meta() { // if the lesson meta is included within theme load that instead of the function content $template = Sensei_Templates::locate_template('single-lesson/lesson-meta.php'); if (!empty($template)) { Sensei_Templates::get_template('single-lesson/lesson-meta.php'); return; } // Get the meta info $lesson_course_id = absint(get_post_meta(get_the_ID(), '_lesson_course', true)); $is_preview = Sensei_Utils::is_preview_lesson(get_the_ID()); // Complete Lesson Logic do_action('sensei_complete_lesson'); // Check that the course has been started if (Sensei()->access_settings() || Sensei_Utils::user_started_course($lesson_course_id, get_current_user_id()) || $is_preview) { ?> <section class="lesson-meta"> <?php if (apply_filters('sensei_video_position', 'top', get_the_ID()) == 'bottom') { do_action('sensei_lesson_video', get_the_ID()); } ?> <?php do_action('sensei_frontend_messages'); ?> <?php if (!$is_preview || Sensei_Utils::user_started_course($lesson_course_id, get_current_user_id())) { sensei_do_deprecated_action('sensei_lesson_quiz_meta', '1.9.0', 'sensei_single_lesson_content_inside_before', array(get_the_ID(), get_current_user_id())); } ?> </section> <?php do_action('sensei_lesson_back_link', $lesson_course_id); ?> <?php } do_action('sensei_lesson_meta_extra', get_the_ID()); }
/** * Show the user not taking course message if it is the case * * @since 1.9.0 */ public static function user_not_taking_course_message() { $lesson_id = get_the_ID(); if ('lesson' != get_post_type($lesson_id)) { return; } $is_preview = Sensei_Utils::is_preview_lesson($lesson_id); $pre_requisite_complete = self::is_prerequisite_complete($lesson_id, get_current_user_id()); $lesson_course_id = get_post_meta($lesson_id, '_lesson_course', true); $user_taking_course = Sensei_Utils::user_started_course($lesson_course_id, get_current_user_id()); if ($pre_requisite_complete && $is_preview && !$user_taking_course) { ?> <div class="sensei-message alert"> <?php echo Sensei()->permissions_message['message']; ?> </div> <?php } // end if }
public function sensei_lesson_preview_title($title = '', $id = 0) { global $post, $current_user; // Limit to lessons and check if lesson ID matches filtered post ID // @see https://github.com/woothemes/sensei/issues/574 if (isset($post->ID) && $id == $post->ID && 'lesson' == get_post_type($post)) { // Limit to main query only if (is_main_query()) { // Get the course ID $course_id = get_post_meta($post->ID, '_lesson_course', true); // Check if the user is taking the course if (is_singular('lesson') && Sensei_Utils::is_preview_lesson($post->ID) && !Sensei_Utils::user_started_course($course_id, $current_user->ID) && $post->ID == $id) { $title .= ' ' . $this->sensei_lesson_preview_title_text($course_id); } } } return $title; }
/** * Hooking into the single lesson page to alter the * user access permissions based on if they have purchased the * course the lesson belongs to. * * This function will only return false or the passed in user_access value. * It doesn't return true in order to avoid altering other options. * * @since 1.9.0 * * @param $can_user_view_lesson * @param $lesson_id * @param $user_id * @return bool */ public static function alter_can_user_view_lesson($can_user_view_lesson, $lesson_id, $user_id) { // do not override access to admins if (sensei_all_access() || Sensei_Utils::is_preview_lesson($lesson_id)) { return true; } // check if the course has a valid product attached to it // which the user should have purchased if they want to access // the current lesson $course_id = get_post_meta($lesson_id, '_lesson_course', true); $wc_post_id = get_post_meta($course_id, '_course_woocommerce_product', true); $product = Sensei()->sensei_get_woocommerce_product_object($wc_post_id); if (isset($product) && is_object($product)) { // valid product found $order_id = self::get_learner_course_active_order_id($user_id, $course_id); // product has a successful order so this user may access the content // this function may only return false or the default // returning true may override other negatives which we don't want if (!$order_id) { return false; } } // return the passed in value return $can_user_view_lesson; }
/** * check_user_permissions function. * * @access public * @param string $page (default: '') * * @return bool */ public function check_user_permissions($page = '') { // REFACTOR global $current_user, $post; // if use is not logged in // skipped for single lesson if (empty($current_user->caps) && Sensei()->settings->get('access_permission') && 'lesson-single' != $page) { $this->permissions_message['title'] = __('Restricted Access', 'woothemes-sensei'); $this->permissions_message['message'] = sprintf(__('You must be logged in to view this %s'), get_post_type()); return false; } $user_allowed = false; switch ($page) { case 'course-single': // check for prerequisite course or lesson, $course_prerequisite_id = (int) get_post_meta($post->ID, '_course_prerequisite', true); $update_course = $this->woocommerce_course_update($post->ID); // Count completed lessons if (0 < absint($course_prerequisite_id)) { $prerequisite_complete = Sensei_Utils::user_completed_course($course_prerequisite_id, $current_user->ID); } else { $prerequisite_complete = true; } // End If Statement // Handles restrictions if (!$prerequisite_complete && 0 < absint($course_prerequisite_id)) { $this->permissions_message['title'] = get_the_title($post->ID) . ': ' . __('Restricted Access', 'woothemes-sensei'); $course_link = '<a href="' . esc_url(get_permalink($course_prerequisite_id)) . '">' . __('course', 'woothemes-sensei') . '</a>'; $this->permissions_message['message'] = sprintf(__('Please complete the previous %1$s before taking this course.', 'woothemes-sensei'), $course_link); } else { $user_allowed = true; } // End If Statement break; case 'lesson-single': // Check for WC purchase $lesson_course_id = get_post_meta($post->ID, '_lesson_course', true); $update_course = $this->woocommerce_course_update($lesson_course_id); $is_preview = Sensei_Utils::is_preview_lesson($post->ID); if ($this->access_settings() && Sensei_Utils::user_started_course($lesson_course_id, $current_user->ID)) { $user_allowed = true; } elseif ($this->access_settings() && false == $is_preview) { $user_allowed = true; } else { $this->permissions_message['title'] = get_the_title($post->ID) . ': ' . __('Restricted Access', 'woothemes-sensei'); $course_link = '<a href="' . esc_url(get_permalink($lesson_course_id)) . '">' . __('course', 'woothemes-sensei') . '</a>'; $wc_post_id = get_post_meta($lesson_course_id, '_course_woocommerce_product', true); if (Sensei_WC::is_woocommerce_active() && 0 < $wc_post_id) { if ($is_preview) { $this->permissions_message['message'] = sprintf(__('This is a preview lesson. Please purchase the %1$s to access all lessons.', 'woothemes-sensei'), $course_link); } else { $this->permissions_message['message'] = sprintf(__('Please purchase the %1$s before starting this Lesson.', 'woothemes-sensei'), $course_link); } } else { if ($is_preview) { $this->permissions_message['message'] = sprintf(__('This is a preview lesson. Please sign up for the %1$s to access all lessons.', 'woothemes-sensei'), $course_link); } else { /** This filter is documented in class-woothemes-sensei-frontend.php */ $this->permissions_message['message'] = sprintf(__('Please sign up for the %1$s before starting the lesson.', 'woothemes-sensei'), $course_link); } } // End If Statement } // End If Statement break; case 'quiz-single': $lesson_id = get_post_meta($post->ID, '_quiz_lesson', true); $lesson_course_id = get_post_meta($lesson_id, '_lesson_course', true); $update_course = $this->woocommerce_course_update($lesson_course_id); if ($this->access_settings() && Sensei_Utils::user_started_course($lesson_course_id, $current_user->ID) || sensei_all_access()) { // Check for prerequisite lesson for this quiz $lesson_prerequisite_id = (int) get_post_meta($lesson_id, '_lesson_prerequisite', true); $user_lesson_prerequisite_complete = Sensei_Utils::user_completed_lesson($lesson_prerequisite_id, $current_user->ID); // Handle restrictions if (sensei_all_access()) { $user_allowed = true; } else { if (0 < absint($lesson_prerequisite_id) && !$user_lesson_prerequisite_complete) { $this->permissions_message['title'] = get_the_title($post->ID) . ': ' . __('Restricted Access', 'woothemes-sensei'); $lesson_link = '<a href="' . esc_url(get_permalink($lesson_prerequisite_id)) . '">' . __('lesson', 'woothemes-sensei') . '</a>'; $this->permissions_message['message'] = sprintf(__('Please complete the previous %1$s before taking this Quiz.', 'woothemes-sensei'), $lesson_link); } else { $user_allowed = true; } // End If Statement } // End If Statement } elseif ($this->access_settings()) { // Check if the user has started the course if (is_user_logged_in() && !Sensei_Utils::user_started_course($lesson_course_id, $current_user->ID) && (isset($this->settings->settings['access_permission']) && true == $this->settings->settings['access_permission'])) { $user_allowed = false; $this->permissions_message['title'] = get_the_title($post->ID) . ': ' . __('Restricted Access', 'woothemes-sensei'); $course_link = '<a href="' . esc_url(get_permalink($lesson_course_id)) . '">' . __('course', 'woothemes-sensei') . '</a>'; $wc_post_id = get_post_meta($lesson_course_id, '_course_woocommerce_product', true); if (Sensei_WC::is_woocommerce_active() && 0 < $wc_post_id) { $this->permissions_message['message'] = sprintf(__('Please purchase the %1$s before starting this Quiz.', 'woothemes-sensei'), $course_link); } else { $this->permissions_message['message'] = sprintf(__('Please sign up for the %1$s before starting this Quiz.', 'woothemes-sensei'), $course_link); } // End If Statement } else { $user_allowed = true; } // End If Statement } else { $this->permissions_message['title'] = get_the_title($post->ID) . ': ' . __('Restricted Access', 'woothemes-sensei'); $course_link = '<a href="' . esc_url(get_permalink(get_post_meta(get_post_meta($post->ID, '_quiz_lesson', true), '_lesson_course', true))) . '">' . __('course', 'woothemes-sensei') . '</a>'; $this->permissions_message['message'] = sprintf(__('Please sign up for the %1$s before taking this Quiz.', 'woothemes-sensei'), $course_link); } // End If Statement break; default: $user_allowed = true; break; } // End Switch Statement /** * filter the permissions message shown on sensei post types. * * @since 1.8.7 * * @param array $permissions_message{ * * @type string $title * @type string $message * * } * @param string $post_id */ $this->permissions_message = apply_filters('sensei_permissions_message', $this->permissions_message, $post->ID); if (sensei_all_access() || Sensei_Utils::is_preview_lesson($post->ID)) { $user_allowed = true; } return apply_filters('sensei_access_permissions', $user_allowed); }
<a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?> " > <?php the_title(); ?> <?php $course_id = Sensei()->lesson->get_course_id(get_the_ID()); if (Sensei_Utils::is_preview_lesson(get_the_ID()) && !Sensei_Utils::user_started_course($course_id, get_current_user_id())) { ?> <span class="preview-label"><?php _e('Free Preview', 'woothemes-sensei'); ?> </span> <?php } ?> </a> </li>