| 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 |
add_filter( 'template_include', array($this, 'load_assignment_template'), 99 ); |
| 27 |
|
| 28 |
add_filter( 'template_include', array($this, 'student_public_profile'), 99 ); |
| 29 |
add_filter( 'template_include', array($this, 'tutor_dashboard'), 99 ); |
| 30 |
add_filter( 'pre_get_document_title', array($this, 'student_public_profile_title') ); |
| 31 |
|
| 32 |
add_filter('the_content', array($this, 'convert_static_page_to_template')); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param $template |
| 37 |
* |
| 38 |
* @return bool|string |
| 39 |
* |
| 40 |
* Load default template for course |
| 41 |
* |
| 42 |
* @since v.1.0.0 |
| 43 |
* |
| 44 |
*/ |
| 45 |
public function load_course_archive_template($template){ |
| 46 |
global $wp_query; |
| 47 |
|
| 48 |
$post_type = get_query_var('post_type'); |
| 49 |
$course_category = get_query_var('course-category'); |
| 50 |
|
| 51 |
if ( ($post_type === $this->course_post_type || ! empty($course_category) ) && $wp_query->is_archive){ |
| 52 |
$template = tutor_get_template('archive-course'); |
| 53 |
return $template; |
| 54 |
} |
| 55 |
|
| 56 |
return $template; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @param $query |
| 61 |
* |
| 62 |
* limit for course archive listing |
| 63 |
* |
| 64 |
* Make a page to archive listing for courses |
| 65 |
*/ |
| 66 |
public function limit_course_query_archive($query){ |
| 67 |
if ($query->is_main_query() && ! $query->is_feed() && ! is_admin() && is_page() ){ |
| 68 |
$queried_object = get_queried_object(); |
| 69 |
if ($queried_object instanceof \WP_Post){ |
| 70 |
$page_id = $queried_object->ID; |
| 71 |
$selected_archive_page = (int) tutor_utils()->get_option('course_archive_page'); |
| 72 |
|
| 73 |
if ($page_id === $selected_archive_page){ |
| 74 |
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; |
| 75 |
query_posts(array('post_type' => $this->course_post_type, 'paged' => $paged )); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ( $query->is_archive && $query->is_main_query() && ! $query->is_feed() && ! is_admin() ){ |
| 81 |
$post_type = get_query_var('post_type'); |
| 82 |
$course_category = get_query_var('course-category'); |
| 83 |
if ( ($post_type === $this->course_post_type || ! empty($course_category) )){ |
| 84 |
$courses_per_page = (int) tutor_utils()->get_option('courses_per_page', 10); |
| 85 |
$query->set('posts_per_page', $courses_per_page); |
| 86 |
|
| 87 |
$course_filter = 'newest_first'; |
| 88 |
if ( ! empty($_GET['tutor_course_filter'])){ |
| 89 |
$course_filter = sanitize_text_field($_GET['tutor_course_filter']); |
| 90 |
} |
| 91 |
switch ($course_filter){ |
| 92 |
case 'newest_first': |
| 93 |
$query->set('orderby', 'ID'); |
| 94 |
$query->set('order', 'desc'); |
| 95 |
break; |
| 96 |
case 'oldest_first': |
| 97 |
$query->set('orderby', 'ID'); |
| 98 |
$query->set('order', 'asc'); |
| 99 |
break; |
| 100 |
case 'course_title_az': |
| 101 |
$query->set('orderby', 'post_title'); |
| 102 |
$query->set('order', 'asc'); |
| 103 |
break; |
| 104 |
case 'course_title_za': |
| 105 |
$query->set('orderby', 'post_title'); |
| 106 |
$query->set('order', 'desc'); |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param $template |
| 116 |
* |
| 117 |
* @return bool|string |
| 118 |
* |
| 119 |
* Load Single Course Template |
| 120 |
* |
| 121 |
* @since v.1.0.0 |
| 122 |
*/ |
| 123 |
public function load_single_course_template($template){ |
| 124 |
global $wp_query; |
| 125 |
|
| 126 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->course_post_type){ |
| 127 |
$student_must_login_to_view_course = tutor_utils()->get_option('student_must_login_to_view_course'); |
| 128 |
if ($student_must_login_to_view_course){ |
| 129 |
if ( ! is_user_logged_in() ) { |
| 130 |
return tutor_get_template( 'login' ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (empty( $wp_query->query_vars['course_subpage'])) { |
| 135 |
$template = tutor_get_template( 'single-course' ); |
| 136 |
if ( is_user_logged_in() ) { |
| 137 |
if ( tutor_utils()->is_enrolled() ) { |
| 138 |
$template = tutor_get_template( 'single-course-enrolled' ); |
| 139 |
} |
| 140 |
} |
| 141 |
}else{ |
| 142 |
//If Course Subpage Exists |
| 143 |
if ( is_user_logged_in() ) { |
| 144 |
$course_subpage = $wp_query->query_vars['course_subpage']; |
| 145 |
$template = tutor_get_template( 'single-course-enrolled-'.$course_subpage); |
| 146 |
}else{ |
| 147 |
$template = tutor_get_template('login'); |
| 148 |
} |
| 149 |
} |
| 150 |
return $template; |
| 151 |
} |
| 152 |
return $template; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param $template |
| 157 |
* |
| 158 |
* @return bool|string |
| 159 |
* |
| 160 |
* Load lesson template |
| 161 |
* |
| 162 |
* @since v.1.0.0 |
| 163 |
*/ |
| 164 |
|
| 165 |
public function load_single_lesson_template($template){ |
| 166 |
global $wp_query; |
| 167 |
|
| 168 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->lesson_post_type){ |
| 169 |
$page_id = get_the_ID(); |
| 170 |
|
| 171 |
do_action('tutor_lesson_load_before', $template); |
| 172 |
|
| 173 |
setup_postdata($page_id); |
| 174 |
|
| 175 |
if (is_user_logged_in()){ |
| 176 |
$is_course_enrolled = tutor_utils()->is_course_enrolled_by_lesson(); |
| 177 |
|
| 178 |
if ($is_course_enrolled) { |
| 179 |
$template = tutor_get_template( 'single-lesson' ); |
| 180 |
}else{ |
| 181 |
//You need to enroll first |
| 182 |
$template = tutor_get_template( 'single.lesson.required-enroll' ); |
| 183 |
} |
| 184 |
}else{ |
| 185 |
$template = tutor_get_template('login'); |
| 186 |
} |
| 187 |
wp_reset_postdata(); |
| 188 |
|
| 189 |
return apply_filters('tutor_lesson_template', $template); |
| 190 |
} |
| 191 |
return $template; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @param $template |
| 196 |
* |
| 197 |
* @return mixed |
| 198 |
* |
| 199 |
* Play the video in this url. |
| 200 |
*/ |
| 201 |
public function play_private_video($template){ |
| 202 |
global $wp_query; |
| 203 |
|
| 204 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['lesson_video']) && $wp_query->query_vars['lesson_video'] === 'true') { |
| 205 |
|
| 206 |
$isPublicVideo = apply_filters('tutor_video_stream_is_public', false, get_the_ID()); |
| 207 |
if ($isPublicVideo){ |
| 208 |
$video_info = tutor_utils()->get_video_info(); |
| 209 |
if ( $video_info ) { |
| 210 |
$stream = new Video_Stream( $video_info->path ); |
| 211 |
$stream->start(); |
| 212 |
} |
| 213 |
exit(); |
| 214 |
} |
| 215 |
|
| 216 |
if (tutor_utils()->is_course_enrolled_by_lesson()) { |
| 217 |
$video_info = tutor_utils()->get_video_info(); |
| 218 |
if ( $video_info ) { |
| 219 |
$stream = new Video_Stream( $video_info->path ); |
| 220 |
$stream->start(); |
| 221 |
} |
| 222 |
}else{ |
| 223 |
_e('Permission denied', 'tutor'); |
| 224 |
} |
| 225 |
exit(); |
| 226 |
} |
| 227 |
|
| 228 |
return $template; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @param $content |
| 233 |
* |
| 234 |
* @return mixed |
| 235 |
* |
| 236 |
* Tutor Dashboard Page, Responsible to show dashboard stuffs |
| 237 |
* |
| 238 |
* @since v.1.0.0 |
| 239 |
*/ |
| 240 |
public function convert_static_page_to_template($content){ |
| 241 |
//Student Registration Page |
| 242 |
$student_dashboard_page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id'); |
| 243 |
if ($student_dashboard_page_id === get_the_ID()){ |
| 244 |
$shortcode = new Shortcode(); |
| 245 |
return $shortcode->tutor_dashboard(); |
| 246 |
} |
| 247 |
|
| 248 |
//Instructor Registration Page |
| 249 |
$instructor_register_page_page_id = (int) tutor_utils()->get_option('instructor_register_page'); |
| 250 |
if ($instructor_register_page_page_id === get_the_ID()){ |
| 251 |
$shortcode = new Shortcode(); |
| 252 |
return $shortcode->instructor_registration_form(); |
| 253 |
} |
| 254 |
|
| 255 |
$student_register_page_id = (int) tutor_utils()->get_option('student_register_page'); |
| 256 |
if ($student_register_page_id === get_the_ID()){ |
| 257 |
$shortcode = new Shortcode(); |
| 258 |
return $shortcode->student_registration_form(); |
| 259 |
} |
| 260 |
|
| 261 |
return $content; |
| 262 |
} |
| 263 |
|
| 264 |
public function tutor_dashboard($template){ |
| 265 |
global $wp_query; |
| 266 |
|
| 267 |
if ($wp_query->is_page) { |
| 268 |
$student_dashboard_page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id'); |
| 269 |
if ($student_dashboard_page_id === get_the_ID()) { |
| 270 |
/** |
| 271 |
* Handle if logout URL |
| 272 |
* @since v.1.1.2 |
| 273 |
*/ |
| 274 |
if (tutor_utils()->array_get('tutor_dashboard_page', $wp_query->query_vars) === 'logout'){ |
| 275 |
$redirect = get_permalink($student_dashboard_page_id); |
| 276 |
wp_logout(); |
| 277 |
wp_redirect($redirect); |
| 278 |
die(); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Load view page based on dashboard Endpoint |
| 283 |
*/ |
| 284 |
if (is_user_logged_in()) { |
| 285 |
$template = tutor_get_template( 'dashboard' ); |
| 286 |
/** |
| 287 |
* Check page page permission |
| 288 |
* |
| 289 |
* @since v.1.3.4 |
| 290 |
*/ |
| 291 |
$query_var = tutor_utils()->array_get('tutor_dashboard_page', $wp_query->query_vars); |
| 292 |
$dashboard_pages = tutor_utils()->tutor_dashboard_pages(); |
| 293 |
$dashboard_page_item = tutor_utils()->array_get($query_var, $dashboard_pages); |
| 294 |
$auth_cap = tutor_utils()->array_get('auth_cap', $dashboard_page_item); |
| 295 |
if ($auth_cap && ! current_user_can($auth_cap) ){ |
| 296 |
wp_die(__('Permission Denied', 'tutor')); |
| 297 |
} |
| 298 |
|
| 299 |
}else{ |
| 300 |
$template = tutor_get_template( 'login' ); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return $template; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @param $template |
| 310 |
* |
| 311 |
* @return bool|string |
| 312 |
* |
| 313 |
* @since v.1.0.0 |
| 314 |
*/ |
| 315 |
public function load_quiz_template($template){ |
| 316 |
global $wp_query; |
| 317 |
|
| 318 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === 'tutor_quiz'){ |
| 319 |
if (is_user_logged_in()){ |
| 320 |
$template = tutor_get_template( 'single-quiz' ); |
| 321 |
}else{ |
| 322 |
$template = tutor_get_template('login'); |
| 323 |
} |
| 324 |
return $template; |
| 325 |
} |
| 326 |
return $template; |
| 327 |
} |
| 328 |
|
| 329 |
public function load_assignment_template($template){ |
| 330 |
global $wp_query; |
| 331 |
|
| 332 |
if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === 'tutor_assignments'){ |
| 333 |
if (is_user_logged_in()){ |
| 334 |
$template = tutor_get_template( 'single-assignment' ); |
| 335 |
}else{ |
| 336 |
$template = tutor_get_template('login'); |
| 337 |
} |
| 338 |
return $template; |
| 339 |
} |
| 340 |
|
| 341 |
return $template; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* @param $template |
| 346 |
* |
| 347 |
* @return bool|string |
| 348 |
* |
| 349 |
* @since v.1.0.0 |
| 350 |
*/ |
| 351 |
public function student_public_profile($template){ |
| 352 |
global $wp_query; |
| 353 |
|
| 354 |
if ( ! empty($wp_query->query['tutor_student_username'])){ |
| 355 |
$template = tutor_get_template( 'student-public-profile' ); |
| 356 |
} |
| 357 |
|
| 358 |
return $template; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* @return string |
| 363 |
* Show student Profile |
| 364 |
* |
| 365 |
* @since v.1.0.0 |
| 366 |
*/ |
| 367 |
public function student_public_profile_title(){ |
| 368 |
global $wp_query; |
| 369 |
|
| 370 |
if ( ! empty($wp_query->query['tutor_student_username'])){ |
| 371 |
global $wpdb; |
| 372 |
|
| 373 |
$user_name = sanitize_text_field($wp_query->query['tutor_student_username']); |
| 374 |
$user = $wpdb->get_row("select display_name from {$wpdb->users} WHERE user_login = '{$user_name}' limit 1; "); |
| 375 |
|
| 376 |
if ( ! empty($user->display_name)){ |
| 377 |
return sprintf("%s's Profile page ", $user->display_name ); |
| 378 |
} |
| 379 |
} |
| 380 |
return ''; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
} |