| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template Class |
| 4 |
* |
| 5 |
* @since: v.1.0.0 |
| 6 |
*/ |
| 7 |
namespace TUTOR; |
| 8 |
|
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) |
| 11 |
exit; |
| 12 |
|
| 13 |
|
| 14 |
class Template extends Tutor_Base { |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
parent::__construct(); |
| 18 |
|
| 19 |
add_action( 'pre_get_posts', array($this, 'limit_course_query_archive'), 1 ); |
| 20 |
|
| 21 |
add_filter( 'template_include', array($this, 'load_course_archive_template'), 99 ); |
| 22 |
add_filter( 'template_include', array($this, 'load_single_course_template'), 99 ); |
| 23 |
add_filter( 'template_include', array($this, 'load_single_lesson_template'), 99 ); |
| 24 |
add_filter( 'template_include', array($this, 'play_private_video'), 99 ); |
| 25 |
add_filter( 'template_include', array($this, 'load_quiz_template'), 99 ); |
| 26 |
|
| 27 |
add_filter( 'template_include', array($this, 'student_public_profile'), 99 ); |
| 28 |
add_filter( 'pre_get_document_title', array($this, 'student_public_profile_title') ); |
| 29 |
|
| 30 |
add_filter('the_content', array($this, 'convert_static_page_to_template')); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param $template |
| 35 |
* |
| 36 |
* @return bool|string |
| 37 |
* |
| 38 |
* Load default template for course |
| 39 |
* |
| 40 |
* @since v.1.0.0 |
| 41 |
* |
| 42 |
*/ |
| 43 |
public function load_course_archive_template($template){ |
| 44 |
global $wp_query; |
| 45 |
|
| 46 |
$post_type = get_query_var('post_type'); |
| 47 |
$course_category = get_query_var('course-category'); |
| 48 |
|
| 49 |
if ( ($post_type === $this->course_post_type || ! empty($course_category) ) && $wp_query->is_archive){ |
| 50 |
$template = tutor_get_template('archive-course'); |
| 51 |
return $template; |
| 52 |
} |
| 53 |
|
| 54 |
return $template; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param $query |
| 59 |
* |
| 60 |
* limit for course archive listing |
| 61 |
* |
| 62 |
* Make a page to archive listing for courses |
| 63 |
*/ |
| 64 |
public function limit_course_query_archive($query){ |
| 65 |
if ($query->is_main_query() && ! $query->is_feed() && ! is_admin() && is_page() ){ |
| 66 |
$queried_object = get_queried_object(); |
| 67 |
if ($queried_object instanceof \WP_Post){ |
| 68 |
$page_id = $queried_object->ID; |
| 69 |
$selected_archive_page = (int) tutor_utils()->get_option('course_archive_page'); |
| 70 |
|
| 71 |
if ($page_id === $selected_archive_page){ |
| 72 |
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; |
| 73 |
query_posts(array('post_type' => $this->course_post_type, 'paged' => $paged )); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ( $query->is_archive && $query->is_main_query() && ! $query->is_feed() && ! is_admin() ){ |
| 79 |
$post_type = get_query_var('post_type'); |
| 80 |
$course_category = get_query_var('course-category'); |
| 81 |
if ( ($post_type === $this->course_post_type || ! empty($course_category) )){ |
| 82 |
$courses_per_page = (int) tutor_utils()->get_option('courses_per_page', 10); |
| 83 |
$query->set('posts_per_page', $courses_per_page); |
| 84 |
|
| 85 |
$course_filter = 'newest_first'; |
| 86 |
if ( ! empty($_GET['tutor_course_filter'])){ |
| 87 |
$course_filter = sanitize_text_field($_GET['tutor_course_filter']); |
| 88 |
} |
| 89 |
switch ($course_filter){ |
| 90 |
case 'newest_first': |
| 91 |
$query->set('orderby', 'ID'); |
| 92 |
$query->set('order', 'desc'); |
| 93 |
break; |
| 94 |
case 'oldest_first': |
| 95 |
$query->set('orderby', 'ID'); |
| 96 |
$query->set('order', 'asc'); |
| 97 |
break; |
| 98 |
case 'course_title_az': |
| 99 |
$query->set('orderby', 'post_title'); |
| 100 |
$query->set('order', 'asc'); |
| 101 |
break; |
| 102 |
case 'course_title_za': |
| 103 |
$query->set('orderby', 'post_title'); |
| 104 |
$query->set('order', 'desc'); |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param $template |
| 114 |
* |
| 115 |
* @return bool|string |
| 116 |
* |
| 117 |
* Load Single Course Template |
| 118 |
* |
| 119 |
* @since v.1.0.0 |
| 120 |
*/ |
| 121 |
public function load_single_course_template($template){ |
| 122 |
global $wp_query; |
| 123 |
|
| 124 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->course_post_type){ |
| 125 |
$student_must_login_to_view_course = tutor_utils()->get_option('student_must_login_to_view_course'); |
| 126 |
if ($student_must_login_to_view_course){ |
| 127 |
return tutor_get_template('login'); |
| 128 |
} |
| 129 |
|
| 130 |
if (empty( $wp_query->query_vars['course_subpage'])) { |
| 131 |
$template = tutor_get_template( 'single-course' ); |
| 132 |
if ( is_user_logged_in() ) { |
| 133 |
if ( tutor_utils()->is_enrolled() ) { |
| 134 |
$template = tutor_get_template( 'single-course-enrolled' ); |
| 135 |
} |
| 136 |
} |
| 137 |
}else{ |
| 138 |
//If Course Subpage Exists |
| 139 |
if ( is_user_logged_in() ) { |
| 140 |
$course_subpage = $wp_query->query_vars['course_subpage']; |
| 141 |
$template = tutor_get_template( 'single-course-enrolled-'.$course_subpage); |
| 142 |
}else{ |
| 143 |
$template = tutor_get_template('login'); |
| 144 |
} |
| 145 |
} |
| 146 |
return $template; |
| 147 |
} |
| 148 |
return $template; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @param $template |
| 153 |
* |
| 154 |
* @return bool|string |
| 155 |
* |
| 156 |
* Load lesson template |
| 157 |
* |
| 158 |
* @since v.1.0.0 |
| 159 |
*/ |
| 160 |
|
| 161 |
public function load_single_lesson_template($template){ |
| 162 |
global $wp_query; |
| 163 |
|
| 164 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->lesson_post_type){ |
| 165 |
$page_id = get_the_ID(); |
| 166 |
|
| 167 |
do_action('tutor_lesson_load_before', $template); |
| 168 |
|
| 169 |
setup_postdata($page_id); |
| 170 |
|
| 171 |
if (is_user_logged_in()){ |
| 172 |
$is_course_enrolled = tutor_utils()->is_course_enrolled_by_lesson(); |
| 173 |
|
| 174 |
if ($is_course_enrolled) { |
| 175 |
$template = tutor_get_template( 'single-lesson' ); |
| 176 |
}else{ |
| 177 |
//You need to enroll first |
| 178 |
$template = tutor_get_template( 'single.lesson.required-enroll' ); |
| 179 |
} |
| 180 |
}else{ |
| 181 |
$template = tutor_get_template('login'); |
| 182 |
} |
| 183 |
wp_reset_postdata(); |
| 184 |
|
| 185 |
return apply_filters('tutor_lesson_template', $template); |
| 186 |
} |
| 187 |
return $template; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @param $template |
| 192 |
* |
| 193 |
* @return mixed |
| 194 |
* |
| 195 |
* Play the video in this url. |
| 196 |
*/ |
| 197 |
public function play_private_video($template){ |
| 198 |
global $wp_query; |
| 199 |
|
| 200 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['lesson_video']) && $wp_query->query_vars['lesson_video'] === 'true') { |
| 201 |
|
| 202 |
$isPublicVideo = apply_filters('tutor_video_stream_is_public', false, get_the_ID()); |
| 203 |
if ($isPublicVideo){ |
| 204 |
$video_info = tutor_utils()->get_video_info(); |
| 205 |
if ( $video_info ) { |
| 206 |
$stream = new Video_Stream( $video_info->path ); |
| 207 |
$stream->start(); |
| 208 |
} |
| 209 |
exit(); |
| 210 |
} |
| 211 |
|
| 212 |
if (tutor_utils()->is_course_enrolled_by_lesson()) { |
| 213 |
$video_info = tutor_utils()->get_video_info(); |
| 214 |
if ( $video_info ) { |
| 215 |
$stream = new Video_Stream( $video_info->path ); |
| 216 |
$stream->start(); |
| 217 |
} |
| 218 |
}else{ |
| 219 |
_e('Permission denied', 'tutor'); |
| 220 |
} |
| 221 |
exit(); |
| 222 |
} |
| 223 |
|
| 224 |
return $template; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @param $content |
| 229 |
* |
| 230 |
* @return mixed |
| 231 |
* |
| 232 |
* Tutor Dashboard Page, Responsible to show student dashboard |
| 233 |
* |
| 234 |
* @since v.1.0.0 |
| 235 |
*/ |
| 236 |
public function convert_static_page_to_template($content){ |
| 237 |
//Student Registration Page |
| 238 |
$student_dashboard_page_id = (int) tutor_utils()->get_option('student_dashboard'); |
| 239 |
if ($student_dashboard_page_id === get_the_ID()){ |
| 240 |
$shortcode = new Shortcode(); |
| 241 |
return $shortcode->tutor_student_dashboard(); |
| 242 |
} |
| 243 |
|
| 244 |
//Instructor Registration Page |
| 245 |
$instructor_register_page_page_id = (int) tutor_utils()->get_option('instructor_register_page'); |
| 246 |
if ($instructor_register_page_page_id === get_the_ID()){ |
| 247 |
$shortcode = new Shortcode(); |
| 248 |
return $shortcode->instructor_registration_form(); |
| 249 |
} |
| 250 |
|
| 251 |
$student_register_page_id = (int) tutor_utils()->get_option('student_register_page'); |
| 252 |
if ($student_register_page_id === get_the_ID()){ |
| 253 |
$shortcode = new Shortcode(); |
| 254 |
return $shortcode->student_registration_form(); |
| 255 |
} |
| 256 |
|
| 257 |
return $content; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* @param $template |
| 262 |
* |
| 263 |
* @return bool|string |
| 264 |
* |
| 265 |
* @since v.1.0.0 |
| 266 |
*/ |
| 267 |
public function load_quiz_template($template){ |
| 268 |
global $wp_query; |
| 269 |
|
| 270 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === 'tutor_quiz'){ |
| 271 |
if (is_user_logged_in()){ |
| 272 |
$template = tutor_get_template( 'single-quiz' ); |
| 273 |
}else{ |
| 274 |
$template = tutor_get_template('login'); |
| 275 |
} |
| 276 |
return $template; |
| 277 |
} |
| 278 |
return $template; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* @param $template |
| 283 |
* |
| 284 |
* @return bool|string |
| 285 |
* |
| 286 |
* @since v.1.0.0 |
| 287 |
*/ |
| 288 |
public function student_public_profile($template){ |
| 289 |
global $wp_query; |
| 290 |
|
| 291 |
if ( ! empty($wp_query->query['tutor_student_username'])){ |
| 292 |
$template = tutor_get_template( 'student-public-profile' ); |
| 293 |
} |
| 294 |
|
| 295 |
return $template; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* @return string |
| 300 |
* Show student Profile |
| 301 |
* |
| 302 |
* @since v.1.0.0 |
| 303 |
*/ |
| 304 |
public function student_public_profile_title(){ |
| 305 |
global $wp_query; |
| 306 |
|
| 307 |
if ( ! empty($wp_query->query['tutor_student_username'])){ |
| 308 |
global $wpdb; |
| 309 |
|
| 310 |
$user_name = sanitize_text_field($wp_query->query['tutor_student_username']); |
| 311 |
$user = $wpdb->get_row("select display_name from {$wpdb->users} WHERE user_login = '{$user_name}' limit 1; "); |
| 312 |
|
| 313 |
if ( ! empty($user->display_name)){ |
| 314 |
return sprintf("%s's Profile page ", $user->display_name ); |
| 315 |
} |
| 316 |
} |
| 317 |
return ''; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
} |