PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.1
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / lp-template-functions.php

lp-template-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.1, at inc/lp-template-functions.php

1,724 lines 43.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * All functions for LearnPress template
4 *
5 * @author ThimPress
6 * @package LearnPress/Functions
7 * @version 1.0
8 */
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * @see LP_Template_Course::button_retry()
15 * @see LP_Template_Course::course_continue_button()
16 * @see LP_Template_Course::course_external_button()
17 */
18 if ( ! function_exists( 'learn_press_add_course_buttons' ) ) {
19 function learn_press_add_course_buttons() {
20 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_enroll_button' ), 5 );
21 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_purchase_button' ), 10 );
22 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_external_button' ), 15 );
23 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'button_retry' ), 20 );
24 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_continue_button' ), 25 );
25 add_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_finish_button' ), 30 );
26 }
27 }
28
29 if ( ! function_exists( 'learn_press_remove_course_buttons' ) ) {
30 function learn_press_remove_course_buttons() {
31 remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_enroll_button' ), 5 );
32 remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_purchase_button' ), 10 );
33 //remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_external_button' ), 15 );
34 remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'button_retry' ), 20 );
35 remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_continue_button' ), 25 );
36 remove_action( 'learn-press/course-buttons', LearnPress::instance()->template( 'course' )->func( 'course_finish_button' ), 30 );
37 }
38 }
39
40 if ( ! function_exists( 'learn_press_get_course_tabs' ) ) {
41 /**
42 * Return an array of tabs display in single course page.
43 *
44 * @return array
45 */
46 function learn_press_get_course_tabs() {
47 $course = learn_press_get_course();
48 $user = learn_press_get_current_user();
49
50 $defaults = array();
51
52 /**
53 * Show tab overview if
54 * 1. Course is preview
55 * OR
56 * 2. Course's content not empty
57 */
58 if ( isset( $_GET['preview'] ) || $course ) {
59 $defaults['overview'] = array(
60 'title' => esc_html__( 'Overview', 'learnpress' ),
61 'priority' => 10,
62 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/overview.php' ),
63 );
64 }
65
66 $defaults['curriculum'] = array(
67 'title' => esc_html__( 'Curriculum', 'learnpress' ),
68 'priority' => 30,
69 'callback' => LearnPress::instance()->template( 'course' )->func( 'course_curriculum' ),
70 );
71
72 $defaults['instructor'] = array(
73 'title' => esc_html__( 'Instructor', 'learnpress' ),
74 'priority' => 40,
75 'callback' => LearnPress::instance()->template( 'course' )->callback( 'single-course/tabs/instructor.php' ),
76 );
77
78 if ( $course->get_faqs() ) {
79 $defaults['faqs'] = array(
80 'title' => esc_html__( 'FAQs', 'learnpress' ),
81 'priority' => 50,
82 'callback' => LearnPress::instance()->template( 'course' )->func( 'faqs' ),
83 );
84 }
85
86 $tabs = apply_filters( 'learn-press/course-tabs', $defaults );
87
88 if ( $tabs ) {
89 uasort( $tabs, 'learn_press_sort_list_by_priority_callback' );
90 $request_tab = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] ?? '' );
91 $has_active = false;
92
93 foreach ( $tabs as $k => $v ) {
94 $v['id'] = ! empty( $v['id'] ) ? $v['id'] : 'tab-' . $k;
95
96 if ( $request_tab === $v['id'] ) {
97 $v['active'] = true;
98 $has_active = $k;
99 } elseif ( isset( $v['active'] ) && $v['active'] ) {
100 $has_active = true;
101 }
102 $tabs[ $k ] = $v;
103 }
104
105 if ( ! $has_active ) {
106 if ( $course && $user->has_course_status(
107 $course->get_id(),
108 array(
109 'enrolled',
110 'finished',
111 )
112 ) && ! empty( $tabs['curriculum'] )
113 ) {
114 $tabs['curriculum']['active'] = true;
115 } elseif ( ! empty( $tabs['overview'] ) ) {
116 $tabs['overview']['active'] = true;
117 } else {
118 $keys = array_keys( $tabs );
119 $first_key = reset( $keys );
120 $tabs[ $first_key ]['active'] = true;
121 }
122 }
123 }
124
125 return $tabs;
126 }
127 }
128
129 if ( ! function_exists( 'learn_press_content_item_summary_question' ) ) {
130 function learn_press_content_item_summary_question() {
131 $quiz = LP_Global::course_item_quiz();
132 $question = $quiz->get_viewing_question();
133
134 if ( $question ) {
135 $course = learn_press_get_course();
136 $user = learn_press_get_current_user();
137 $answered = false;
138 $course_data = $user->get_course_data( $course->get_id() );
139 $user_quiz = $course_data->get_item_quiz( $quiz->get_id() );
140
141 if ( $user_quiz ) {
142 $answered = $user_quiz->get_question_answer( $question->get_id() );
143 $question->show_correct_answers(
144 $user->has_checked_answer(
145 $question->get_id(),
146 $quiz->get_id(),
147 $course->get_id()
148 ) ? 'yes' : false
149 );
150 $question->disable_answers( $user_quiz->get_status() == 'completed' ? 'yes' : false );
151 }
152
153 $question->render( $answered );
154 }
155 }
156 }
157
158
159 if ( ! function_exists( 'learn_press_content_item_body_class' ) ) {
160 // Add more assets into page that displaying content of an item
161 add_filter( 'body_class', 'learn_press_content_item_body_class', 10 );
162
163 function learn_press_content_item_body_class( $classes ) {
164 global $lp_course_item;
165
166 if ( wp_is_mobile() ) {
167 $sidebar_toggle_class = 'lp-sidebar-toggle__close';
168 } else {
169 $sidebar_toggle_class = learn_press_cookie_get( 'sidebar-toggle' ) ? 'lp-sidebar-toggle__close' : 'lp-sidebar-toggle__open';
170 }
171
172 if ( $lp_course_item ) {
173 $classes[] = 'course-item-popup';
174 $classes[] = 'viewing-course-item';
175 $classes[] = 'viewing-course-item-' . $lp_course_item->get_id();
176 $classes[] = 'course-item-' . $lp_course_item->get_item_type();
177 $classes[] = $sidebar_toggle_class;
178 }
179
180 return $classes;
181 }
182 }
183
184 if ( ! function_exists( 'learn_press_content_item_edit_links' ) ) {
185 /**
186 * Add edit links for course item question to admin bar.
187 */
188 function learn_press_content_item_edit_links() {
189 global $wp_admin_bar, $post, $lp_course_item, $lp_quiz_question;
190
191 if ( ! ( ! is_admin() && is_user_logged_in() ) ) {
192 return;
193 }
194
195 /**
196 * Edit link for lesson/quiz or any other course's item.
197 */
198 if ( $lp_course_item ) {
199 $post_type_object = get_post_type_object( $lp_course_item->get_item_type() );
200
201 if ( $post_type_object && current_user_can(
202 'edit_post',
203 $lp_course_item->get_id()
204 ) && $post_type_object->show_in_admin_bar && get_edit_post_link( $lp_course_item->get_id() ) ) {
205 $type = get_post_type( $lp_course_item->get_id() );
206
207 if ( apply_filters( 'learn-press/edit-admin-bar-button', true, $lp_course_item ) ) {
208 $wp_admin_bar->add_menu(
209 array(
210 'id' => 'edit-' . $type,
211 'title' => $post_type_object->labels->edit_item,
212 'href' => get_edit_post_link( $lp_course_item->get_id() ),
213 )
214 );
215 }
216 }
217 }
218
219 /**
220 * Edit link for quiz's question.
221 */
222 if ( $lp_quiz_question ) {
223 $post_type_object = get_post_type_object( $lp_quiz_question->get_item_type() );
224 $edit_post_link = get_edit_post_link( $lp_quiz_question->get_id() );
225
226 if ( $post_type_object && current_user_can(
227 'edit_post',
228 $lp_quiz_question->get_id()
229 ) && $post_type_object->show_in_admin_bar && $edit_post_link ) {
230 $type = get_post_type( $lp_quiz_question->get_id() );
231 $wp_admin_bar->add_menu(
232 array(
233 'id' => 'edit-' . $type,
234 'title' => $post_type_object->labels->edit_item,
235 'href' => $edit_post_link,
236 )
237 );
238 }
239 }
240
241 }
242 }
243 add_filter( 'admin_bar_menu', 'learn_press_content_item_edit_links', 90 );
244
245 if ( ! function_exists( 'learn_press_single_quiz_args' ) ) {
246 function learn_press_single_quiz_args() {
247 $args = array();
248
249 if ( LP_PAGE_QUIZ !== LP_Page_Controller::page_current() ) {
250 return $args;
251 }
252
253 $quiz = LP_Global::course_item_quiz();
254 $course = learn_press_get_course();
255
256 if ( $quiz && $course ) {
257 $user = learn_press_get_current_user();
258 $course_id = $course->get_id();
259 $user_quiz = $user->get_item_data( $quiz->get_id(), $course_id );
260
261 if ( $user_quiz ) {
262 $remaining_time = $user_quiz->get_time_remaining();
263 } else {
264 $remaining_time = false;
265 }
266
267 $args = array(
268 'id' => $quiz->get_id(),
269 //'totalTime' => -1,
270 //'remainingTime' => $remaining_time ? $remaining_time->get() : $quiz->get_duration()->get(),
271 'status' => $user->get_item_status( $quiz->get_id(), $course_id ),
272 'checkNorequizenroll' => $course->is_no_required_enroll(),
273 'navigationPosition' => LP_Settings::get_option( 'navigation_position', 'yes' ),
274 );
275 }
276
277 return $args;
278 }
279 }
280
281 if ( ! function_exists( 'learn_press_single_document_title_parts' ) ) {
282 /**
283 * Custom document title depending on LP current page.
284 * E.g: Single course, profile, etc...
285 *
286 * @param array $title
287 *
288 * @return array
289 * @since 3.0.0
290 */
291 function learn_press_single_document_title_parts( $title ) {
292 if ( learn_press_is_course() ) {
293 $item = LP_Global::course_item();
294
295 if ( $item ) {
296 $title['title'] = join(
297 ' ',
298 apply_filters(
299 'learn-press/document-course-title-parts',
300 array(
301 $title['title'],
302 ' &rarr; ',
303 $item->get_title(),
304 )
305 )
306 );
307 }
308 } elseif ( learn_press_is_courses() ) {
309 if ( learn_press_is_search() ) {
310 $title['title'] = esc_html__( 'Course Search Results', 'learnpress' );
311 } else {
312 $title['title'] = esc_html__( 'Courses', 'learnpress' );
313 }
314 } elseif ( learn_press_is_profile() ) {
315 $profile = LP_Profile::instance();
316 $tab_slug = $profile->get_current_tab();
317 $tab = $profile->get_tab_at( $tab_slug );
318 $page_id = learn_press_get_page_id( 'profile' );
319
320 if ( $page_id ) {
321 $page_title = get_the_title( $page_id );
322 } else {
323 $page_title = '';
324 }
325
326 if ( $tab ) {
327 $title['title'] = join(
328 ' ',
329 apply_filters(
330 'learn-press/document-profile-title-parts',
331 array(
332 $page_title,
333 '&rarr;',
334 $tab['title'],
335 )
336 )
337 );
338 }
339 }
340
341 return $title;
342 }
343 }
344
345 if ( ! function_exists( 'learn_press_course_item_class' ) ) {
346 function learn_press_course_item_class( $item_id, $course_id = 0, $class = null ) {
347 switch ( get_post_type( $item_id ) ) {
348 case 'lp_lesson':
349 learn_press_course_lesson_class( $item_id, $course_id, $class );
350 break;
351 case 'lp_quiz':
352 learn_press_course_quiz_class( $item_id, $course_id, $class );
353 break;
354 }
355 }
356 }
357
358 if ( ! function_exists( 'learn_press_course_lesson_class' ) ) {
359 /**
360 * The class of lesson in course curriculum
361 *
362 * @param int $lesson_id
363 * @param int $course_id
364 * @param array|string $class
365 * @param boolean $echo
366 *
367 * @return mixed
368 */
369 function learn_press_course_lesson_class( $lesson_id = null, $course_id = 0, $class = null, $echo = true ) {
370 $user = learn_press_get_current_user();
371
372 if ( ! $course_id ) {
373 $course_id = get_the_ID();
374 }
375
376 $course = learn_press_get_course( $course_id );
377 if ( ! $course ) {
378 return '';
379 }
380
381 if ( is_string( $class ) && $class ) {
382 $class = preg_split( '!\s+!', $class );
383 } else {
384 $class = array();
385 }
386
387 $classes = array(
388 'course-lesson course-item course-item-' . $lesson_id,
389 );
390
391 $user = learn_press_get_current_user();
392 $status = $user->get_item_status( $lesson_id );
393
394 if ( $status ) {
395 $classes[] = "item-has-status item-{$status}";
396 }
397
398 if ( $lesson_id && $course->is_current_item( $lesson_id ) ) {
399 $classes[] = 'item-current';
400 }
401
402 if ( learn_press_is_course() ) {
403 if ( $course->is_free() ) {
404 $classes[] = 'free-item';
405 }
406 }
407
408 $lesson = LP_Lesson::get_lesson( $lesson_id );
409
410 if ( $lesson && $lesson->is_preview() ) {
411 $classes[] = 'preview-item';
412 }
413
414 $classes = array_unique( array_merge( $classes, $class ) );
415
416 if ( $echo ) {
417 echo 'class="' . implode( ' ', $classes ) . '"';
418 }
419
420 return $classes;
421 }
422 }
423
424 if ( ! function_exists( 'learn_press_course_quiz_class' ) ) {
425 /**
426 * The class of lesson in course curriculum
427 *
428 * @param int $quiz_id
429 * @param int $course_id
430 * @param string|array $class
431 * @param boolean $echo
432 *
433 * @return mixed
434 */
435 function learn_press_course_quiz_class( $quiz_id = null, $course_id = 0, $class = null, $echo = true ) {
436 $user = learn_press_get_current_user();
437
438 if ( ! $course_id ) {
439 $course_id = get_the_ID();
440 }
441
442 if ( is_string( $class ) && $class ) {
443 $class = preg_split( '!\s+!', $class );
444 } else {
445 $class = array();
446 }
447
448 $course = learn_press_get_course( $course_id );
449
450 if ( ! $course ) {
451 return '';
452 }
453
454 $classes = array(
455 'course-quiz course-item course-item-' . $quiz_id,
456 );
457
458 $status = $user->get_item_status( $quiz_id );
459
460 if ( $status ) {
461 $classes[] = "item-has-status item-{$status}";
462 }
463
464 if ( $quiz_id && $course->is_current_item( $quiz_id ) ) {
465 $classes[] = 'item-current';
466 }
467
468 /*
469 if ( $user->can_view_item( $quiz_id, $course_id )->flag ) {
470 $classes[] = 'viewable';
471 }*/
472
473 if ( $course->is_final_quiz( $quiz_id ) ) {
474 $classes[] = 'final-quiz';
475 }
476
477 $classes = array_unique( array_merge( $classes, $class ) );
478
479 if ( $echo ) {
480 echo 'class="' . implode( ' ', $classes ) . '"';
481 }
482
483 return $classes;
484 }
485 }
486
487 if ( ! function_exists( 'learn_press_course_class' ) ) {
488 /**
489 * Append new class to course post type
490 *
491 * @param $classes
492 * @param $class
493 * @param $post_id
494 *
495 * @return string
496 */
497 function learn_press_course_class( $classes, $class, $post_id = '' ) {
498 if ( is_learnpress() ) {
499 $classes = (array) $classes;
500 }
501
502 if ( $post_id === 0 ) {
503 $classes[] = 'page type-page';
504 }
505
506 if ( ! $post_id || 'lp_course' !== get_post_type( $post_id ) ) {
507 return $classes;
508 }
509
510 $classes[] = 'course';
511
512 return apply_filters( 'learn_press_course_class', $classes );
513 }
514 }
515
516 function learn_press_setup_user() {
517 $GLOBALS['lp_user'] = learn_press_get_current_user();
518 }
519 add_action( 'init', 'learn_press_setup_user', 1000 );
520
521 /**
522 * Display a message immediately with out push into queue
523 *
524 * @param $message
525 * @param string $type
526 */
527
528 function learn_press_display_message( $message, $type = 'success' ) {
529 $messages = learn_press_session_get( learn_press_session_message_id() );
530 learn_press_session_set( learn_press_session_message_id(), null );
531
532 // add new notice and display
533 learn_press_add_message( $message, $type );
534 echo learn_press_get_messages( true );
535
536 // store back messages
537 learn_press_session_set( learn_press_session_message_id(), $messages );
538 }
539
540 /**
541 * Returns all notices added
542 *
543 * @param bool $clear
544 *
545 * @return string
546 */
547 function learn_press_get_messages( $clear = false ) {
548 ob_start();
549 learn_press_print_messages( $clear );
550
551 return ob_get_clean();
552 }
553
554 /**
555 * Add new message into queue for displaying.
556 *
557 * @param string $message
558 * @param string $type
559 * @param array $options
560 * @param int|bool $current_user . @since 3.0.9 - add for current user only
561 */
562 function learn_press_add_message( $message, $type = 'success', $options = array(), $current_user = true ) {
563 if ( is_string( $options ) ) {
564 $options = array( 'id' => $options );
565 }
566
567 $options = wp_parse_args(
568 $options,
569 array(
570 'id' => '',
571 )
572 );
573
574 if ( $current_user ) {
575 if ( true === $current_user ) {
576 $current_user = get_current_user_id();
577 }
578 }
579
580 $key = "messages{$current_user}";
581
582 $messages = learn_press_session_get( $key );
583
584 if ( empty( $messages[ $type ] ) ) {
585 $messages[ $type ] = array();
586 }
587
588 $messages[ $type ][ $options['id'] ] = array(
589 'content' => $message,
590 'options' => $options,
591 );
592
593 learn_press_session_set( $key, $messages );
594 }
595
596 function learn_press_get_message( $message, $type = 'success' ) {
597 ob_start();
598 learn_press_display_message( $message, $type );
599 $message = ob_get_clean();
600
601 return $message;
602 }
603
604 /**
605 * Remove message added into queue by id and/or type.
606 *
607 * @param string $id
608 * @param string|array $type
609 *
610 * @since 3.0.0
611 */
612 function learn_press_remove_message( $id = '', $type = '' ) {
613 $groups = learn_press_session_get( learn_press_session_message_id() );
614
615 if ( ! $groups ) {
616 return;
617 }
618
619 settype( $type, 'array' );
620
621 if ( $id ) {
622 foreach ( $groups as $message_type => $messages ) {
623 if ( ! sizeof( $type ) ) {
624 if ( isset( $groups[ $message_type ][ $id ] ) ) {
625 unset( $groups[ $message_type ][ $id ] );
626 }
627 } elseif ( in_array( $message_type, $type ) ) {
628 if ( isset( $groups[ $message_type ][ $id ] ) ) {
629 unset( $groups[ $message_type ][ $id ] );
630 }
631 }
632 }
633 } elseif ( sizeof( $type ) ) {
634 foreach ( $type as $t ) {
635 if ( isset( $groups[ $t ] ) ) {
636 unset( $groups[ $t ] );
637 }
638 }
639 } else {
640 $groups = array();
641 }
642
643 learn_press_session_set( learn_press_session_message_id(), $groups );
644 }
645
646 /**
647 * Print out the message stored in the queue
648 *
649 * @param bool
650 */
651 function learn_press_print_messages( $clear = true ) {
652 $messages = learn_press_session_get( learn_press_session_message_id() );
653 learn_press_get_template( 'global/message.php', array( 'messages' => $messages ) );
654
655 if ( $clear ) {
656 learn_press_session_set( learn_press_session_message_id(), array() );
657 }
658 }
659
660 function learn_press_message_count( $type = '' ) {
661 $count = 0;
662 $messages = learn_press_session_get( learn_press_session_message_id(), array() );
663
664 if ( isset( $messages[ $type ] ) ) {
665 $count = absint( sizeof( $messages[ $type ] ) );
666 } elseif ( empty( $type ) ) {
667 foreach ( $messages as $message ) {
668 $count += absint( sizeof( $message ) );
669 }
670 }
671
672 return $count;
673 }
674
675 function learn_press_session_message_id() {
676 return 'messages' . get_current_user_id();
677 }
678
679 /**
680 * Displays messages before main content
681 */
682 function _learn_press_print_messages() {
683 $item = LP_Global::course_item();
684 if ( ( 'learn_press_before_main_content' == current_action() ) && $item ) {
685 return;
686 }
687 learn_press_print_messages( true );
688 }
689
690 add_action( 'learn_press_before_main_content', '_learn_press_print_messages', 50 );
691 add_action( 'learn-press/before-course-item-content', '_learn_press_print_messages', 50 );
692
693 if ( ! function_exists( 'learn_press_page_title' ) ) {
694
695 /**
696 * learn_press_page_title function.
697 *
698 * @param boolean $echo
699 * @return string
700 */
701 function learn_press_page_title( bool $echo = false ): string {
702 $page_title = '';
703
704 if ( is_search() ) {
705 // Comment by tungnx
706 /*$page_title = sprintf( __( 'Search Results for: &ldquo;%s&rdquo;', 'learnpress' ), get_search_query() );
707
708 if ( get_query_var( 'paged' ) ) {
709 $page_title .= sprintf( __( '&nbsp;&ndash; Page %s', 'learnpress' ), get_query_var( 'paged' ) );
710 }*/
711 } elseif ( is_tax() ) {
712 $page_title = single_term_title( '', false );
713 } else {
714 $courses_page_id = learn_press_get_page_id( 'courses' );
715 $page_title = get_the_title( $courses_page_id );
716 }
717
718 return apply_filters( 'learn_press_page_title', $page_title );
719 }
720 }
721
722 /**
723 * Get template part.
724 *
725 * @param string $slug
726 * @param string $name
727 *
728 * @return string
729 */
730 function learn_press_get_template_part( $slug, $name = '' ) {
731 $template = '';
732
733 if ( $name ) {
734 $template = locate_template(
735 array(
736 "{$slug}-{$name}.php",
737 learn_press_template_path() . "/{$slug}-{$name}.php",
738 )
739 );
740 }
741
742 // Get default slug-name.php
743 if ( ! $template && $name && file_exists( LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php" ) ) {
744 $template = LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php";
745 }
746
747 // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/learnpress/slug.php
748 if ( ! $template ) {
749 $template = locate_template( array( "{$slug}.php", learn_press_template_path() . "/{$slug}.php" ) );
750 }
751 // override path of child theme in parent theme - Fix for eduma by tuanta
752 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
753 if ( $file_ct_in_pr && $name ) {
754 $template_child = locate_template(
755 array(
756 "{$slug}-{$name}.php",
757 'lp-child-path/' . learn_press_template_path() . '/' . $file_ct_in_pr . "/{$slug}-{$name}.php",
758 )
759 );
760 if ( $template_child && file_exists( $template_child ) ) {
761 $template = $template_child;
762 }
763 // check in child theme if have filter learn_press_child_in_parrent_template_path
764
765 $check_child_theme = get_stylesheet_directory() . '/' . learn_press_template_path() . "{$slug}-{$name}.php";
766 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
767 $template = $check_child_theme;
768 }
769 }
770
771 // Allow 3rd party plugin filter template file from their plugin
772 if ( $template ) {
773 $template = apply_filters( 'learn_press_get_template_part', $template, $slug, $name );
774 }
775 if ( $template && file_exists( $template ) ) {
776 load_template( $template, false );
777 }
778
779 return $template;
780 }
781
782 /**
783 * Get other templates passing attributes and including the file.
784 *
785 * @param string $template_name .
786 * @param array $args (default: array()) .
787 * @param string $template_path (default: '').
788 * @param string $default_path (default: '').
789 *
790 * @return void
791 */
792 function learn_press_get_template( $template_name = '', $args = array(), $template_path = '', $default_path = '' ) {
793 if ( $args && is_array( $args ) ) {
794 extract( $args );
795 }
796
797 if ( false === strpos( $template_name, '.php' ) ) {
798 $template_name .= '.php';
799 }
800
801 $located = learn_press_locate_template( $template_name, $template_path, $default_path );
802
803 if ( ! file_exists( $located ) ) {
804 _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
805
806 $log = sprintf( 'TEMPLATE MISSING: Template %s doesn\'t exists.', $template_name );
807 error_log( $log );
808
809 if ( LP_Debug::is_debug() ) {
810 echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log );
811 }
812
813 return;
814 }
815
816 // Allow 3rd party plugin filter template file from their plugin
817 $located = apply_filters(
818 'learn_press_get_template',
819 $located,
820 $template_name,
821 $args,
822 $template_path,
823 $default_path
824 );
825 if ( $located != '' ) {
826 do_action( 'learn_press_before_template_part', $template_name, $template_path, $located, $args );
827
828 include $located;
829
830 do_action( 'learn_press_after_template_part', $template_name, $template_path, $located, $args );
831 }
832 }
833
834 /**
835 * Get template content
836 *
837 * @param $template_name
838 * @param array $args
839 * @param string $template_path
840 * @param string $default_path
841 *
842 * @return string
843 * @uses learn_press_get_template();
844 */
845 function learn_press_get_template_content( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
846 ob_start();
847 learn_press_get_template( $template_name, $args, $template_path, $default_path );
848
849 return ob_get_clean();
850 }
851
852 /**
853 * Locate a template and return the path for inclusion.
854 *
855 * @param string $template_name
856 * @param string $template_path (default: '')
857 * @param string $default_path (default: '')
858 *
859 * @return string
860 */
861 function learn_press_locate_template( $template_name, $template_path = '', $default_path = '' ) {
862 if ( ! $template_path ) {
863 $template_path = learn_press_template_path();
864 }
865
866 if ( ! $default_path ) {
867 $default_path = LP_PLUGIN_PATH . 'templates/';
868 }
869
870 /**
871 * Disable override templates in theme by default since LP 4.0.0
872 */
873 if ( learn_press_override_templates() ) {
874 $template = locate_template(
875 array(
876 trailingslashit( $template_path ) . $template_name,
877 $template_name,
878 )
879 );
880 // override path of child theme in parent theme - Fix for eduma by tuanta
881 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
882 if ( $file_ct_in_pr ) {
883 $template_child = locate_template(
884 array(
885 trailingslashit( 'lp-child-path/' . $template_path . '/' . $file_ct_in_pr ) . $template_name,
886 $template_name,
887 )
888 );
889 if ( $template_child && file_exists( $template_child ) ) {
890 $template = $template_child;
891 }
892 // check in child theme if have filter learn_press_child_in_parrent_template_path
893 $check_child_theme = get_stylesheet_directory() . '/' . trailingslashit( $template_path ) . $template_name;
894 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
895 $template = $check_child_theme;
896 }
897 }
898 }
899 if ( ! isset( $template ) || ! $template ) {
900 $template = trailingslashit( $default_path ) . $template_name;
901 }
902
903 return apply_filters( 'learn_press_locate_template', $template, $template_name, $template_path );
904 }
905
906 /**
907 * Returns the name of folder contains template files in theme
908 *
909 * @param bool
910 *
911 * @return string
912 */
913 function learn_press_template_path( $slash = false ) {
914 return apply_filters( 'learn_press_template_path', 'learnpress', $slash ) . ( $slash ? '/' : '' );
915 }
916
917 /**
918 * Disable override templates in theme by default
919 *
920 * @return bool
921 * @since 4.0.0
922 */
923 function learn_press_override_templates() {
924 return apply_filters( 'learn-press/override-templates', false );
925 }
926
927 if ( ! function_exists( 'learn_press_is_404' ) ) {
928 /**
929 * Set header is 404
930 */
931 function learn_press_is_404() {
932 global $wp_query;
933 if ( ! empty( $_REQUEST['debug-404'] ) ) {
934 learn_press_debug( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $_REQUEST['debug-404'] ) );
935 }
936 $wp_query->set_404();
937 status_header( 404 );
938 }
939 }
940
941 if ( ! function_exists( 'learn_press_404_page' ) ) {
942 /**
943 * Display 404 page
944 */
945 function learn_press_404_page() {
946 learn_press_is_404();
947 }
948 }
949
950 if ( ! function_exists( 'learn_press_generate_template_information' ) ) {
951 function learn_press_generate_template_information( $template_name, $template_path, $located, $args ) {
952 $debug = learn_press_get_request( 'show-template-location' );
953 if ( $debug == 'on' ) {
954 echo '<!-- Template Location:' . str_replace( array( LP_PLUGIN_PATH, ABSPATH ), '', $located ) . ' -->';
955 }
956 }
957 }
958
959 if ( ! function_exists( 'learn_press_course_remaining_time' ) ) {
960 /**
961 * Show the time remain of a course
962 */
963 function learn_press_course_remaining_time() {
964 $user = learn_press_get_current_user();
965 if ( ! $user->has_finished_course( get_the_ID() ) && $text = learn_press_get_course( get_the_ID() )->get_user_duration_html( $user->get_id() ) ) {
966 learn_press_display_message( $text );
967 }
968 }
969 }
970
971 if ( ! function_exists( 'learn_press_item_meta_type' ) ) {
972 function learn_press_item_meta_type( $course, $item ) {
973 ?>
974
975 <?php if ( $item->post_type == 'lp_quiz' ) { ?>
976
977 <span class="lp-label lp-label-quiz"><?php _e( 'Quiz', 'learnpress' ); ?></span>
978
979 <?php if ( $course->final_quiz == $item->ID ) { ?>
980
981 <span class="lp-label lp-label-final"><?php _e( 'Final', 'learnpress' ); ?></span>
982
983 <?php } ?>
984
985 <?php } elseif ( $item->post_type == 'lp_lesson' ) { ?>
986
987 <span class="lp-label lp-label-lesson"><?php _e( 'Lesson', 'learnpress' ); ?></span>
988 <?php if ( get_post_meta( $item->ID, '_lp_preview', true ) == 'yes' ) { ?>
989
990 <span class="lp-label lp-label-preview"><?php _e( 'Preview', 'learnpress' ); ?></span>
991
992 <?php } ?>
993
994 <?php } else { ?>
995
996 <?php do_action( 'learn_press_item_meta_type', $course, $item ); ?>
997
998 <?php
999 }
1000 }
1001 }
1002
1003 if ( ! function_exists( 'learn_press_sort_course_tabs' ) ) {
1004 function learn_press_sort_course_tabs( $tabs = array() ) {
1005 uasort( $tabs, 'learn_press_sort_list_by_priority_callback' );
1006
1007 return $tabs;
1008 }
1009 }
1010
1011 if ( ! function_exists( 'learn_press_get_profile_display_name' ) ) {
1012 /**
1013 * Get Display name publicly as in Profile page
1014 */
1015 function learn_press_get_profile_display_name( $user ) {
1016 if ( empty( $user ) ) {
1017 return '';
1018 }
1019
1020 $id = '';
1021
1022 if ( $user instanceof LP_Abstract_User ) {
1023 $id = $user->get_id();
1024 } elseif ( $user instanceof WP_User ) {
1025 $id = $user->ID;
1026 } elseif ( is_numeric( $user ) ) {
1027 $id = $user;
1028 }
1029
1030 if ( ! isset( $id ) ) {
1031 return '';
1032 }
1033
1034 $info = get_userdata( $id );
1035
1036 return $info ? $info->display_name : '';
1037 }
1038 }
1039
1040 /**
1041 * @deprecated 4.1.6.9
1042 */
1043 /*if ( ! function_exists( 'learn_press_content_item_comments' ) ) {
1044 function learn_press_content_item_comments() {
1045 $item = LP_Global::course_item();
1046
1047 if ( ! $item ) {
1048 return;
1049 }
1050
1051 if ( ! $item->is_support( 'comments' ) ) {
1052 return;
1053 }
1054
1055 global $post;
1056
1057 $post = get_post( $item->get_id() );
1058
1059 setup_postdata( $post );
1060
1061 if ( ! have_comments() ) {
1062 return;
1063 }
1064
1065 add_filter( 'deprecated_file_trigger_error', '__return_false' );
1066 comments_template();
1067 remove_filter( 'deprecated_file_trigger_error', '__return_false' );
1068
1069 wp_reset_postdata();
1070 }
1071 }*/
1072
1073 function learn_press_course_comments_open( $open, $post_id ) {
1074 $post = get_post( $post_id );
1075
1076 if ( LP_COURSE_CPT == $post->post_type ) {
1077 $open = false;
1078 }
1079
1080 return $open;
1081 }
1082
1083 function learn_press_is_content_item_only() {
1084 return ! empty( $_REQUEST['content-item-only'] );
1085 }
1086
1087 function learn_press_label_html( $label, $type = '' ) {
1088 ?>
1089 <span class="lp-label label-<?php echo esc_attr( $type ? $type : $label ); ?>">
1090 <?php echo esc_html( $label ); ?>
1091 </span>
1092 <?php
1093 }
1094
1095 /**
1096 * @deprecated 4.1.6.4
1097 */
1098 function learn_press_get_course_redirect( $link ) {
1099 _deprecated_function( __FUNCTION__, '4.1.6.4' );
1100 if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
1101 return $link;
1102 }
1103
1104 $referer = $_SERVER['HTTP_REFERER'];
1105 $info_a = parse_url( $referer );
1106 $info_b = parse_url( $link );
1107
1108 $a = explode( '/', $info_a['path'] );
1109 $a = array_filter( $a );
1110
1111 $b = explode( '/', $info_b['path'] );
1112 $b = array_filter( $b );
1113
1114 $same = array_intersect_assoc( $a, $b );
1115
1116 $a = array_diff_assoc( $a, $same );
1117 $b = array_diff_assoc( $b, $same );
1118
1119 $a = array_values( $a );
1120 $b = array_values( $b );
1121
1122 if ( array_shift( $a ) === 'popup' ) {
1123 unset( $a[0] );
1124
1125 if ( ! ( array_diff_assoc( $a, $b ) ) ) {
1126 $link = '';
1127 foreach ( array( 'scheme', 'host', 'port', 'path' ) as $v ) {
1128 if ( ! isset( $info_a[ $v ] ) ) {
1129 continue;
1130 }
1131
1132 if ( $v == 'scheme' ) {
1133 $sep = '://';
1134 } elseif ( $v == 'host' ) {
1135 $sep = '';
1136 } elseif ( $v == 'port' ) {
1137 $link .= ':';
1138 $sep = '';
1139 } else {
1140 $sep = '/';
1141 }
1142 $link = $link . $info_a[ $v ] . $sep;
1143 }
1144
1145 if ( ! empty( $info_b['query'] ) ) {
1146 $link .= '?' . $info_b['query'];
1147 }
1148
1149 if ( ! empty( $info_b['fragment'] ) ) {
1150 $link .= '#' . $info_b['fragment'];
1151 }
1152 }
1153 }
1154
1155 return $link;
1156 }
1157
1158 /**
1159 * @param LP_Quiz $item
1160 */
1161 function learn_press_quiz_meta_final( $item ) {
1162 $course = learn_press_get_course();
1163
1164 if ( ! $course->is_final_quiz( $item->get_id() ) ) {
1165 return;
1166 }
1167 echo '<span class="item-meta final-quiz">' . esc_html__( 'Final', 'learnpress' ) . '</span>';
1168 }
1169
1170 function learn_press_comments_template_query_args( $comment_args ) {
1171 $post_type = get_post_type( $comment_args['post_id'] );
1172
1173 if ( $post_type == LP_COURSE_CPT ) {
1174 $comment_args['type__not_in'] = 'review';
1175 }
1176
1177 return $comment_args;
1178 }
1179
1180 if ( ! function_exists( 'learn_press_filter_get_comments_number' ) ) {
1181 function learn_press_filter_get_comments_number( $count, $post_id = 0 ) {
1182 global $wpdb;
1183
1184 if ( ! $post_id ) {
1185 $post_id = learn_press_get_course_id();
1186 }
1187
1188 if ( ! $post_id ) {
1189 return $count;
1190 }
1191
1192 if ( get_post_type( $post_id ) == LP_COURSE_CPT ) {
1193 $sql = $wpdb->prepare(
1194 ' SELECT count(*) '
1195 . " FROM {$wpdb->comments} "
1196 . ' WHERE comment_post_ID = %d '
1197 . ' AND comment_approved = 1 '
1198 . ' AND comment_type != %s ',
1199 $post_id,
1200 'review'
1201 );
1202
1203 $count = $wpdb->get_var( $sql );
1204
1205 // @since 3.0.0
1206 $count = apply_filters( 'learn-press/course-comments-number', $count, $post_id );
1207 }
1208
1209 return $count;
1210 }
1211 }
1212
1213 /**
1214 * Add custom classes to body tag class name
1215 *
1216 * @param array $classes
1217 *
1218 * @return array
1219 *
1220 * @since 3.0.0
1221 */
1222 function learn_press_body_classes( $classes ) {
1223 $pages = learn_press_static_page_ids();
1224
1225 if ( $pages ) {
1226 $is_lp_page = false;
1227 settype( $classes, 'array' );
1228
1229 foreach ( $pages as $slug => $id ) {
1230 if ( is_page( $id ) ) {
1231 $classes[] = $slug;
1232 $is_lp_page = true;
1233 }
1234 }
1235
1236 if ( $is_lp_page || is_learnpress() ) {
1237 $classes[] = get_stylesheet();
1238 $classes[] = 'learnpress';
1239 $classes[] = 'learnpress-page';
1240 }
1241 }
1242
1243 return $classes;
1244 }
1245
1246 add_filter( 'body_class', 'learn_press_body_classes', 10 );
1247
1248 /**
1249 * Return true if user is learning a course
1250 *
1251 * @param int $course_id
1252 *
1253 * @return bool
1254 * @since 3.0
1255 * @version 1.0.1
1256 */
1257 function learn_press_is_learning_course( int $course_id = 0 ): bool {
1258 $is_learning = false;
1259 $user = learn_press_get_current_user();
1260 $course = learn_press_get_course( $course_id );
1261 if ( ! $course ) {
1262 return $is_learning;
1263 }
1264
1265 if ( $user ) {
1266 return $user->has_enrolled_or_finished( $course_id );
1267 }
1268
1269 if ( $course->is_no_required_enroll() ) {
1270 $is_learning = true;
1271 }
1272
1273 return apply_filters( 'lp/is-learning-course', $is_learning, $course_id );
1274 }
1275
1276 /**
1277 * Output custom css from settings
1278 *
1279 * @since 4.0.0
1280 */
1281 if ( ! function_exists( 'learn_press_print_custom_styles' ) ) {
1282 function learn_press_print_custom_styles() {
1283 $primary_color = LP_Settings::instance()->get( 'primary_color' );
1284 $secondary_color = LP_Settings::instance()->get( 'secondary_color' );
1285 ?>
1286
1287 <style id="learn-press-custom-css">
1288 :root {
1289 --lp-primary-color: <?php echo ! empty( $primary_color ) ? $primary_color : '#ffb606'; ?>;
1290 --lp-secondary-color: <?php echo ! empty( $secondary_color ) ? $secondary_color : '#442e66'; ?>;
1291 }
1292 </style>
1293
1294 <?php
1295 }
1296
1297 add_action( 'wp_head', 'learn_press_print_custom_styles' );
1298 }
1299
1300 /**
1301 * Return TRUE if current user has already enroll course in single view.
1302 *
1303 * @return bool
1304 * @since 3.0.0
1305 * @editor tungnx
1306 * @version 4.1.3
1307 */
1308 function learn_press_current_user_enrolled_course() {
1309 _deprecated_function( __FUNCTION__, '4.1.3' );
1310 }
1311
1312 /**
1313 * Check if an user can access content of a course.
1314 *
1315 * @param int $course_id
1316 * @param int $user_id
1317 *
1318 * @return bool
1319 * @since 3.x.x
1320 * @editor tungnx
1321 * @modify 4.1.3
1322 * @reason comment - not use
1323 */
1324
1325 function learn_press_user_can_access_course( $course_id, $user_id = 0 ) {
1326 _deprecated_function( __FUNCTION__, '4.1.2' );
1327 }
1328
1329 function learn_press_content_item_summary_class( $more = '', $echo = true ) {
1330 $classes = array( 'content-item-summary' );
1331 $classes = LP_Helper::merge_class( $classes, $more );
1332 $classes = apply_filters( 'learn-press/content-item-summary-class', $classes );
1333 $output = 'class="' . esc_attr( join( ' ', $classes ) ) . '"';
1334
1335 if ( $echo ) {
1336 echo wp_kses_post( $output );
1337 }
1338
1339 return $output;
1340 }
1341
1342 function learn_press_content_item_summary_classes( $classes ) {
1343 $item = LP_Global::course_item();
1344
1345 if ( ! $item ) {
1346 return $classes;
1347 }
1348
1349 if ( $item->get_post_type() !== LP_LESSON_CPT ) {
1350 return $classes;
1351 }
1352
1353 return $classes;
1354 }
1355
1356 function learn_press_maybe_load_comment_js() {
1357 $item = LP_Global::course_item();
1358
1359 if ( $item ) {
1360 wp_enqueue_script( 'comment-reply' );
1361 }
1362 }
1363
1364 add_action( 'wp_enqueue_scripts', 'learn_press_maybe_load_comment_js' );
1365
1366 /**
1367 * Get list layouts archive course setting
1368 *
1369 * @editor tungnx
1370 * @modify 4.1.3
1371 */
1372 function learn_press_courses_layouts() {
1373 return apply_filters(
1374 'learnpress/archive-courses-layouts',
1375 [
1376 'grid' => 'Grid',
1377 'list' => 'List',
1378 ]
1379 );
1380 }
1381
1382 /**
1383 * Get layout template for archive course page.
1384 *
1385 * @return mixed
1386 * @since 3.3.0
1387 * @editor tungnx
1388 * @modify 4.1.3
1389 */
1390 function learn_press_get_courses_layout() {
1391 $layout = LP_Request::get_cookie( 'courses-layout' );
1392
1393 if ( ! $layout ) {
1394 $layout = LP_Settings::get_option( 'archive_courses_layout', 'list' );
1395 }
1396
1397 return $layout;
1398 }
1399
1400 function learn_press_register_sidebars() {
1401 register_sidebar(
1402 array(
1403 'name' => esc_html__( 'Course Sidebar', 'learnpress' ),
1404 'id' => 'course-sidebar',
1405 'description' => esc_html__( 'Widgets in this area will be shown in a single course', 'learnpress' ),
1406 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1407 'after_widget' => '</div>',
1408 'before_title' => '<h2 class="widgettitle">',
1409 'after_title' => '</h2>',
1410 )
1411 );
1412 register_sidebar(
1413 array(
1414 'name' => esc_html__( 'All Courses', 'learnpress' ),
1415 'id' => 'archive-courses-sidebar',
1416 'description' => esc_html__( 'Widgets in this area will be shown on all course pages', 'learnpress' ),
1417 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1418 'after_widget' => '</div>',
1419 'before_title' => '<h2 class="widgettitle">',
1420 'after_title' => '</h2>',
1421 )
1422 );
1423 }
1424
1425 add_action( 'widgets_init', 'learn_press_register_sidebars' );
1426
1427 function learn_press_setup_theme() {
1428 $support = array(
1429 'widgets' => array(
1430 // Place three core-defined widgets in the sidebar area.
1431 'course-sidebar' => array(
1432 'xxx' => array( 'lp-widget-course-progress' ),
1433 'yyy' => array( 'lp-widget-course-info' ),
1434 ),
1435 ),
1436 );
1437
1438 add_theme_support( 'starter-content', $support );
1439 }
1440
1441 add_action( 'after_setup_theme', 'learn_press_setup_theme' );
1442
1443 /**
1444 * @param LP_Question $question
1445 * @param array $args
1446 *
1447 * @return array
1448 * @since 4.x.x
1449 */
1450 function learn_press_get_question_options_for_js( $question, $args = array() ) {
1451 $args = wp_parse_args(
1452 $args,
1453 array(
1454 'cryptoJsAes' => false,
1455 'include_is_true' => true,
1456 'answer' => false,
1457 )
1458 );
1459
1460 if ( $args['cryptoJsAes'] ) {
1461 $options = array_values( $question->get_answer_options() );
1462
1463 $key = uniqid();
1464 $options = array(
1465 'data' => cryptoJsAesEncrypt( $key, wp_json_encode( $options ) ),
1466 'key' => $key,
1467 );
1468 } else {
1469 $exclude_option_key = array( 'question_id', 'order' );
1470 if ( ! $args['include_is_true'] ) {
1471 $exclude_option_key[] = 'is_true';
1472 }
1473
1474 $options = array_values(
1475 $question->get_answer_options(
1476 array(
1477 'exclude' => $exclude_option_key,
1478 'map' => array( 'question_answer_id' => 'uid' ),
1479 'answer' => $args['answer'],
1480 )
1481 )
1482 );
1483 }
1484
1485 return $options;
1486 }
1487
1488 function learn_press_custom_excerpt_length( $length ) {
1489 return 20;
1490 }
1491
1492 /**
1493 * Get post meta with key _lp_duration and translate.
1494 *
1495 * @param int $post_id
1496 * @param string $default
1497 *
1498 * @return string
1499 * @since 4.0.0
1500 */
1501 function learn_press_get_post_translated_duration( $post_id, $default = '' ) {
1502 $duration = get_post_meta( $post_id, '_lp_duration', true );
1503
1504 $duration_arr = explode( ' ', $duration );
1505 $duration_str = '';
1506
1507 if ( count( $duration_arr ) > 1 ) {
1508 $duration_number = $duration_arr[0];
1509 $duration_text = $duration_arr[1];
1510
1511 switch ( strtolower( $duration_text ) ) {
1512 case 'minute':
1513 $duration_str = sprintf(
1514 _n( '%s minute', '%s minutes', $duration_number, 'learnpress' ),
1515 $duration_number
1516 );
1517 break;
1518 case 'hour':
1519 $duration_str = sprintf(
1520 _n( '%s hour', '%s hours', $duration_number, 'learnpress' ),
1521 $duration_number
1522 );
1523 break;
1524 case 'day':
1525 $duration_str = sprintf( _n( '%s day', '%s days', $duration_number, 'learnpress' ), $duration_number );
1526 break;
1527 case 'week':
1528 $duration_str = sprintf(
1529 _n( '%s week', '%s weeks', $duration_number, 'learnpress' ),
1530 $duration_number
1531 );
1532 break;
1533 default:
1534 $duration_str = $duration;
1535 }
1536 }
1537
1538 return empty( absint( $duration ) ) ? $default : $duration_str;
1539 }
1540
1541 /**
1542 * Get level post meta.
1543 *
1544 * @param int $post_id
1545 *
1546 * @return string
1547 */
1548 function learn_press_get_post_level( $post_id ) {
1549 $level = get_post_meta( $post_id, '_lp_level', true );
1550
1551 return apply_filters(
1552 'learn-press/level-label',
1553 ! empty( $level ) ? lp_course_level()[ $level ] : esc_html__( 'All levels', 'learnpress' ),
1554 $post_id
1555 );
1556 }
1557
1558 function lp_course_level() {
1559 return apply_filters(
1560 'lp/template/function/course/level',
1561 array(
1562 '' => esc_html__( 'All levels', 'learnpress' ),
1563 'beginner' => esc_html__( 'Beginner', 'learnpress' ),
1564 'intermediate' => esc_html__( 'Intermediate', 'learnpress' ),
1565 'expert' => esc_html__( 'Expert', 'learnpress' ),
1566 )
1567 );
1568 }
1569
1570 // function learn_press_is_preview_course() {
1571 // $course_id = isset( $GLOBALS['preview_course'] ) ? $GLOBALS['preview_course'] : 0;
1572 //
1573 // return $course_id && get_post_type( $course_id ) === LP_COURSE_CPT;
1574 // }
1575
1576 /**
1577 * Get slug for logout action in user profile.
1578 *
1579 * @return string
1580 * @since 4.0.0
1581 */
1582 function learn_press_profile_logout_slug() {
1583 return apply_filters( 'learn-press/profile-logout-slug', 'lp-logout' );
1584 }
1585
1586 function lp_get_email_content( $format, $meta = array(), $field = array() ) {
1587 if ( $meta && isset( $meta[ $format ] ) ) {
1588 $content = stripslashes( $meta[ $format ] );
1589 } else {
1590 $template = ! empty( $field[ "template_{$format}" ] ) ? $field[ "template_{$format}" ] : null;
1591 $template_file = $field['template_base'] . $template;
1592 $content = LP_WP_Filesystem::instance()->file_get_contents( $template_file );
1593 }
1594
1595 return $content;
1596 }
1597
1598 function lp_skeleton_animation_html( $count_li = 3, $width = 'random', $styleli = '', $styleul = '' ) {
1599 ?>
1600 <ul class="lp-skeleton-animation" style="<?php echo esc_attr( $styleul ); ?>">
1601 <?php for ( $i = 0; $i < absint( $count_li ); $i ++ ) : ?>
1602 <li style="width: <?php echo esc_attr( $width === 'random' ? wp_rand( 60, 100 ) . '%' : $width ); ?>; <?php echo ! empty( $styleli ) ? $styleli : ''; ?>"></li>
1603 <?php endfor; ?>
1604 </ul>
1605
1606 <?php
1607 }
1608
1609 add_filter( 'lp_format_page_content', 'wptexturize' );
1610 add_filter( 'lp_format_page_content', 'convert_smilies' );
1611 add_filter( 'lp_format_page_content', 'convert_chars' );
1612 add_filter( 'lp_format_page_content', 'wpautop' );
1613 add_filter( 'lp_format_page_content', 'shortcode_unautop' );
1614 add_filter( 'lp_format_page_content', 'prepend_attachment' );
1615 add_filter( 'lp_format_page_content', 'do_shortcode', 11 );
1616 add_filter( 'lp_format_page_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
1617
1618 if ( function_exists( 'do_blocks' ) ) {
1619 add_filter( 'lp_format_page_content', 'do_blocks', 9 );
1620 }
1621
1622 function lp_format_page_content( $raw_string ) {
1623 return apply_filters( 'lp_format_page_content', $raw_string );
1624 }
1625
1626 if ( ! function_exists( 'lp_profile_page_content' ) ) {
1627 function lp_profile_page_content() {
1628 $profile_id = learn_press_get_page_id( 'profile' );
1629
1630 if ( $profile_id ) {
1631 $profile_page = get_post( $profile_id );
1632
1633 // remove_shortcode( 'learn_press_profile' );
1634 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1635
1636 if ( $description ) {
1637 echo '<div class="lp-profile-page__content">' . $description . '</div>';
1638 }
1639 }
1640 }
1641 }
1642
1643 if ( ! function_exists( 'lp_archive_course_page_content' ) ) {
1644 add_action( 'lp/template/archive-course/description', 'lp_archive_course_page_content' );
1645
1646 function lp_archive_course_page_content() {
1647 if ( is_search() ) {
1648 return;
1649 }
1650
1651 if ( is_post_type_archive( LP_COURSE_CPT ) && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) {
1652 $profile_id = learn_press_get_page_id( 'courses' );
1653
1654 if ( $profile_id ) {
1655 $profile_page = get_post( $profile_id );
1656
1657 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1658 if ( $description ) {
1659 echo '<div class="lp-course-page__content">' . $description . '</div>';
1660 }
1661 }
1662 }
1663 }
1664 }
1665
1666 if ( ! function_exists( 'lp_taxonomy_archive_course_description' ) ) {
1667 add_action( 'lp/template/archive-course/description', 'lp_taxonomy_archive_course_description' );
1668
1669 function lp_taxonomy_archive_course_description() {
1670
1671 if ( learn_press_is_course_tax() && 0 === absint( get_query_var( 'paged' ) ) ) {
1672 $term = get_queried_object();
1673
1674 if ( $term && ! empty( $term->description ) ) {
1675 echo '<div class="lp-archive-course-term-description">' . lp_format_page_content( wp_kses_post( $term->description ) ) . '</div>';
1676 }
1677 }
1678 }
1679 }
1680
1681 /**
1682 * Params to query courses on Archive Course.
1683 *
1684 * @return array
1685 */
1686 function lp_archive_skeleton_get_args(): array {
1687 global $post, $wp;
1688
1689 $args = array();
1690
1691 if ( ! empty( $_GET ) ) {
1692 $args = (array) $_GET;
1693 }
1694
1695 $params = apply_filters(
1696 'lp/template/archive-course/skeleton/args',
1697 array(
1698 'paged' => 1,
1699 'c_search' => '',
1700 'orderby' => '',
1701 'order' => '',
1702 )
1703 );
1704
1705 if ( learn_press_is_course_category() || learn_press_is_course_tag() ) {
1706 $cat = get_queried_object();
1707
1708 $args['term_id'] = $cat->term_id;
1709 $args['taxonomy'] = $cat->taxonomy;
1710 }
1711
1712 if ( learn_press_is_course_archive() ) {
1713 foreach ( $params as $key => $param ) {
1714 if ( isset( $_REQUEST[ $key ] ) ) {
1715 $args[ $key ] = LP_Helper::sanitize_params_submitted( $_REQUEST[ $key ] );
1716 } else {
1717 $args[ $key ] = $param;
1718 }
1719 }
1720 }
1721
1722 return $args;
1723 }
1724