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

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

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