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

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