PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.2
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.2, at inc/lp-template-functions.php

1,741 lines 44.2 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 * @depecated 4.1.6.4
724 */
725 function learn_press_template_redirect() {
726 _deprecated_function( __FUNCTION__, '4.1.6.4' );
727 global $wp_query, $wp;
728
729 // When default permalinks are enabled, redirect shop page to post type archive url
730 if ( ! empty( $_GET['page_id'] ) && get_option( 'permalink_structure' ) == '' && $_GET['page_id'] == learn_press_get_page_id( 'courses' ) ) {
731 wp_safe_redirect( get_post_type_archive_link( 'lp_course' ) );
732 exit;
733 }
734 }
735
736 // add_action( 'template_redirect', 'learn_press_template_redirect' );
737
738
739 /**
740 * Get template part.
741 *
742 * @param string $slug
743 * @param string $name
744 *
745 * @return string
746 */
747 function learn_press_get_template_part( $slug, $name = '' ) {
748 $template = '';
749
750 if ( $name ) {
751 $template = locate_template(
752 array(
753 "{$slug}-{$name}.php",
754 learn_press_template_path() . "/{$slug}-{$name}.php",
755 )
756 );
757 }
758
759 // Get default slug-name.php
760 if ( ! $template && $name && file_exists( LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php" ) ) {
761 $template = LP_PLUGIN_PATH . "/templates/{$slug}-{$name}.php";
762 }
763
764 // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/learnpress/slug.php
765 if ( ! $template ) {
766 $template = locate_template( array( "{$slug}.php", learn_press_template_path() . "/{$slug}.php" ) );
767 }
768 // override path of child theme in parent theme - Fix for eduma by tuanta
769 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
770 if ( $file_ct_in_pr && $name ) {
771 $template_child = locate_template(
772 array(
773 "{$slug}-{$name}.php",
774 'lp-child-path/' . learn_press_template_path() . '/' . $file_ct_in_pr . "/{$slug}-{$name}.php",
775 )
776 );
777 if ( $template_child && file_exists( $template_child ) ) {
778 $template = $template_child;
779 }
780 // check in child theme if have filter learn_press_child_in_parrent_template_path
781
782 $check_child_theme = get_stylesheet_directory() . '/' . learn_press_template_path() . "{$slug}-{$name}.php";
783 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
784 $template = $check_child_theme;
785 }
786 }
787
788 // Allow 3rd party plugin filter template file from their plugin
789 if ( $template ) {
790 $template = apply_filters( 'learn_press_get_template_part', $template, $slug, $name );
791 }
792 if ( $template && file_exists( $template ) ) {
793 load_template( $template, false );
794 }
795
796 return $template;
797 }
798
799 /**
800 * Get other templates passing attributes and including the file.
801 *
802 * @param string $template_name .
803 * @param array $args (default: array()) .
804 * @param string $template_path (default: '').
805 * @param string $default_path (default: '').
806 *
807 * @return void
808 */
809 function learn_press_get_template( $template_name = '', $args = array(), $template_path = '', $default_path = '' ) {
810 if ( $args && is_array( $args ) ) {
811 extract( $args );
812 }
813
814 if ( false === strpos( $template_name, '.php' ) ) {
815 $template_name .= '.php';
816 }
817
818 $located = learn_press_locate_template( $template_name, $template_path, $default_path );
819
820 if ( ! file_exists( $located ) ) {
821 _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
822
823 $log = sprintf( 'TEMPLATE MISSING: Template %s doesn\'t exists.', $template_name );
824 error_log( $log );
825
826 if ( LP_Debug::is_debug() ) {
827 echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log );
828 }
829
830 return;
831 }
832
833 // Allow 3rd party plugin filter template file from their plugin
834 $located = apply_filters(
835 'learn_press_get_template',
836 $located,
837 $template_name,
838 $args,
839 $template_path,
840 $default_path
841 );
842 if ( $located != '' ) {
843 do_action( 'learn_press_before_template_part', $template_name, $template_path, $located, $args );
844
845 include $located;
846
847 do_action( 'learn_press_after_template_part', $template_name, $template_path, $located, $args );
848 }
849 }
850
851 /**
852 * Get template content
853 *
854 * @param $template_name
855 * @param array $args
856 * @param string $template_path
857 * @param string $default_path
858 *
859 * @return string
860 * @uses learn_press_get_template();
861 */
862 function learn_press_get_template_content( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
863 ob_start();
864 learn_press_get_template( $template_name, $args, $template_path, $default_path );
865
866 return ob_get_clean();
867 }
868
869 /**
870 * Locate a template and return the path for inclusion.
871 *
872 * @param string $template_name
873 * @param string $template_path (default: '')
874 * @param string $default_path (default: '')
875 *
876 * @return string
877 */
878 function learn_press_locate_template( $template_name, $template_path = '', $default_path = '' ) {
879 if ( ! $template_path ) {
880 $template_path = learn_press_template_path();
881 }
882
883 if ( ! $default_path ) {
884 $default_path = LP_PLUGIN_PATH . 'templates/';
885 }
886
887 /**
888 * Disable override templates in theme by default since LP 4.0.0
889 */
890 if ( learn_press_override_templates() ) {
891 $template = locate_template(
892 array(
893 trailingslashit( $template_path ) . $template_name,
894 $template_name,
895 )
896 );
897 // override path of child theme in parent theme - Fix for eduma by tuanta
898 $file_ct_in_pr = apply_filters( 'learn_press_child_in_parrent_template_path', '' );
899 if ( $file_ct_in_pr ) {
900 $template_child = locate_template(
901 array(
902 trailingslashit( 'lp-child-path/' . $template_path . '/' . $file_ct_in_pr ) . $template_name,
903 $template_name,
904 )
905 );
906 if ( $template_child && file_exists( $template_child ) ) {
907 $template = $template_child;
908 }
909 // check in child theme if have filter learn_press_child_in_parrent_template_path
910 $check_child_theme = get_stylesheet_directory() . '/' . trailingslashit( $template_path ) . $template_name;
911 if ( $check_child_theme && file_exists( $check_child_theme ) ) {
912 $template = $check_child_theme;
913 }
914 }
915 }
916 if ( ! isset( $template ) || ! $template ) {
917 $template = trailingslashit( $default_path ) . $template_name;
918 }
919
920 return apply_filters( 'learn_press_locate_template', $template, $template_name, $template_path );
921 }
922
923 /**
924 * Returns the name of folder contains template files in theme
925 *
926 * @param bool
927 *
928 * @return string
929 */
930 function learn_press_template_path( $slash = false ) {
931 return apply_filters( 'learn_press_template_path', 'learnpress', $slash ) . ( $slash ? '/' : '' );
932 }
933
934 /**
935 * Disable override templates in theme by default
936 *
937 * @return bool
938 * @since 4.0.0
939 */
940 function learn_press_override_templates() {
941 return apply_filters( 'learn-press/override-templates', false );
942 }
943
944 if ( ! function_exists( 'learn_press_is_404' ) ) {
945 /**
946 * Set header is 404
947 */
948 function learn_press_is_404() {
949 global $wp_query;
950 if ( ! empty( $_REQUEST['debug-404'] ) ) {
951 learn_press_debug( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $_REQUEST['debug-404'] ) );
952 }
953 $wp_query->set_404();
954 status_header( 404 );
955 }
956 }
957
958 if ( ! function_exists( 'learn_press_404_page' ) ) {
959 /**
960 * Display 404 page
961 */
962 function learn_press_404_page() {
963 learn_press_is_404();
964 }
965 }
966
967 if ( ! function_exists( 'learn_press_generate_template_information' ) ) {
968 function learn_press_generate_template_information( $template_name, $template_path, $located, $args ) {
969 $debug = learn_press_get_request( 'show-template-location' );
970 if ( $debug == 'on' ) {
971 echo '<!-- Template Location:' . str_replace( array( LP_PLUGIN_PATH, ABSPATH ), '', $located ) . ' -->';
972 }
973 }
974 }
975
976 if ( ! function_exists( 'learn_press_course_remaining_time' ) ) {
977 /**
978 * Show the time remain of a course
979 */
980 function learn_press_course_remaining_time() {
981 $user = learn_press_get_current_user();
982 if ( ! $user->has_finished_course( get_the_ID() ) && $text = learn_press_get_course( get_the_ID() )->get_user_duration_html( $user->get_id() ) ) {
983 learn_press_display_message( $text );
984 }
985 }
986 }
987
988 if ( ! function_exists( 'learn_press_item_meta_type' ) ) {
989 function learn_press_item_meta_type( $course, $item ) {
990 ?>
991
992 <?php if ( $item->post_type == 'lp_quiz' ) { ?>
993
994 <span class="lp-label lp-label-quiz"><?php _e( 'Quiz', 'learnpress' ); ?></span>
995
996 <?php if ( $course->final_quiz == $item->ID ) { ?>
997
998 <span class="lp-label lp-label-final"><?php _e( 'Final', 'learnpress' ); ?></span>
999
1000 <?php } ?>
1001
1002 <?php } elseif ( $item->post_type == 'lp_lesson' ) { ?>
1003
1004 <span class="lp-label lp-label-lesson"><?php _e( 'Lesson', 'learnpress' ); ?></span>
1005 <?php if ( get_post_meta( $item->ID, '_lp_preview', true ) == 'yes' ) { ?>
1006
1007 <span class="lp-label lp-label-preview"><?php _e( 'Preview', 'learnpress' ); ?></span>
1008
1009 <?php } ?>
1010
1011 <?php } else { ?>
1012
1013 <?php do_action( 'learn_press_item_meta_type', $course, $item ); ?>
1014
1015 <?php
1016 }
1017 }
1018 }
1019
1020 if ( ! function_exists( 'learn_press_sort_course_tabs' ) ) {
1021 function learn_press_sort_course_tabs( $tabs = array() ) {
1022 uasort( $tabs, 'learn_press_sort_list_by_priority_callback' );
1023
1024 return $tabs;
1025 }
1026 }
1027
1028 if ( ! function_exists( 'learn_press_get_profile_display_name' ) ) {
1029 /**
1030 * Get Display name publicly as in Profile page
1031 */
1032 function learn_press_get_profile_display_name( $user ) {
1033 if ( empty( $user ) ) {
1034 return '';
1035 }
1036
1037 $id = '';
1038
1039 if ( $user instanceof LP_Abstract_User ) {
1040 $id = $user->get_id();
1041 } elseif ( $user instanceof WP_User ) {
1042 $id = $user->ID;
1043 } elseif ( is_numeric( $user ) ) {
1044 $id = $user;
1045 }
1046
1047 if ( ! isset( $id ) ) {
1048 return '';
1049 }
1050
1051 $info = get_userdata( $id );
1052
1053 return $info ? $info->display_name : '';
1054 }
1055 }
1056
1057 /**
1058 * @depecated 4.1.6.9
1059 */
1060 /*if ( ! function_exists( 'learn_press_content_item_comments' ) ) {
1061 function learn_press_content_item_comments() {
1062 $item = LP_Global::course_item();
1063
1064 if ( ! $item ) {
1065 return;
1066 }
1067
1068 if ( ! $item->is_support( 'comments' ) ) {
1069 return;
1070 }
1071
1072 global $post;
1073
1074 $post = get_post( $item->get_id() );
1075
1076 setup_postdata( $post );
1077
1078 if ( ! have_comments() ) {
1079 return;
1080 }
1081
1082 add_filter( 'deprecated_file_trigger_error', '__return_false' );
1083 comments_template();
1084 remove_filter( 'deprecated_file_trigger_error', '__return_false' );
1085
1086 wp_reset_postdata();
1087 }
1088 }*/
1089
1090 function learn_press_course_comments_open( $open, $post_id ) {
1091 $post = get_post( $post_id );
1092
1093 if ( LP_COURSE_CPT == $post->post_type ) {
1094 $open = false;
1095 }
1096
1097 return $open;
1098 }
1099
1100 function learn_press_is_content_item_only() {
1101 return ! empty( $_REQUEST['content-item-only'] );
1102 }
1103
1104 function learn_press_label_html( $label, $type = '' ) {
1105 ?>
1106 <span class="lp-label label-<?php echo esc_attr( $type ? $type : $label ); ?>">
1107 <?php echo esc_html( $label ); ?>
1108 </span>
1109 <?php
1110 }
1111
1112 /**
1113 * @depecated 4.1.6.4
1114 */
1115 function learn_press_get_course_redirect( $link ) {
1116 _deprecated_function( __FUNCTION__, '4.1.6.4' );
1117 if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
1118 return $link;
1119 }
1120
1121 $referer = $_SERVER['HTTP_REFERER'];
1122 $info_a = parse_url( $referer );
1123 $info_b = parse_url( $link );
1124
1125 $a = explode( '/', $info_a['path'] );
1126 $a = array_filter( $a );
1127
1128 $b = explode( '/', $info_b['path'] );
1129 $b = array_filter( $b );
1130
1131 $same = array_intersect_assoc( $a, $b );
1132
1133 $a = array_diff_assoc( $a, $same );
1134 $b = array_diff_assoc( $b, $same );
1135
1136 $a = array_values( $a );
1137 $b = array_values( $b );
1138
1139 if ( array_shift( $a ) === 'popup' ) {
1140 unset( $a[0] );
1141
1142 if ( ! ( array_diff_assoc( $a, $b ) ) ) {
1143 $link = '';
1144 foreach ( array( 'scheme', 'host', 'port', 'path' ) as $v ) {
1145 if ( ! isset( $info_a[ $v ] ) ) {
1146 continue;
1147 }
1148
1149 if ( $v == 'scheme' ) {
1150 $sep = '://';
1151 } elseif ( $v == 'host' ) {
1152 $sep = '';
1153 } elseif ( $v == 'port' ) {
1154 $link .= ':';
1155 $sep = '';
1156 } else {
1157 $sep = '/';
1158 }
1159 $link = $link . $info_a[ $v ] . $sep;
1160 }
1161
1162 if ( ! empty( $info_b['query'] ) ) {
1163 $link .= '?' . $info_b['query'];
1164 }
1165
1166 if ( ! empty( $info_b['fragment'] ) ) {
1167 $link .= '#' . $info_b['fragment'];
1168 }
1169 }
1170 }
1171
1172 return $link;
1173 }
1174
1175 /**
1176 * @param LP_Quiz $item
1177 */
1178 function learn_press_quiz_meta_final( $item ) {
1179 $course = learn_press_get_course();
1180
1181 if ( ! $course->is_final_quiz( $item->get_id() ) ) {
1182 return;
1183 }
1184 echo '<span class="item-meta final-quiz">' . esc_html__( 'Final', 'learnpress' ) . '</span>';
1185 }
1186
1187 function learn_press_comments_template_query_args( $comment_args ) {
1188 $post_type = get_post_type( $comment_args['post_id'] );
1189
1190 if ( $post_type == LP_COURSE_CPT ) {
1191 $comment_args['type__not_in'] = 'review';
1192 }
1193
1194 return $comment_args;
1195 }
1196
1197 if ( ! function_exists( 'learn_press_filter_get_comments_number' ) ) {
1198 function learn_press_filter_get_comments_number( $count, $post_id = 0 ) {
1199 global $wpdb;
1200
1201 if ( ! $post_id ) {
1202 $post_id = learn_press_get_course_id();
1203 }
1204
1205 if ( ! $post_id ) {
1206 return $count;
1207 }
1208
1209 if ( get_post_type( $post_id ) == LP_COURSE_CPT ) {
1210 $sql = $wpdb->prepare(
1211 ' SELECT count(*) '
1212 . " FROM {$wpdb->comments} "
1213 . ' WHERE comment_post_ID = %d '
1214 . ' AND comment_approved = 1 '
1215 . ' AND comment_type != %s ',
1216 $post_id,
1217 'review'
1218 );
1219
1220 $count = $wpdb->get_var( $sql );
1221
1222 // @since 3.0.0
1223 $count = apply_filters( 'learn-press/course-comments-number', $count, $post_id );
1224 }
1225
1226 return $count;
1227 }
1228 }
1229
1230 /**
1231 * Add custom classes to body tag class name
1232 *
1233 * @param array $classes
1234 *
1235 * @return array
1236 *
1237 * @since 3.0.0
1238 */
1239 function learn_press_body_classes( $classes ) {
1240 $pages = learn_press_static_page_ids();
1241
1242 if ( $pages ) {
1243 $is_lp_page = false;
1244 settype( $classes, 'array' );
1245
1246 foreach ( $pages as $slug => $id ) {
1247 if ( is_page( $id ) ) {
1248 $classes[] = $slug;
1249 $is_lp_page = true;
1250 }
1251 }
1252
1253 if ( $is_lp_page || is_learnpress() ) {
1254 $classes[] = get_stylesheet();
1255 $classes[] = 'learnpress';
1256 $classes[] = 'learnpress-page';
1257 }
1258 }
1259
1260 return $classes;
1261 }
1262
1263 add_filter( 'body_class', 'learn_press_body_classes', 10 );
1264
1265 /**
1266 * Return true if user is learning a course
1267 *
1268 * @param int $course_id
1269 *
1270 * @return bool
1271 * @since 3.0
1272 * @version 1.0.1
1273 */
1274 function learn_press_is_learning_course( int $course_id = 0 ): bool {
1275 $is_learning = false;
1276 $user = learn_press_get_current_user();
1277 $course = learn_press_get_course( $course_id );
1278 if ( ! $course ) {
1279 return $is_learning;
1280 }
1281
1282 if ( $user ) {
1283 return $user->has_enrolled_or_finished( $course_id );
1284 }
1285
1286 if ( $course->is_no_required_enroll() ) {
1287 $is_learning = true;
1288 }
1289
1290 return apply_filters( 'lp/is-learning-course', $is_learning, $course_id );
1291 }
1292
1293 /**
1294 * Output custom css from settings
1295 *
1296 * @since 4.0.0
1297 */
1298 if ( ! function_exists( 'learn_press_print_custom_styles' ) ) {
1299 function learn_press_print_custom_styles() {
1300 $primary_color = LP_Settings::instance()->get( 'primary_color' );
1301 $secondary_color = LP_Settings::instance()->get( 'secondary_color' );
1302 ?>
1303
1304 <style id="learn-press-custom-css">
1305 :root {
1306 --lp-primary-color: <?php echo ! empty( $primary_color ) ? $primary_color : '#ffb606'; ?>;
1307 --lp-secondary-color: <?php echo ! empty( $secondary_color ) ? $secondary_color : '#442e66'; ?>;
1308 }
1309 </style>
1310
1311 <?php
1312 }
1313
1314 add_action( 'wp_head', 'learn_press_print_custom_styles' );
1315 }
1316
1317 /**
1318 * Return TRUE if current user has already enroll course in single view.
1319 *
1320 * @return bool
1321 * @since 3.0.0
1322 * @editor tungnx
1323 * @version 4.1.3
1324 */
1325 function learn_press_current_user_enrolled_course() {
1326 _deprecated_function( __FUNCTION__, '4.1.3' );
1327 }
1328
1329 /**
1330 * Check if an user can access content of a course.
1331 *
1332 * @param int $course_id
1333 * @param int $user_id
1334 *
1335 * @return bool
1336 * @since 3.x.x
1337 * @editor tungnx
1338 * @modify 4.1.3
1339 * @reason comment - not use
1340 */
1341
1342 function learn_press_user_can_access_course( $course_id, $user_id = 0 ) {
1343 _deprecated_function( __FUNCTION__, '4.1.2' );
1344 }
1345
1346 function learn_press_content_item_summary_class( $more = '', $echo = true ) {
1347 $classes = array( 'content-item-summary' );
1348 $classes = LP_Helper::merge_class( $classes, $more );
1349 $classes = apply_filters( 'learn-press/content-item-summary-class', $classes );
1350 $output = 'class="' . esc_attr( join( ' ', $classes ) ) . '"';
1351
1352 if ( $echo ) {
1353 echo wp_kses_post( $output );
1354 }
1355
1356 return $output;
1357 }
1358
1359 function learn_press_content_item_summary_classes( $classes ) {
1360 $item = LP_Global::course_item();
1361
1362 if ( ! $item ) {
1363 return $classes;
1364 }
1365
1366 if ( $item->get_post_type() !== LP_LESSON_CPT ) {
1367 return $classes;
1368 }
1369
1370 return $classes;
1371 }
1372
1373 function learn_press_maybe_load_comment_js() {
1374 $item = LP_Global::course_item();
1375
1376 if ( $item ) {
1377 wp_enqueue_script( 'comment-reply' );
1378 }
1379 }
1380
1381 add_action( 'wp_enqueue_scripts', 'learn_press_maybe_load_comment_js' );
1382
1383 /**
1384 * Get list layouts archive course setting
1385 *
1386 * @editor tungnx
1387 * @modify 4.1.3
1388 */
1389 function learn_press_courses_layouts() {
1390 return apply_filters(
1391 'learnpress/archive-courses-layouts',
1392 [
1393 'grid' => 'Grid',
1394 'list' => 'List',
1395 ]
1396 );
1397 }
1398
1399 /**
1400 * Get layout template for archive course page.
1401 *
1402 * @return mixed
1403 * @since 3.3.0
1404 * @editor tungnx
1405 * @modify 4.1.3
1406 */
1407 function learn_press_get_courses_layout() {
1408 $layout = LP_Request::get_cookie( 'courses-layout' );
1409
1410 if ( ! $layout ) {
1411 $layout = LP_Settings::get_option( 'archive_courses_layout', 'list' );
1412 }
1413
1414 return $layout;
1415 }
1416
1417 function learn_press_register_sidebars() {
1418 register_sidebar(
1419 array(
1420 'name' => esc_html__( 'Course Sidebar', 'learnpress' ),
1421 'id' => 'course-sidebar',
1422 'description' => esc_html__( 'Widgets in this area will be shown in single course', 'learnpress' ),
1423 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1424 'after_widget' => '</div>',
1425 'before_title' => '<h2 class="widgettitle">',
1426 'after_title' => '</h2>',
1427 )
1428 );
1429 register_sidebar(
1430 array(
1431 'name' => esc_html__( 'All Courses', 'learnpress' ),
1432 'id' => 'archive-courses-sidebar',
1433 'description' => esc_html__( 'Widgets in this area will be shown in all courses page', 'learnpress' ),
1434 'before_widget' => '<div id="%1$s" class="widget %2$s">',
1435 'after_widget' => '</div>',
1436 'before_title' => '<h2 class="widgettitle">',
1437 'after_title' => '</h2>',
1438 )
1439 );
1440 }
1441
1442 add_action( 'widgets_init', 'learn_press_register_sidebars' );
1443
1444 function learn_press_setup_theme() {
1445 $support = array(
1446 'widgets' => array(
1447 // Place three core-defined widgets in the sidebar area.
1448 'course-sidebar' => array(
1449 'xxx' => array( 'lp-widget-course-progress' ),
1450 'yyy' => array( 'lp-widget-course-info' ),
1451 ),
1452 ),
1453 );
1454
1455 add_theme_support( 'starter-content', $support );
1456 }
1457
1458 add_action( 'after_setup_theme', 'learn_press_setup_theme' );
1459
1460 /**
1461 * @param LP_Question $question
1462 * @param array $args
1463 *
1464 * @return array
1465 * @since 4.x.x
1466 */
1467 function learn_press_get_question_options_for_js( $question, $args = array() ) {
1468 $args = wp_parse_args(
1469 $args,
1470 array(
1471 'cryptoJsAes' => false,
1472 'include_is_true' => true,
1473 'answer' => false,
1474 )
1475 );
1476
1477 if ( $args['cryptoJsAes'] ) {
1478 $options = array_values( $question->get_answer_options() );
1479
1480 $key = uniqid();
1481 $options = array(
1482 'data' => cryptoJsAesEncrypt( $key, wp_json_encode( $options ) ),
1483 'key' => $key,
1484 );
1485 } else {
1486 $exclude_option_key = array( 'question_id', 'order' );
1487 if ( ! $args['include_is_true'] ) {
1488 $exclude_option_key[] = 'is_true';
1489 }
1490
1491 $options = array_values(
1492 $question->get_answer_options(
1493 array(
1494 'exclude' => $exclude_option_key,
1495 'map' => array( 'question_answer_id' => 'uid' ),
1496 'answer' => $args['answer'],
1497 )
1498 )
1499 );
1500 }
1501
1502 return $options;
1503 }
1504
1505 function learn_press_custom_excerpt_length( $length ) {
1506 return 20;
1507 }
1508
1509 /**
1510 * Get post meta with key _lp_duration and translate.
1511 *
1512 * @param int $post_id
1513 * @param string $default
1514 *
1515 * @return string
1516 * @since 4.0.0
1517 */
1518 function learn_press_get_post_translated_duration( $post_id, $default = '' ) {
1519 $duration = get_post_meta( $post_id, '_lp_duration', true );
1520
1521 $duration_arr = explode( ' ', $duration );
1522 $duration_str = '';
1523
1524 if ( count( $duration_arr ) > 1 ) {
1525 $duration_number = $duration_arr[0];
1526 $duration_text = $duration_arr[1];
1527
1528 switch ( strtolower( $duration_text ) ) {
1529 case 'minute':
1530 $duration_str = sprintf(
1531 _n( '%s minute', '%s minutes', $duration_number, 'learnpress' ),
1532 $duration_number
1533 );
1534 break;
1535 case 'hour':
1536 $duration_str = sprintf(
1537 _n( '%s hour', '%s hours', $duration_number, 'learnpress' ),
1538 $duration_number
1539 );
1540 break;
1541 case 'day':
1542 $duration_str = sprintf( _n( '%s day', '%s days', $duration_number, 'learnpress' ), $duration_number );
1543 break;
1544 case 'week':
1545 $duration_str = sprintf(
1546 _n( '%s week', '%s weeks', $duration_number, 'learnpress' ),
1547 $duration_number
1548 );
1549 break;
1550 default:
1551 $duration_str = $duration;
1552 }
1553 }
1554
1555 return empty( absint( $duration ) ) ? $default : $duration_str;
1556 }
1557
1558 /**
1559 * Get level post meta.
1560 *
1561 * @param int $post_id
1562 *
1563 * @return string
1564 */
1565 function learn_press_get_post_level( $post_id ) {
1566 $level = get_post_meta( $post_id, '_lp_level', true );
1567
1568 return apply_filters(
1569 'learn-press/level-label',
1570 ! empty( $level ) ? lp_course_level()[ $level ] : esc_html__( 'All levels', 'learnpress' ),
1571 $post_id
1572 );
1573 }
1574
1575 function lp_course_level() {
1576 return apply_filters(
1577 'lp/template/function/course/level',
1578 array(
1579 '' => esc_html__( 'All levels', 'learnpress' ),
1580 'beginner' => esc_html__( 'Beginner', 'learnpress' ),
1581 'intermediate' => esc_html__( 'Intermediate', 'learnpress' ),
1582 'expert' => esc_html__( 'Expert', 'learnpress' ),
1583 )
1584 );
1585 }
1586
1587 // function learn_press_is_preview_course() {
1588 // $course_id = isset( $GLOBALS['preview_course'] ) ? $GLOBALS['preview_course'] : 0;
1589 //
1590 // return $course_id && get_post_type( $course_id ) === LP_COURSE_CPT;
1591 // }
1592
1593 /**
1594 * Get slug for logout action in user profile.
1595 *
1596 * @return string
1597 * @since 4.0.0
1598 */
1599 function learn_press_profile_logout_slug() {
1600 return apply_filters( 'learn-press/profile-logout-slug', 'lp-logout' );
1601 }
1602
1603 function lp_get_email_content( $format, $meta = array(), $field = array() ) {
1604 if ( $meta && isset( $meta[ $format ] ) ) {
1605 $content = stripslashes( $meta[ $format ] );
1606 } else {
1607 $template = ! empty( $field[ "template_{$format}" ] ) ? $field[ "template_{$format}" ] : null;
1608 $template_file = $field['template_base'] . $template;
1609 $content = LP_WP_Filesystem::instance()->file_get_contents( $template_file );
1610 }
1611
1612 return $content;
1613 }
1614
1615 function lp_skeleton_animation_html( $count_li = 3, $width = 'random', $styleli = '', $styleul = '' ) {
1616 ?>
1617 <ul class="lp-skeleton-animation" style="<?php echo esc_attr( $styleul ); ?>">
1618 <?php for ( $i = 0; $i < absint( $count_li ); $i ++ ) : ?>
1619 <li style="width: <?php echo esc_attr( $width === 'random' ? wp_rand( 60, 100 ) . '%' : $width ); ?>; <?php echo ! empty( $styleli ) ? $styleli : ''; ?>"></li>
1620 <?php endfor; ?>
1621 </ul>
1622
1623 <?php
1624 }
1625
1626 add_filter( 'lp_format_page_content', 'wptexturize' );
1627 add_filter( 'lp_format_page_content', 'convert_smilies' );
1628 add_filter( 'lp_format_page_content', 'convert_chars' );
1629 add_filter( 'lp_format_page_content', 'wpautop' );
1630 add_filter( 'lp_format_page_content', 'shortcode_unautop' );
1631 add_filter( 'lp_format_page_content', 'prepend_attachment' );
1632 add_filter( 'lp_format_page_content', 'do_shortcode', 11 );
1633 add_filter( 'lp_format_page_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
1634
1635 if ( function_exists( 'do_blocks' ) ) {
1636 add_filter( 'lp_format_page_content', 'do_blocks', 9 );
1637 }
1638
1639 function lp_format_page_content( $raw_string ) {
1640 return apply_filters( 'lp_format_page_content', $raw_string );
1641 }
1642
1643 if ( ! function_exists( 'lp_profile_page_content' ) ) {
1644 function lp_profile_page_content() {
1645 $profile_id = learn_press_get_page_id( 'profile' );
1646
1647 if ( $profile_id ) {
1648 $profile_page = get_post( $profile_id );
1649
1650 // remove_shortcode( 'learn_press_profile' );
1651 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1652
1653 if ( $description ) {
1654 echo '<div class="lp-profile-page__content">' . $description . '</div>';
1655 }
1656 }
1657 }
1658 }
1659
1660 if ( ! function_exists( 'lp_archive_course_page_content' ) ) {
1661 add_action( 'lp/template/archive-course/description', 'lp_archive_course_page_content' );
1662
1663 function lp_archive_course_page_content() {
1664 if ( is_search() ) {
1665 return;
1666 }
1667
1668 if ( is_post_type_archive( LP_COURSE_CPT ) && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) {
1669 $profile_id = learn_press_get_page_id( 'courses' );
1670
1671 if ( $profile_id ) {
1672 $profile_page = get_post( $profile_id );
1673
1674 $description = lp_format_page_content( wp_kses_post( $profile_page->post_content ) );
1675 if ( $description ) {
1676 echo '<div class="lp-course-page__content">' . $description . '</div>';
1677 }
1678 }
1679 }
1680 }
1681 }
1682
1683 if ( ! function_exists( 'lp_taxonomy_archive_course_description' ) ) {
1684 add_action( 'lp/template/archive-course/description', 'lp_taxonomy_archive_course_description' );
1685
1686 function lp_taxonomy_archive_course_description() {
1687
1688 if ( learn_press_is_course_tax() && 0 === absint( get_query_var( 'paged' ) ) ) {
1689 $term = get_queried_object();
1690
1691 if ( $term && ! empty( $term->description ) ) {
1692 echo '<div class="lp-archive-course-term-description">' . lp_format_page_content( wp_kses_post( $term->description ) ) . '</div>';
1693 }
1694 }
1695 }
1696 }
1697
1698 /**
1699 * Params to query courses on Archive Course.
1700 *
1701 * @return array
1702 */
1703 function lp_archive_skeleton_get_args(): array {
1704 global $post, $wp;
1705
1706 $args = array();
1707
1708 if ( ! empty( $_GET ) ) {
1709 $args = (array) $_GET;
1710 }
1711
1712 $params = apply_filters(
1713 'lp/template/archive-course/skeleton/args',
1714 array(
1715 'paged' => 1,
1716 'c_search' => '',
1717 'orderby' => '',
1718 'order' => '',
1719 )
1720 );
1721
1722 if ( learn_press_is_course_category() || learn_press_is_course_tag() ) {
1723 $cat = get_queried_object();
1724
1725 $args['term_id'] = $cat->term_id;
1726 $args['taxonomy'] = $cat->taxonomy;
1727 }
1728
1729 if ( learn_press_is_course_archive() ) {
1730 foreach ( $params as $key => $param ) {
1731 if ( isset( $_REQUEST[ $key ] ) ) {
1732 $args[ $key ] = LP_Helper::sanitize_params_submitted( $_REQUEST[ $key ] );
1733 } else {
1734 $args[ $key ] = $param;
1735 }
1736 }
1737 }
1738
1739 return $args;
1740 }
1741