PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 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, at inc/lp-template-functions.php

1,749 lines 44.1 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', LP()->template( 'course' )->func( 'course_enroll_button' ), 5 );
21 add_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_purchase_button' ), 10 );
22 add_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_external_button' ), 15 );
23 add_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'button_retry' ), 20 );
24 add_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_continue_button' ), 25 );
25 add_action( 'learn-press/course-buttons', LP()->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', LP()->template( 'course' )->func( 'course_enroll_button' ), 5 );
32 remove_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_purchase_button' ), 10 );
33 //remove_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_external_button' ), 15 );
34 remove_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'button_retry' ), 20 );
35 remove_action( 'learn-press/course-buttons', LP()->template( 'course' )->func( 'course_continue_button' ), 25 );
36 remove_action( 'learn-press/course-buttons', LP()->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' => LP()->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' => LP()->template( 'course' )->func( 'course_curriculum' ),
70 );
71
72 $defaults['instructor'] = array(
73 'title' => esc_html__( 'Instructor', 'learnpress' ),
74 'priority' => 40,
75 'callback' => LP()->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' => LP()->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' => $quiz->get_duration()->get(),
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 /*
415 if ( $user->can_view_item( $lesson_id, $course_id )->flag ) {
416 $classes[] = 'viewable';
417 }*/
418
419 $classes = array_unique( array_merge( $classes, $class ) );
420
421 if ( $echo ) {
422 echo 'class="' . implode( ' ', $classes ) . '"';
423 }
424
425 return $classes;
426 }
427 }
428
429 if ( ! function_exists( 'learn_press_course_quiz_class' ) ) {
430 /**
431 * The class of lesson in course curriculum
432 *
433 * @param int $quiz_id
434 * @param int $course_id
435 * @param string|array $class
436 * @param boolean $echo
437 *
438 * @return mixed
439 */
440 function learn_press_course_quiz_class( $quiz_id = null, $course_id = 0, $class = null, $echo = true ) {
441 $user = learn_press_get_current_user();
442
443 if ( ! $course_id ) {
444 $course_id = get_the_ID();
445 }
446
447 if ( is_string( $class ) && $class ) {
448 $class = preg_split( '!\s+!', $class );
449 } else {
450 $class = array();
451 }
452
453 $course = learn_press_get_course( $course_id );
454
455 if ( ! $course ) {
456 return '';
457 }
458
459 $classes = array(
460 'course-quiz course-item course-item-' . $quiz_id,
461 );
462
463 $status = $user->get_item_status( $quiz_id );
464
465 if ( $status ) {
466 $classes[] = "item-has-status item-{$status}";
467 }
468
469 if ( $quiz_id && $course->is_current_item( $quiz_id ) ) {
470 $classes[] = 'item-current';
471 }
472
473 /*
474 if ( $user->can_view_item( $quiz_id, $course_id )->flag ) {
475 $classes[] = 'viewable';
476 }*/
477
478 if ( $course->is_final_quiz( $quiz_id ) ) {
479 $classes[] = 'final-quiz';
480 }
481
482 $classes = array_unique( array_merge( $classes, $class ) );
483
484 if ( $echo ) {
485 echo 'class="' . implode( ' ', $classes ) . '"';
486 }
487
488 return $classes;
489 }
490 }
491
492 if ( ! function_exists( 'learn_press_course_class' ) ) {
493 /**
494 * Append new class to course post type
495 *
496 * @param $classes
497 * @param $class
498 * @param $post_id
499 *
500 * @return string
501 */
502 function learn_press_course_class( $classes, $class, $post_id = '' ) {
503 if ( is_learnpress() ) {
504 $classes = (array) $classes;
505 }
506
507 if ( $post_id === 0 ) {
508 $classes[] = 'page type-page';
509 }
510
511 if ( ! $post_id || 'lp_course' !== get_post_type( $post_id ) ) {
512 return $classes;
513 }
514
515 $classes[] = 'course';
516
517 return apply_filters( 'learn_press_course_class', $classes );
518 }
519 }
520
521 function learn_press_setup_user() {
522 $GLOBALS['lp_user'] = learn_press_get_current_user();
523 }
524 add_action( 'init', 'learn_press_setup_user', 1000 );
525
526 /**
527 * Display a message immediately with out push into queue
528 *
529 * @param $message
530 * @param string $type
531 */
532
533 function learn_press_display_message( $message, $type = 'success' ) {
534 $messages = learn_press_session_get( learn_press_session_message_id() );
535 learn_press_session_set( learn_press_session_message_id(), null );
536
537 // add new notice and display
538 learn_press_add_message( $message, $type );
539 echo learn_press_get_messages( true );
540
541 // store back messages
542 learn_press_session_set( learn_press_session_message_id(), $messages );
543 }
544
545 /**
546 * Returns all notices added
547 *
548 * @param bool $clear
549 *
550 * @return string
551 */
552 function learn_press_get_messages( $clear = false ) {
553 ob_start();
554 learn_press_print_messages( $clear );
555
556 return ob_get_clean();
557 }
558
559 /**
560 * Add new message into queue for displaying.
561 *
562 * @param string $message
563 * @param string $type
564 * @param array $options
565 * @param int|bool $current_user . @since 3.0.9 - add for current user only
566 */
567 function learn_press_add_message( $message, $type = 'success', $options = array(), $current_user = true ) {
568 if ( is_string( $options ) ) {
569 $options = array( 'id' => $options );
570 }
571
572 $options = wp_parse_args(
573 $options,
574 array(
575 'id' => '',
576 )
577 );
578
579 if ( $current_user ) {
580 if ( true === $current_user ) {
581 $current_user = get_current_user_id();
582 }
583 }
584
585 $key = "messages{$current_user}";
586
587 $messages = learn_press_session_get( $key );
588
589 if ( empty( $messages[ $type ] ) ) {
590 $messages[ $type ] = array();
591 }
592
593 $messages[ $type ][ $options['id'] ] = array(
594 'content' => $message,
595 'options' => $options,
596 );
597
598 learn_press_session_set( $key, $messages );
599 }
600
601 function learn_press_get_message( $message, $type = 'success' ) {
602 ob_start();
603 learn_press_display_message( $message, $type );
604 $message = ob_get_clean();
605
606 return $message;
607 }
608
609 /**
610 * Remove message added into queue by id and/or type.
611 *
612 * @param string $id
613 * @param string|array $type
614 *
615 * @since 3.0.0
616 */
617 function learn_press_remove_message( $id = '', $type = '' ) {
618 $groups = learn_press_session_get( learn_press_session_message_id() );
619
620 if ( ! $groups ) {
621 return;
622 }
623
624 settype( $type, 'array' );
625
626 if ( $id ) {
627 foreach ( $groups as $message_type => $messages ) {
628 if ( ! sizeof( $type ) ) {
629 if ( isset( $groups[ $message_type ][ $id ] ) ) {
630 unset( $groups[ $message_type ][ $id ] );
631 }
632 } elseif ( in_array( $message_type, $type ) ) {
633 if ( isset( $groups[ $message_type ][ $id ] ) ) {
634 unset( $groups[ $message_type ][ $id ] );
635 }
636 }
637 }
638 } elseif ( sizeof( $type ) ) {
639 foreach ( $type as $t ) {
640 if ( isset( $groups[ $t ] ) ) {
641 unset( $groups[ $t ] );
642 }
643 }
644 } else {
645 $groups = array();
646 }
647
648 learn_press_session_set( learn_press_session_message_id(), $groups );
649 }
650
651 /**
652 * Print out the message stored in the queue
653 *
654 * @param bool
655 */
656 function learn_press_print_messages( $clear = true ) {
657 $messages = learn_press_session_get( learn_press_session_message_id() );
658 learn_press_get_template( 'global/message.php', array( 'messages' => $messages ) );
659
660 if ( $clear ) {
661 learn_press_session_set( learn_press_session_message_id(), array() );
662 }
663 }
664
665 function learn_press_message_count( $type = '' ) {
666 $count = 0;
667 $messages = learn_press_session_get( learn_press_session_message_id(), array() );
668
669 if ( isset( $messages[ $type ] ) ) {
670 $count = absint( sizeof( $messages[ $type ] ) );
671 } elseif ( empty( $type ) ) {
672 foreach ( $messages as $message ) {
673 $count += absint( sizeof( $message ) );
674 }
675 }
676
677 return $count;
678 }
679
680 function learn_press_session_message_id() {
681 return 'messages' . get_current_user_id();
682 }
683
684 /**
685 * Displays messages before main content
686 */
687 function _learn_press_print_messages() {
688 $item = LP_Global::course_item();
689 if ( ( 'learn_press_before_main_content' == current_action() ) && $item ) {
690 return;
691 }
692 learn_press_print_messages( true );
693 }
694
695 add_action( 'learn_press_before_main_content', '_learn_press_print_messages', 50 );
696 add_action( 'learn-press/before-course-item-content', '_learn_press_print_messages', 50 );
697
698 if ( ! function_exists( 'learn_press_page_title' ) ) {
699
700 /**
701 * learn_press_page_title function.
702 *
703 * @param boolean $echo
704 * @return string
705 */
706 function learn_press_page_title( bool $echo = false ): string {
707 $page_title = '';
708
709 if ( is_search() ) {
710 // Comment by tungnx
711 /*$page_title = sprintf( __( 'Search Results for: &ldquo;%s&rdquo;', 'learnpress' ), get_search_query() );
712
713 if ( get_query_var( 'paged' ) ) {
714 $page_title .= sprintf( __( '&nbsp;&ndash; Page %s', 'learnpress' ), get_query_var( 'paged' ) );
715 }*/
716 } elseif ( is_tax() ) {
717 $page_title = single_term_title( '', false );
718 } else {
719 $courses_page_id = learn_press_get_page_id( 'courses' );
720 $page_title = get_the_title( $courses_page_id );
721 }
722
723 return apply_filters( 'learn_press_page_title', $page_title );
724 }
725 }
726
727 /**
728 * @depecated 4.1.6.4
729 */
730 function learn_press_template_redirect() {
731 _deprecated_function( __FUNCTION__, '4.1.6.4' );
732 global $wp_query, $wp;
733
734 // When default permalinks are enabled, redirect shop page to post type archive url
735 if ( ! empty( $_GET['page_id'] ) && get_option( 'permalink_structure' ) == '' && $_GET['page_id'] == learn_press_get_page_id( 'courses' ) ) {
736 wp_safe_redirect( get_post_type_archive_link( 'lp_course' ) );
737 exit;
738 }
739 }
740
741 // add_action( 'template_redirect', 'learn_press_template_redirect' );
742
743
744 /**
745 * Get template part.
746 *
747 * @param string $slug
748 * @param string $name
749 *
750 * @return string
751 */
752 function learn_press_get_template_part( $slug, $name = '' ) {
753 $template = '';
754
755 if ( $name ) {
756 $template = locate_template(
757 array(
758 "{$slug}-{$name}.php",
759 learn_press_template_path() . "/{$slug}-{$name}.php",
760 )
761 );
762 }
763
764 // Get default slug-name.php
765 if ( ! $template && $name && file_exists( LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php" ) ) {
766 $template = LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php";
767 }
768
769 // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/learnpress/slug.php
770 if ( ! $template ) {
771 $template = locate_template( array( "{$slug}.php", learn_press_template_path() . "/{$slug}.php" ) );
772 }
773 // override path of child theme in parent theme - Fix for eduma by tuanta
774 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
775 if ( $file_ct_in_pr && $name ) {
776 $template_child = locate_template(
777 array(
778 "{$slug}-{$name}.php",
779 'lp-child-path/' . learn_press_template_path() . '/' . $file_ct_in_pr . "/{$slug}-{$name}.php",
780 )
781 );
782 if ( $template_child && file_exists( $template_child ) ) {
783 $template = $template_child;
784 }
785 // check in child theme if have filter learn_press_child_in_parrent_template_path
786
787 $check_child_theme = get_stylesheet_directory() . '/' . learn_press_template_path() . "{$slug}-{$name}.php";
788 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
789 $template = $check_child_theme;
790 }
791 }
792
793 // Allow 3rd party plugin filter template file from their plugin
794 if ( $template ) {
795 $template = apply_filters( 'learn_press_get_template_part', $template, $slug, $name );
796 }
797 if ( $template && file_exists( $template ) ) {
798 load_template( $template, false );
799 }
800
801 return $template;
802 }
803
804 /**
805 * Get other templates passing attributes and including the file.
806 *
807 * @param string $template_name .
808 * @param array $args (default: array()) .
809 * @param string $template_path (default: '').
810 * @param string $default_path (default: '').
811 *
812 * @return void
813 */
814 function learn_press_get_template( $template_name = '', $args = array(), $template_path = '', $default_path = '' ) {
815 if ( $args && is_array( $args ) ) {
816 extract( $args );
817 }
818
819 if ( false === strpos( $template_name, '.php' ) ) {
820 $template_name .= '.php';
821 }
822
823 $located = learn_press_locate_template( $template_name, $template_path, $default_path );
824
825 if ( ! file_exists( $located ) ) {
826 _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
827
828 $log = sprintf( 'TEMPLATE MISSING: Template %s doesn\'t exists.', $template_name );
829 error_log( $log );
830
831 if ( LP_Debug::is_debug() ) {
832 echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log );
833 }
834
835 return;
836 }
837
838 // Allow 3rd party plugin filter template file from their plugin
839 $located = apply_filters(
840 'learn_press_get_template',
841 $located,
842 $template_name,
843 $args,
844 $template_path,
845 $default_path
846 );
847 if ( $located != '' ) {
848 do_action( 'learn_press_before_template_part', $template_name, $template_path, $located, $args );
849
850 include $located;
851
852 do_action( 'learn_press_after_template_part', $template_name, $template_path, $located, $args );
853 }
854 }
855
856 /**
857 * Get template content
858 *
859 * @param $template_name
860 * @param array $args
861 * @param string $template_path
862 * @param string $default_path
863 *
864 * @return string
865 * @uses learn_press_get_template();
866 */
867 function learn_press_get_template_content( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
868 ob_start();
869 learn_press_get_template( $template_name, $args, $template_path, $default_path );
870
871 return ob_get_clean();
872 }
873
874 /**
875 * Locate a template and return the path for inclusion.
876 *
877 * @param string $template_name
878 * @param string $template_path (default: '')
879 * @param string $default_path (default: '')
880 *
881 * @return string
882 */
883 function learn_press_locate_template( $template_name, $template_path = '', $default_path = '' ) {
884 if ( ! $template_path ) {
885 $template_path = learn_press_template_path();
886 }
887
888 if ( ! $default_path ) {
889 $default_path = LP_PLUGIN_PATH . 'templates/';
890 }
891
892 /**
893 * Disable override templates in theme by default since LP 4.0.0
894 */
895 if ( learn_press_override_templates() ) {
896 $template = locate_template(
897 array(
898 trailingslashit( $template_path ) . $template_name,
899 $template_name,
900 )
901 );
902 // override path of child theme in parent theme - Fix for eduma by tuanta
903 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
904 if ( $file_ct_in_pr ) {
905 $template_child = locate_template(
906 array(
907 trailingslashit( 'lp-child-path/' . $template_path . '/' . $file_ct_in_pr ) . $template_name,
908 $template_name,
909 )
910 );
911 if ( $template_child && file_exists( $template_child ) ) {
912 $template = $template_child;
913 }
914 // check in child theme if have filter learn_press_child_in_parrent_template_path
915 $check_child_theme = get_stylesheet_directory() . '/' . trailingslashit( $template_path ) . $template_name;
916 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
917 $template = $check_child_theme;
918 }
919 }
920 }
921 if ( ! isset( $template ) || ! $template ) {
922 $template = trailingslashit( $default_path ) . $template_name;
923 }
924
925 return apply_filters( 'learn_press_locate_template', $template, $template_name, $template_path );
926 }
927
928 /**
929 * Returns the name of folder contains template files in theme
930 *
931 * @param bool
932 *
933 * @return string
934 */
935 function learn_press_template_path( $slash = false ) {
936 return apply_filters( 'learn_press_template_path', 'learnpress', $slash ) . ( $slash ? '/' : '' );
937 }
938
939 /**
940 * Disable override templates in theme by default
941 *
942 * @return bool
943 * @since 4.0.0
944 */
945 function learn_press_override_templates() {
946 return apply_filters( 'learn-press/override-templates', false );
947 }
948
949 if ( ! function_exists( 'learn_press_is_404' ) ) {
950 /**
951 * Set header is 404
952 */
953 function learn_press_is_404() {
954 global $wp_query;
955 if ( ! empty( $_REQUEST['debug-404'] ) ) {
956 learn_press_debug( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $_REQUEST['debug-404'] ) );
957 }
958 $wp_query->set_404();
959 status_header( 404 );
960 }
961 }
962
963 if ( ! function_exists( 'learn_press_404_page' ) ) {
964 /**
965 * Display 404 page
966 */
967 function learn_press_404_page() {
968 learn_press_is_404();
969 }
970 }
971
972 if ( ! function_exists( 'learn_press_generate_template_information' ) ) {
973 function learn_press_generate_template_information( $template_name, $template_path, $located, $args ) {
974 $debug = learn_press_get_request( 'show-template-location' );
975 if ( $debug == 'on' ) {
976 echo '<!-- Template Location:' . str_replace( array( LP_PLUGIN_PATH, ABSPATH ), '', $located ) . ' -->';
977 }
978 }
979 }
980
981 if ( ! function_exists( 'learn_press_course_remaining_time' ) ) {
982 /**
983 * Show the time remain of a course
984 */
985 function learn_press_course_remaining_time() {
986 $user = learn_press_get_current_user();
987 if ( ! $user->has_finished_course( get_the_ID() ) && $text = learn_press_get_course( get_the_ID() )->get_user_duration_html( $user->get_id() ) ) {
988 learn_press_display_message( $text );
989 }
990 }
991 }
992
993 if ( ! function_exists( 'learn_press_item_meta_type' ) ) {
994 function learn_press_item_meta_type( $course, $item ) {
995 ?>
996
997 <?php if ( $item->post_type == 'lp_quiz' ) { ?>
998
999 <span class="lp-label lp-label-quiz"><?php _e( 'Quiz', 'learnpress' ); ?></span>
1000
1001 <?php if ( $course->final_quiz == $item->ID ) { ?>
1002
1003 <span class="lp-label lp-label-final"><?php _e( 'Final', 'learnpress' ); ?></span>
1004
1005 <?php } ?>
1006
1007 <?php } elseif ( $item->post_type == 'lp_lesson' ) { ?>
1008
1009 <span class="lp-label lp-label-lesson"><?php _e( 'Lesson', 'learnpress' ); ?></span>
1010 <?php if ( get_post_meta( $item->ID, '_lp_preview', true ) == 'yes' ) { ?>
1011
1012 <span class="lp-label lp-label-preview"><?php _e( 'Preview', 'learnpress' ); ?></span>
1013
1014 <?php } ?>
1015
1016 <?php } else { ?>
1017
1018 <?php do_action( 'learn_press_item_meta_type', $course, $item ); ?>
1019
1020 <?php
1021 }
1022 }
1023 }
1024
1025 if ( ! function_exists( 'learn_press_sort_course_tabs' ) ) {
1026 function learn_press_sort_course_tabs( $tabs = array() ) {
1027 uasort( $tabs, 'learn_press_sort_list_by_priority_callback' );
1028
1029 return $tabs;
1030 }
1031 }
1032
1033 if ( ! function_exists( 'learn_press_get_profile_display_name' ) ) {
1034 /**
1035 * Get Display name publicly as in Profile page
1036 */
1037 function learn_press_get_profile_display_name( $user ) {
1038 if ( empty( $user ) ) {
1039 return '';
1040 }
1041
1042 $id = '';
1043
1044 if ( $user instanceof LP_Abstract_User ) {
1045 $id = $user->get_id();
1046 } elseif ( $user instanceof WP_User ) {
1047 $id = $user->ID;
1048 } elseif ( is_numeric( $user ) ) {
1049 $id = $user;
1050 }
1051
1052 if ( ! isset( $id ) ) {
1053 return '';
1054 }
1055
1056 $info = get_userdata( $id );
1057
1058 return $info ? $info->display_name : '';
1059 }
1060 }
1061
1062 /**
1063 * @depecated 4.1.6.9
1064 */
1065 /*if ( ! function_exists( 'learn_press_content_item_comments' ) ) {
1066 function learn_press_content_item_comments() {
1067 $item = LP_Global::course_item();
1068
1069 if ( ! $item ) {
1070 return;
1071 }
1072
1073 if ( ! $item->is_support( 'comments' ) ) {
1074 return;
1075 }
1076
1077 global $post;
1078
1079 $post = get_post( $item->get_id() );
1080
1081 setup_postdata( $post );
1082
1083 if ( ! have_comments() ) {
1084 return;
1085 }
1086
1087 add_filter( 'deprecated_file_trigger_error', '__return_false' );
1088 comments_template();
1089 remove_filter( 'deprecated_file_trigger_error', '__return_false' );
1090
1091 wp_reset_postdata();
1092 }
1093 }*/
1094
1095 function learn_press_course_comments_open( $open, $post_id ) {
1096 $post = get_post( $post_id );
1097
1098 if ( LP_COURSE_CPT == $post->post_type ) {
1099 $open = false;
1100 }
1101
1102 return $open;
1103 }
1104
1105 function learn_press_is_content_item_only() {
1106 return ! empty( $_REQUEST['content-item-only'] );
1107 }
1108
1109 function learn_press_label_html( $label, $type = '' ) {
1110 ?>
1111 <span class="lp-label label-<?php echo esc_attr( $type ? $type : $label ); ?>">
1112 <?php echo esc_html( $label ); ?>
1113 </span>
1114 <?php
1115 }
1116
1117 /**
1118 * @depecated 4.1.6.4
1119 */
1120 function learn_press_get_course_redirect( $link ) {
1121 _deprecated_function( __FUNCTION__, '4.1.6.4' );
1122 if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
1123 return $link;
1124 }
1125
1126 $referer = $_SERVER['HTTP_REFERER'];
1127 $info_a = parse_url( $referer );
1128 $info_b = parse_url( $link );
1129
1130 $a = explode( '/', $info_a['path'] );
1131 $a = array_filter( $a );
1132
1133 $b = explode( '/', $info_b['path'] );
1134 $b = array_filter( $b );
1135
1136 $same = array_intersect_assoc( $a, $b );
1137
1138 $a = array_diff_assoc( $a, $same );
1139 $b = array_diff_assoc( $b, $same );
1140
1141 $a = array_values( $a );
1142 $b = array_values( $b );
1143
1144 if ( array_shift( $a ) === 'popup' ) {
1145 unset( $a[0] );
1146
1147 if ( ! ( array_diff_assoc( $a, $b ) ) ) {
1148 $link = '';
1149 foreach ( array( 'scheme', 'host', 'port', 'path' ) as $v ) {
1150 if ( ! isset( $info_a[ $v ] ) ) {
1151 continue;
1152 }
1153
1154 if ( $v == 'scheme' ) {
1155 $sep = '://';
1156 } elseif ( $v == 'host' ) {
1157 $sep = '';
1158 } elseif ( $v == 'port' ) {
1159 $link .= ':';
1160 $sep = '';
1161 } else {
1162 $sep = '/';
1163 }
1164 $link = $link . $info_a[ $v ] . $sep;
1165 }
1166
1167 if ( ! empty( $info_b['query'] ) ) {
1168 $link .= '?' . $info_b['query'];
1169 }
1170
1171 if ( ! empty( $info_b['fragment'] ) ) {
1172 $link .= '#' . $info_b['fragment'];
1173 }
1174 }
1175 }
1176
1177 return $link;
1178 }
1179
1180 /**
1181 * @param LP_Quiz $item
1182 */
1183 function learn_press_quiz_meta_final( $item ) {
1184 $course = learn_press_get_course();
1185
1186 if ( ! $course->is_final_quiz( $item->get_id() ) ) {
1187 return;
1188 }
1189 echo '<span class="item-meta final-quiz">' . esc_html__( 'Final', 'learnpress' ) . '</span>';
1190 }
1191
1192 function learn_press_comments_template_query_args( $comment_args ) {
1193 $post_type = get_post_type( $comment_args['post_id'] );
1194
1195 if ( $post_type == LP_COURSE_CPT ) {
1196 $comment_args['type__not_in'] = 'review';
1197 }
1198
1199 return $comment_args;
1200 }
1201
1202 if ( ! function_exists( 'learn_press_filter_get_comments_number' ) ) {
1203 function learn_press_filter_get_comments_number( $count, $post_id = 0 ) {
1204 global $wpdb;
1205
1206 if ( ! $post_id ) {
1207 $post_id = learn_press_get_course_id();
1208 }
1209
1210 if ( ! $post_id ) {
1211 return $count;
1212 }
1213
1214 if ( get_post_type( $post_id ) == LP_COURSE_CPT ) {
1215 $sql = $wpdb->prepare(
1216 ' SELECT count(*) '
1217 . " FROM {$wpdb->comments} "
1218 . ' WHERE comment_post_ID = %d '
1219 . ' AND comment_approved = 1 '
1220 . ' AND comment_type != %s ',
1221 $post_id,
1222 'review'
1223 );
1224
1225 $count = $wpdb->get_var( $sql );
1226
1227 // @since 3.0.0
1228 $count = apply_filters( 'learn-press/course-comments-number', $count, $post_id );
1229 }
1230
1231 return $count;
1232 }
1233 }
1234
1235 /**
1236 * Add custom classes to body tag class name
1237 *
1238 * @param array $classes
1239 *
1240 * @return array
1241 *
1242 * @since 3.0.0
1243 */
1244 function learn_press_body_classes( $classes ) {
1245 $pages = learn_press_static_page_ids();
1246
1247 if ( $pages ) {
1248 $is_lp_page = false;
1249 settype( $classes, 'array' );
1250
1251 foreach ( $pages as $slug => $id ) {
1252 if ( is_page( $id ) ) {
1253 $classes[] = $slug;
1254 $is_lp_page = true;
1255 }
1256 }
1257
1258 if ( $is_lp_page || is_learnpress() ) {
1259 $classes[] = get_stylesheet();
1260 $classes[] = 'learnpress';
1261 $classes[] = 'learnpress-page';
1262 }
1263 }
1264
1265 return $classes;
1266 }
1267
1268 add_filter( 'body_class', 'learn_press_body_classes', 10 );
1269
1270 /**
1271 * Return true if user is learning a course
1272 *
1273 * @param int $course_id
1274 *
1275 * @return bool|mixed
1276 * @since 3.0
1277 */
1278 function learn_press_is_learning_course( $course_id = 0 ) {
1279 $user = learn_press_get_current_user();
1280 $course = learn_press_get_course( $course_id );
1281 $is_learning = false;
1282 $has_status = false;
1283
1284 if ( $user && $course ) {
1285 $has_status = $user->has_course_status(
1286 $course->get_id(),
1287 array(
1288 'enrolled',
1289 'finished',
1290 )
1291 );
1292 }
1293
1294 if ( $course && ( ! $course->is_required_enroll() || $has_status ) ) {
1295 $is_learning = true;
1296 }
1297
1298 return apply_filters( 'learn-press/is-learning-course', $is_learning );
1299 }
1300
1301 /**
1302 * Output custom css from settings
1303 *
1304 * @since 4.0.0
1305 */
1306 if ( ! function_exists( 'learn_press_print_custom_styles' ) ) {
1307 function learn_press_print_custom_styles() {
1308 $primary_color = LP_Settings::instance()->get( 'primary_color' );
1309 $secondary_color = LP_Settings::instance()->get( 'secondary_color' );
1310 ?>
1311
1312 <style id="learn-press-custom-css">
1313 :root {
1314 --lp-primary-color: <?php echo ! empty( $primary_color ) ? $primary_color : '#ffb606'; ?>;
1315 --lp-secondary-color: <?php echo ! empty( $secondary_color ) ? $secondary_color : '#442e66'; ?>;
1316 }
1317 </style>
1318
1319 <?php
1320 }
1321
1322 add_action( 'wp_head', 'learn_press_print_custom_styles' );
1323 }
1324
1325 /**
1326 * Return TRUE if current user has already enroll course in single view.
1327 *
1328 * @return bool
1329 * @since 3.0.0
1330 * @editor tungnx
1331 * @version 4.1.3
1332 */
1333 function learn_press_current_user_enrolled_course() {
1334 _deprecated_function( __FUNCTION__, '4.1.3' );
1335 }
1336
1337 /**
1338 * Check if an user can access content of a course.
1339 *
1340 * @param int $course_id
1341 * @param int $user_id
1342 *
1343 * @return bool
1344 * @since 3.x.x
1345 * @editor tungnx
1346 * @modify 4.1.3
1347 * @reason comment - not use
1348 */
1349
1350 function learn_press_user_can_access_course( $course_id, $user_id = 0 ) {
1351 _deprecated_function( __FUNCTION__, '4.1.2' );
1352 }
1353
1354 function learn_press_content_item_summary_class( $more = '', $echo = true ) {
1355 $classes = array( 'content-item-summary' );
1356 $classes = LP_Helper::merge_class( $classes, $more );
1357 $classes = apply_filters( 'learn-press/content-item-summary-class', $classes );
1358 $output = 'class="' . esc_attr( join( ' ', $classes ) ) . '"';
1359
1360 if ( $echo ) {
1361 echo wp_kses_post( $output );
1362 }
1363
1364 return $output;
1365 }
1366
1367 function learn_press_content_item_summary_classes( $classes ) {
1368 $item = LP_Global::course_item();
1369
1370 if ( ! $item ) {
1371 return $classes;
1372 }
1373
1374 if ( $item->get_post_type() !== LP_LESSON_CPT ) {
1375 return $classes;
1376 }
1377
1378 return $classes;
1379 }
1380
1381 function learn_press_maybe_load_comment_js() {
1382 $item = LP_Global::course_item();
1383
1384 if ( $item ) {
1385 wp_enqueue_script( 'comment-reply' );
1386 }
1387 }
1388
1389 add_action( 'wp_enqueue_scripts', 'learn_press_maybe_load_comment_js' );
1390
1391 /**
1392 * Get list layouts archive course setting
1393 *
1394 * @editor tungnx
1395 * @modify 4.1.3
1396 */
1397 function learn_press_courses_layouts() {
1398 return apply_filters(
1399 'learnpress/archive-courses-layouts',
1400 [
1401 'grid' => 'Grid',
1402 'list' => 'List',
1403 ]
1404 );
1405 }
1406
1407 /**
1408 * Get layout template for archive course page.
1409 *
1410 * @return mixed
1411 * @since 3.3.0
1412 * @editor tungnx
1413 * @modify 4.1.3
1414 */
1415 function learn_press_get_courses_layout() {
1416 $layout = LP_Request::get_cookie( 'courses-layout' );
1417
1418 if ( ! $layout ) {
1419 $layout = LP_Settings::get_option( 'archive_courses_layout', 'list' );
1420 }
1421
1422 return $layout;
1423 }
1424
1425 function learn_press_register_sidebars() {
1426 register_sidebar(
1427 array(
1428 'name' => esc_html__( 'Course Sidebar', 'learnpress' ),
1429 'id' => 'course-sidebar',
1430 'description' => esc_html__( 'Widgets in this area will be shown in single course', 'learnpress' ),
1431 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1432 'after_widget' => '</div>',
1433 'before_title' => '<h2 class="widgettitle">',
1434 'after_title' => '</h2>',
1435 )
1436 );
1437 register_sidebar(
1438 array(
1439 'name' => esc_html__( 'All Courses', 'learnpress' ),
1440 'id' => 'archive-courses-sidebar',
1441 'description' => esc_html__( 'Widgets in this area will be shown in all courses page', 'learnpress' ),
1442 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1443 'after_widget' => '</div>',
1444 'before_title' => '<h2 class="widgettitle">',
1445 'after_title' => '</h2>',
1446 )
1447 );
1448 }
1449
1450 add_action( 'widgets_init', 'learn_press_register_sidebars' );
1451
1452 function learn_press_setup_theme() {
1453 $support = array(
1454 'widgets' => array(
1455 // Place three core-defined widgets in the sidebar area.
1456 'course-sidebar' => array(
1457 'xxx' => array( 'lp-widget-course-progress' ),
1458 'yyy' => array( 'lp-widget-course-info' ),
1459 ),
1460 ),
1461 );
1462
1463 add_theme_support( 'starter-content', $support );
1464 }
1465
1466 add_action( 'after_setup_theme', 'learn_press_setup_theme' );
1467
1468 /**
1469 * @param LP_Question $question
1470 * @param array $args
1471 *
1472 * @return array
1473 * @since 4.x.x
1474 */
1475 function learn_press_get_question_options_for_js( $question, $args = array() ) {
1476 $args = wp_parse_args(
1477 $args,
1478 array(
1479 'cryptoJsAes' => false,
1480 'include_is_true' => true,
1481 'answer' => false,
1482 )
1483 );
1484
1485 if ( $args['cryptoJsAes'] ) {
1486 $options = array_values( $question->get_answer_options() );
1487
1488 $key = uniqid();
1489 $options = array(
1490 'data' => cryptoJsAesEncrypt( $key, wp_json_encode( $options ) ),
1491 'key' => $key,
1492 );
1493 } else {
1494 $exclude_option_key = array( 'question_id', 'order' );
1495 if ( ! $args['include_is_true'] ) {
1496 $exclude_option_key[] = 'is_true';
1497 }
1498
1499 $options = array_values(
1500 $question->get_answer_options(
1501 array(
1502 'exclude' => $exclude_option_key,
1503 'map' => array( 'question_answer_id' => 'uid' ),
1504 'answer' => $args['answer'],
1505 )
1506 )
1507 );
1508 }
1509
1510 return $options;
1511 }
1512
1513 function learn_press_custom_excerpt_length( $length ) {
1514 return 20;
1515 }
1516
1517 /**
1518 * Get post meta with key _lp_duration and translate.
1519 *
1520 * @param int $post_id
1521 * @param string $default
1522 *
1523 * @return string
1524 * @since 4.0.0
1525 */
1526 function learn_press_get_post_translated_duration( $post_id, $default = '' ) {
1527 $duration = get_post_meta( $post_id, '_lp_duration', true );
1528
1529 $duration_arr = explode( ' ', $duration );
1530 $duration_str = '';
1531
1532 if ( count( $duration_arr ) > 1 ) {
1533 $duration_number = $duration_arr[0];
1534 $duration_text = $duration_arr[1];
1535
1536 switch ( strtolower( $duration_text ) ) {
1537 case 'minute':
1538 $duration_str = sprintf(
1539 _n( '%s minute', '%s minutes', $duration_number, 'learnpress' ),
1540 $duration_number
1541 );
1542 break;
1543 case 'hour':
1544 $duration_str = sprintf(
1545 _n( '%s hour', '%s hours', $duration_number, 'learnpress' ),
1546 $duration_number
1547 );
1548 break;
1549 case 'day':
1550 $duration_str = sprintf( _n( '%s day', '%s days', $duration_number, 'learnpress' ), $duration_number );
1551 break;
1552 case 'week':
1553 $duration_str = sprintf(
1554 _n( '%s week', '%s weeks', $duration_number, 'learnpress' ),
1555 $duration_number
1556 );
1557 break;
1558 default:
1559 $duration_str = $duration;
1560 }
1561 }
1562
1563 return empty( absint( $duration ) ) ? $default : $duration_str;
1564 }
1565
1566 /**
1567 * Get level post meta.
1568 *
1569 * @param int $post_id
1570 *
1571 * @return string
1572 */
1573 function learn_press_get_post_level( $post_id ) {
1574 $level = get_post_meta( $post_id, '_lp_level', true );
1575
1576 return apply_filters(
1577 'learn-press/level-label',
1578 ! empty( $level ) ? lp_course_level()[ $level ] : esc_html__( 'All levels', 'learnpress' ),
1579 $post_id
1580 );
1581 }
1582
1583 function lp_course_level() {
1584 return apply_filters(
1585 'lp/template/function/course/level',
1586 array(
1587 '' => esc_html__( 'All levels', 'learnpress' ),
1588 'beginner' => esc_html__( 'Beginner', 'learnpress' ),
1589 'intermediate' => esc_html__( 'Intermediate', 'learnpress' ),
1590 'expert' => esc_html__( 'Expert', 'learnpress' ),
1591 )
1592 );
1593 }
1594
1595 // function learn_press_is_preview_course() {
1596 // $course_id = isset( $GLOBALS['preview_course'] ) ? $GLOBALS['preview_course'] : 0;
1597 //
1598 // return $course_id && get_post_type( $course_id ) === LP_COURSE_CPT;
1599 // }
1600
1601 /**
1602 * Get slug for logout action in user profile.
1603 *
1604 * @return string
1605 * @since 4.0.0
1606 */
1607 function learn_press_profile_logout_slug() {
1608 return apply_filters( 'learn-press/profile-logout-slug', 'lp-logout' );
1609 }
1610
1611 function lp_get_email_content( $format, $meta = array(), $field = array() ) {
1612 if ( $meta && isset( $meta[ $format ] ) ) {
1613 $content = stripslashes( $meta[ $format ] );
1614 } else {
1615 $template = ! empty( $field[ "template_{$format}" ] ) ? $field[ "template_{$format}" ] : null;
1616 $template_file = $field['template_base'] . $template;
1617 $content = LP_WP_Filesystem::instance()->file_get_contents( $template_file );
1618 }
1619
1620 return $content;
1621 }
1622
1623 function lp_skeleton_animation_html( $count_li = 3, $width = 'random', $styleli = '', $styleul = '' ) {
1624 ?>
1625 <ul class="lp-skeleton-animation" style="<?php echo esc_attr( $styleul ); ?>">
1626 <?php for ( $i = 0; $i < absint( $count_li ); $i ++ ) : ?>
1627 <li style="width: <?php echo esc_attr( $width === 'random' ? wp_rand( 60, 100 ) . '%' : $width ); ?>; <?php echo ! empty( $styleli ) ? $styleli : ''; ?>"></li>
1628 <?php endfor; ?>
1629 </ul>
1630
1631 <?php
1632 }
1633
1634 add_filter( 'lp_format_page_content', 'wptexturize' );
1635 add_filter( 'lp_format_page_content', 'convert_smilies' );
1636 add_filter( 'lp_format_page_content', 'convert_chars' );
1637 add_filter( 'lp_format_page_content', 'wpautop' );
1638 add_filter( 'lp_format_page_content', 'shortcode_unautop' );
1639 add_filter( 'lp_format_page_content', 'prepend_attachment' );
1640 add_filter( 'lp_format_page_content', 'do_shortcode', 11 );
1641 add_filter( 'lp_format_page_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
1642
1643 if ( function_exists( 'do_blocks' ) ) {
1644 add_filter( 'lp_format_page_content', 'do_blocks', 9 );
1645 }
1646
1647 function lp_format_page_content( $raw_string ) {
1648 return apply_filters( 'lp_format_page_content', $raw_string );
1649 }
1650
1651 if ( ! function_exists( 'lp_profile_page_content' ) ) {
1652 function lp_profile_page_content() {
1653 $profile_id = learn_press_get_page_id( 'profile' );
1654
1655 if ( $profile_id ) {
1656 $profile_page = get_post( $profile_id );
1657
1658 // remove_shortcode( 'learn_press_profile' );
1659 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1660
1661 if ( $description ) {
1662 echo '<div class="lp-profile-page__content">' . $description . '</div>';
1663 }
1664 }
1665 }
1666 }
1667
1668 if ( ! function_exists( 'lp_archive_course_page_content' ) ) {
1669 add_action( 'lp/template/archive-course/description', 'lp_archive_course_page_content' );
1670
1671 function lp_archive_course_page_content() {
1672 if ( is_search() ) {
1673 return;
1674 }
1675
1676 if ( is_post_type_archive( LP_COURSE_CPT ) && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) {
1677 $profile_id = learn_press_get_page_id( 'courses' );
1678
1679 if ( $profile_id ) {
1680 $profile_page = get_post( $profile_id );
1681
1682 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1683 if ( $description ) {
1684 echo '<div class="lp-course-page__content">' . $description . '</div>';
1685 }
1686 }
1687 }
1688 }
1689 }
1690
1691 if ( ! function_exists( 'lp_taxonomy_archive_course_description' ) ) {
1692 add_action( 'lp/template/archive-course/description', 'lp_taxonomy_archive_course_description' );
1693
1694 function lp_taxonomy_archive_course_description() {
1695
1696 if ( learn_press_is_course_tax() && 0 === absint( get_query_var( 'paged' ) ) ) {
1697 $term = get_queried_object();
1698
1699 if ( $term && ! empty( $term->description ) ) {
1700 echo '<div class="lp-archive-course-term-description">' . lp_format_page_content( wp_kses_post( $term->description ) ) . '</div>';
1701 }
1702 }
1703 }
1704 }
1705
1706 /**
1707 * Params to query courses on Archive Course.
1708 *
1709 * @return array
1710 */
1711 function lp_archive_skeleton_get_args(): array {
1712 global $post, $wp;
1713
1714 $args = array();
1715
1716 if ( ! empty( $_GET ) ) {
1717 $args = (array) $_GET;
1718 }
1719
1720 $params = apply_filters(
1721 'lp/template/archive-course/skeleton/args',
1722 array(
1723 'paged' => 1,
1724 'c_search' => '',
1725 'orderby' => '',
1726 'order' => '',
1727 )
1728 );
1729
1730 if ( learn_press_is_course_category() || learn_press_is_course_tag() ) {
1731 $cat = get_queried_object();
1732
1733 $args['term_id'] = $cat->term_id;
1734 $args['taxonomy'] = $cat->taxonomy;
1735 }
1736
1737 if ( learn_press_is_course_archive() ) {
1738 foreach ( $params as $key => $param ) {
1739 if ( isset( $_REQUEST[ $key ] ) ) {
1740 $args[ $key ] = LP_Helper::sanitize_params_submitted( $_REQUEST[ $key ] );
1741 } else {
1742 $args[ $key ] = $param;
1743 }
1744 }
1745 }
1746
1747 return $args;
1748 }
1749