-
Notifications
You must be signed in to change notification settings - Fork 63
/
Template.php
475 lines (415 loc) · 13.7 KB
/
Template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* Template Class
*
* @package Tutor\Template
* @author Themeum <support@themeum.com>
* @link https://themeum.com
* @since 1.0.0
*/
namespace TUTOR;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handle template before include
*
* @since 1.0.0
*/
class Template extends Tutor_Base {
/**
* Register Hooks
*
* @since 1.0.0
*/
public function __construct() {
parent::__construct();
/**
* Should Load Template Override
* Integration for specially oxygen builder
* If we found false of below filter, then we will not use this file
*/
$template_override = apply_filters( 'tutor_lms_should_template_override', true );
if ( ! $template_override ) {
return;
}
add_filter( 'template_include', array( $this, 'load_course_archive_template' ), 99 );
add_filter( 'template_include', array( $this, 'load_single_course_template' ), 99 );
add_filter( 'template_include', array( $this, 'load_single_lesson_template' ), 99 );
add_filter( 'template_include', array( $this, 'play_private_video' ), 99 );
add_filter( 'template_include', array( $this, 'load_quiz_template' ), 99 );
add_filter( 'template_include', array( $this, 'load_assignment_template' ), 99 );
add_filter( 'template_include', array( $this, 'student_public_profile' ), 99 );
add_filter( 'template_include', array( $this, 'tutor_dashboard' ), 99 );
add_filter( 'pre_get_document_title', array( $this, 'student_public_profile_title' ) );
add_filter( 'the_content', array( $this, 'convert_static_page_to_template' ) );
add_action( 'pre_get_posts', array( $this, 'limit_course_query_archive' ), 99 );
}
/**
* Load default template for course
*
* @since v.1.0.0
*
* @param sting $template template name.
*
* @return bool|string
*/
public function load_course_archive_template( $template ) {
global $wp_query;
$post_type = get_query_var( 'post_type' );
if ( ! is_array( $post_type ) ) {
$post_type = array( $post_type );
}
$course_category = get_query_var( 'course-category' );
if ( ( in_array( $this->course_post_type, $post_type, true ) || ! empty( $course_category ) ) && $wp_query->is_archive ) {
$template = tutor_get_template( 'archive-course' );
return $template;
}
return $template;
}
/**
* Limit for course archive listing
*
* Make a page to archive listing for courses
*
* @since 1.0.0
*
* @param mixed $query query argument.
*
* @return void
*/
public function limit_course_query_archive( $query ) {
$courses_per_page = (int) tutor_utils()->get_option( 'courses_per_page', 12 );
if ( $query->is_main_query() && ! $query->is_feed() && ! is_admin() && is_page() ) {
$queried_object = get_queried_object();
if ( $queried_object instanceof \WP_Post ) {
$page_id = $queried_object->ID;
$selected_archive_page = (int) tutor_utils()->get_option( 'course_archive_page' );
if ( $page_id === $selected_archive_page ) {
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$search_query = get_search_query();
query_posts(
array(
'post_type' => $this->course_post_type,
'paged' => $paged,
's' => $search_query,
'posts_per_page' => $courses_per_page,
)
);
}
}
}
if ( $query->is_archive && $query->is_main_query() && ! $query->is_feed() && ! is_admin() ) {
$post_type = get_query_var( 'post_type' );
$course_category = get_query_var( 'course-category' );
if ( ( $post_type === $this->course_post_type || ! empty( $course_category ) ) ) {
$query->set( 'posts_per_page', $courses_per_page );
$query->set( 'post_type', apply_filters( 'tutor_course_archive_post_types', array( $this->course_post_type ) ) );
$course_filter = 'newest_first';
if ( ! empty( Input::get( 'tutor_course_filter', '' ) ) ) {
$course_filter = Input::get( 'tutor_course_filter' );
}
switch ( $course_filter ) {
case 'newest_first':
$query->set( 'orderby', 'ID' );
$query->set( 'order', 'desc' );
break;
case 'oldest_first':
$query->set( 'orderby', 'ID' );
$query->set( 'order', 'asc' );
break;
case 'course_title_az':
$query->set( 'orderby', 'post_title' );
$query->set( 'order', 'asc' );
break;
case 'course_title_za':
$query->set( 'orderby', 'post_title' );
$query->set( 'order', 'desc' );
break;
}
}
}
}
/**
* Load Single Course Template
*
* @since v.1.0.0
*
* @param string $template template name to load.
*
* @return bool|string
*/
public function load_single_course_template( $template ) {
global $wp_query;
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] === $this->course_post_type ) {
do_action( 'single_course_template_before_load', get_the_ID() );
wp_reset_query();
return tutor_get_template( 'single-course' );
}
return $template;
}
/**
* Get root post parent id
*
* @param int $id post id.
*
* @return int root post id
*/
private function get_root_post_parent_id( $id ) {
$ancestors = get_post_ancestors( $id );
$root = is_array( $ancestors ) ? end( $ancestors ) : null;
return is_numeric( $root ) ? $root : $id;
}
/**
* Load lesson template
*
* @since v.1.0.0
*
* @param string $template template name to load.
*
* @return bool|string
*/
public function load_single_lesson_template( $template ) {
global $wp_query;
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] === $this->lesson_post_type ) {
$page_id = get_the_ID();
do_action( 'tutor_lesson_load_before', $template );
setup_postdata( $page_id );
if ( is_user_logged_in() ) {
$has_content_access = tutor_utils()->has_enrolled_content_access( 'lesson' );
if ( $has_content_access ) {
$template = tutor_get_template( 'single-lesson' );
} else {
$template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first
}
} else {
$template = tutor_get_template( 'login' );
}
wp_reset_postdata();
// Forcefully show lessons if it is public and not paid.
$course_id = $this->get_root_post_parent_id( $page_id );
if ( 'yes' === get_post_meta( $course_id, '_tutor_is_public_course', true ) && ! tutor_utils()->is_course_purchasable( $course_id ) ) {
$template = tutor_get_template( 'single-lesson' );
}
return apply_filters( 'tutor_lesson_template', $template );
}
return $template;
}
/**
* Play the video in this url.
*
* @param string $template template to load.
*
* @return mixed
*/
public function play_private_video( $template ) {
global $wp_query;
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['lesson_video'] ) && 'true' === $wp_query->query_vars['lesson_video'] ) {
$is_public_video = apply_filters( 'tutor_video_stream_is_public', false, get_the_ID() );
if ( $is_public_video ) {
$video_info = tutor_utils()->get_video_info();
if ( $video_info ) {
$stream = new Video_Stream( $video_info->path );
$stream->start();
}
exit();
}
if ( tutor_utils()->is_course_enrolled_by_lesson() ) {
$video_info = tutor_utils()->get_video_info();
if ( $video_info ) {
$stream = new Video_Stream( $video_info->path );
$stream->start();
}
} else {
esc_html_e( 'Permission denied', 'tutor' );
}
exit();
}
return $template;
}
/**
* Tutor Dashboard Page, Responsible to show dashboard stuffs
*
* @since v.1.0.0
*
* @param string $content page content.
*
* @return mixed
*/
public function convert_static_page_to_template( $content ) {
// Dashboard Page.
$student_dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
if ( get_the_ID() === $student_dashboard_page_id ) {
$shortcode = new Shortcode();
return $shortcode->tutor_dashboard();
}
// Instructor Registration Page.
$instructor_register_page_page_id = (int) tutor_utils()->get_option( 'instructor_register_page' );
if ( get_the_ID() === $instructor_register_page_page_id ) {
$shortcode = new Shortcode();
return $shortcode->instructor_registration_form();
}
$student_register_page_id = (int) tutor_utils()->get_option( 'student_register_page' );
if ( get_the_ID() === $student_register_page_id ) {
$shortcode = new Shortcode();
return $shortcode->student_registration_form();
}
return $content;
}
/**
* Tutor dashboard
*
* @since 1.0.0
*
* @param string $template template name.
*
* @return string
*/
public function tutor_dashboard( $template ) {
global $wp_query;
$is_page = apply_filters( 'tutor_determine_is_page', $wp_query->is_page, $template );
if ( $is_page ) {
$student_dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
$student_dashboard_page_id = apply_filters( 'tutor_dashboard_page_id_filter', $student_dashboard_page_id );
$is_dashboard_page = apply_filters( 'tutor_determine_is_dashboard_page', get_the_ID() == $student_dashboard_page_id );
if ( $is_dashboard_page ) {
/**
* Handle if logout URL
*
* @since v.1.1.2
*/
if ( tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars ) === 'logout' ) {
$redirect = apply_filters( 'tutor_dashboard_logout_redirect_url', get_permalink( $student_dashboard_page_id ) );
wp_logout();
wp_safe_redirect( $redirect );
die();
}
$dashboard_page = tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars );
$get_dashboard_config = tutor_utils()->tutor_dashboard_permalinks();
$target_dashboard_page = tutor_utils()->array_get( $dashboard_page, $get_dashboard_config );
if ( isset( $target_dashboard_page['login_require'] ) && false === $target_dashboard_page['login_require'] ) {
$template = tutor_load_template_part( "template-part.{$dashboard_page}" );
} else {
/**
* Load view page based on dashboard Endpoint
*/
if ( is_user_logged_in() ) {
global $wp;
$full_path = explode( '/', trim( str_replace( get_home_url(), '', home_url( $wp->request ) ), '/' ) );
$template = tutor_get_template( 'create-course' === end( $full_path ) ? 'dashboard.create-course' : 'dashboard' );
/**
* Check page page permission
*
* @since 1.3.4
*/
$query_var = tutor_utils()->array_get( 'tutor_dashboard_page', $wp_query->query_vars );
$dashboard_pages = tutor_utils()->tutor_dashboard_pages();
$dashboard_page_item = tutor_utils()->array_get( $query_var, $dashboard_pages );
$auth_cap = tutor_utils()->array_get( 'auth_cap', $dashboard_page_item );
if ( $auth_cap && ! current_user_can( $auth_cap ) ) {
$template = tutor_get_template( 'permission-denied' );
}
} else {
$template = tutor_get_template( 'login' );
}
}
}
}
return $template;
}
/**
* Load quiz template
*
* @since 1.0.0
*
* If course public then enrollment not required
*
* @since 2.0.2
*
* @param string $template template to load.
*
* @return bool|string
*/
public function load_quiz_template( $template ) {
global $wp_query, $post;
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && 'tutor_quiz' === $wp_query->query_vars['post_type'] ) {
if ( is_user_logged_in() ) {
$has_content_access = tutor_utils()->has_enrolled_content_access( 'quiz' );
$course_id = tutor_utils()->get_course_id_by_content( $post );
$is_public = Course_List::is_public( $course_id );
// if public course don't need to be enrolled.
if ( $has_content_access || $is_public ) {
$template = tutor_get_template( 'single-quiz' );
} else {
$template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first.
}
} else {
$template = tutor_get_template( 'login' );
}
return $template;
}
return $template;
}
/**
* Load assignment template
*
* @since 1.0.0
*
* @param string $template template file to load.
*
* @return string template path
*/
public function load_assignment_template( $template ) {
global $wp_query;
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && 'tutor_assignments' === $wp_query->query_vars['post_type'] ) {
if ( is_user_logged_in() ) {
$has_content_access = tutor_utils()->has_enrolled_content_access( 'assignment' );
if ( $has_content_access ) {
$template = tutor_get_template( 'single-assignment' );
} else {
$template = tutor_get_template( 'single.lesson.required-enroll' ); // You need to enroll first
}
} else {
$template = tutor_get_template( 'login' );
}
return $template;
}
return $template;
}
/**
* Student public profile
*
* @since 1.0.0
*
* @param string $template profile template.
*
* @return bool|string
*/
public function student_public_profile( $template ) {
global $wp_query;
$query_var = $wp_query->query_vars;
if ( ! empty( $wp_query->query['tutor_profile_username'] ) ) {
$template = tutor_get_template( 'public-profile' );
}
return $template;
}
/**
* Show student Profile
*
* @since 1.0.0
*
* @return string
*/
public function student_public_profile_title() {
global $wp_query;
if ( ! empty( $wp_query->query['tutor_profile_username'] ) ) {
global $wpdb;
$user_name = sanitize_text_field( $wp_query->query['tutor_profile_username'] );
$user = $wpdb->get_row( $wpdb->prepare( "SELECT display_name from {$wpdb->users} WHERE user_login = %s limit 1; ", $user_name ) );
if ( ! empty( $user->display_name ) ) {
return sprintf( "%s's %s", $user->display_name, __( 'Profile Page', 'tutor' ) );
}
}
return '';
}
}