PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
learnpress / inc / lp-core-functions.php

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

2,325 lines 56.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * LearnPress Core Functions
4 * Define common functions for both front-end and back-end
5 *
6 * @author ThimPress
7 * @package LearnPress/Functions
8 * @version 1.0
9 */
10
11 use LearnPress\Helpers\Config;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Vue write php has script, so when sanitize will error, so use code is mask pass check sanitize, esc
17 *
18 * @param $content
19 *
20 * @return void
21 * @since 4.1.6.9
22 */
23 function learn_press_echo_vuejs_write_on_php( $content ) {
24 echo ( $content );
25 }
26
27 function learnpress_gutenberg_disable_cpt( $can_edit, $post_type ) {
28 $post_types = array(
29 LP_COURSE_CPT => LP_Settings::get_option( 'enable_gutenberg_course', 'no' ),
30 LP_LESSON_CPT => LP_Settings::get_option( 'enable_gutenberg_lesson', 'no' ),
31 LP_QUIZ_CPT => LP_Settings::get_option( 'enable_gutenberg_quiz', 'no' ),
32 LP_QUESTION_CPT => LP_Settings::get_option( 'enable_gutenberg_question', 'no' ),
33 );
34
35 foreach ( $post_types as $key => $pt ) {
36 if ( $post_type === $key && $pt !== 'yes' ) {
37 $can_edit = false;
38 }
39 }
40
41 return $can_edit;
42 }
43 add_filter( 'use_block_editor_for_post_type', 'learnpress_gutenberg_disable_cpt', 10, 2 );
44
45 if ( ! function_exists( 'lp_add_body_class' ) ) {
46 function lp_add_body_class( $classes ) {
47 $classes = (array) $classes;
48
49 if ( LP_Page_Controller::is_page_profile() ) {
50 $classes[] = 'learnpress-profile';
51 } elseif ( learn_press_is_checkout() ) {
52 $classes[] = 'learnpress-checkout';
53 }
54
55 return $classes;
56 }
57 add_filter( 'body_class', 'lp_add_body_class' );
58 }
59
60 /**
61 * Short function to get name of a theme
62 *
63 * @param string $folder
64 *
65 * @return mixed|string
66 */
67 function learn_press_get_theme_name( $folder ) {
68 $theme = wp_get_theme( $folder );
69
70 return ! empty( $theme['Name'] ) ? $theme['Name'] : '';
71 }
72
73 /**
74 * Clean.
75 *
76 * @param [type] $var
77 *
78 * @version 4.0.0
79 * @author Nhamdv <[email protected]>
80 */
81 function learnpress_clean( $var ) {
82 if ( is_array( $var ) ) {
83 return array_map( 'learnpress_clean', $var );
84 } else {
85 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
86 }
87 }
88
89 /**
90 * Display HTML of element for building QuickTip JS.
91 *
92 * @param string $tip
93 * @param bool $echo
94 * @param array $options
95 *
96 * @return string
97 * @since 3.0.0
98 */
99 function learn_press_quick_tip( $tip, $echo = true, $options = array() ) {
100 $atts = '';
101 if ( $options ) {
102 foreach ( $options as $k => $v ) {
103 $options[ $k ] = "data-{$k}=\"{$v}\"";
104 }
105 $atts = ' ' . implode( ' ', $options );
106 }
107
108 $tip = sprintf( '<div class="learn-press-tip" ' . $atts . '><span>%s</span></div>', $tip );
109
110 if ( $echo ) {
111 echo wp_kses_post( $tip );
112 }
113
114 return $tip;
115 }
116
117 /**
118 * Get current post ID.
119 *
120 * @return int
121 */
122 function learn_press_get_post() {
123 global $post;
124
125 $post_id = learn_press_get_request( 'post' );
126
127 if ( ! $post_id ) {
128 $post_id = ! empty( $post ) ? $post->ID : 0;
129 }
130 if ( empty( $post_id ) ) {
131 $post_id = learn_press_get_request( 'post_ID' );
132 }
133
134 return absint( $post_id );
135 }
136
137 /**
138 * Get the LearnPress plugin url
139 *
140 * @param string $sub_dir
141 *
142 * @return string
143 */
144 function learn_press_plugin_url( $sub_dir = '' ) {
145 return LearnPress::instance()->plugin_url( $sub_dir );
146 }
147
148 /**
149 * Get the LearnPress plugin path.
150 *
151 * @param string $sub_dir
152 *
153 * @return string
154 */
155 function learn_press_plugin_path( $sub_dir = '' ) {
156 return LearnPress::instance()->plugin_path( $sub_dir );
157 }
158
159 /**
160 * Includes file base on LearnPress path
161 *
162 * @param string $file
163 * @param string $folder
164 * @param bool $include_once
165 *
166 * @return bool
167 */
168 function learn_press_include( $file, $folder = 'inc', $include_once = true ) {
169 $include = learn_press_plugin_path( "{$folder}/{$file}" );
170
171 if ( file_exists( $include ) ) {
172 if ( $include_once ) {
173 include_once $include;
174 } else {
175 include $include;
176 }
177
178 return true;
179 }
180
181 return false;
182 }
183
184 /**
185 * Get current IP of the user
186 *
187 * @return mixed
188 */
189 function learn_press_get_ip() {
190 if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
191 return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) );
192 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
193 // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
194 // Make sure we always only send through the first IP in the list which should always be the client IP.
195 return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) );
196 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
197 return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
198 }
199 return '';
200 }
201
202 /**
203 * Get user agent.
204 *
205 * @return string
206 */
207 function learn_press_get_user_agent(): string {
208 return LP_Helper::sanitize_params_submitted( $_SERVER['HTTP_USER_AGENT'] ?? '' );
209 }
210
211 /**
212 * Generate an unique string.
213 *
214 * @param string $prefix
215 *
216 * @return string
217 */
218 function learn_press_uniqid( $prefix = '' ) {
219 $hash = str_replace( '.', '', microtime( true ) . uniqid() );
220
221 return apply_filters( 'learn-press/generate-hash', $prefix . $hash, $prefix );
222 }
223
224 function learn_press_random_value( $len = 8 ) {
225 return substr( md5( uniqid( mt_rand(), true ) ), 0, $len );
226 }
227
228 function learn_press_map_columns_format( $columns, $format ) {
229 $return = array();
230 foreach ( $columns as $k => $v ) {
231 if ( ! empty( $format[ $k ] ) ) {
232 $return[] = $format[ $k ];
233 } else {
234 $return[] = '%s'; // default is string
235 }
236 }
237
238 return $return;
239 }
240
241 /**
242 * Check to see if an endpoint is showing in current URL.
243 *
244 * @param bool $endpoint
245 *
246 * @return bool
247 */
248 function learn_press_is_endpoint_url( $endpoint = false ) {
249 global $wp;
250
251 $endpoints = array();
252
253 if ( $endpoint !== false ) {
254 if ( ! isset( $endpoints[ $endpoint ] ) ) {
255 return false;
256 } else {
257 $endpoint_var = $endpoints[ $endpoint ];
258 }
259
260 return isset( $wp->query_vars[ $endpoint_var ] );
261 } else {
262 foreach ( $endpoints as $key => $value ) {
263 if ( isset( $wp->query_vars[ $key ] ) ) {
264 return true;
265 }
266 }
267
268 return false;
269 }
270 }
271
272 /**
273 * Get current URL user is viewing.
274 *
275 * @return string
276 * @deprecated 4.2.2
277 */
278 function learn_press_get_current_url() {
279 //_deprecated_function( __FUNCTION__, '4.2.2', 'LP_Helper::getUrlCurrent' );
280 return LP_Helper::getUrlCurrent();
281
282 $url = untrailingslashit( esc_url_raw( $_SERVER['REQUEST_URI'] ) );
283
284 if ( ! preg_match( '!^https?!', $url ) ) {
285 $siteurl = trailingslashit( get_home_url() );
286 $home_query = '';
287
288 if ( strpos( $siteurl, '?' ) !== false ) {
289 $parts = explode( '?', $siteurl );
290 $home_query = $parts[1];
291 $siteurl = $parts[0];
292 }
293
294 if ( $home_query ) {
295 parse_str( untrailingslashit( $home_query ), $home_query );
296 $url = esc_url_raw( add_query_arg( $home_query, $url ) );
297 }
298
299 $segs1 = explode( '/', $siteurl );
300 $segs2 = explode( '/', $url );
301
302 if ( $removed = array_intersect( $segs1, $segs2 ) ) {
303 if ( $segs2 = array_diff( $segs2, $removed ) ) {
304 $current_url = $siteurl . join( '/', $segs2 );
305 if ( strpos( $current_url, '?' ) === false ) {
306 $current_url = trailingslashit( $current_url );
307 }
308 }
309 }
310 }
311
312 return $current_url;
313 }
314
315 /**
316 * Remove unneeded characters in an URL
317 *
318 * @param string $url
319 * @param bool $trailingslashit
320 *
321 * @return string
322 */
323 function learn_press_sanitize_url( $url, $trailingslashit = true ) {
324 if ( $url ) {
325 preg_match( '!(https?://)?(.*)!', $url, $matches );
326 $url_without_http = $matches[2];
327 $url_without_http = preg_replace( '![/]+!', '/', $url_without_http );
328 $url = $matches[1] . $url_without_http;
329
330 return ( $trailingslashit &&
331 strpos( $url, '?' ) === false ) ? trailingslashit( $url ) : untrailingslashit( $url );
332 }
333
334 return $url;
335 }
336
337 /**
338 * Get all types of question supported
339 *
340 * @return mixed
341 */
342 function learn_press_question_types() {
343 return LP_Question::get_types();
344 }
345
346 /**
347 * Get human name of question's type by slug
348 *
349 * @param string $slug
350 *
351 * @return array
352 */
353 function learn_press_question_name_from_slug( $slug ) {
354 $types = learn_press_question_types();
355 $name = ! empty( $types[ $slug ] ) ? $types[ $slug ] : '';
356
357 return apply_filters( 'learn-press/question/slug-to-name', $name, $slug );
358 }
359
360 /**
361 * Get terms of a course by taxonomy.
362 * E.g: course_tag, course_category
363 *
364 * @param int $course_id
365 * @param string $taxonomy
366 * @param array $args
367 *
368 * @return array|mixed
369 */
370 function learn_press_get_course_terms( $course_id, $taxonomy, $args = array() ) {
371 if ( ! taxonomy_exists( $taxonomy ) ) {
372 return array();
373 }
374
375 // Support ordering by parent
376 if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
377 $fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
378 $orderby = $args['orderby'];
379
380 // Unset for wp_get_post_terms
381 unset( $args['orderby'] );
382 unset( $args['fields'] );
383
384 $terms = wp_get_post_terms( $course_id, $taxonomy, $args );
385
386 switch ( $orderby ) {
387 case 'name_num':
388 usort( $terms, '_learn_press_get_course_terms_name_num_usort_callback' );
389 break;
390 case 'parent':
391 usort( $terms, '_learn_press_get_course_terms_parent_usort_callback' );
392 break;
393 }
394
395 switch ( $fields ) {
396 case 'names':
397 $terms = wp_list_pluck( $terms, 'name' );
398 break;
399 case 'ids':
400 $terms = wp_list_pluck( $terms, 'term_id' );
401 break;
402 case 'slugs':
403 $terms = wp_list_pluck( $terms, 'slug' );
404 break;
405 }
406 } elseif ( ! empty( $args['orderby'] ) && $args['orderby'] === 'menu_order' ) {
407 // wp_get_post_terms doesn't let us use custom sort order
408 $args['include'] = wp_get_post_terms( $course_id, $taxonomy, array( 'fields' => 'ids' ) );
409
410 if ( empty( $args['include'] ) ) {
411 $terms = array();
412 } else {
413 // This isn't needed for get_terms
414 unset( $args['orderby'] );
415
416 // Set args for get_terms
417 $args['menu_order'] = $args['order'] ?? 'ASC';
418 $args['hide_empty'] = $args['hide_empty'] ?? 0;
419 $args['fields'] = $args['fields'] ?? 'names';
420
421 // Ensure slugs is valid for get_terms - slugs isn't supported
422 $args['fields'] = $args['fields'] === 'slugs' ? 'id=>slug' : $args['fields'];
423 $terms = get_terms( $taxonomy, $args );
424 }
425 } else {
426 $terms = wp_get_post_terms( $course_id, $taxonomy, $args );
427 }
428
429 // @deprecated
430 $terms = apply_filters( 'learn_press_get_course_terms', $terms, $course_id, $taxonomy, $args );
431
432 return apply_filters( 'learn-press/course/terms', $terms, $course_id, $taxonomy, $args );
433 }
434
435 /**
436 * Callback function for sorting terms of course by name.
437 *
438 * @param object $a
439 * @param object $b
440 *
441 * @return int
442 */
443 function _learn_press_get_course_terms_name_num_usort_callback( $a, $b ) {
444 if ( $a->name + 0 === $b->name + 0 ) {
445 return 0;
446 }
447
448 return ( $a->name + 0 < $b->name + 0 ) ? - 1 : 1;
449 }
450
451 /**
452 * Callback function for sorting terms of course by parent.
453 *
454 * @param object $a
455 * @param object $b
456 *
457 * @return int
458 */
459 function _learn_press_get_course_terms_parent_usort_callback( $a, $b ) {
460 if ( $a->parent === $b->parent ) {
461 return 0;
462 }
463
464 return ( $a->parent < $b->parent ) ? 1 : - 1;
465 }
466
467 /**
468 * Get posts by it's post-name (slug).
469 *
470 * @param string $name
471 * @param string $type
472 * @param bool $single
473 *
474 * @return array|bool|null|WP_Post
475 */
476 function learn_press_get_post_by_name( $name, $type, $single = true ) {
477 $post_name = sanitize_title( $name );
478 $id = LP_Object_Cache::get( $type . '-' . $post_name, 'learn-press/post-names' );
479
480 if ( false === $id ) {
481 foreach ( array( $name, urldecode( $name ) ) as $_name ) {
482 $args = array(
483 'name' => $_name,
484 'post_type' => array( $type ),
485 );
486
487 $posts = get_posts( $args );
488
489 if ( $posts ) {
490 $post = $posts[0];
491 $id = $post->ID;
492 wp_cache_set( $id, $post, 'posts' );
493 LP_Object_Cache::set( $type . '-' . $name, $id, 'learn-press/post-names' );
494 break;
495 }
496 }
497 }
498
499 return $id ? get_post( $id ) : false;
500 }
501
502 if ( ! function_exists( 'learn_press_is_ajax' ) ) {
503 function learn_press_is_ajax() {
504 return defined( 'LP_DOING_AJAX' ) && LP_DOING_AJAX && 'yes' != learn_press_get_request( 'noajax' );
505 }
506 }
507
508 /**
509 * Get page id from admin settings page
510 *
511 * @param string $name
512 *
513 * @since 3.0.0
514 * @version 1.0.2
515 * @return int
516 */
517 function learn_press_get_page_id( string $name ): int {
518 $page_id = LP_Settings::get_option( "{$name}_page_id", false );
519
520 /*if ( function_exists( 'icl_object_id' ) ) {
521 $page_id = icl_object_id( $page_id, 'page', false, defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '' );
522 }*/
523
524 $page_id = (int) $page_id;
525
526 return apply_filters( 'learn_press_get_page_id', $page_id, $name );
527 }
528
529 /**
530 * display the seconds in time format h:i:s
531 *
532 * @param $seconds
533 * @param string $separator
534 *
535 * @return string
536 */
537 function learn_press_seconds_to_time( $seconds, $separator = ':' ) {
538 return sprintf(
539 '%02d%s%02d%s%02d',
540 floor( $seconds / 3600 ),
541 $separator,
542 ( $seconds / 60 ) % 60,
543 $separator,
544 $seconds % 60
545 );
546 }
547
548 /* nav */
549 if ( ! function_exists( 'learn_press_course_paging_nav' ) ) {
550
551 /**
552 * Display navigation to next/previous set of posts when applicable.
553 *
554 * @param array
555 */
556 function learn_press_course_paging_nav( $args = array() ) {
557 learn_press_paging_nav(
558 array(
559 'num_pages' => $GLOBALS['wp_query']->max_num_pages,
560 'wrapper_class' => 'navigation pagination',
561 )
562 );
563 }
564 }
565
566 /* nav */
567 if ( ! function_exists( 'learn_press_paging_nav' ) ) {
568 function learn_press_paging_nav( $args = array() ) {
569 $args = wp_parse_args(
570 $args,
571 array(
572 'num_pages' => 0,
573 'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
574 'wrapper_class' => 'learn-press-pagination',
575 'base' => false,
576 'format' => '',
577 'echo' => true,
578 )
579 );
580
581 if ( $args['num_pages'] < 2 ) {
582 return false;
583 }
584
585 $paged = $args['paged'];
586 $pagenum_link = html_entity_decode( $args['base'] === false ? get_pagenum_link() : $args['base'] );
587
588 $query_args = array();
589 $url_parts = explode( '?', $pagenum_link );
590
591 if ( isset( $url_parts[1] ) ) {
592 wp_parse_str( $url_parts[1], $query_args );
593 }
594
595 $pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) );
596 $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
597
598 $format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos(
599 $pagenum_link,
600 'index.php'
601 ) ? 'index.php/' : '';
602 $format .= $args['format'] ? $args['format'] : ( $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit(
603 'page/%#%',
604 'paged'
605 ) : '?paged=%#%' );
606
607 $link_args = array(
608 'base' => $pagenum_link,
609 'format' => $format,
610 'total' => $args['num_pages'],
611 'current' => max( 1, $paged ),
612 'mid_size' => 1,
613 'add_args' => array_map( 'urlencode', $query_args ),
614 'prev_text' => __( '<', 'learnpress' ),
615 'next_text' => __( '>', 'learnpress' ),
616 'type' => 'list',
617 );
618
619 // Set up paginated links.
620 $links = paginate_links( $link_args );
621
622 ob_start();
623
624 if ( $links ) {
625 ?>
626 <div class="<?php echo esc_attr( $args['wrapper_class'] ); ?>">
627 <?php echo wp_kses_post( $links ); ?>
628 </div>
629 <?php
630 }
631
632 $output = ob_get_clean();
633
634 if ( $args['echo'] ) {
635 echo wp_kses_post( $output );
636 }
637
638 return $output;
639 }
640 }
641
642 /**
643 * Get number of pages by rows and items per page.
644 *
645 * @param int $total
646 * @param int $limit
647 *
648 * @return int
649 */
650 function learn_press_get_num_pages( $total, $limit = 10 ) {
651 $limit = $limit <= 0 ? 10 : $limit;
652
653 if ( $total <= $limit ) {
654 return 1;
655 }
656
657 $pages = absint( $total / $limit );
658
659 if ( $total % $limit != 0 ) {
660 ++$pages;
661 }
662
663 return $pages;
664 }
665
666 /**
667 * Get text
668 *
669 * @param $status_id
670 *
671 * @return mixed
672 */
673 function learn_press_get_status_text( $status_id ) {
674 switch ( $status_id ) {
675 case 1:
676 $text = 'pending';
677 break;
678 case 2:
679 $text = 'complete';
680 break;
681 case - 1:
682 $text = 'cancel';
683 break;
684 case - 2:
685 $text = 'refund';
686 break;
687 default:
688 $text = 'on-hold';
689 }
690
691 return $text;
692 }
693
694 function learn_press_get_course_duration_support() {
695 return apply_filters(
696 'learn_press_course_duration_support',
697 array(
698 'minute' => esc_html__( 'Minute(s)', 'learnpress' ),
699 'hour' => esc_html__( 'Hour(s)', 'learnpress' ),
700 'day' => esc_html__( 'Day(s)', 'learnpress' ),
701 'week' => esc_html__( 'Week(s)', 'learnpress' ),
702 )
703 );
704 }
705
706 function learn_press_number_to_string_time( $number ) {
707 $str = $number;
708
709 if ( preg_match( '!([0-9.]+) (minute|hour|day|week)!', $number, $matches ) ) {
710 switch ( $matches[2] ) {
711 case 'hour':
712 $minute = $matches[1] * 60;
713 $str = sprintf( '%s hour %s minute', absint( $minute / 60 ), $minute % 60 );
714 break;
715 case 'day':
716 $hour = $matches[1] * 24;
717 $str = sprintf( '%s day %s hour', absint( $hour / 24 ), $hour % 24 );
718 break;
719 case 'week':
720 $day = $matches[1] * 7;
721 $str = sprintf( '%s week %s day', absint( $day / 7 ), $day % 7 );
722 break;
723 }
724 }
725
726 return $str;
727 }
728
729 function learn_press_human_time_to_seconds( $time, $default = '' ) {
730 $duration = learn_press_get_course_duration_support();
731 $duration_keys = array_keys( $duration );
732
733 if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $time, $matches ) ) {
734 $a1 = $matches[1][0];
735 $a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : '';
736 } else {
737 $a1 = absint( $time );
738 $a2 = '';
739 }
740
741 if ( $a2 ) {
742 $b = array(
743 'minute' => 60,
744 'hour' => 3600,
745 'day' => 3600 * 24,
746 'week' => 3600 * 24 * 7,
747 );
748 $a1 = $a1 * $b[ $a2 ];
749 }
750
751 return $a1;
752 }
753
754 /**
755 * @param $user_id
756 *
757 * @return WP_Query
758 */
759 function learn_press_get_own_courses( $user_id ) {
760 $arr_query = array(
761 'post_type' => 'lp_course',
762 'author' => $user_id,
763 'post_status' => 'publish',
764 'ignore_sticky_posts' => true,
765 'posts_per_page' => - 1,
766 );
767 $my_query = new WP_Query( $arr_query );
768
769 return $my_query;
770 }
771
772 /**
773 * Return array list of currency positions.
774 *
775 * @param bool|string $currency
776 *
777 * @return array
778 */
779 function learn_press_currency_positions( $currency = false ) {
780 $positions = array(
781 'left' => __( 'Left', 'learnpress' ),
782 'right' => __( 'Right', 'learnpress' ),
783 'left_with_space' => __( 'Left with space', 'learnpress' ),
784 'right_with_space' => __( 'Right with space', 'learnpress' ),
785 );
786
787 if ( false === $currency ) {
788 $currency = learn_press_get_currency_symbol();
789 }
790
791 $settings = LP_Settings::instance();
792
793 $thousands_separator = '';
794 $decimals_separator = $settings->get( 'decimals_separator', '.' );
795 $number_of_decimals = $settings->get( 'number_of_decimals', 2 );
796
797 if ( $number_of_decimals > 0 ) {
798 $example = '69' . $decimals_separator . str_repeat( '9', $number_of_decimals );
799 } else {
800 $example = '69';
801 }
802
803 foreach ( $positions as $pos => $text ) {
804 switch ( $pos ) {
805 case 'left':
806 $text = sprintf( '%s ( %s%s )', $text, $currency, $example );
807 break;
808 case 'right':
809 $text = sprintf( '%s ( %s%s )', $text, $example, $currency );
810 break;
811 case 'left_with_space':
812 $text = sprintf( '%s ( %s %s )', $text, $currency, $example );
813 break;
814 case 'right_with_space':
815 $text = sprintf( '%s ( %s %s )', $text, $example, $currency );
816 break;
817 }
818 $positions[ $pos ] = $text;
819 }
820
821 $positions = apply_filters( 'learn_press_currency_positions', $positions );
822
823 return apply_filters( 'learn-press/currency-positions', $positions );
824 }
825
826 /**
827 * @return array
828 */
829 function learn_press_get_payment_currencies() {
830 return apply_filters( 'learn_press_get_payment_currencies', learn_press_currencies() );
831 }
832
833 /**
834 * Get the list of currencies with code and name.
835 *
836 * @return array
837 * @version 3.0.1
838 *
839 * @author ThimPress
840 */
841 function learn_press_currencies() {
842 return Config::instance()->get( 'currencies', 'settings' );
843 }
844
845 /**
846 * Return list of common symbols of the currencies on the world.
847 *
848 * @return array
849 * @version 3.0.1
850 */
851 function learn_press_currency_symbols() {
852 return Config::instance()->get( 'currency-symbols', 'settings' );
853 }
854
855 /**
856 * Get current setting of currency.
857 *
858 * @return string
859 */
860 function learn_press_get_currency(): string {
861 $currency = LP_Settings::instance()->get( 'currency', 'USD' );
862
863 return esc_html( apply_filters( 'learn-press/currency', $currency ) );
864 }
865
866 /**
867 * Return currency symbol from the code.
868 *
869 * @param string $currency
870 *
871 * @return string
872 */
873 function learn_press_get_currency_symbol( $currency = '' ) {
874 if ( ! $currency ) {
875 $currency = learn_press_get_currency();
876 }
877 $symbols = learn_press_currency_symbols();
878 $currency_symbol = $symbols[ $currency ] ?? '';
879
880 $currency_symbol = apply_filters( 'learn_press_currency_symbol', $currency_symbol, $currency );
881
882 return apply_filters( 'learn-press/currency-symbol', $currency_symbol, $currency );
883 }
884
885 /**
886 * Get static page for LP page by name.
887 *
888 * @param string $key
889 *
890 * @return string
891 * @editor tungnx
892 * @modify 4.1.4
893 */
894 function learn_press_get_page_link( string $key ): string {
895 $page_id = learn_press_get_page_id( $key );
896 $key_cache = "lp/page-link/{$key}";
897 $link = LP_Cache::cache_load_first( 'get', $key_cache );
898 if ( false !== $link ) {
899 return $link;
900 }
901
902 if ( $page_id && get_post_status( $page_id ) == 'publish' ) {
903 $permalink = get_permalink( $page_id );
904 $link = apply_filters( 'learn-press/get-page-link', trailingslashit( $permalink ), $page_id, $key );
905 LP_Cache::cache_load_first( 'set', $key_cache, $link );
906 }
907
908 return $link;
909 }
910
911 /**
912 * Get static page for LP page by name.
913 *
914 * @param string $key
915 *
916 * @return string
917 */
918 function learn_press_get_page_title( $key ) {
919 $page_id = LP_Settings::instance()->get( $key . '_page_id' );
920 $title = '';
921
922 if ( $page_id && get_post_status( $page_id ) == 'publish' ) {
923 $title = apply_filters( 'learn-press/get-page-title', get_the_title( $page_id ), $page_id, $key );
924 }
925
926 return apply_filters( 'learn-press/get-page-' . $key . '-title', $title, $page_id );
927 }
928
929 /**
930 * get the ID of a course by order ID
931 *
932 * @param $order_id
933 *
934 * @return bool|mixed
935 */
936 function learn_press_get_course_by_order( $order_id ) {
937 $order_items = get_post_meta( $order_id, '_learn_press_order_items', true );
938
939 if ( $order_items && $order_items->products ) {
940 $array_keys = array_keys( $order_items->products );
941
942 return reset( $array_keys );
943 }
944
945 return false;
946 }
947
948 /**
949 * Convert a number of seconds to weeks/days/hours.
950 *
951 * @param int $secs
952 *
953 * @return bool|string
954 */
955 function learn_press_seconds_to_weeks( int $secs = 0 ) {
956 $secs = (int) $secs;
957
958 if ( 0 === $secs ) {
959 return false;
960 }
961 // variables for holding values.
962 $mins = 0;
963 $hours = 0;
964 $days = 0;
965 $weeks = 0;
966 // calculations.
967 if ( $secs >= 60 ) {
968 $mins = (int) ( $secs / 60 );
969 $secs = $secs % 60;
970 }
971 if ( $mins >= 60 ) {
972 $hours = (int) ( $mins / 60 );
973 $mins = $mins % 60;
974 }
975 if ( $hours >= 24 ) {
976 $days = (int) ( $hours / 24 );
977 $hours = $hours % 24;
978 }
979 if ( $days >= 7 ) {
980 $weeks = (int) ( $days / 7 );
981 $days = $days % 7;
982 }
983 // format result.
984 $result = '';
985 if ( $weeks ) {
986 $result .= sprintf( _n( '%s week', '%s weeks', $weeks, 'learnpress' ), $weeks ) . ' ';
987 }
988
989 if ( $days ) {
990 $result .= sprintf( _n( '%s day', '%s days', $days, 'learnpress' ), $days ) . ' ';
991 }
992
993 if ( ! $weeks ) {
994 if ( $hours ) {
995 $result .= sprintf( _n( '%s hour', '%s hours', $hours, 'learnpress' ), $hours ) . ' ';
996 }
997
998 if ( $mins ) {
999 $result .= sprintf( _n( '%s minute', '%s minutes', $mins, 'learnpress' ), $mins ) . ' ';
1000 }
1001 }
1002
1003 $result = rtrim( $result );
1004
1005 return $result;
1006 }
1007
1008 function learn_press_user_maybe_is_a_teacher( $user = null ) {
1009 if ( ! $user ) {
1010 $user = learn_press_get_current_user();
1011 } elseif ( is_numeric( $user ) ) {
1012 $user = learn_press_get_user( $user );
1013 }
1014 if ( ! $user || $user instanceof LP_User_Guest ) {
1015 return false;
1016 }
1017
1018 $role = $user->has_role( 'administrator' ) ? 'administrator' : false;
1019 if ( ! $role ) {
1020 $role = $user->has_role( 'lp_teacher' ) ? 'lp_teacher' : false;
1021 }
1022
1023 return apply_filters( 'learn-press/user/is-teacher', $role, $user->get_id() );
1024 }
1025
1026 function learn_press_become_teacher_sent( $user_id = 0 ) {
1027 if ( func_num_args() == 0 ) {
1028 $user_id = get_current_user_id();
1029 }
1030
1031 return 'yes' === get_user_meta( $user_id, '_requested_become_teacher', true );
1032 }
1033
1034 function _learn_press_translate_user_roles( $translations, $text, $context, $domain ) {
1035 $plugin_domain = 'learnpress';
1036 $roles = array( 'LP Instructor' );
1037
1038 if ( $context === 'User role' && in_array( $text, $roles ) && $domain !== $plugin_domain ) {
1039 return translate_with_gettext_context( $text, $context, $plugin_domain );
1040 }
1041
1042 return $translations;
1043 }
1044
1045 add_filter( 'gettext_with_context', '_learn_press_translate_user_roles', 10, 4 );
1046
1047 if ( ! function_exists( 'learn_press_send_json' ) ) {
1048 function learn_press_send_json( $data ) {
1049 echo '<-- LP_AJAX_START -->';
1050 echo wp_json_encode( $data );
1051 echo '<-- LP_AJAX_END -->';
1052 die;
1053 }
1054 }
1055
1056 /**
1057 * Send json with success signal to browser.
1058 *
1059 * @param array|object|WP_Error $data
1060 *
1061 * @since 3.0.1
1062 */
1063 function learn_press_send_json_error( $data = '' ) {
1064 $response = array( 'success' => false );
1065
1066 if ( isset( $data ) ) {
1067 if ( is_wp_error( $data ) ) {
1068 $result = array();
1069 foreach ( $data->errors as $code => $messages ) {
1070 foreach ( $messages as $message ) {
1071 $result[] = array(
1072 'code' => $code,
1073 'message' => $message,
1074 );
1075 }
1076 }
1077
1078 $response['data'] = $result;
1079 } else {
1080 $response['data'] = $data;
1081 }
1082 }
1083
1084 learn_press_send_json( $response );
1085 }
1086
1087 /**
1088 * Send json with error signal to browser.
1089 *
1090 * @param array|object|WP_Error $data
1091 *
1092 * @since 3.0.0
1093 */
1094 function learn_press_send_json_success( $data = '' ) {
1095 $response = array( 'success' => true );
1096
1097 if ( isset( $data ) ) {
1098 $response['data'] = $data;
1099 }
1100
1101 learn_press_send_json( $response );
1102 }
1103
1104 /**
1105 * Check if ajax is calling then send json data.
1106 *
1107 * @param array $data
1108 * @param mixed $callback
1109 *
1110 * @return bool
1111 */
1112 function learn_press_maybe_send_json( $data, $callback = null ) {
1113 if ( learn_press_is_ajax() ) {
1114 is_callable( $callback ) && call_user_func( $callback );
1115 $message = learn_press_get_messages( true );
1116 if ( empty( $data['message'] ) && $message ) {
1117 $data['message'] = $message;
1118 }
1119 learn_press_send_json( $data );
1120 }
1121
1122 return false;
1123 }
1124
1125 /**
1126 * Get data from request.
1127 *
1128 * @param string $key
1129 * @param mixed $default
1130 * @param mixed $hash
1131 *
1132 * @return mixed
1133 */
1134 function learn_press_get_request( $key, $default = null, $hash = null ) {
1135 $return = $default;
1136
1137 if ( $hash ) {
1138 if ( ! empty( $hash[ $key ] ) ) {
1139 $return = $hash[ $key ];
1140 }
1141 } else {
1142 if ( ! empty( $_POST[ $key ] ) ) {
1143 $return = LP_Helper::sanitize_params_submitted( $_POST[ $key ] );
1144 } elseif ( ! empty( $_GET[ $key ] ) ) {
1145 $return = LP_Helper::sanitize_params_submitted( $_GET[ $key ] );
1146 } elseif ( ! empty( $_REQUEST[ $key ] ) ) {
1147 $return = LP_Helper::sanitize_params_submitted( $_REQUEST[ $key ] );
1148 }
1149 }
1150
1151 return $return;
1152 }
1153
1154 /**
1155 * @return mixed
1156 */
1157 function is_learnpress() {
1158 return apply_filters(
1159 'is_learnpress',
1160 ( learn_press_is_course_archive() || learn_press_is_course_taxonomy() || learn_press_is_course() || learn_press_is_quiz() || learn_press_is_search() ) ? true : false
1161 );
1162 }
1163
1164 if ( ! function_exists( 'learn_press_is_search' ) ) {
1165 function learn_press_is_search() {
1166 return array_key_exists( 's', $_REQUEST ) && array_key_exists( 'ref', $_REQUEST ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course';
1167 }
1168 }
1169
1170 if ( ! function_exists( 'learn_press_is_courses' ) ) {
1171 function learn_press_is_courses() {
1172 return learn_press_is_course_archive();
1173 }
1174 }
1175
1176
1177 if ( ! function_exists( 'learn_press_is_course_archive' ) ) {
1178 function learn_press_is_course_archive() {
1179 global $wp_query;
1180
1181 $queried_object_id = ! empty( $wp_query->queried_object ) ? $wp_query->queried_object : 0;
1182 $is_tag = defined( 'LEARNPRESS_IS_TAG' ) && LEARNPRESS_IS_TAG || is_tax( 'course_tag' );
1183 $is_category = defined( 'LEARNPRESS_IS_CATEGORY' ) && LEARNPRESS_IS_CATEGORY || is_tax( 'course_category' );
1184 $page_id = learn_press_get_page_id( 'courses' );
1185
1186 return ( $is_category || $is_tag ) || is_post_type_archive( 'lp_course' ) || ( $page_id && ( $queried_object_id && is_page( $page_id ) ) );
1187 }
1188 }
1189
1190 if ( ! function_exists( 'learn_press_is_course_tax' ) ) {
1191 function learn_press_is_course_tax() {
1192 return is_tax( get_object_taxonomies( LP_COURSE_CPT ) );
1193 }
1194 }
1195
1196 if ( ! function_exists( 'learn_press_is_course_taxonomy' ) ) {
1197 function learn_press_is_course_taxonomy() {
1198 return ( defined( 'LEARNPRESS_IS_TAX' ) && LEARNPRESS_IS_TAX ) || learn_press_is_course_tax();
1199 }
1200 }
1201
1202
1203 if ( ! function_exists( 'learn_press_is_course_category' ) ) {
1204 function learn_press_is_course_category( $term = '' ) {
1205 return ( defined( 'LEARNPRESS_IS_CATEGORY' ) && LEARNPRESS_IS_CATEGORY ) || is_tax( 'course_category', $term );
1206 }
1207 }
1208
1209
1210 if ( ! function_exists( 'learn_press_is_course_tag' ) ) {
1211 function learn_press_is_course_tag( $term = '' ) {
1212 return ( defined( 'LEARNPRESS_IS_TAG' ) && LEARNPRESS_IS_TAG ) || is_tax( 'course_tag', $term );
1213 }
1214 }
1215
1216 if ( ! function_exists( 'learn_press_is_course' ) ) {
1217 function learn_press_is_course(): bool {
1218 try {
1219 return is_singular( array( LP_COURSE_CPT ) );
1220 } catch ( Throwable $e ) {
1221 }
1222
1223 return false;
1224 }
1225 }
1226
1227 if ( ! function_exists( 'learn_press_is_lesson' ) ) {
1228 function learn_press_is_lesson() {
1229 return is_singular( array( LP_LESSON_CPT ) );
1230 }
1231 }
1232
1233 if ( ! function_exists( 'learn_press_is_quiz' ) ) {
1234 function learn_press_is_quiz() {
1235 return is_singular( array( LP_QUIZ_CPT ) );
1236 }
1237 }
1238
1239 function lp_content_has_shortcode( $tag = '' ) {
1240 global $post;
1241
1242 return is_singular() && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
1243 }
1244
1245 /**
1246 * Returns true when viewing profile page.
1247 *
1248 * @return bool
1249 */
1250 function learn_press_is_profile() {
1251 $page_id = learn_press_get_page_id( 'profile' );
1252
1253 if ( $page_id && is_page( $page_id ) || lp_content_has_shortcode( 'learn_press_profile' ) ) {
1254 return true;
1255 }
1256
1257 return apply_filters( 'learn-press/is-profile', false );
1258 }
1259
1260 /**
1261 * Return true if user is in checking out page
1262 *
1263 * @return bool
1264 */
1265 function learn_press_is_checkout() {
1266 $page_id = learn_press_get_page_id( 'checkout' );
1267
1268 if ( $page_id && is_page( $page_id ) ) {
1269 return true;
1270 }
1271
1272 return apply_filters( 'learn-press/is-checkout', false );
1273 }
1274
1275 function learn_press_is_instructors() {
1276 $page_id = learn_press_get_page_id( 'instructors' );
1277
1278 if ( $page_id && is_page( $page_id ) ) {
1279 return true;
1280 }
1281
1282 return apply_filters( 'learn-press/is-instructor', false );
1283 }
1284
1285
1286 /**
1287 * Return register permalink
1288 *
1289 * @return mixed
1290 */
1291 function learn_press_get_register_url() {
1292 return apply_filters( 'learn_press_register_url', wp_registration_url() );
1293 }
1294
1295 /**
1296 * Set user's cookie
1297 *
1298 * @param string $name
1299 * @param mixed $value
1300 * @param int $expire
1301 * @param bool $httponly
1302 *
1303 * @editor tungnx
1304 * @version 1.0.3
1305 */
1306 function learn_press_setcookie( string $name = '', string $value = '', int $expire = 0, bool $httponly = true ) {
1307 @setcookie( $name, $value, $expire, COOKIEPATH ?: '/', COOKIE_DOMAIN, is_ssl(), $httponly );
1308 }
1309
1310 /**
1311 * Clear cookie
1312 *
1313 * @param string $name
1314 */
1315 function learn_press_remove_cookie( string $name = '' ) {
1316 if ( ! empty( $name ) ) {
1317 setcookie( $name, '', time() - YEAR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true );
1318 }
1319
1320 if ( array_key_exists( $name, $_COOKIE ) ) {
1321 unset( $_COOKIE[ $name ] );
1322 }
1323 }
1324
1325 /**
1326 * Filter the login url so third-party can be customized
1327 *
1328 * @param string $redirect
1329 *
1330 * @return mixed
1331 */
1332 function learn_press_get_login_url( $redirect = null ) {
1333 $url = wp_login_url( $redirect );
1334 $profile_page = learn_press_get_page_link( 'profile' );
1335
1336 if ( 'yes' === LP_Settings::instance()->get( 'enable_login_profile' ) && $profile_page ) {
1337 $parse_url = parse_url( $url );
1338 $url = $profile_page . ( ! empty( $parse_url['query'] ) ? '?' . $parse_url['query'] : '' );
1339 }
1340
1341 return apply_filters( 'learn-press/login-url', $url );
1342 }
1343
1344 /**
1345 * Add variable to an url by checking the permalink structure.
1346 *
1347 * @param string $name
1348 * @param string $value
1349 * @param string $url
1350 *
1351 * @return string
1352 */
1353 function learn_press_get_endpoint_url( $name, $value, $url ) {
1354 if ( ! $url ) {
1355 $url = get_permalink();
1356 }
1357
1358 // Map endpoint to options
1359 $name = isset( LearnPress::instance()->query_vars[ $name ] ) ? LearnPress::instance()->query_vars[ $name ] : $name;
1360
1361 if ( get_option( 'permalink_structure' ) ) {
1362 if ( strstr( $url, '?' ) ) {
1363 $query_string = '?' . parse_url( $url, PHP_URL_QUERY );
1364 $url = current( explode( '?', $url ) );
1365 } else {
1366 $query_string = '';
1367 }
1368 $url = trailingslashit( $url ) . ( $name ? $name . '/' : '' ) . $value . $query_string;
1369
1370 } else {
1371 $url = esc_url_raw( add_query_arg( $name, $value, $url ) );
1372 }
1373
1374 return apply_filters( 'learn_press_get_endpoint_url', esc_url_raw( $url ), $name, $value, $url );
1375 }
1376
1377 /**
1378 * @deprecated 4.2.3
1379 */
1380 function learn_press_is_yes( $value ) {
1381 _deprecated_function( __FUNCTION__, '4.2.3' );
1382 return ( $value === 1 ) || ( $value === '1' ) || ( $value == 'yes' ) || ( $value == true ) || ( $value == 'on' );
1383 }
1384
1385 /**
1386 * @param mixed $value
1387 *
1388 * @return bool
1389 * @deprecated 4.2.3
1390 */
1391 function _is_false_value( $value ) {
1392 _deprecated_function( __FUNCTION__, '4.2.3' );
1393 if ( is_numeric( $value ) ) {
1394 return $value == 0;
1395 } elseif ( is_string( $value ) ) {
1396 return ( empty( $value ) || is_null( $value ) || in_array( $value, array( 'no', 'off', 'false' ) ) );
1397 }
1398
1399 return ! ! $value;
1400 }
1401
1402 /**
1403 * Map the query vars from LP to query vars of WP core
1404 * when WP parse the requesting.
1405 */
1406 function learn_press_parse_request() {
1407 global $wp;
1408
1409 // Map query vars to their keys, or get them if endpoints are not supported
1410 foreach ( LearnPress::instance()->query_vars as $key => $var ) {
1411 if ( isset( $_GET[ $var ] ) ) {
1412 $wp->query_vars[ $key ] = LP_Helper::sanitize_params_submitted( $_GET[ $var ] );
1413 } elseif ( isset( $wp->query_vars[ $var ] ) ) {
1414 $wp->query_vars[ $key ] = LP_Helper::sanitize_params_submitted( $wp->query_vars[ $var ] );
1415 }
1416 }
1417 }
1418
1419 add_action( 'parse_request', 'learn_press_parse_request' );
1420
1421 if ( ! function_exists( 'learn_press_reset_auto_increment' ) ) {
1422 /**
1423 * Reset AUTO INCREMENT of the table.
1424 *
1425 * @param $table
1426 */
1427 function learn_press_reset_auto_increment( $table ) {
1428 global $wpdb;
1429 $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}$table AUTO_INCREMENT = %d", 1 ) );
1430 }
1431 }
1432
1433 /**
1434 * Get the cart object in checkout page
1435 *
1436 * @return LP_Cart
1437 * @deprecated 4.2.0
1438 */
1439 function learn_press_get_checkout_cart() {
1440 _deprecated_function( __FUNCTION__, '4.2.0', 'LearnPress::instance()->cart' );
1441 return apply_filters( 'learn_press_checkout_cart', LearnPress::instance()->cart );
1442 }
1443
1444 function learn_press_user_time( $time, $format = 'timestamp' ) {
1445 if ( is_string( $time ) ) {
1446 $time = @strtotime( $time );
1447 }
1448 $time = $time + ( get_option( 'gmt_offset' ) - $_COOKIE['timezone'] / 60 ) * HOUR_IN_SECONDS;
1449 switch ( $format ) {
1450 case 'timestamp':
1451 return $time;
1452 default:
1453 return date( 'Y-m-d H:i:s', $time );
1454 }
1455 }
1456
1457 function learn_press_get_current_version() {
1458 $data = get_plugin_data( LP_PLUGIN_FILE, $markup = true, $translate = true );
1459
1460 return $data['Version'];
1461 }
1462
1463 /**
1464 * Get current tab is displaying in user profile.
1465 * If there is no tab then get the first tab in
1466 * the list of tabs.
1467 *
1468 * @param bool $default
1469 *
1470 * @return mixed|string
1471 * @deprecated 4.2.2
1472 * addon commission 4.0.2 still use this function
1473 */
1474 function learn_press_get_current_profile_tab( $default = true ) {
1475 global $wp_query, $wp;
1476 $current = '';
1477
1478 // Only load on profile page.
1479 if ( ! LP_Page_Controller::is_page_profile() ) {
1480 return false;
1481 }
1482
1483 if ( ! empty( $_REQUEST['tab'] ) ) {
1484 $current = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] );
1485 } elseif ( ! empty( $wp_query->query_vars['tab'] ) ) {
1486 $current = $wp_query->query_vars['tab'];
1487 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
1488 $current = $wp->query_vars['view'];
1489 } else {
1490 $tabs = learn_press_get_user_profile_tabs();
1491 if ( $default && $tabs ) {
1492 // Fixed for array_keys does not work with ArrayAccess instance
1493 if ( $tabs instanceof LP_Profile_Tabs ) {
1494 $tabs = $tabs->tabs();
1495 }
1496
1497 $tab_keys = array_keys( $tabs );
1498 $current = reset( $tab_keys );
1499 }
1500 }
1501
1502 return $current;
1503 }
1504
1505 //add_action( 'init', 'learn_press_get_current_profile_tab' );
1506
1507 function learn_press_profile_tab_exists( $tab ) {
1508 $tabs = learn_press_get_user_profile_tabs();
1509
1510 if ( $tabs ) {
1511 return ! empty( $tabs[ $tab ] ) ? true : false;
1512 }
1513
1514 return false;
1515 }
1516
1517 function learn_press_single_term_title( $prefix = '', $display = true ) {
1518 $term = get_queried_object();
1519
1520 if ( ! $term ) {
1521 return '';
1522 }
1523
1524 if ( learn_press_is_course_category() ) {
1525 $term_name = apply_filters( 'single_course_category_title', $term->name );
1526 } elseif ( learn_press_is_course_tag() ) {
1527 $term_name = apply_filters( 'single_course_tag_title', $term->name );
1528 } elseif ( learn_press_is_course_taxonomy() ) {
1529 $term_name = apply_filters( 'single_course_term_title', $term->name );
1530 } else {
1531 return single_term_title( $prefix, $display );
1532 }
1533
1534 if ( empty( $term_name ) ) {
1535 return single_term_title( $prefix, $display );
1536 }
1537
1538 if ( $display ) {
1539 echo wp_kses_post( $prefix . $term_name );
1540 }
1541
1542 return $prefix . $term_name;
1543 }
1544
1545 /**
1546 * Control the template file if user is searching course.
1547 * Use the template of archive course to display the
1548 * result if there is a flag in request to search course.
1549 *
1550 * @param string $template
1551 *
1552 * @return string
1553 */
1554 function learn_press_search_template( $template ) {
1555 if ( ! empty( $_REQUEST['ref'] ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course' ) {
1556 $template = learn_press_locate_template( 'archive-course.php' );
1557 }
1558
1559 return $template;
1560 }
1561
1562 /**
1563 * Auto enroll user to a course after an order is completed
1564 * if the option auto-enroll is turn on.
1565 *
1566 * @param int $order_id
1567 *
1568 * @return mixed
1569 * @editor tungnx
1570 */
1571 function learn_press_auto_enroll_user_to_courses( $order_id ) {
1572 _deprecated_function( __FUNCTION__, '4.1.3' );
1573 }
1574
1575 // add_action( 'learn_press_order_status_completed', 'learn_press_auto_enroll_user_to_courses' );
1576
1577 /**
1578 * Return true if enable cart
1579 *
1580 * @return bool
1581 */
1582 function learn_press_is_enable_cart() {
1583 return defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART == true;
1584 }
1585
1586 /**
1587 * Short way to get checkout object
1588 *
1589 * @param array
1590 *
1591 * @return LP_Checkout
1592 */
1593 function learn_press_get_checkout( $args = null ) {
1594 $checkout = LP_Checkout::instance();
1595
1596 if ( is_array( $args ) ) {
1597 foreach ( $args as $k => $v ) {
1598 $checkout->{$k} = $v;
1599 }
1600 }
1601
1602 return $checkout;
1603 }
1604
1605 if ( defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART ) {
1606 add_filter( 'learn_press_checkout_settings', '_learn_press_cart_settings', 10, 2 );
1607 function _learn_press_cart_settings( $settings, $class ) {
1608 $settings = array_merge(
1609 $settings,
1610 array(
1611 array(
1612 'title' => __( 'Cart', 'learnpress' ),
1613 'type' => 'title',
1614 ),
1615 array(
1616 'title' => __( 'Enable cart', 'learnpress' ),
1617 'desc' => __(
1618 'Check this option to enable users to purchase multiple courses at one time.',
1619 'learnpress'
1620 ),
1621 'id' => $class->get_field_name( 'enable_cart' ),
1622 'default' => 'yes',
1623 'type' => 'checkbox',
1624 ),
1625 array(
1626 'title' => __( 'Add to cart redirect', 'learnpress' ),
1627 'desc' => __( 'Redirect to checkout immediately after adding the course to the cart.', 'learnpress' ),
1628 'id' => $class->get_field_name( 'redirect_after_add' ),
1629 'default' => 'yes',
1630 'type' => 'checkbox',
1631 ),
1632 array(
1633 'title' => __( 'AJAX add to cart', 'learnpress' ),
1634 'desc' => __( 'Using AJAX to add the course to the cart.', 'learnpress' ),
1635 'id' => $class->get_field_name( 'ajax_add_to_cart' ),
1636 'default' => 'no',
1637 'type' => 'checkbox',
1638 ),
1639 array(
1640 'title' => __( 'Cart page', 'learnpress' ),
1641 'id' => $class->get_field_name( 'cart_page_id' ),
1642 'default' => '',
1643 'type' => 'pages-dropdown',
1644 ),
1645 )
1646 );
1647
1648 return $settings;
1649 }
1650 } else {
1651 add_filter( 'learn_press_enable_cart', '_learn_press_enable_cart', 1000 );
1652 function _learn_press_enable_cart( $r ) {
1653 return false;
1654 }
1655
1656 add_filter( 'learn_press_get_template', '_learn_press_enroll_button', 1000, 5 );
1657 function _learn_press_enroll_button( $located, $template_name, $args, $template_path, $default_path ) {
1658 if ( $template_name == 'single-course/enroll-button.php' ) {
1659 $located = learn_press_locate_template(
1660 'single-course/enroll-button-new.php',
1661 $template_path,
1662 $default_path
1663 );
1664 }
1665
1666 return $located;
1667 }
1668 }
1669
1670 /**
1671 * Returns checkout url from setting
1672 *
1673 * @return string
1674 */
1675 function learn_press_get_checkout_url() {
1676 $checkout_url = learn_press_get_page_link( 'checkout' );
1677
1678 return apply_filters( 'learn_press_get_checkout_url', $checkout_url );
1679 }
1680
1681 /**
1682 * @return string
1683 */
1684 function learn_press_checkout_needs_payment() {
1685 return LearnPress::instance()->cart->needs_payment();
1686 }
1687
1688 /**
1689 * Return plugin basename
1690 *
1691 * @param string $filepath
1692 *
1693 * @return string
1694 */
1695 /*
1696 function learn_press_plugin_basename( $filepath ) {
1697 $file = str_replace( '\\', '/', $filepath );
1698 $file = preg_replace( '|/+|', '/', $file );
1699 $plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR );
1700 $plugin_dir = preg_replace( '|/+|', '/', $plugin_dir );
1701 $mu_plugin_dir = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
1702 $mu_plugin_dir = preg_replace( '|/+|', '/', $mu_plugin_dir );
1703 $sp_plugin_dir = dirname( $filepath );
1704 $sp_plugin_dir = dirname( $sp_plugin_dir );
1705 $sp_plugin_dir = str_replace( '\\', '/', $sp_plugin_dir );
1706 $sp_plugin_dir = preg_replace( '|/+|', '/', $sp_plugin_dir );
1707
1708 $file = preg_replace(
1709 '#^' . preg_quote( $sp_plugin_dir, '#' ) . '/|^' . preg_quote(
1710 $plugin_dir,
1711 '#'
1712 ) . '/|^' . preg_quote(
1713 $mu_plugin_dir,
1714 '#'
1715 ) . '/#',
1716 '',
1717 $file
1718 );
1719 $file = trim( $file, '/' );
1720
1721 return strtolower( $file );
1722 }*/
1723
1724 /**
1725 * Update log data for each LP version into wp option.
1726 *
1727 * @param string $version
1728 * @param mixed $data
1729 */
1730 function learn_press_update_log( $version, $data ) {
1731 $logs = get_option( 'learn_press_update_logs' );
1732 if ( ! $logs ) {
1733 $logs = array( $version => $data );
1734 } else {
1735 $logs[ $version ] = $data;
1736 }
1737 update_option( 'learn_press_update_logs', $logs );
1738 }
1739
1740 /**
1741 * Output variables to screen for debugging.
1742 */
1743 function learn_press_debug() {
1744 $args = func_get_args();
1745 $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
1746
1747 echo '<pre>';
1748 print_r( $debug );
1749 $arg = false;
1750
1751 if ( $args ) {
1752 foreach ( $args as $arg ) {
1753 echo "\n======LearnPress Debug=======\n";
1754 print_r( $arg );
1755 echo "\n=============================\n";
1756 }
1757 }
1758 echo '</pre>';
1759
1760 if ( true === $arg ) {
1761 die( __FUNCTION__ );
1762 }
1763 }
1764
1765 function learn_press_get_requested_post_type() {
1766 global $pagenow;
1767 if ( $pagenow == 'post-new.php' && ! empty( $_REQUEST['post_type'] ) ) {
1768 $post_type = LP_Helper::sanitize_params_submitted( $_REQUEST['post_type'] );
1769 } else {
1770 $post_id = learn_press_get_post();
1771 $post_type = learn_press_get_post_type( $post_id );
1772 }
1773
1774 return $post_type;
1775 }
1776
1777 /**
1778 * Get human string from grade slug.
1779 *
1780 * @param string $slug
1781 *
1782 * @return string
1783 */
1784 function learn_press_get_graduation_text( $slug ) {
1785 switch ( $slug ) {
1786 case 'passed':
1787 $text = esc_html__( 'Passed', 'learnpress' );
1788 break;
1789 case 'failed':
1790 $text = esc_html__( 'Failed', 'learnpress' );
1791 break;
1792 case 'in-progress':
1793 $text = esc_html__( 'In Progress', 'learnpress' );
1794 break;
1795 default:
1796 $text = $slug;
1797 }
1798
1799 return apply_filters( 'learn-press/get-graduation-text', $text, $slug );
1800 }
1801
1802 if ( ! function_exists( 'learn_press_is_negative_value' ) ) {
1803 function learn_press_is_negative_value( $value ) {
1804 $return = in_array( $value, array( 'no', 'off', 'false', '0' ) ) || ! $value || $value == '' || $value == null;
1805
1806 return $return;
1807 }
1808 }
1809
1810 /**
1811 * Filter to comment reply link to fix bug the link is invalid for
1812 * lesson or quiz.
1813 *
1814 * @param string $link
1815 * @param array $args
1816 * @param WP_Comment $comment
1817 * @param WP_Post $post
1818 *
1819 * @return string
1820 */
1821 function learn_press_comment_reply_link( $link, $args = array(), $comment = null, $post = null ) {
1822
1823 $post_type = learn_press_get_post_type( $post );
1824
1825 if ( ! learn_press_is_support_course_item_type( $post_type ) ) {
1826 return $link;
1827 }
1828
1829 $course_item = LP_Global::course_item();
1830
1831 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
1832 $link = sprintf(
1833 '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
1834 esc_url_raw( wp_login_url( get_permalink() ) ),
1835 $args['login_text']
1836 );
1837 } elseif ( $course_item ) {
1838 $onclick = sprintf(
1839 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
1840 $args['add_below'],
1841 $comment->comment_ID,
1842 $args['respond_id'],
1843 $post->ID
1844 );
1845
1846 $link = sprintf(
1847 "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
1848 esc_url_raw(
1849 add_query_arg(
1850 array(
1851 'replytocom' => $comment->comment_ID,
1852 ),
1853 $course_item->get_permalink()
1854 )
1855 ) . '#' . $args['respond_id'],
1856 $onclick,
1857 esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
1858 $args['reply_text']
1859 );
1860 }
1861
1862 return $link;
1863 }
1864
1865 add_filter( 'comment_reply_link', 'learn_press_comment_reply_link', 10, 4 );
1866
1867 /**
1868 * Sanitize content of tooltip
1869 *
1870 * @param string $tooltip
1871 * @param bool $html
1872 *
1873 * @return string
1874 */
1875 function learn_press_sanitize_tooltip( $tooltip, $html = false ) {
1876 if ( $html ) {
1877 $tooltip = htmlspecialchars(
1878 wp_kses(
1879 html_entity_decode( $tooltip ),
1880 array(
1881 'br' => array(),
1882 'em' => array(),
1883 'strong' => array(),
1884 'small' => array(),
1885 'span' => array(),
1886 'ul' => array(),
1887 'li' => array(),
1888 'ol' => array(),
1889 'p' => array(),
1890 )
1891 )
1892 );
1893 } else {
1894 $tooltip = esc_attr( $tooltip );
1895 }
1896
1897 return $tooltip;
1898 }
1899
1900 function learn_press_tooltip( $tooltip, $html = false ) {
1901 $tooltip = learn_press_sanitize_tooltip( $tooltip, $html );
1902 echo '<span class="learn-press-tooltip" data-tooltip="' . esc_attr( $tooltip ) . '"></span>';
1903 }
1904
1905 /**
1906 * Get default static pages of LP.
1907 *
1908 * @return array
1909 *
1910 * @since 3.0.0
1911 * @deprecated 4.2.3
1912 */
1913 function learn_press_static_page_ids() {
1914 $pages = LP_Object_Cache::get( 'static-page-ids', 'learn-press' );
1915
1916 if ( false === $pages ) {
1917 $pages = array(
1918 'checkout' => learn_press_get_page_id( 'checkout' ),
1919 'courses' => learn_press_get_page_id( 'courses' ),
1920 'profile' => learn_press_get_page_id( 'profile' ),
1921 'become_a_teacher' => learn_press_get_page_id( 'become_a_teacher' ),
1922 );
1923
1924 foreach ( $pages as $name => $id ) {
1925 if ( ! get_post( $id ) ) {
1926 $pages[ $name ] = 0;
1927 }
1928 }
1929
1930 LP_Object_Cache::set( 'static-page-ids', $pages, 'learn-press' );
1931 }
1932
1933 return apply_filters( 'learn-press/static-page-ids', $pages );
1934 }
1935
1936 /**
1937 * Callback function for sorting to array|object by key|prop priority.
1938 *
1939 * @param array|object $a
1940 * @param array|object $b
1941 *
1942 * @return int
1943 * @since 3.0.0
1944 * @deprecated 4.4.5
1945 */
1946 /*function learn_press_sort_list_by_priority_callback( $a, $b ) {
1947 $a_priority = null;
1948 $b_priority = null;
1949
1950 if ( is_array( $a ) && array_key_exists( 'priority', $a ) ) {
1951 $a_priority = $a['priority'];
1952 } elseif ( is_object( $a ) ) {
1953 if ( is_callable( array( $a, 'get_priority' ) ) ) {
1954 $a_priority = $a->get_priority();
1955 } elseif ( property_exists( $a, 'priority' ) ) {
1956 $a_priority = $a->priority;
1957 }
1958 }
1959
1960 if ( is_array( $b ) && array_key_exists( 'priority', $b ) ) {
1961 $b_priority = $b['priority'];
1962 } elseif ( is_object( $b ) ) {
1963 if ( is_callable( array( $b, 'get_priority' ) ) ) {
1964 $b_priority = $b->get_priority();
1965 } elseif ( property_exists( $b, 'priority' ) ) {
1966 $b_priority = $b->priority;
1967 }
1968 }
1969
1970 if ( $a_priority === $b_priority ) {
1971 return 0;
1972 }
1973
1974 return ( $a_priority < $b_priority ) ? - 1 : 1;
1975 }*/
1976
1977 /**
1978 * Localize date with custom format.
1979 *
1980 * @param int|bool $timestamp
1981 * @param string $format
1982 * @param bool $gmt
1983 *
1984 * @return string
1985 * @since 3.0.0
1986 * @deprcated 4.2.6 use LP_Datetime format instead
1987 */
1988 function learn_press_date_i18n( $timestamp = 0, string $format = '', bool $gmt = false ): string {
1989 if ( ! $format ) {
1990 $format = get_option( 'date_format' );
1991 }
1992
1993 $date_str = date_i18n( $format, $timestamp, $gmt );
1994 if ( ! is_string( $date_str ) ) {
1995 $date_str = '';
1996 }
1997
1998 return $date_str;
1999 }
2000
2001 /**
2002 * Get item types of course support for blocking. Default is lp_lesson
2003 *
2004 * @return array
2005 * @since 3.0.0
2006 */
2007 function learn_press_get_block_course_item_types() {
2008 return apply_filters( 'learn-press/block-course-item-types', array( LP_LESSON_CPT, LP_QUIZ_CPT ) );
2009 }
2010
2011 /**
2012 * Get post type of a post from cache.
2013 * If there is no data stored in cache then
2014 * get it from WP API.
2015 *
2016 * @param int|WP_Post $post
2017 *
2018 * @return string
2019 * @since 3.1.0
2020 */
2021 function learn_press_get_post_type( $post ) {
2022 $post_types = LP_Object_Cache::get( 'post-types', 'learn-press' );
2023
2024 if ( false === $post_types ) {
2025 $post_types = array();
2026 }
2027
2028 if ( is_object( $post ) ) {
2029 $post_id = $post->ID;
2030 } else {
2031 $post_id = absint( $post );
2032 }
2033
2034 if ( empty( $post_types[ $post_id ] ) ) {
2035 $post_type = get_post_type( $post_id );
2036 $post_types[ $post_id ] = $post_type;
2037 LP_Object_Cache::set( 'post-types', $post_types, 'learn-press' );
2038 } else {
2039 $post_type = $post_types[ $post_id ];
2040 }
2041
2042 return $post_type;
2043 }
2044
2045 /**
2046 * Update option to enable shuffle themes for ad.
2047 *
2048 * @since 3.2.1
2049 */
2050 function _learn_press_schedule_enable_shuffle_themes() {
2051 update_option( 'learn_press_ad_shuffle_themes', 'yes' );
2052 }
2053
2054 add_action( 'learn-press/schedule-enable-shuffle-themes', '_learn_press_schedule_enable_shuffle_themes' );
2055
2056 /**
2057 * Return localize script data for admin.
2058 *
2059 * @return array
2060 * @since 3.2.6
2061 * @version 1.0.1
2062 */
2063 function learn_press_global_script_params(): array {
2064 $localize = [
2065 'ajax' => admin_url( 'admin-ajax.php' ),
2066 'plugin_url' => LearnPress::instance()->plugin_url(),
2067 'siteurl' => home_url(),
2068 //'current_url' => learn_press_get_current_url(),
2069 'theme' => get_stylesheet(),
2070 'localize' => array(
2071 'button_ok' => __( 'OK', 'learnpress' ),
2072 'button_cancel' => __( 'Cancel', 'learnpress' ),
2073 'button_yes' => __( 'Yes', 'learnpress' ),
2074 'button_no' => __( 'No', 'learnpress' ),
2075 ),
2076 'rest' => esc_url_raw( rest_url() ),
2077 'nonce' => wp_create_nonce( 'wp_rest' ),
2078 'is_admin' => current_user_can( ADMIN_ROLE ),
2079 ];
2080
2081 return apply_filters( 'lp/admin/localize/scripts', $localize );
2082 }
2083
2084 /**
2085 * Return list types of questions that support answer options.
2086 *
2087 * @return array
2088 * @since 3.3.0
2089 */
2090 function learn_press_get_question_support_answer_options() {
2091 $questions = learn_press_get_question_support_feature( 'answer-options' );
2092
2093 return apply_filters( 'learn-press/questions-support-answer-options', $questions );
2094 }
2095
2096 /**
2097 * Return list types of question that support a feature.
2098 *
2099 * @param string $feature
2100 *
2101 * @return array
2102 * @since 3.3.0
2103 */
2104 function learn_press_get_question_support_feature( $feature ) {
2105 $questions = array();
2106 $types = LP_Global::get_object_supports( 'question' );
2107
2108 if ( $types ) {
2109 foreach ( $types as $type => $features ) {
2110 if ( array_key_exists( $feature, $features ) ) {
2111 $questions[] = $type;
2112 }
2113 }
2114 }
2115
2116 return $questions;
2117 }
2118
2119 /**
2120 * LP Cookie
2121 *
2122 * @param $name
2123 * @param $namespace
2124 *
2125 * @return mixed|null
2126 */
2127 function learn_press_cookie_get( $name, $namespace = 'LP' ) {
2128 if ( $namespace ) {
2129 $cookie = ! empty( $_COOKIE[ $namespace ] ) ? (array) json_decode( LP_Helper::sanitize_params_submitted( stripslashes( $_COOKIE[ $namespace ] ), 'html' ) ) : array();
2130 } else {
2131 $cookie = $_COOKIE;
2132 }
2133
2134 return $cookie[ $name ] ?? null;
2135 }
2136
2137 /**
2138 * Get default methods to evaluate course results.
2139 *
2140 * @param string $return - Optional. 'keys' will return keys instead of all.
2141 *
2142 * @return array
2143 * @since 3.x.x
2144 */
2145 function learn_press_course_evaluation_methods( $postid, $return = '', $final_quizz_passing = '' ) {
2146 $final_quiz_btn = sprintf(
2147 '<a href="#" class="lp-metabox-get-final-quiz" data-postid="%d" data-loading="%s">%s</a>',
2148 $postid,
2149 esc_attr__( 'Loading...', 'learnpress' ),
2150 esc_html__( 'Get A Passing Grade', 'learnpress' )
2151 );
2152
2153 $evaluations_desc = array(
2154 'evaluate_lesson' => sprintf(
2155 '<p>%s<br/>%s</p>',
2156 __( 'Evaluate by the number of completed lessons per the total number of lessons.', 'learnpress' ),
2157 __( 'E.g: If a course has 10 lessons and a user completes 5 lessons, then the result is 5/10 (50%).', 'learnpress' )
2158 ),
2159 'evaluate_final_quiz' => __(
2160 'Evaluate by the result of the final quiz in the course. You have to add a quiz at the end of the course.',
2161 'learnpress'
2162 ),
2163 'evaluate_quiz' => sprintf(
2164 '<p>%s<br/>%s</p>',
2165 __( 'Evaluate by the number of passed quizzes per the total number of quizzes.', 'learnpress' ),
2166 __(
2167 'E.g: If the course has 10 quizzes and the user passes 5 quizzes, then the result is 5/10 (50%).',
2168 'learnpress'
2169 )
2170 ),
2171 'evaluate_questions' => sprintf(
2172 '<p>%s<br/>%s</p>',
2173 __( 'Evaluate by the number of correct answers per the total number of questions.', 'learnpress' ),
2174 __(
2175 'E.g: If the course has 10 questions and the user corrects 5 questions, then the result is 5/10 (50%).',
2176 'learnpress'
2177 )
2178 ),
2179 'evaluate_mark' => __( 'Evaluate by the number of achieved scores per the total score of the questions.', 'learnpress' ),
2180 );
2181
2182 $methods = apply_filters(
2183 'learnpress/course-evaluation/methods',
2184 array(
2185 'evaluate_lesson' => sprintf(
2186 '%s %s',
2187 __( 'Evaluate via lessons', 'learnpress' ),
2188 learn_press_quick_tip( $evaluations_desc['evaluate_lesson'], false )
2189 ),
2190 'evaluate_final_quiz' => sprintf(
2191 '%s %s %s %s',
2192 __( 'Evaluate via results of the final quiz', 'learnpress' ),
2193 learn_press_quick_tip( $evaluations_desc['evaluate_final_quiz'], false ),
2194 $final_quiz_btn,
2195 $final_quizz_passing
2196 ),
2197 'evaluate_quiz' => sprintf(
2198 '%s %s',
2199 __( 'Evaluate via passed quizzes', 'learnpress' ),
2200 learn_press_quick_tip( $evaluations_desc['evaluate_quiz'], false )
2201 ),
2202 'evaluate_questions' => sprintf(
2203 '%s %s',
2204 __( 'Evaluate via questions', 'learnpress' ),
2205 learn_press_quick_tip( $evaluations_desc['evaluate_questions'], false )
2206 ),
2207 'evaluate_mark' => sprintf(
2208 '%s %s',
2209 __( 'Evaluate via mark', 'learnpress' ),
2210 learn_press_quick_tip( $evaluations_desc['evaluate_mark'], false )
2211 ),
2212 ),
2213 $postid
2214 );
2215
2216 return apply_filters(
2217 'learn-press/course-evaluation-methods',
2218 $return === 'keys' ? array_keys( $methods ) : $methods,
2219 $return
2220 );
2221 }
2222
2223 /**
2224 * Get max retrying quiz allowed.
2225 *
2226 * @param int $quiz_id
2227 * @param int $course_id
2228 *
2229 * @return int
2230 * @since 4.0.0
2231 */
2232 function learn_press_get_quiz_max_retrying( $quiz_id = 0, $course_id = 0 ) {
2233 return apply_filters( 'learn-press/max-retry-quiz-allowed', 1, $quiz_id, $course_id );
2234 }
2235
2236 /**
2237 * Get slug for status of course/lesson/quiz if user
2238 * completed/finished and graduation is passed.
2239 *
2240 * @param string $type
2241 *
2242 * @return string
2243 * @since 4.0.0
2244 */
2245 function learn_press_user_item_in_progress_slug( $type = '' ) {
2246 return apply_filters( 'learn-press/user-item-in-progress-slug', 'in-progress', $type );
2247 }
2248
2249 /**
2250 * Get slug for status of course/lesson/quiz if user
2251 * completed/finished and result is under-evaluation
2252 *
2253 * @param string $item - Optional. Type of item
2254 *
2255 * @return string
2256 * @since 4.0.0
2257 */
2258 function learn_press_user_item_under_evaluation_slug( $item = '' ) {
2259 return apply_filters( 'learn-press/user-item-under-evaluation-slug', 'in-progress', $item );
2260 }
2261
2262 function learn_press_is_enrolled_slug( $slug ) {
2263 return in_array(
2264 $slug,
2265 array(
2266 'in-progress',
2267 'enrolled',
2268 )
2269 );
2270 }
2271
2272 /**
2273 * @return array
2274 * @since 4.0.0
2275 */
2276 function lp_item_course_class( $class = array() ) {
2277 $classes = array_merge(
2278 $class,
2279 array( 'learn-press-courses' )
2280 );
2281 echo 'class="' . esc_attr( implode( ' ', apply_filters( 'lp_item_course_class', $classes ) ) ) . '"';
2282 }
2283
2284 //require_once dirname( __FILE__ ) . '/lp-custom-hooks.php';
2285
2286 // Notify update message for LearnPress plugin
2287 /*add_action(
2288 'in_plugin_update_message-learnpress/learnpress.php',
2289 function ( $plugin_data ) {
2290 if ( version_compare( $plugin_data['new_version'], LEARNPRESS_VERSION, '<=' ) ) {
2291 return;
2292 }
2293
2294 echo sprintf(
2295 '<hr/><h3>%s</h3><div>%s</div>',
2296 esc_html__( 'Heads up! Please backup before upgrading!', 'learnpress' ),
2297 esc_html__( 'The latest update includes some substantial changes across different areas of the plugin. We highly recommend you backup your site before upgrading, and make sure you first update in a staging environment', 'learnpress' )
2298 );
2299 }
2300 );*/
2301
2302 /**
2303 * If Elementor Pro set Theme builder type "Archive", will not show content on page "Archive course"
2304 *
2305 * @editor tungnx
2306 * @author nhamdv
2307 *
2308 * @since 4.0.6
2309 * @version 1.0.1
2310 */
2311 add_filter(
2312 'elementor/theme/get_location_templates/template_id',
2313 function ( $theme_template_id ) {
2314 $elementor_template_type = get_post_meta( $theme_template_id, '_elementor_template_type', true );
2315
2316 if ( in_array( $elementor_template_type, array( 'archive' ) ) ) {
2317 if ( LP_PAGE_COURSES === LP_Page_Controller::page_current() && class_exists( 'ElementorPro\Modules\ThemeBuilder\Conditions\Archive' ) ) {
2318 return false;
2319 }
2320 }
2321
2322 return $theme_template_id;
2323 }
2324 );
2325