PluginProbe
Tutor LMS – eLearning and online course solution / 1.6.6
Tutor LMS – eLearning and online course solution v1.6.6
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | classes/Template.php +420 -600 trunk1.6.6 View file →
@@ -1,600 +1,420 @@
1 -<?php
2 -/**
3 - * Template Class
4 - *
5 - * @package Tutor\Template
6 - * @author Themeum <support@themeum.com>
7 - * @link https://themeum.com
8 - * @since 1.0.0
9 - */
10 -
11 -namespace TUTOR;
12 -
13 -use Tutor\Helpers\UrlHelper;
14 -
15 -if ( ! defined( 'ABSPATH' ) ) {
16 - exit;
17 -}
18 -
19 -/**
20 - * Handle template before include
21 - *
22 - * @since 1.0.0
23 - */
24 -class Template extends Tutor_Base {
25 -
26 - /**
27 - * Store Shortcode Object
28 - *
29 - * @var Shortcode
30 - */
31 - public $shortcode_obj;
32 -
33 - /**
34 - * Register Hooks
35 - *
36 - * @since 1.0.0
37 - */
38 - public function __construct() {
39 - parent::__construct();
40 -
41 - /**
42 - * Should Load Template Override
43 - * Integration for specially oxygen builder
44 - * If we found false of below filter, then we will not use this file
45 - */
46 -
47 - $template_override = apply_filters( 'tutor_lms_should_template_override', true );
48 - if ( ! $template_override ) {
49 - return;
50 - }
51 -
52 - add_filter( 'template_include', array( $this, 'load_course_archive_template' ), 99 );
53 - add_filter( 'template_include', array( $this, 'load_single_course_template' ), 99 );
54 - add_filter( 'template_include', array( $this, 'play_private_video' ), 99 );
55 - add_filter( 'tutor_single_content_template', array( $this, 'load_single_lesson_template' ), 99, 2 );
56 - add_filter( 'tutor_single_content_template', array( $this, 'load_quiz_template' ), 99, 2 );
57 -
58 - add_filter( 'template_include', array( $this, 'student_public_profile' ), 99 );
59 - add_filter( 'template_include', array( $this, 'tutor_dashboard' ), 99 );
60 - add_filter( 'pre_get_document_title', array( $this, 'student_public_profile_title' ) );
61 -
62 - add_filter( 'the_content', array( $this, 'convert_static_page_to_template' ) );
63 - add_action( 'pre_get_posts', array( $this, 'limit_course_query_archive' ), 99 );
64 - add_filter( 'template_include', array( $this, 'load_learning_template' ) );
65 -
66 - $this->shortcode_obj = new Shortcode( false );
67 - }
68 -
69 - /**
70 - * Load default template for course
71 - *
72 - * @since v.1.0.0
73 - *
74 - * @param sting $template template name.
75 - *
76 - * @return bool|string
77 - */
78 - public function load_course_archive_template( $template ) {
79 - global $wp_query;
80 -
81 - $post_type = get_query_var( 'post_type' );
82 - if ( ! is_array( $post_type ) ) {
83 - $post_type = array( $post_type );
84 - }
85 -
86 - $course_category = get_query_var( 'course-category' );
87 -
88 - if ( ( in_array( $this->course_post_type, $post_type, true ) || ! empty( $course_category ) ) && $wp_query->is_archive ) {
89 - $template = tutor_get_template( 'archive-course' );
90 - return $template;
91 - }
92 -
93 - return $template;
94 - }
95 -
96 - /**
97 - * Limit for course archive listing
98 - *
99 - * Make a page to archive listing for courses
100 - *
101 - * @since 1.0.0
102 - *
103 - * @param mixed $query query argument.
104 - *
105 - * @return void
106 - */
107 - public function limit_course_query_archive( $query ) {
108 - $courses_per_page = (int) tutor_utils()->get_option( 'courses_per_page', 12 );
109 -
110 - if ( $query->is_main_query() && ! $query->is_feed() && ! is_admin() && is_page() ) {
111 - $queried_object = get_queried_object();
112 - if ( $queried_object instanceof \WP_Post ) {
113 - $page_id = $queried_object->ID;
114 - $selected_archive_page = (int) apply_filters( 'tutor_filter_course_archive_page', tutor_utils()->get_option( 'course_archive_page' ) );
115 -
116 - if ( $page_id === $selected_archive_page ) {
117 - $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
118 - $search_query = get_search_query();
119 - query_posts(
120 - array(
121 - 'post_type' => $this->course_post_type,
122 - 'paged' => $paged,
123 - 's' => $search_query,
124 - 'posts_per_page' => $courses_per_page,
125 - )
126 - );
127 - }
128 - }
129 - }
130 -
131 - if ( $query->is_archive && $query->is_main_query() && ! $query->is_feed() && ! is_admin() ) {
132 - $post_type = get_query_var( 'post_type' );
133 - $course_category = get_query_var( 'course-category' );
134 - if ( ( $post_type === $this->course_post_type || ! empty( $course_category ) ) ) {
135 - $query->set( 'posts_per_page', $courses_per_page );
136 - $query->set( 'post_type', apply_filters( 'tutor_course_archive_post_types', array( $this->course_post_type ) ) );
137 - $query = apply_filters( 'tutor_limit_course_archive_list_filter', $query );
138 -
139 - $course_filter = 'newest_first';
140 - if ( ! empty( Input::get( 'tutor_course_filter', '' ) ) ) {
141 - $course_filter = Input::get( 'tutor_course_filter' );
142 - }
143 - switch ( $course_filter ) {
144 - case 'newest_first':
145 - $query->set( 'orderby', 'post_date' );
146 - $query->set( 'order', 'desc' );
147 - break;
148 - case 'oldest_first':
149 - $query->set( 'orderby', 'post_date' );
150 - $query->set( 'order', 'asc' );
151 - break;
152 - case 'course_title_az':
153 - $query->set( 'orderby', 'post_title' );
154 - $query->set( 'order', 'asc' );
155 - break;
156 - case 'course_title_za':
157 - $query->set( 'orderby', 'post_title' );
158 - $query->set( 'order', 'desc' );
159 - break;
160 - }
161 - }
162 - }
163 - }
164 -
165 - /**
166 - * Load Single Course Template
167 - *
168 - * @since v.1.0.0
169 - *
170 - * @param string $template template name to load.
171 - *
172 - * @return bool|string
173 - */
174 - public function load_single_course_template( $template ) {
175 - global $wp_query;
176 - if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] === $this->course_post_type ) {
177 - // Check if the slug contains subpage of learning area.
178 - if ( Input::has( 'subpage' ) ) {
179 - $template = tutor_get_template( 'learning-area.index' );
180 - } else {
181 - do_action( 'single_course_template_before_load', get_the_ID() );
182 - wp_reset_query();
183 - $template = tutor_get_template( 'single-course' );
184 - }
185 - }
186 -
187 - return $template;
188 - }
189 -
190 - /**
191 - * Get root post parent id
192 - *
193 - * @param int $id post id.
194 - *
195 - * @return int root post id
196 - */
197 - private function get_root_post_parent_id( $id ) {
198 - $ancestors = get_post_ancestors( $id );
199 - $root = is_array( $ancestors ) ? end( $ancestors ) : null;
200 -
201 - return is_numeric( $root ) ? $root : $id;
202 - }
203 -
204 - /**
205 - * Load lesson template
206 - *
207 - * @since v.1.0.0
208 - *
209 - * @since 4.0.0 Post type argument added
210 - *
211 - * @param string $template template name to load.
212 - * @param string $post_type Post type added.
213 - *
214 - * @return bool|string
215 - */
216 - public function load_single_lesson_template( $template, $post_type ) {
217 - if ( tutor()->lesson_post_type !== $post_type ) {
218 - return $template;
219 - }
220 -
221 - $is_lesson_post_type = apply_filters( 'tutor_is_lesson_post_type', true, $post_type );
222 -
223 - if ( $is_lesson_post_type ) {
224 - $page_id = get_the_ID();
225 -
226 - do_action( 'tutor_lesson_load_before', $template );
227 - setup_postdata( $page_id );
228 -
229 - if ( is_user_logged_in() ) {
230 - $has_content_access = tutor_utils()->has_enrolled_content_access( 'lesson' );
231 - if ( $has_content_access ) {
232 - $template = tutor_get_template( 'single-lesson' );
233 - } else {
234 - $template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first.
235 - }
236 - } else {
237 - $template = tutor_get_template( 'login' );
238 - }
239 - wp_reset_postdata();
240 -
241 - // Forcefully show lessons if it is public and not paid.
242 - $course_id = $this->get_root_post_parent_id( $page_id );
243 - if ( 'yes' === get_post_meta( $course_id, '_tutor_is_public_course', true ) && ! tutor_utils()->is_course_purchasable( $course_id ) ) {
244 - $template = tutor_get_template( 'single-lesson' );
245 - }
246 -
247 - return apply_filters( 'tutor_lesson_template', $template );
248 - }
249 -
250 - return $template;
251 - }
252 -
253 - /**
254 - * Play the video in this url.
255 - *
256 - * @param string $template template to load.
257 - *
258 - * @return mixed
259 - */
260 - public function play_private_video( $template ) {
261 - global $wp_query;
262 -
263 - if ( $wp_query->is_single && ! empty( $wp_query->query_vars['lesson_video'] ) && 'true' === $wp_query->query_vars['lesson_video'] ) {
264 -
265 - $is_public_video = apply_filters( 'tutor_video_stream_is_public', false, get_the_ID() );
266 - if ( $is_public_video ) {
267 - $video_info = tutor_utils()->get_video_info();
268 - if ( $video_info ) {
269 - $stream = new Video_Stream( $video_info->path );
270 - $stream->start();
271 - }
272 - exit();
273 - }
274 -
275 - if ( tutor_utils()->is_course_enrolled_by_lesson() ) {
276 - $video_info = tutor_utils()->get_video_info();
277 - if ( $video_info ) {
278 - $stream = new Video_Stream( $video_info->path );
279 - $stream->start();
280 - }
281 - } else {
282 - esc_html_e( 'Permission denied', 'tutor' );
283 - }
284 - exit();
285 - }
286 -
287 - return $template;
288 - }
289 -
290 - /**
291 - * Tutor Dashboard Page, Responsible to show dashboard stuffs
292 - *
293 - * @since 1.0.0
294 - *
295 - * @param string $content page content.
296 - *
297 - * @return mixed
298 - */
299 - public function convert_static_page_to_template( $content ) {
300 - /**
301 - * Avoid rendering dynamic templates or shortcodes during head generation.
302 - *
303 - * Some plugins (e.g., Yoast SEO) may process `the_content` while generating
304 - * frontend metadata in `wp_head`, which can cause templates or shortcodes
305 - * to be rendered multiple times within the same request.
306 - *
307 - * @since 4.0.0
308 - */
309 - if ( ! is_admin() && doing_action( 'wp_head' ) ) {
310 - return $content;
311 - }
312 -
313 - $page_id = get_the_ID();
314 -
315 - // Dashboard Page.
316 - $student_dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
317 - if ( $page_id === $student_dashboard_page_id ) {
318 - return $this->shortcode_obj->tutor_dashboard();
319 - }
320 -
321 - // Instructor Registration Page.
322 - $instructor_register_page_page_id = (int) tutor_utils()->get_option( 'instructor_register_page' );
323 - if ( $page_id === $instructor_register_page_page_id ) {
324 - return $this->shortcode_obj->instructor_registration_form();
325 - }
326 -
327 - $student_register_page_id = (int) tutor_utils()->get_option( 'student_register_page' );
328 - if ( $page_id === $student_register_page_id ) {
329 - return $this->shortcode_obj->student_registration_form();
330 - }
331 -
332 - if ( tutor_utils()->is_monetize_by_tutor() ) {
333 - $tutor_cart_page_id = (int) tutor_utils()->get_option( 'tutor_cart_page_id' );
334 - if ( $page_id === $tutor_cart_page_id ) {
335 - return $this->shortcode_obj->tutor_cart_page();
336 - }
337 -
338 - $tutor_checkout_page_id = (int) tutor_utils()->get_option( 'tutor_checkout_page_id' );
339 - if ( $page_id === $tutor_checkout_page_id ) {
340 - if ( ! apply_filters( 'tutor_should_load_checkout_page', true ) ) {
341 - return '';
342 - }
343 -
344 - return $this->shortcode_obj->tutor_checkout_page();
345 - }
346 - }
347 -
348 - return $content;
349 - }
350 -
351 - /**
352 - * Tutor dashboard
353 - *
354 - * @since 1.0.0
355 - *
356 - * @param string $template template name.
357 - *
358 - * @return string
359 - */
360 - public function tutor_dashboard( $template ) {
361 - global $wp_query;
362 - $is_page = apply_filters( 'tutor_determine_is_page', $wp_query->is_page, $template );
363 - if ( $is_page ) {
364 - $student_dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
365 - $student_dashboard_page_id = apply_filters( 'tutor_dashboard_page_id_filter', $student_dashboard_page_id );
366 - $is_dashboard_page = apply_filters( 'tutor_determine_is_dashboard_page', get_the_ID() == $student_dashboard_page_id );
367 -
368 - if ( $is_dashboard_page ) {
369 - /**
370 - * Handle if logout URL
371 - *
372 - * @since v.1.1.2
373 - */
374 - if ( tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars ) === 'logout' ) {
375 - $redirect = apply_filters( 'tutor_dashboard_logout_redirect_url', get_permalink( $student_dashboard_page_id ) );
376 - wp_logout();
377 - wp_safe_redirect( $redirect );
378 - die();
379 - }
380 -
381 - $dashboard_page = tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars );
382 -
383 - $get_dashboard_config = tutor_utils()->tutor_dashboard_permalinks();
384 - $target_dashboard_page = tutor_utils()->array_get( $dashboard_page, $get_dashboard_config );
385 -
386 - if ( isset( $target_dashboard_page['login_require'] ) && false === $target_dashboard_page['login_require'] ) {
387 - $template = tutor_load_template_part( "template-part.{$dashboard_page}" );
388 - } else {
389 -
390 - /**
391 - * Load view page based on dashboard Endpoint
392 - */
393 - if ( is_user_logged_in() ) {
394 -
395 - global $wp;
396 - $full_path = explode( '/', trim( str_replace( get_home_url(), '', home_url( $wp->request ) ), '/' ) );
397 -
398 - $template = tutor_get_template( 'create-course' === end( $full_path ) ? 'dashboard.create-course' : 'dashboard' );
399 -
400 - /**
401 - * Check page page permission
402 - *
403 - * @since 1.3.4
404 - */
405 - $query_var = tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars );
406 - $dashboard_pages = tutor_utils()->tutor_dashboard_pages();
407 - $dashboard_page_item = tutor_utils()->array_get( $query_var, $dashboard_pages );
408 - $auth_cap = tutor_utils()->array_get( 'auth_cap', $dashboard_page_item );
409 -
410 - $can_access_instructor_item = tutor()->instructor_role === $auth_cap && User::can_view_instructor_dashboard();
411 -
412 - if ( $auth_cap && ! User::is_admin() && ! current_user_can( $auth_cap ) && ! $can_access_instructor_item
413 - ) {
414 - $template = tutor_get_template( 'permission-denied' );
415 - }
416 -
417 - /**
418 - * Account pages of dashboard.
419 - *
420 - * @since 4.0.0
421 - */
422 - if ( Dashboard::ACCOUNT_PAGE_SLUG === $dashboard_page ) {
423 - $subpage = tutor_utils()->array_get( 'tutor_dashboard_sub_page', $wp_query->query_vars, 'profile' );
424 - $account_pages = Dashboard::get_account_pages();
425 - $page_data = $account_pages[ $subpage ] ?? array();
426 - $page_template = $page_data['template'] ?? '';
427 -
428 - if ( file_exists( $page_template ) ) {
429 - $template = tutor_get_template( 'account' );
430 - }
431 - }
432 -
433 - $dashboard_subpage = tutor_utils()->array_get( 'tutor_dashboard_sub_page', $wp_query->query_vars );
434 - $is_isolated = Dashboard::is_isolated_page_request( $dashboard_page, $dashboard_subpage );
435 -
436 - if ( $is_isolated ) {
437 - $template = tutor_get_template( 'dashboard-isolated' );
438 - }
439 - } else {
440 - $template = tutor_get_template( 'login' );
441 - }
442 - }
443 - }
444 - }
445 - return $template;
446 - }
447 -
448 - /**
449 - * Load quiz template
450 - *
451 - * @since 1.0.0
452 - *
453 - * If course public then enrollment not required
454 - *
455 - * @since 2.0.2
456 - * @since 4.0.0 $post_type param added.
457 - *
458 - * @param string $template template to load.
459 - * @param string $post_type Post type.
460 - *
461 - * @return bool|string
462 - */
463 - public function load_quiz_template( $template, $post_type ) {
464 - if ( tutor()->quiz_post_type !== $post_type ) {
465 - return $template;
466 - }
467 -
468 - global $post;
469 -
470 - if ( is_user_logged_in() ) {
471 - $has_content_access = tutor_utils()->has_enrolled_content_access( 'quiz' );
472 - $course_id = tutor_utils()->get_course_id_by_content( $post );
473 - $is_public = Course_List::is_public( $course_id );
474 -
475 - // if public course don't need to be enrolled.
476 - if ( $has_content_access || $is_public ) {
477 - $template = tutor_get_template( 'single-quiz' );
478 - } else {
479 - $template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first.
480 - }
481 - } else {
482 - $template = tutor_get_template( 'login' );
483 - }
484 -
485 - return $template;
486 - }
487 -
488 - /**
489 - * Student public profile
490 - *
491 - * @since 1.0.0
492 - *
493 - * @param string $template profile template.
494 - *
495 - * @return bool|string
496 - */
497 - public function student_public_profile( $template ) {
498 - global $wp_query;
499 - $query_var = $wp_query->query_vars;
500 - if ( ! empty( $wp_query->query['tutor_profile_username'] ) ) {
501 - $template = tutor_get_template( 'public-profile' );
502 - }
503 -
504 - return $template;
505 - }
506 -
507 - /**
508 - * Show student Profile
509 - *
510 - * @since 1.0.0
511 - *
512 - * @return string
513 - */
514 - public function student_public_profile_title() {
515 - global $wp_query;
516 -
517 - if ( ! empty( $wp_query->query['tutor_profile_username'] ) ) {
518 - global $wpdb;
519 -
520 - $user_name = sanitize_text_field( $wp_query->query['tutor_profile_username'] );
521 - $user = $wpdb->get_row( $wpdb->prepare( "SELECT display_name from {$wpdb->users} WHERE user_login = %s limit 1; ", $user_name ) );
522 -
523 - if ( ! empty( $user->display_name ) ) {
524 - return sprintf( "%s's %s", $user->display_name, __( 'Profile Page', 'tutor' ) );
525 - }
526 - }
527 - return '';
528 - }
529 -
530 - /**
531 - * Load learning template
532 - *
533 - * @since 4.0.0
534 - *
535 - * @param string $template template to load.
536 - *
537 - * @return string
538 - */
539 - public function load_learning_template( string $template ): string {
540 - if ( tutor_utils()->is_learning_area() ) {
541 - $user_id = get_current_user_id();
542 - $post_type = get_post_type();
543 - $post_id = get_the_ID();
544 - $course_id = tutor()->course_post_type === $post_type ? $post_id : tutor_utils()->get_course_id_by_subcontent( $post_id );
545 - if ( ! in_array( get_post_status( $course_id ), array( 'publish', 'private' ), true ) && ! tutor_utils()->has_user_course_content_access( $user_id, $course_id ) ) {
546 - global $wp_query;
547 - $wp_query->set_404();
548 - status_header( 404 );
549 - return get_404_template();
550 - }
551 -
552 - $legacy_mode = Options_V2::LEARNING_MODE_LEGACY === tutor_utils()->get_option( 'learning_mode' );
553 -
554 - $template_path = apply_filters( 'tutor_single_content_template', $template, $post_type );
555 -
556 - $template = $legacy_mode ? $template_path : tutor_get_template( 'learning-area.index' );
557 - }
558 -
559 - return $template;
560 - }
561 -
562 - /**
563 - * Make learning area sub pages nav items
564 - *
565 - * @since 4.0.0
566 - *
567 - * @param string $base_url Nav items base url.
568 - *
569 - * @return array
570 - */
571 - public static function make_learning_area_sub_page_nav_items( $base_url = '' ): array {
572 - if ( empty( $base_url ) ) {
573 - $base_url = get_permalink();
574 - }
575 -
576 - $menu_items = apply_filters( 'tutor_learning_area_sub_page_nav_item', array(), $base_url );
577 -
578 - $menu_items['course-info'] = array(
579 - 'title' => __( 'Course Info', 'tutor' ),
580 - 'icon' => Icon::INFO_OCTAGON,
581 - 'url' => UrlHelper::add_query_params( $base_url, array( 'subpage' => 'course-info' ) ),
582 - 'template' => tutor_get_template( 'learning-area.subpages.course-info' ),
583 - );
584 -
585 - return apply_filters( 'tutor_learning_area_sub_page_menu_items', $menu_items, $base_url );
586 - }
587 -
588 - /**
589 - * Get learning area active subpage
590 - *
591 - * @since 4.0.0
592 - *
593 - * @return string
594 - */
595 - public static function learning_area_active_subpage(): string {
596 - $active_menu = Input::get( 'subpage', '' );
597 -
598 - return apply_filters( 'tutor_learning_area_active_subpage', $active_menu );
599 - }
600 -}
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'), 99 );
20 +
21 +
22 + /**
23 + * Should Load Template Override
24 + * Integration for specially oxygen builder
25 + * If we found false of below filter, then we will not use this file
26 + */
27 +
28 + $template_override = apply_filters('tutor_lms_should_template_override', true);
29 + if ( ! $template_override){
30 + return;
31 + }
32 +
33 + add_filter( 'template_include', array($this, 'load_course_archive_template'), 99 );
34 + add_filter( 'template_include', array($this, 'load_single_course_template'), 99 );
35 + add_filter( 'template_include', array($this, 'load_single_lesson_template'), 99 );
36 + add_filter( 'template_include', array($this, 'play_private_video'), 99 );
37 + add_filter( 'template_include', array($this, 'load_quiz_template'), 99 );
38 + add_filter( 'template_include', array($this, 'load_assignment_template'), 99 );
39 +
40 + add_filter( 'template_include', array($this, 'student_public_profile'), 99 );
41 + add_filter( 'template_include', array($this, 'tutor_dashboard'), 99 );
42 + add_filter( 'pre_get_document_title', array($this, 'student_public_profile_title') );
43 +
44 + add_filter( 'the_content', array($this, 'convert_static_page_to_template'));
45 + }
46 +
47 + /**
48 + * @param $template
49 + *
50 + * @return bool|string
51 + *
52 + * Load default template for course
53 + *
54 + * @since v.1.0.0
55 + *
56 + */
57 + public function load_course_archive_template($template){
58 + global $wp_query;
59 +
60 + $post_type = get_query_var('post_type');
61 + $course_category = get_query_var('course-category');
62 +
63 + if ( ($post_type === $this->course_post_type || ! empty($course_category) ) && $wp_query->is_archive){
64 + $template = tutor_get_template('archive-course');
65 + return $template;
66 + }
67 +
68 + return $template;
69 + }
70 +
71 + /**
72 + * @param $query
73 + *
74 + * limit for course archive listing
75 + *
76 + * Make a page to archive listing for courses
77 + */
78 + public function limit_course_query_archive($query){
79 + $courses_per_page = (int) tutor_utils()->get_option('courses_per_page', 10);
80 +
81 + if ($query->is_main_query() && ! $query->is_feed() && ! is_admin() && is_page() ){
82 + $queried_object = get_queried_object();
83 + if ($queried_object instanceof \WP_Post){
84 + $page_id = $queried_object->ID;
85 + $selected_archive_page = (int) tutor_utils()->get_option('course_archive_page');
86 +
87 + if ($page_id === $selected_archive_page){
88 + $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
89 + $search_query = get_search_query();
90 + query_posts(array('post_type' => $this->course_post_type, 'paged' => $paged, 's' => $search_query, 'posts_per_page' => $courses_per_page ));
91 + }
92 + }
93 + }
94 +
95 + if ( $query->is_archive && $query->is_main_query() && ! $query->is_feed() && ! is_admin() ){
96 + $post_type = get_query_var('post_type');
97 + $course_category = get_query_var('course-category');
98 + if ( ($post_type === $this->course_post_type || ! empty($course_category) )){
99 + $query->set('posts_per_page', $courses_per_page);
100 +
101 + $course_filter = 'newest_first';
102 + if ( ! empty($_GET['tutor_course_filter'])){
103 + $course_filter = sanitize_text_field($_GET['tutor_course_filter']);
104 + }
105 + switch ($course_filter){
106 + case 'newest_first':
107 + $query->set('orderby', 'ID');
108 + $query->set('order', 'desc');
109 + break;
110 + case 'oldest_first':
111 + $query->set('orderby', 'ID');
112 + $query->set('order', 'asc');
113 + break;
114 + case 'course_title_az':
115 + $query->set('orderby', 'post_title');
116 + $query->set('order', 'asc');
117 + break;
118 + case 'course_title_za':
119 + $query->set('orderby', 'post_title');
120 + $query->set('order', 'desc');
121 + break;
122 + }
123 +
124 + }
125 + }
126 + }
127 +
128 + /**
129 + * @param $template
130 + *
131 + * @return bool|string
132 + *
133 + * Load Single Course Template
134 + *
135 + * @since v.1.0.0
136 + * @updated v.1.3.5
137 + */
138 + public function load_single_course_template($template){
139 + global $wp_query;
140 +
141 + if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->course_post_type){
142 + $student_must_login_to_view_course = tutor_utils()->get_option('student_must_login_to_view_course');
143 + if ($student_must_login_to_view_course){
144 + if ( ! is_user_logged_in() ) {
145 + return tutor_get_template( 'login' );
146 + }
147 + }
148 +
149 + wp_reset_query();
150 + if (empty( $wp_query->query_vars['course_subpage'])) {
151 + $template = tutor_get_template( 'single-course' );
152 + if ( is_user_logged_in() ) {
153 + if ( tutor_utils()->is_enrolled() ) {
154 + $template = tutor_get_template( 'single-course-enrolled' );
155 + }
156 + }
157 + }else{
158 + //If Course Subpage Exists
159 + if ( is_user_logged_in() ) {
160 + $course_subpage = $wp_query->query_vars['course_subpage'];
161 + $template = tutor_get_template_path( 'single-course-enrolled-' . $course_subpage );
162 + if ( ! file_exists( $template ) ) {
163 + $template = tutor_get_template( 'single-course-enrolled-subpage' );
164 + }
165 + }else{
166 + $template = tutor_get_template('login');
167 + }
168 + }
169 + return $template;
170 + }
171 + return $template;
172 + }
173 +
174 + /**
175 + * @param $template
176 + *
177 + * @return bool|string
178 + *
179 + * Load lesson template
180 + *
181 + * @since v.1.0.0
182 + */
183 +
184 + public function load_single_lesson_template($template){
185 + global $wp_query;
186 +
187 + if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === $this->lesson_post_type){
188 + $page_id = get_the_ID();
189 +
190 + do_action('tutor_lesson_load_before', $template);
191 +
192 + setup_postdata($page_id);
193 +
194 + if (is_user_logged_in()){
195 + $is_course_enrolled = tutor_utils()->is_course_enrolled_by_lesson();
196 +
197 + if ($is_course_enrolled) {
198 + $template = tutor_get_template( 'single-lesson' );
199 + }else{
200 + //You need to enroll first
201 + $template = tutor_get_template( 'single.lesson.required-enroll' );
202 +
203 + //Check Lesson edit access to support page builders
204 + if(current_user_can(tutor()->instructor_role) && tutils()->has_lesson_edit_access()){
205 + $template = tutor_get_template( 'single-lesson' );
206 + }
207 + }
208 + }else{
209 + $template = tutor_get_template('login');
210 + }
211 + wp_reset_postdata();
212 +
213 + return apply_filters('tutor_lesson_template', $template);
214 + }
215 + return $template;
216 + }
217 +
218 + /**
219 + * @param $template
220 + *
221 + * @return mixed
222 + *
223 + * Play the video in this url.
224 + */
225 + public function play_private_video($template){
226 + global $wp_query;
227 +
228 + if ($wp_query->is_single && ! empty($wp_query->query_vars['lesson_video']) && $wp_query->query_vars['lesson_video'] === 'true') {
229 +
230 + $isPublicVideo = apply_filters('tutor_video_stream_is_public', false, get_the_ID());
231 + if ($isPublicVideo){
232 + $video_info = tutor_utils()->get_video_info();
233 + if ( $video_info ) {
234 + $stream = new Video_Stream( $video_info->path );
235 + $stream->start();
236 + }
237 + exit();
238 + }
239 +
240 + if (tutor_utils()->is_course_enrolled_by_lesson()) {
241 + $video_info = tutor_utils()->get_video_info();
242 + if ( $video_info ) {
243 + $stream = new Video_Stream( $video_info->path );
244 + $stream->start();
245 + }
246 + }else{
247 + _e('Permission denied', 'tutor');
248 + }
249 + exit();
250 + }
251 +
252 + return $template;
253 + }
254 +
255 + /**
256 + * @param $content
257 + *
258 + * @return mixed
259 + *
260 + * Tutor Dashboard Page, Responsible to show dashboard stuffs
261 + *
262 + * @since v.1.0.0
263 + */
264 + public function convert_static_page_to_template($content){
265 + //Dashboard Page
266 + $student_dashboard_page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id');
267 + if ($student_dashboard_page_id === get_the_ID()){
268 + $shortcode = new Shortcode();
269 + return $shortcode->tutor_dashboard();
270 + }
271 +
272 + //Instructor Registration Page
273 + $instructor_register_page_page_id = (int) tutor_utils()->get_option('instructor_register_page');
274 + if ($instructor_register_page_page_id === get_the_ID()){
275 + $shortcode = new Shortcode();
276 + return $shortcode->instructor_registration_form();
277 + }
278 +
279 + $student_register_page_id = (int) tutor_utils()->get_option('student_register_page');
280 + if ($student_register_page_id === get_the_ID()){
281 + $shortcode = new Shortcode();
282 + return $shortcode->student_registration_form();
283 + }
284 +
285 + return $content;
286 + }
287 +
288 + public function tutor_dashboard($template){
289 + global $wp_query;
290 +
291 + if ($wp_query->is_page) {
292 + $student_dashboard_page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id');
293 + if ($student_dashboard_page_id === get_the_ID()) {
294 + /**
295 + * Handle if logout URL
296 + * @since v.1.1.2
297 + */
298 + if (tutor_utils()->array_get('tutor_dashboard_page', $wp_query->query_vars) === 'logout'){
299 + $redirect = get_permalink($student_dashboard_page_id);
300 + wp_logout();
301 + wp_redirect($redirect);
302 + die();
303 + }
304 +
305 +
306 + $dashboard_page = tutor_utils()->array_get('tutor_dashboard_page', $wp_query->query_vars);
307 + $get_dashboard_config = tutils()->tutor_dashboard_permalinks();
308 + $target_dashboard_page = tutils()->array_get($dashboard_page, $get_dashboard_config);
309 +
310 + if (isset($target_dashboard_page['login_require']) && $target_dashboard_page['login_require'] === false){
311 + $template = tutor_load_template_part( "template-part.{$dashboard_page}" );
312 + }else{
313 +
314 + /**
315 + * Load view page based on dashboard Endpoint
316 + */
317 + if (is_user_logged_in()) {
318 + $template = tutor_get_template( 'dashboard' );
319 + /**
320 + * Check page page permission
321 + *
322 + * @since v.1.3.4
323 + */
324 + $query_var = tutor_utils()->array_get('tutor_dashboard_page', $wp_query->query_vars);
325 + $dashboard_pages = tutor_utils()->tutor_dashboard_pages();
326 + $dashboard_page_item = tutor_utils()->array_get($query_var, $dashboard_pages);
327 + $auth_cap = tutor_utils()->array_get('auth_cap', $dashboard_page_item);
328 + if ($auth_cap && ! current_user_can($auth_cap) ){
329 + wp_die(__('Permission Denied', 'tutor'));
330 + }
331 +
332 + }else{
333 + $template = tutor_get_template( 'login' );
334 + }
335 +
336 + }
337 +
338 + }
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 load_quiz_template($template){
352 + global $wp_query;
353 +
354 + if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === 'tutor_quiz'){
355 + if (is_user_logged_in()){
356 + $template = tutor_get_template( 'single-quiz' );
357 + }else{
358 + $template = tutor_get_template('login');
359 + }
360 + return $template;
361 + }
362 + return $template;
363 + }
364 +
365 + public function load_assignment_template($template){
366 + global $wp_query;
367 +
368 + if ($wp_query->is_single && ! empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] === 'tutor_assignments'){
369 + if (is_user_logged_in()){
370 + $template = tutor_get_template( 'single-assignment' );
371 + }else{
372 + $template = tutor_get_template('login');
373 + }
374 + return $template;
375 + }
376 +
377 + return $template;
378 + }
379 +
380 + /**
381 + * @param $template
382 + *
383 + * @return bool|string
384 + *
385 + * @since v.1.0.0
386 + */
387 + public function student_public_profile($template){
388 + global $wp_query;
389 +
390 + if ( ! empty($wp_query->query['tutor_student_username'])){
391 + $template = tutor_get_template( 'student-public-profile' );
392 + }
393 +
394 + return $template;
395 + }
396 +
397 + /**
398 + * @return string
399 + * Show student Profile
400 + *
401 + * @since v.1.0.0
402 + */
403 + public function student_public_profile_title(){
404 + global $wp_query;
405 +
406 + if ( ! empty($wp_query->query['tutor_student_username'])){
407 + global $wpdb;
408 +
409 + $user_name = sanitize_text_field($wp_query->query['tutor_student_username']);
410 + $user = $wpdb->get_row("select display_name from {$wpdb->users} WHERE user_login = '{$user_name}' limit 1; ");
411 +
412 + if ( ! empty($user->display_name)){
413 + return sprintf("%s's Profile page ", $user->display_name );
414 + }
415 + }
416 + return '';
417 + }
418 +
419 +
420 +}