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

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

3,242 lines 82.0 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 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Vue write php has script, so when sanitize will error, so use code is mask pass check sanitize, esc
15 *
16 * @param $content
17 *
18 * @return void
19 * @since 4.1.6.9
20 */
21 function learn_press_echo_vuejs_write_on_php( $content ) {
22 echo ( $content );
23 }
24
25 function learnpress_gutenberg_disable_cpt( $can_edit, $post_type ) {
26 $post_types = array(
27 LP_COURSE_CPT => LP_Settings::get_option( 'enable_gutenberg_course', 'no' ),
28 LP_LESSON_CPT => LP_Settings::get_option( 'enable_gutenberg_lesson', 'no' ),
29 LP_QUIZ_CPT => LP_Settings::get_option( 'enable_gutenberg_quiz', 'no' ),
30 LP_QUESTION_CPT => LP_Settings::get_option( 'enable_gutenberg_question', 'no' ),
31 );
32
33 foreach ( $post_types as $key => $pt ) {
34 if ( $post_type === $key && $pt !== 'yes' ) {
35 $can_edit = false;
36 }
37 }
38
39 return $can_edit;
40 }
41 add_filter( 'use_block_editor_for_post_type', 'learnpress_gutenberg_disable_cpt', 10, 2 );
42
43 /**
44 * Get instance of a CURD class by type
45 *
46 * @param string $type
47 *
48 * @return bool|LP_Course_CURD|LP_User_CURD|LP_Quiz_CURD|LP_Question_CURD
49 * @depecated 4.1.7.2
50 */
51 /*function learn_press_get_curd( $type ) {
52 $curds = array(
53 'user' => 'LP_User_CURD',
54 'course' => 'LP_Course_CURD',
55 'quiz' => 'LP_Quiz_CURD',
56 'question' => 'LP_Question_CURD',
57 );
58
59 $curd = false;
60
61 if ( ! empty( $curds[ $type ] ) && class_exists( $curds[ $type ] ) ) {
62 $curd = new $curds[ $type ]();
63 }
64
65 return apply_filters( 'learn-press/curd', $curd, $type, $curds );
66 }*/
67
68 if ( ! function_exists( 'lp_add_body_class' ) ) {
69 function lp_add_body_class( $classes ) {
70 $classes = (array) $classes;
71
72 if ( learn_press_is_profile() ) {
73 $classes[] = 'learnpress-profile';
74 } elseif ( learn_press_is_checkout() ) {
75 $classes[] = 'learnpress-checkout';
76 }
77
78 return $classes;
79 }
80 add_filter( 'body_class', 'lp_add_body_class' );
81 }
82
83 /**
84 * Short function to get name of a theme
85 *
86 * @param string $folder
87 *
88 * @return mixed|string
89 */
90 function learn_press_get_theme_name( $folder ) {
91 $theme = wp_get_theme( $folder );
92
93 return ! empty( $theme['Name'] ) ? $theme['Name'] : '';
94 }
95
96 /**
97 * Clean.
98 *
99 * @param [type] $var
100 *
101 * @version 4.0.0
102 * @author Nhamdv <daonham95@gmail.com>
103 */
104 function learnpress_clean( $var ) {
105 if ( is_array( $var ) ) {
106 return array_map( 'learnpress_clean', $var );
107 } else {
108 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
109 }
110 }
111
112 /**
113 * Display HTML of element for building QuickTip JS.
114 *
115 * @param string $tip
116 * @param bool $echo
117 * @param array $options
118 *
119 * @return string
120 * @since 3.0.0
121 */
122 function learn_press_quick_tip( $tip, $echo = true, $options = array() ) {
123 $atts = '';
124 if ( $options ) {
125 foreach ( $options as $k => $v ) {
126 $options[ $k ] = "data-{$k}=\"{$v}\"";
127 }
128 $atts = ' ' . implode( ' ', $options );
129 }
130
131 $tip = sprintf( '<span class="learn-press-tip" ' . $atts . '>%s</span>', $tip );
132
133 if ( $echo ) {
134 echo wp_kses_post( $tip );
135 }
136
137 return $tip;
138 }
139
140 /**
141 * Get current post ID.
142 *
143 * @return int
144 */
145 function learn_press_get_post() {
146 global $post;
147
148 $post_id = learn_press_get_request( 'post' );
149
150 if ( ! $post_id ) {
151 $post_id = ! empty( $post ) ? $post->ID : 0;
152 }
153 if ( empty( $post_id ) ) {
154 $post_id = learn_press_get_request( 'post_ID' );
155 }
156
157 return absint( $post_id );
158 }
159
160 /**
161 * Get the LearnPress plugin url
162 *
163 * @param string $sub_dir
164 *
165 * @return string
166 */
167 function learn_press_plugin_url( $sub_dir = '' ) {
168 return LearnPress::instance()->plugin_url( $sub_dir );
169 }
170
171 /**
172 * Get the LearnPress plugin path.
173 *
174 * @param string $sub_dir
175 *
176 * @return string
177 */
178 function learn_press_plugin_path( $sub_dir = '' ) {
179 return LearnPress::instance()->plugin_path( $sub_dir );
180 }
181
182 /**
183 * Includes file base on LearnPress path
184 *
185 * @param string $file
186 * @param string $folder
187 * @param bool $include_once
188 *
189 * @return bool
190 */
191 function learn_press_include( $file, $folder = 'inc', $include_once = true ) {
192 $include = learn_press_plugin_path( "{$folder}/{$file}" );
193
194 if ( file_exists( $include ) ) {
195 if ( $include_once ) {
196 include_once $include;
197 } else {
198 include $include;
199 }
200
201 return true;
202 }
203
204 return false;
205 }
206
207 /**
208 * Get current IP of the user
209 *
210 * @return mixed
211 */
212 function learn_press_get_ip() {
213 if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
214 return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) );
215 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
216 // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
217 // Make sure we always only send through the first IP in the list which should always be the client IP.
218 return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) );
219 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
220 return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
221 }
222 return '';
223 }
224
225 /**
226 * Get user agent.
227 *
228 * @return string
229 */
230 function learn_press_get_user_agent(): string {
231 return LP_Helper::sanitize_params_submitted( $_SERVER['HTTP_USER_AGENT'] ?? '' );
232 }
233
234 /**
235 * Generate an unique string.
236 *
237 * @param string $prefix
238 *
239 * @return string
240 */
241 function learn_press_uniqid( $prefix = '' ) {
242 $hash = str_replace( '.', '', microtime( true ) . uniqid() );
243
244 return apply_filters( 'learn-press/generate-hash', $prefix . $hash, $prefix );
245 }
246
247 function learn_press_random_value( $len = 8 ) {
248 return substr( md5( uniqid( mt_rand(), true ) ), 0, $len );
249 }
250
251 function learn_press_map_columns_format( $columns, $format ) {
252 $return = array();
253 foreach ( $columns as $k => $v ) {
254 if ( ! empty( $format[ $k ] ) ) {
255 $return[] = $format[ $k ];
256 } else {
257 $return[] = '%s'; // default is string
258 }
259 }
260
261 return $return;
262 }
263
264 /**
265 * Check to see if an endpoint is showing in current URL.
266 *
267 * @param bool $endpoint
268 *
269 * @return bool
270 */
271 function learn_press_is_endpoint_url( $endpoint = false ) {
272 global $wp;
273
274 $endpoints = array();
275
276 if ( $endpoint !== false ) {
277 if ( ! isset( $endpoints[ $endpoint ] ) ) {
278 return false;
279 } else {
280 $endpoint_var = $endpoints[ $endpoint ];
281 }
282
283 return isset( $wp->query_vars[ $endpoint_var ] );
284 } else {
285 foreach ( $endpoints as $key => $value ) {
286 if ( isset( $wp->query_vars[ $key ] ) ) {
287 return true;
288 }
289 }
290
291 return false;
292 }
293 }
294
295 /**
296 * Get current URL user is viewing.
297 *
298 * @return string
299 */
300 function learn_press_get_current_url() {
301 static $current_url;
302
303 if ( ! $current_url ) {
304 $url = untrailingslashit( esc_url_raw( $_SERVER['REQUEST_URI'] ) );
305
306 if ( ! preg_match( '!^https?!', $url ) ) {
307 $siteurl = trailingslashit( get_home_url() );
308 $home_query = '';
309
310 if ( strpos( $siteurl, '?' ) !== false ) {
311 $parts = explode( '?', $siteurl );
312 $home_query = $parts[1];
313 $siteurl = $parts[0];
314 }
315
316 if ( $home_query ) {
317 parse_str( untrailingslashit( $home_query ), $home_query );
318 $url = esc_url_raw( add_query_arg( $home_query, $url ) );
319 }
320
321 $segs1 = explode( '/', $siteurl );
322 $segs2 = explode( '/', $url );
323
324 if ( $removed = array_intersect( $segs1, $segs2 ) ) {
325 if ( $segs2 = array_diff( $segs2, $removed ) ) {
326 $current_url = $siteurl . join( '/', $segs2 );
327 if ( strpos( $current_url, '?' ) === false ) {
328 $current_url = trailingslashit( $current_url );
329 }
330 }
331 }
332 }
333 }
334
335 return $current_url;
336 }
337
338 /**
339 * Compares an url with current URL user is viewing
340 *
341 * @param string $url
342 *
343 * @return bool
344 */
345 function learn_press_is_current_url( $url ) {
346 $current_url = learn_press_get_current_url();
347
348 return ( $current_url && $url ) && strcmp( $current_url, learn_press_sanitize_url( $url ) ) == 0;
349 }
350
351 /**
352 * Remove unneeded characters in an URL
353 *
354 * @param string $url
355 * @param bool $trailingslashit
356 *
357 * @return string
358 */
359 function learn_press_sanitize_url( $url, $trailingslashit = true ) {
360 if ( $url ) {
361 preg_match( '!(https?://)?(.*)!', $url, $matches );
362 $url_without_http = $matches[2];
363 $url_without_http = preg_replace( '![/]+!', '/', $url_without_http );
364 $url = $matches[1] . $url_without_http;
365
366 return ( $trailingslashit &&
367 strpos( $url, '?' ) === false ) ? trailingslashit( $url ) : untrailingslashit( $url );
368 }
369
370 return $url;
371 }
372
373 /**
374 * Get all types of question supported
375 *
376 * @return mixed
377 */
378 function learn_press_question_types() {
379 return LP_Question::get_types();
380 }
381
382 /**
383 * Get human name of question's type by slug
384 *
385 * @param string $slug
386 *
387 * @return array
388 */
389 function learn_press_question_name_from_slug( $slug ) {
390 $types = learn_press_question_types();
391 $name = ! empty( $types[ $slug ] ) ? $types[ $slug ] : '';
392
393 return apply_filters( 'learn-press/question/slug-to-name', $name, $slug );
394 }
395
396 /**
397 * Get terms of a course by taxonomy.
398 * E.g: course_tag, course_category
399 *
400 * @param int $course_id
401 * @param string $taxonomy
402 * @param array $args
403 *
404 * @return array|mixed
405 */
406 function learn_press_get_course_terms( $course_id, $taxonomy, $args = array() ) {
407 if ( ! taxonomy_exists( $taxonomy ) ) {
408 return array();
409 }
410
411 // Support ordering by parent
412 if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
413 $fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
414 $orderby = $args['orderby'];
415
416 // Unset for wp_get_post_terms
417 unset( $args['orderby'] );
418 unset( $args['fields'] );
419
420 $terms = wp_get_post_terms( $course_id, $taxonomy, $args );
421
422 switch ( $orderby ) {
423 case 'name_num':
424 usort( $terms, '_learn_press_get_course_terms_name_num_usort_callback' );
425 break;
426 case 'parent':
427 usort( $terms, '_learn_press_get_course_terms_parent_usort_callback' );
428 break;
429 }
430
431 switch ( $fields ) {
432 case 'names':
433 $terms = wp_list_pluck( $terms, 'name' );
434 break;
435 case 'ids':
436 $terms = wp_list_pluck( $terms, 'term_id' );
437 break;
438 case 'slugs':
439 $terms = wp_list_pluck( $terms, 'slug' );
440 break;
441 }
442 } elseif ( ! empty( $args['orderby'] ) && $args['orderby'] === 'menu_order' ) {
443 // wp_get_post_terms doesn't let us use custom sort order
444 $args['include'] = wp_get_post_terms( $course_id, $taxonomy, array( 'fields' => 'ids' ) );
445
446 if ( empty( $args['include'] ) ) {
447 $terms = array();
448 } else {
449 // This isn't needed for get_terms
450 unset( $args['orderby'] );
451
452 // Set args for get_terms
453 $args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
454 $args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0;
455 $args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names';
456
457 // Ensure slugs is valid for get_terms - slugs isn't supported
458 $args['fields'] = $args['fields'] === 'slugs' ? 'id=>slug' : $args['fields'];
459 $terms = get_terms( $taxonomy, $args );
460 }
461 } else {
462 $terms = wp_get_post_terms( $course_id, $taxonomy, $args );
463 }
464
465 // @deprecated
466 $terms = apply_filters( 'learn_press_get_course_terms', $terms, $course_id, $taxonomy, $args );
467
468 return apply_filters( 'learn-press/course/terms', $terms, $course_id, $taxonomy, $args );
469 }
470
471 /**
472 * Callback function for sorting terms of course by name.
473 *
474 * @param object $a
475 * @param object $b
476 *
477 * @return int
478 */
479 function _learn_press_get_course_terms_name_num_usort_callback( $a, $b ) {
480 if ( $a->name + 0 === $b->name + 0 ) {
481 return 0;
482 }
483
484 return ( $a->name + 0 < $b->name + 0 ) ? - 1 : 1;
485 }
486
487 /**
488 * Callback function for sorting terms of course by parent.
489 *
490 * @param object $a
491 * @param object $b
492 *
493 * @return int
494 */
495 function _learn_press_get_course_terms_parent_usort_callback( $a, $b ) {
496 if ( $a->parent === $b->parent ) {
497 return 0;
498 }
499
500 return ( $a->parent < $b->parent ) ? 1 : - 1;
501 }
502
503 /**
504 * Get posts by it's post-name (slug).
505 *
506 * @param string $name
507 * @param string $type
508 * @param bool $single
509 *
510 * @return array|bool|null|WP_Post
511 */
512 function learn_press_get_post_by_name( $name, $type, $single = true ) {
513 $post_name = sanitize_title( $name );
514 $id = LP_Object_Cache::get( $type . '-' . $post_name, 'learn-press/post-names' );
515
516 if ( false === $id ) {
517 foreach ( array( $name, urldecode( $name ) ) as $_name ) {
518 $args = array(
519 'name' => $_name,
520 'post_type' => array( $type ),
521 );
522
523 $posts = get_posts( $args );
524
525 if ( $posts ) {
526 $post = $posts[0];
527 $id = $post->ID;
528 wp_cache_set( $id, $post, 'posts' );
529 LP_Object_Cache::set( $type . '-' . $name, $id, 'learn-press/post-names' );
530 break;
531 }
532 }
533 }
534
535 return $id ? get_post( $id ) : false;
536 }
537
538 if ( ! function_exists( 'learn_press_is_ajax' ) ) {
539 function learn_press_is_ajax() {
540 return defined( 'LP_DOING_AJAX' ) && LP_DOING_AJAX && 'yes' != learn_press_get_request( 'noajax' );
541 }
542 }
543
544 /**
545 * Get page id from admin settings page
546 *
547 * @param string $name
548 *
549 * @return int
550 */
551 function learn_press_get_page_id( string $name ): int {
552 $page_id = LP_Settings::instance()->get( "{$name}_page_id", false );
553
554 if ( function_exists( 'icl_object_id' ) ) {
555 $page_id = icl_object_id( $page_id, 'page', false, defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '' );
556 }
557
558 $page_id = (int) $page_id;
559
560 return apply_filters( 'learn_press_get_page_id', $page_id, $name );
561 }
562
563 /**
564 * display the seconds in time format h:i:s
565 *
566 * @param $seconds
567 * @param string $separator
568 *
569 * @return string
570 */
571 function learn_press_seconds_to_time( $seconds, $separator = ':' ) {
572 return sprintf(
573 '%02d%s%02d%s%02d',
574 floor( $seconds / 3600 ),
575 $separator,
576 ( $seconds / 60 ) % 60,
577 $separator,
578 $seconds % 60
579 );
580 }
581
582 /* nav */
583 if ( ! function_exists( 'learn_press_course_paging_nav' ) ) {
584
585 /**
586 * Display navigation to next/previous set of posts when applicable.
587 *
588 * @param array
589 */
590 function learn_press_course_paging_nav( $args = array() ) {
591 learn_press_paging_nav(
592 array(
593 'num_pages' => $GLOBALS['wp_query']->max_num_pages,
594 'wrapper_class' => 'navigation pagination',
595 )
596 );
597 }
598 }
599
600 /* nav */
601 if ( ! function_exists( 'learn_press_paging_nav' ) ) {
602 function learn_press_paging_nav( $args = array() ) {
603 $args = wp_parse_args(
604 $args,
605 array(
606 'num_pages' => 0,
607 'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
608 'wrapper_class' => 'learn-press-pagination',
609 'base' => false,
610 'format' => '',
611 'echo' => true,
612 )
613 );
614
615 if ( $args['num_pages'] < 2 ) {
616 return false;
617 }
618
619 $paged = $args['paged'];
620 $pagenum_link = html_entity_decode( $args['base'] === false ? get_pagenum_link() : $args['base'] );
621
622 $query_args = array();
623 $url_parts = explode( '?', $pagenum_link );
624
625 if ( isset( $url_parts[1] ) ) {
626 wp_parse_str( $url_parts[1], $query_args );
627 }
628
629 $pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) );
630 $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
631
632 $format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos(
633 $pagenum_link,
634 'index.php'
635 ) ? 'index.php/' : '';
636 $format .= $args['format'] ? $args['format'] : ( $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit(
637 'page/%#%',
638 'paged'
639 ) : '?paged=%#%' );
640
641 $link_args = array(
642 'base' => $pagenum_link,
643 'format' => $format,
644 'total' => $args['num_pages'],
645 'current' => max( 1, $paged ),
646 'mid_size' => 1,
647 'add_args' => array_map( 'urlencode', $query_args ),
648 'prev_text' => __( '<', 'learnpress' ),
649 'next_text' => __( '>', 'learnpress' ),
650 'type' => 'list',
651 );
652
653 // Set up paginated links.
654 $links = paginate_links( $link_args );
655
656 ob_start();
657
658 if ( $links ) {
659 ?>
660 <div class="<?php echo esc_attr( $args['wrapper_class'] ); ?>">
661 <?php echo wp_kses_post( $links ); ?>
662 </div>
663 <?php
664 }
665
666 $output = ob_get_clean();
667
668 if ( $args['echo'] ) {
669 echo wp_kses_post( $output );
670 }
671
672 return $output;
673 }
674 }
675
676 /**
677 * Get number of pages by rows and items per page.
678 *
679 * @param int $total
680 * @param int $limit
681 *
682 * @return int
683 */
684 function learn_press_get_num_pages( $total, $limit = 10 ) {
685 $limit = $limit <= 0 ? 10 : $limit;
686
687 if ( $total <= $limit ) {
688 return 1;
689 }
690
691 $pages = absint( $total / $limit );
692
693 if ( $total % $limit != 0 ) {
694 $pages ++;
695 }
696
697 return $pages;
698 }
699
700 /**
701 * Get text
702 *
703 * @param $status_id
704 *
705 * @return mixed
706 */
707 function learn_press_get_status_text( $status_id ) {
708 switch ( $status_id ) {
709 case 1:
710 $text = 'pending';
711 break;
712 case 2:
713 $text = 'complete';
714 break;
715 case - 1:
716 $text = 'cancel';
717 break;
718 case - 2:
719 $text = 'refund';
720 break;
721 default:
722 $text = 'on-hold';
723 }
724
725 return $text;
726 }
727
728 function learn_press_get_course_duration_support() {
729 return apply_filters(
730 'learn_press_course_duration_support',
731 array(
732 'minute' => esc_html__( 'Minute(s)', 'learnpress' ),
733 'hour' => esc_html__( 'Hour(s)', 'learnpress' ),
734 'day' => esc_html__( 'Day(s)', 'learnpress' ),
735 'week' => esc_html__( 'Week(s)', 'learnpress' ),
736 )
737 );
738 }
739
740 function learn_press_number_to_string_time( $number ) {
741 $str = $number;
742
743 if ( preg_match( '!([0-9.]+) (minute|hour|day|week)!', $number, $matches ) ) {
744 switch ( $matches[2] ) {
745 case 'hour':
746 $minute = $matches[1] * 60;
747 $str = sprintf( '%s hour %s minute', absint( $minute / 60 ), $minute % 60 );
748 break;
749 case 'day':
750 $hour = $matches[1] * 24;
751 $str = sprintf( '%s day %s hour', absint( $hour / 24 ), $hour % 24 );
752 break;
753 case 'week':
754 $day = $matches[1] * 7;
755 $str = sprintf( '%s week %s day', absint( $day / 7 ), $day % 7 );
756 break;
757 }
758 }
759
760 return $str;
761 }
762
763 function learn_press_human_time_to_seconds( $time, $default = '' ) {
764 $duration = learn_press_get_course_duration_support();
765 $duration_keys = array_keys( $duration );
766
767 if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $time, $matches ) ) {
768 $a1 = $matches[1][0];
769 $a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : '';
770 } else {
771 $a1 = absint( $time );
772 $a2 = '';
773 }
774
775 if ( $a2 ) {
776 $b = array(
777 'minute' => 60,
778 'hour' => 3600,
779 'day' => 3600 * 24,
780 'week' => 3600 * 24 * 7,
781 );
782 $a1 = $a1 * $b[ $a2 ];
783 }
784
785 return $a1;
786 }
787
788 /**
789 * Send email notification.
790 *
791 * @param string $to .
792 * @param string $action .
793 * @param array $vars .
794 *
795 * @return bool
796 * @depecated 4.1.7.2
797 */
798 /*function learn_press_send_mail( $to = '', $action = '', $vars = array() ) {
799 $email_settings = LP_Settings::instance();
800
801 if ( ! $email_settings->get( $action . '.enable' ) ) {
802 return "The action {$action} doesnt support";
803 }
804
805 $user = get_user_by( 'email', $to );
806
807 $vars['log_in'] = apply_filters( 'learn_press_site_url', get_home_url() );
808
809 // Send email.
810 $email = new LP_Email();
811 $email->set_action( $action );
812 $email->parse_email( $vars );
813 $email->add_recipient( $to );
814
815 return $email->send();
816 }*/
817
818 /*
819 * Send email notification when a course be published
820 * @deprecated 4.1.7.2
821 */
822 /*function learn_press_publish_course( $new_status, $old_status, $post ) {
823 if ( $old_status == 'pending' && $new_status == 'publish' && $post->post_type == 'lp_course' ) {
824 $instructor = get_userdata( $post->post_author );
825 $mail_to = $instructor->user_email;
826
827 learn_press_send_mail(
828 $mail_to,
829 'published_course',
830 apply_filters(
831 'learn_press_vars_enrolled_course',
832 array(
833 'user_name' => $instructor->display_name,
834 'course_name' => $post->post_title,
835 'course_link' => get_permalink( $post->ID ),
836 ),
837 $post,
838 $instructor
839 )
840 );
841 }
842 }*/
843
844 //add_action( 'transition_post_status', 'learn_press_publish_course', 10, 3 );
845
846 /**
847 * @param $user_id
848 *
849 * @return WP_Query
850 */
851 function learn_press_get_own_courses( $user_id ) {
852 $arr_query = array(
853 'post_type' => 'lp_course',
854 'author' => $user_id,
855 'post_status' => 'publish',
856 'ignore_sticky_posts' => true,
857 'posts_per_page' => - 1,
858 );
859 $my_query = new WP_Query( $arr_query );
860
861 return $my_query;
862 }
863
864 /**
865 * Return array list of currency positions.
866 *
867 * @param bool|string $currency
868 *
869 * @return array
870 */
871 function learn_press_currency_positions( $currency = false ) {
872 $positions = array(
873 'left' => __( 'Left', 'learnpress' ),
874 'right' => __( 'Right', 'learnpress' ),
875 'left_with_space' => __( 'Left with space', 'learnpress' ),
876 'right_with_space' => __( 'Right with space', 'learnpress' ),
877 );
878
879 if ( false === $currency ) {
880 $currency = learn_press_get_currency_symbol();
881 }
882
883 $settings = LP_Settings::instance();
884
885 $thousands_separator = '';
886 $decimals_separator = $settings->get( 'decimals_separator', '.' );
887 $number_of_decimals = $settings->get( 'number_of_decimals', 2 );
888
889 if ( $number_of_decimals > 0 ) {
890 $example = '69' . $decimals_separator . str_repeat( '9', $number_of_decimals );
891 } else {
892 $example = '69';
893 }
894
895 foreach ( $positions as $pos => $text ) {
896 switch ( $pos ) {
897 case 'left':
898 $text = sprintf( '%s ( %s%s )', $text, $currency, $example );
899 break;
900 case 'right':
901 $text = sprintf( '%s ( %s%s )', $text, $example, $currency );
902 break;
903 case 'left_with_space':
904 $text = sprintf( '%s ( %s %s )', $text, $currency, $example );
905 break;
906 case 'right_with_space':
907 $text = sprintf( '%s ( %s %s )', $text, $example, $currency );
908 break;
909 }
910 $positions[ $pos ] = $text;
911 }
912
913 $positions = apply_filters( 'learn_press_currency_positions', $positions );
914
915 return apply_filters( 'learn-press/currency-positions', $positions );
916 }
917
918 /**
919 * @return array
920 */
921 function learn_press_get_payment_currencies() {
922 return apply_filters( 'learn_press_get_payment_currencies', learn_press_currencies() );
923 }
924
925 /**
926 * Get the list of currencies with code and name.
927 *
928 * @return array
929 * @version 3.0.0
930 *
931 * @author ThimPress
932 */
933 function learn_press_currencies() {
934 $currencies = array(
935 'AFN' => __( 'Afghan afghani', 'learnpress' ),
936 'ALL' => __( 'Albanian lek', 'learnpress' ),
937 'DZD' => __( 'Algerian dinar', 'learnpress' ),
938 'EUR' => __( 'Euro', 'learnpress' ),
939 'AOA' => __( 'Angolan kwanza', 'learnpress' ),
940 'XCD' => __( 'East Caribbean dollar', 'learnpress' ),
941 'ARS' => __( 'Argentine peso', 'learnpress' ),
942 'AMD' => __( 'Armenian dram', 'learnpress' ),
943 'AWG' => __( 'Aruban florin', 'learnpress' ),
944 'AUD' => __( 'Australian dollar', 'learnpress' ),
945 'AZN' => __( 'Azerbaijani manat', 'learnpress' ),
946 'BSD' => __( 'Bahamian dollar', 'learnpress' ),
947 'BHD' => __( 'Bahraini dinar', 'learnpress' ),
948 'BDT' => __( 'Bangladeshi taka', 'learnpress' ),
949 'BBD' => __( 'Barbadian dollar', 'learnpress' ),
950 'BYR' => __( 'Belarusian ruble', 'learnpress' ),
951 'BZD' => __( 'Belizean dollar', 'learnpress' ),
952 'XOF' => __( 'West African CFA franc', 'learnpress' ),
953 'BMD' => __( 'Bermudian dollar', 'learnpress' ),
954 'BTN' => __( 'Bhutanese ngultrum', 'learnpress' ),
955 'BOB' => __( 'Bolivian boliviano', 'learnpress' ),
956 'USD' => __( 'US dollar', 'learnpress' ),
957 'BAM' => __( 'Bosnia and Herzegovina convertible mark', 'learnpress' ),
958 'BWP' => __( 'Botswana pula', 'learnpress' ),
959 'BRL' => __( 'Brazilian real', 'learnpress' ),
960 'BND' => __( 'Brunei dollar', 'learnpress' ),
961 'BGN' => __( 'Bulgarian lev', 'learnpress' ),
962 'MMK' => __( 'Burmese kyat', 'learnpress' ),
963 'BIF' => __( 'Burundian franc', 'learnpress' ),
964 'KHR' => __( 'Cambodian riel', 'learnpress' ),
965 'XAF' => __( 'Central African CFA franc', 'learnpress' ),
966 'CAD' => __( 'Canadian dollar', 'learnpress' ),
967 'CVE' => __( 'Cape Verdean escudo', 'learnpress' ),
968 'KYD' => __( 'Cayman Islands dollar', 'learnpress' ),
969 'CLP' => __( 'Chilean peso', 'learnpress' ),
970 'CNY' => __( 'Chinese renminbi', 'learnpress' ),
971 'COP' => __( 'Colombian peso', 'learnpress' ),
972 'KMF' => __( 'Comorian franc', 'learnpress' ),
973 'CDF' => __( 'Congolese franc', 'learnpress' ),
974 'NZD' => __( 'New Zealand dollar', 'learnpress' ),
975 'CRC' => __( 'Costa Rican colón', 'learnpress' ),
976 'HRK' => __( 'Croatian kuna', 'learnpress' ),
977 'CUC' => __( 'Cuban peso', 'learnpress' ),
978 'ANG' => __( 'Netherlands Antilles guilder', 'learnpress' ),
979 'CZK' => __( 'Czech koruna', 'learnpress' ),
980 'DKK' => __( 'Danish krone', 'learnpress' ),
981 'DJF' => __( 'Djiboutian franc', 'learnpress' ),
982 'DOP' => __( 'Dominican peso', 'learnpress' ),
983 'EGP' => __( 'Egyptian pound', 'learnpress' ),
984 'SVC' => __( 'Salvadoran colón', 'learnpress' ),
985 'ERN' => __( 'Eritrean nakfa', 'learnpress' ),
986 'ETB' => __( 'Ethiopian birr', 'learnpress' ),
987 'FKP' => __( 'Falkland Islands pound', 'learnpress' ),
988 'FJD' => __( 'Fijian dollar', 'learnpress' ),
989 'XPF' => __( 'CFP franc', 'learnpress' ),
990 'GMD' => __( 'Gambian dalasi', 'learnpress' ),
991 'GEL' => __( 'Georgian lari', 'learnpress' ),
992 'GHS' => __( 'Ghanian cedi', 'learnpress' ),
993 'GIP' => __( 'Gibraltar pound', 'learnpress' ),
994 'GTQ' => __( 'Guatemalan quetzal', 'learnpress' ),
995 'GBP' => __( 'British pound', 'learnpress' ),
996 'GNF' => __( 'Guinean franc', 'learnpress' ),
997 'GYD' => __( 'Guyanese dollar', 'learnpress' ),
998 'HTG' => __( 'Haitian gourde', 'learnpress' ),
999 'HNL' => __( 'Honduran lempira', 'learnpress' ),
1000 'HKD' => __( 'Hong Kong dollar', 'learnpress' ),
1001 'HUF' => __( 'Hungarian forint', 'learnpress' ),
1002 'ISK' => __( 'Icelandic króna', 'learnpress' ),
1003 'INR' => __( 'Indian rupee', 'learnpress' ),
1004 'IDR' => __( 'Indonesian rupiah', 'learnpress' ),
1005 'IRR' => __( 'Iranian rial', 'learnpress' ),
1006 'IQD' => __( 'Iraqi dinar', 'learnpress' ),
1007 'ILS' => __( 'Israeli new sheqel', 'learnpress' ),
1008 'JMD' => __( 'Jamaican dollar', 'learnpress' ),
1009 'JPY' => __( 'Japanese yen ', 'learnpress' ),
1010 'JOD' => __( 'Jordanian dinar', 'learnpress' ),
1011 'KZT' => __( 'Kazakhstani tenge', 'learnpress' ),
1012 'KES' => __( 'Kenyan shilling', 'learnpress' ),
1013 'KPW' => __( 'North Korean won', 'learnpress' ),
1014 'KWD' => __( 'Kuwaiti dinar', 'learnpress' ),
1015 'KGS' => __( 'Kyrgyzstani som', 'learnpress' ),
1016 'KRW' => __( 'South Korean won', 'learnpress' ),
1017 'LAK' => __( 'Lao kip', 'learnpress' ),
1018 'LVL' => __( 'Latvian lats', 'learnpress' ),
1019 'LBP' => __( 'Lebanese pound', 'learnpress' ),
1020 'LSL' => __( 'Lesotho loti', 'learnpress' ),
1021 'LRD' => __( 'Liberian dollar', 'learnpress' ),
1022 'LD' => __( 'Libyan dinar', 'learnpress' ),
1023 'CHF' => __( 'Swiss franc', 'learnpress' ),
1024 'LTL' => __( 'Lithuanian litas', 'learnpress' ),
1025 'MOP' => __( 'Macanese pataca', 'learnpress' ),
1026 'MKD' => __( 'Macedonian denar', 'learnpress' ),
1027 'MGA' => __( 'Malagasy ariary', 'learnpress' ),
1028 'MWK' => __( 'Malawian kwacha', 'learnpress' ),
1029 'MYR' => __( 'Malaysian ringgit', 'learnpress' ),
1030 'MVR' => __( 'Maldivian rufiyaa', 'learnpress' ),
1031 'MRO' => __( 'Mauritanian ouguiya', 'learnpress' ),
1032 'MUR' => __( 'Mauritian rupee', 'learnpress' ),
1033 'MXN' => __( 'Mexican peso', 'learnpress' ),
1034 'MDL' => __( 'Moldovan leu', 'learnpress' ),
1035 'MNT' => __( 'Mongolian tugrik', 'learnpress' ),
1036 'MAD' => __( 'Moroccan dirham', 'learnpress' ),
1037 'MZN' => __( 'Mozambican metical', 'learnpress' ),
1038 'NAD' => __( 'Namibian dollar', 'learnpress' ),
1039 'NPR' => __( 'Nepalese rupee', 'learnpress' ),
1040 'NIO' => __( 'Nicaraguan córdoba', 'learnpress' ),
1041 'NGN' => __( 'Nigerian naira', 'learnpress' ),
1042 'NOK' => __( 'Norwegian krone', 'learnpress' ),
1043 'OMR' => __( 'Omani rial', 'learnpress' ),
1044 'PKR' => __( 'Pakistani rupee', 'learnpress' ),
1045 'PAB' => __( 'Panamanian balboa', 'learnpress' ),
1046 'PGK' => __( 'Papua New Guinea kina', 'learnpress' ),
1047 'PYG' => __( 'Paraguayan guarani', 'learnpress' ),
1048 'PEN' => __( 'Peruvian nuevo sol', 'learnpress' ),
1049 'PHP' => __( 'Philippine peso', 'learnpress' ),
1050 'PLN' => __( 'Polish zloty', 'learnpress' ),
1051 'QAR' => __( 'Qatari riyal', 'learnpress' ),
1052 'RON' => __( 'Romanian leu', 'learnpress' ),
1053 'RUB' => __( 'Russian ruble', 'learnpress' ),
1054 'RWF' => __( 'Rwandan franc', 'learnpress' ),
1055 'WST' => __( 'Samoan tālā', 'learnpress' ),
1056 'STD' => __( 'São Tomé and Príncipe dobra', 'learnpress' ),
1057 'SAR' => __( 'Saudi riyal', 'learnpress' ),
1058 'RSD' => __( 'Serbian dinar', 'learnpress' ),
1059 'SCR' => __( 'Seychellois rupee', 'learnpress' ),
1060 'SLL' => __( 'Sierra Leonean leone', 'learnpress' ),
1061 'SGD' => __( 'Singapore dollar', 'learnpress' ),
1062 'SBD' => __( 'Solomon Islands dollar', 'learnpress' ),
1063 'SOS' => __( 'Somali shilling', 'learnpress' ),
1064 'ZAR' => __( 'South African rand', 'learnpress' ),
1065 'LKR' => __( 'Sri Lankan rupee', 'learnpress' ),
1066 'SHP' => __( 'St. Helena pound', 'learnpress' ),
1067 'SDG' => __( 'Sudanese pound', 'learnpress' ),
1068 'SRD' => __( 'Surinamese dollar', 'learnpress' ),
1069 'SZL' => __( 'Swazi lilangeni', 'learnpress' ),
1070 'SEK' => __( 'Swedish krona', 'learnpress' ),
1071 'SYP' => __( 'Syrian pound', 'learnpress' ),
1072 'TWD' => __( 'New Taiwan dollar', 'learnpress' ),
1073 'TJS' => __( 'Tajikistani somoni', 'learnpress' ),
1074 'TZS' => __( 'Tanzanian shilling', 'learnpress' ),
1075 'THB' => __( 'Thai baht ', 'learnpress' ),
1076 'TOP' => __( 'Tongan pa’anga', 'learnpress' ),
1077 'TTD' => __( 'Trinidad and Tobago dollar', 'learnpress' ),
1078 'TND' => __( 'Tunisian dinar', 'learnpress' ),
1079 'TRY' => __( 'Turkish lira', 'learnpress' ),
1080 'TMT' => __( 'Turkmenistani manat', 'learnpress' ),
1081 'UGX' => __( 'Ugandan shilling', 'learnpress' ),
1082 'UAH' => __( 'Ukrainian hryvnia', 'learnpress' ),
1083 'AED' => __( 'United Arab Emirates dirham', 'learnpress' ),
1084 'UYU' => __( 'Uruguayan peso', 'learnpress' ),
1085 'UZS' => __( 'Uzbekistani som', 'learnpress' ),
1086 'VUV' => __( 'Vanuatu vatu', 'learnpress' ),
1087 'VEF' => __( 'Venezuelan bolivar', 'learnpress' ),
1088 'VND' => __( 'Vietnamese dong', 'learnpress' ),
1089 'YER' => __( 'Yemeni rial', 'learnpress' ),
1090 'ZMK' => __( 'Zambian kwacha', 'learnpress' ),
1091 'ZWL' => __( 'Zimbabwean dollar', 'learnpress' ),
1092 'JEP' => __( 'Jersey pound', 'learnpress' ),
1093 'LYD' => __( 'Libyan dinar', 'learnpress' ),
1094 );
1095
1096 asort( $currencies );
1097
1098 return apply_filters( 'learn-press/currencies', $currencies );
1099 }
1100
1101 /**
1102 * Get current setting of currency.
1103 *
1104 * @return string
1105 */
1106 function learn_press_get_currency() {
1107 $currency = apply_filters( 'learn_press_currency', LP_Settings::instance()->get( 'currency', 'USD' ) );
1108
1109 return apply_filters( 'learn-press/currency', $currency );
1110 }
1111
1112 /**
1113 * Return list of common symbols of the currencies on the world.
1114 *
1115 * @return array
1116 */
1117 function learn_press_currency_symbols() {
1118 $symbols = array(
1119 'AED' => '&#x62f;.&#x625;',
1120 'AFN' => '&#x60b;',
1121 'ALL' => 'L',
1122 'AMD' => 'AMD',
1123 'ANG' => '&fnof;',
1124 'AOA' => 'Kz',
1125 'ARS' => '&#36;',
1126 'AUD' => '&#36;',
1127 'AWG' => 'Afl.',
1128 'AZN' => 'AZN',
1129 'BAM' => 'KM',
1130 'BBD' => '&#36;',
1131 'BDT' => '&#2547;&nbsp;',
1132 'BGN' => '&#1083;&#1074;.',
1133 'BHD' => '.&#x62f;.&#x628;',
1134 'BIF' => 'Fr',
1135 'BMD' => '&#36;',
1136 'BND' => '&#36;',
1137 'BOB' => 'Bs.',
1138 'BRL' => '&#82;&#36;',
1139 'BSD' => '&#36;',
1140 'BTC' => '&#3647;',
1141 'BTN' => 'Nu.',
1142 'BWP' => 'P',
1143 'BYR' => 'Br',
1144 'BYN' => 'Br',
1145 'BZD' => '&#36;',
1146 'CAD' => '&#36;',
1147 'CDF' => 'Fr',
1148 'CHF' => '&#67;&#72;&#70;',
1149 'CLP' => '&#36;',
1150 'CNY' => '&yen;',
1151 'COP' => '&#36;',
1152 'CRC' => '&#x20a1;',
1153 'CUC' => '&#36;',
1154 'CUP' => '&#36;',
1155 'CVE' => '&#36;',
1156 'CZK' => '&#75;&#269;',
1157 'DJF' => 'Fr',
1158 'DKK' => 'DKK',
1159 'DOP' => 'RD&#36;',
1160 'DZD' => '&#x62f;.&#x62c;',
1161 'EGP' => 'EGP',
1162 'ERN' => 'Nfk',
1163 'ETB' => 'Br',
1164 'EUR' => '&euro;',
1165 'FJD' => '&#36;',
1166 'FKP' => '&pound;',
1167 'GBP' => '&pound;',
1168 'GEL' => '&#x20be;',
1169 'GGP' => '&pound;',
1170 'GHS' => '&#x20b5;',
1171 'GIP' => '&pound;',
1172 'GMD' => 'D',
1173 'GNF' => 'Fr',
1174 'GTQ' => 'Q',
1175 'GYD' => '&#36;',
1176 'HKD' => '&#36;',
1177 'HNL' => 'L',
1178 'HRK' => 'kn',
1179 'HTG' => 'G',
1180 'HUF' => '&#70;&#116;',
1181 'IDR' => 'Rp',
1182 'ILS' => '&#8362;',
1183 'IMP' => '&pound;',
1184 'INR' => '&#8377;',
1185 'IQD' => '&#x639;.&#x62f;',
1186 'IRR' => '&#xfdfc;',
1187 'IRT' => '&#x062A;&#x0648;&#x0645;&#x0627;&#x0646;',
1188 'ISK' => 'kr.',
1189 'JEP' => '&pound;',
1190 'JMD' => '&#36;',
1191 'JOD' => '&#x62f;.&#x627;',
1192 'JPY' => '&yen;',
1193 'KES' => 'KSh',
1194 'KGS' => '&#x441;&#x43e;&#x43c;',
1195 'KHR' => '&#x17db;',
1196 'KMF' => 'Fr',
1197 'KPW' => '&#x20a9;',
1198 'KRW' => '&#8361;',
1199 'KWD' => '&#x62f;.&#x643;',
1200 'KYD' => '&#36;',
1201 'KZT' => '&#8376;',
1202 'LAK' => '&#8365;',
1203 'LBP' => '&#x644;.&#x644;',
1204 'LKR' => '&#xdbb;&#xdd4;',
1205 'LRD' => '&#36;',
1206 'LSL' => 'L',
1207 'LYD' => '&#x644;.&#x62f;',
1208 'MAD' => '&#x62f;.&#x645;.',
1209 'MDL' => 'MDL',
1210 'MGA' => 'Ar',
1211 'MKD' => '&#x434;&#x435;&#x43d;',
1212 'MMK' => 'Ks',
1213 'MNT' => '&#x20ae;',
1214 'MOP' => 'P',
1215 'MRU' => 'UM',
1216 'MUR' => '&#x20a8;',
1217 'MVR' => '.&#x783;',
1218 'MWK' => 'MK',
1219 'MXN' => '&#36;',
1220 'MYR' => '&#82;&#77;',
1221 'MZN' => 'MT',
1222 'NAD' => 'N&#36;',
1223 'NGN' => '&#8358;',
1224 'NIO' => 'C&#36;',
1225 'NOK' => '&#107;&#114;',
1226 'NPR' => '&#8360;',
1227 'NZD' => '&#36;',
1228 'OMR' => '&#x631;.&#x639;.',
1229 'PAB' => 'B/.',
1230 'PEN' => 'S/',
1231 'PGK' => 'K',
1232 'PHP' => '&#8369;',
1233 'PKR' => '&#8360;',
1234 'PLN' => '&#122;&#322;',
1235 'PRB' => '&#x440;.',
1236 'PYG' => '&#8370;',
1237 'QAR' => '&#x631;.&#x642;',
1238 'RMB' => '&yen;',
1239 'RON' => 'lei',
1240 'RSD' => '&#1088;&#1089;&#1076;',
1241 'RUB' => '&#8381;',
1242 'RWF' => 'Fr',
1243 'SAR' => '&#x631;.&#x633;',
1244 'SBD' => '&#36;',
1245 'SCR' => '&#x20a8;',
1246 'SDG' => '&#x62c;.&#x633;.',
1247 'SEK' => '&#107;&#114;',
1248 'SGD' => '&#36;',
1249 'SHP' => '&pound;',
1250 'SLL' => 'Le',
1251 'SOS' => 'Sh',
1252 'SRD' => '&#36;',
1253 'SSP' => '&pound;',
1254 'STN' => 'Db',
1255 'SYP' => '&#x644;.&#x633;',
1256 'SZL' => 'L',
1257 'THB' => '&#3647;',
1258 'TJS' => '&#x405;&#x41c;',
1259 'TMT' => 'm',
1260 'TND' => '&#x62f;.&#x62a;',
1261 'TOP' => 'T&#36;',
1262 'TRY' => '&#8378;',
1263 'TTD' => '&#36;',
1264 'TWD' => '&#78;&#84;&#36;',
1265 'TZS' => 'Sh',
1266 'UAH' => '&#8372;',
1267 'UGX' => 'UGX',
1268 'USD' => '&#36;',
1269 'UYU' => '&#36;',
1270 'UZS' => 'UZS',
1271 'VEF' => 'Bs F',
1272 'VES' => 'Bs.S',
1273 'VND' => '&#8363;',
1274 'VUV' => 'Vt',
1275 'WST' => 'T',
1276 'XAF' => 'CFA',
1277 'XCD' => '&#36;',
1278 'XOF' => 'CFA',
1279 'XPF' => 'Fr',
1280 'YER' => '&#xfdfc;',
1281 'ZAR' => '&#82;',
1282 'ZMW' => 'ZK',
1283 );
1284
1285 return apply_filters( 'learn-press/currency-symbols', $symbols );
1286 }
1287
1288 /**
1289 * Return currency symbol from the code.
1290 *
1291 * @param string $currency
1292 *
1293 * @return string
1294 */
1295 function learn_press_get_currency_symbol( $currency = '' ) {
1296 if ( ! $currency ) {
1297 $currency = learn_press_get_currency();
1298 }
1299 $symbols = learn_press_currency_symbols();
1300 $currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : '';
1301
1302 $currency_symbol = apply_filters( 'learn_press_currency_symbol', $currency_symbol, $currency );
1303
1304 return apply_filters( 'learn-press/currency-symbol', $currency_symbol, $currency );
1305 }
1306
1307 /**
1308 * Get static page for LP page by name.
1309 *
1310 * @param string $key
1311 *
1312 * @return string
1313 * @editor tungnx
1314 * @modify 4.1.4
1315 */
1316 function learn_press_get_page_link( string $key ): string {
1317 $page_id = learn_press_get_page_id( $key );
1318 $link = '';
1319
1320 if ( $page_id && get_post_status( $page_id ) == 'publish' ) {
1321 $permalink = get_permalink( $page_id );
1322 $link = apply_filters( 'learn-press/get-page-link', trailingslashit( $permalink ), $page_id, $key );
1323 }
1324
1325 return $link;
1326 }
1327
1328 /**
1329 * Get static page for LP page by name.
1330 *
1331 * @param string $key
1332 *
1333 * @return string
1334 */
1335 function learn_press_get_page_title( $key ) {
1336 $page_id = LP_Settings::instance()->get( $key . '_page_id' );
1337 $title = '';
1338
1339 if ( $page_id && get_post_status( $page_id ) == 'publish' ) {
1340 $title = apply_filters( 'learn-press/get-page-title', get_the_title( $page_id ), $page_id, $key );
1341 }
1342
1343 return apply_filters( 'learn-press/get-page-' . $key . '-title', $title, $page_id );
1344 }
1345
1346 /**
1347 * get the ID of a course by order ID
1348 *
1349 * @param $order_id
1350 *
1351 * @return bool|mixed
1352 */
1353 function learn_press_get_course_by_order( $order_id ) {
1354 $order_items = get_post_meta( $order_id, '_learn_press_order_items', true );
1355
1356 if ( $order_items && $order_items->products ) {
1357 $array_keys = array_keys( $order_items->products );
1358
1359 return reset( $array_keys );
1360 }
1361
1362 return false;
1363 }
1364
1365 /**
1366 * Convert a number of seconds to weeks/days/hours.
1367 *
1368 * @param int $secs
1369 *
1370 * @return bool|string
1371 */
1372 function learn_press_seconds_to_weeks( int $secs = 0 ) {
1373 $secs = (int) $secs;
1374
1375 if ( 0 === $secs ) {
1376 return false;
1377 }
1378 // variables for holding values.
1379 $mins = 0;
1380 $hours = 0;
1381 $days = 0;
1382 $weeks = 0;
1383 // calculations.
1384 if ( $secs >= 60 ) {
1385 $mins = (int) ( $secs / 60 );
1386 $secs = $secs % 60;
1387 }
1388 if ( $mins >= 60 ) {
1389 $hours = (int) ( $mins / 60 );
1390 $mins = $mins % 60;
1391 }
1392 if ( $hours >= 24 ) {
1393 $days = (int) ( $hours / 24 );
1394 $hours = $hours % 24;
1395 }
1396 if ( $days >= 7 ) {
1397 $weeks = (int) ( $days / 7 );
1398 $days = $days % 7;
1399 }
1400 // format result.
1401 $result = '';
1402 if ( $weeks ) {
1403 $result .= sprintf( _n( '%s week', '%s weeks', $weeks, 'learnpress' ), $weeks ) . ' ';
1404 }
1405
1406 if ( $days ) {
1407 $result .= sprintf( _n( '%s day', '%s days', $days, 'learnpress' ), $days ) . ' ';
1408 }
1409
1410 if ( ! $weeks ) {
1411 if ( $hours ) {
1412 $result .= sprintf( _n( '%s hour', '%s hours', $hours, 'learnpress' ), $hours ) . ' ';
1413 }
1414
1415 if ( $mins ) {
1416 $result .= sprintf( _n( '%s minute', '%s minutes', $mins, 'learnpress' ), $mins ) . ' ';
1417 }
1418 }
1419
1420 $result = rtrim( $result );
1421
1422 return $result;
1423 }
1424
1425 function learn_press_course_lesson_permalink_friendly( $permalink, $lesson_id, $course_id ) {
1426 if ( '' != get_option( 'permalink_structure' ) ) {
1427 if ( preg_match( '!\?lesson=([^\?\&]*)!', $permalink, $matches ) ) {
1428 $permalink = preg_replace(
1429 '!/?\?lesson=([^\?\&]*)!',
1430 '/' . basename( get_permalink( $matches[1] ) ),
1431 untrailingslashit( $permalink )
1432 );
1433 }
1434 }
1435
1436 return $permalink;
1437 }
1438
1439 function learn_press_course_question_permalink_friendly( $permalink, $lesson_id, $course_id ) {
1440 if ( '' != get_option( 'permalink_structure' ) ) {
1441 if ( preg_match( '!\?lesson=([^\?\&]*)!', $permalink, $matches ) ) {
1442 $permalink = preg_replace(
1443 '!/?\?lesson=([^\?\&]*)!',
1444 '/' . basename( get_permalink( $matches[1] ) ),
1445 untrailingslashit( $permalink )
1446 );
1447 }
1448 }
1449
1450 return $permalink;
1451 }
1452
1453 add_filter( 'learn_press_course_lesson_permalink', 'learn_press_course_lesson_permalink_friendly', 10, 3 );
1454
1455 function learn_press_user_maybe_is_a_teacher( $user = null ) {
1456 if ( ! $user ) {
1457 $user = learn_press_get_current_user();
1458 } elseif ( is_numeric( $user ) ) {
1459 $user = learn_press_get_user( $user );
1460 }
1461 if ( ! $user ) {
1462 return false;
1463 }
1464
1465 $role = $user->has_role( 'administrator' ) ? 'administrator' : false;
1466 if ( ! $role ) {
1467 $role = $user->has_role( 'lp_teacher' ) ? 'lp_teacher' : false;
1468 }
1469
1470 return apply_filters( 'learn-press/user/is-teacher', $role, $user->get_id() );
1471 }
1472
1473 function learn_press_become_teacher_sent( $user_id = 0 ) {
1474 if ( func_num_args() == 0 ) {
1475 $user_id = get_current_user_id();
1476 }
1477
1478 return 'yes' === get_user_meta( $user_id, '_requested_become_teacher', true );
1479 }
1480
1481 function _learn_press_translate_user_roles( $translations, $text, $context, $domain ) {
1482 $plugin_domain = 'learnpress';
1483 $roles = array( 'LP Instructor' );
1484
1485 if ( $context === 'User role' && in_array( $text, $roles ) && $domain !== $plugin_domain ) {
1486 return translate_with_gettext_context( $text, $context, $plugin_domain );
1487 }
1488
1489 return $translations;
1490 }
1491
1492 add_filter( 'gettext_with_context', '_learn_press_translate_user_roles', 10, 4 );
1493
1494 /**
1495 * Modifies the statement $where to make the search works correct
1496 *
1497 * @param string
1498 *
1499 * @return string
1500 * @deprecated 4.1.7.2
1501 */
1502 //function learn_press_posts_where_statement_search( $where ) {
1503 // // gets the global query var object
1504 // global $wp_query, $wpdb;
1505 //
1506 // /**
1507 // * Need to wrap this block into () in order to make it works correctly when filter by specific post type => maybe a bug :)
1508 // * from => ( wp_2_posts.post_status = 'publish' OR wp_2_posts.post_status = 'private') OR wp_2_terms.name LIKE '%s%'
1509 // * to => ( ( wp_2_posts.post_status = 'publish' OR wp_2_posts.post_status = 'private') OR wp_2_terms.name LIKE '%s%' )
1510 // */
1511 // $a = preg_match( '!(' . $wpdb->posts . '.post_status)!', $where );
1512 // $b = preg_match( '!(OR\s+' . $wpdb->terms . '.name LIKE \'%' . $wp_query->get( 's' ) . '%\')!', $where );
1513 //
1514 // if ( $a && $b ) {
1515 // // append ( to the start of the block
1516 // $where = preg_replace( '!(' . $wpdb->posts . '.post_status)!', '( $1', $where, 1 );
1517 //
1518 // // append ) to the end of the block
1519 // $where = preg_replace(
1520 // '!(OR\s+' . $wpdb->terms . '.name LIKE \'%' . $wp_query->get( 's' ) . '%\')!',
1521 // '$1 )',
1522 // $where
1523 // );
1524 // }
1525 // remove_filter( 'posts_where', 'learn_press_posts_where_statement_search', 99 );
1526 //
1527 // return $where;
1528 //}
1529
1530 /**
1531 * Filter post type for search function
1532 * Only search lpr_course if see the param ref=course in request
1533 *
1534 * @param WP_Query $q
1535 * @deprecated 4.1.7.2
1536 */
1537 /*function learn_press_filter_search( $q ) {
1538 if ( $q->is_main_query() && $q->is_search() && ( ! empty( $_REQUEST['ref'] ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course' ) ) {
1539 $q->set( 'post_type', 'lp_course' );
1540
1541 add_filter( 'posts_where', 'learn_press_posts_where_statement_search', 99 );
1542 remove_filter( 'pre_get_posts', 'learn_press_filter_search', 99 );
1543 }
1544 }*/
1545 //add_filter( 'pre_get_posts', 'learn_press_filter_search', 99 );
1546
1547 if ( ! function_exists( 'learn_press_send_json' ) ) {
1548 function learn_press_send_json( $data ) {
1549 echo '<-- LP_AJAX_START -->';
1550 echo wp_json_encode( $data );
1551 echo '<-- LP_AJAX_END -->';
1552 die;
1553 }
1554 }
1555
1556 /**
1557 * Send json with success signal to browser.
1558 *
1559 * @param array|object|WP_Error $data
1560 *
1561 * @since 3.0.1
1562 */
1563 function learn_press_send_json_error( $data = '' ) {
1564 $response = array( 'success' => false );
1565
1566 if ( isset( $data ) ) {
1567 if ( is_wp_error( $data ) ) {
1568 $result = array();
1569 foreach ( $data->errors as $code => $messages ) {
1570 foreach ( $messages as $message ) {
1571 $result[] = array(
1572 'code' => $code,
1573 'message' => $message,
1574 );
1575 }
1576 }
1577
1578 $response['data'] = $result;
1579 } else {
1580 $response['data'] = $data;
1581 }
1582 }
1583
1584 learn_press_send_json( $response );
1585 }
1586
1587 /**
1588 * Send json with error signal to browser.
1589 *
1590 * @param array|object|WP_Error $data
1591 *
1592 * @since 3.0.0
1593 */
1594 function learn_press_send_json_success( $data = '' ) {
1595 $response = array( 'success' => true );
1596
1597 if ( isset( $data ) ) {
1598 $response['data'] = $data;
1599 }
1600
1601 learn_press_send_json( $response );
1602 }
1603
1604 /**
1605 * Check if ajax is calling then send json data.
1606 *
1607 * @param array $data
1608 * @param mixed $callback
1609 *
1610 * @return bool
1611 */
1612 function learn_press_maybe_send_json( $data, $callback = null ) {
1613 if ( learn_press_is_ajax() ) {
1614 is_callable( $callback ) && call_user_func( $callback );
1615 $message = learn_press_get_messages( true );
1616 if ( empty( $data['message'] ) && $message ) {
1617 $data['message'] = $message;
1618 }
1619 learn_press_send_json( $data );
1620 }
1621
1622 return false;
1623 }
1624
1625 /**
1626 * Get data from request.
1627 *
1628 * @param string $key
1629 * @param mixed $default
1630 * @param mixed $hash
1631 *
1632 * @return mixed
1633 */
1634 function learn_press_get_request( $key, $default = null, $hash = null ) {
1635 $return = $default;
1636
1637 if ( $hash ) {
1638 if ( ! empty( $hash[ $key ] ) ) {
1639 $return = $hash[ $key ];
1640 }
1641 } else {
1642 if ( ! empty( $_POST[ $key ] ) ) {
1643 $return = LP_Helper::sanitize_params_submitted( $_POST[ $key ] );
1644 } elseif ( ! empty( $_GET[ $key ] ) ) {
1645 $return = LP_Helper::sanitize_params_submitted( $_GET[ $key ] );
1646 } elseif ( ! empty( $_REQUEST[ $key ] ) ) {
1647 $return = LP_Helper::sanitize_params_submitted( $_REQUEST[ $key ] );
1648 }
1649 }
1650
1651 return $return;
1652 }
1653
1654 /**
1655 * @return mixed
1656 */
1657 function is_learnpress() {
1658 return apply_filters(
1659 'is_learnpress',
1660 ( learn_press_is_course_archive() || learn_press_is_course_taxonomy() || learn_press_is_course() || learn_press_is_quiz() || learn_press_is_search() ) ? true : false
1661 );
1662 }
1663
1664 if ( ! function_exists( 'learn_press_is_search' ) ) {
1665 function learn_press_is_search() {
1666 return array_key_exists( 's', $_REQUEST ) && array_key_exists( 'ref', $_REQUEST ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course';
1667 }
1668 }
1669
1670 if ( ! function_exists( 'learn_press_is_courses' ) ) {
1671 function learn_press_is_courses() {
1672 return learn_press_is_course_archive();
1673 }
1674 }
1675
1676
1677 if ( ! function_exists( 'learn_press_is_course_archive' ) ) {
1678 function learn_press_is_course_archive() {
1679 global $wp_query;
1680
1681 $queried_object_id = ! empty( $wp_query->queried_object ) ? $wp_query->queried_object : 0;
1682 $is_courses = defined( 'LEARNPRESS_IS_COURSES' ) && LEARNPRESS_IS_COURSES;
1683 $is_tag = defined( 'LEARNPRESS_IS_TAG' ) && LEARNPRESS_IS_TAG || is_tax( 'course_tag' );
1684 $is_category = defined( 'LEARNPRESS_IS_CATEGORY' ) && LEARNPRESS_IS_CATEGORY || is_tax( 'course_category' );
1685 $page_id = learn_press_get_page_id( 'courses' );
1686
1687 return ( $is_courses || $is_category || $is_tag ) || is_post_type_archive( 'lp_course' ) || ( $page_id && ( $queried_object_id && is_page( $page_id ) ) );
1688 }
1689 }
1690
1691 if ( ! function_exists( 'learn_press_is_course_tax' ) ) {
1692 function learn_press_is_course_tax() {
1693 return is_tax( get_object_taxonomies( LP_COURSE_CPT ) );
1694 }
1695 }
1696
1697 if ( ! function_exists( 'learn_press_is_course_taxonomy' ) ) {
1698 function learn_press_is_course_taxonomy() {
1699 return ( defined( 'LEARNPRESS_IS_TAX' ) && LEARNPRESS_IS_TAX ) || learn_press_is_course_tax();
1700 }
1701 }
1702
1703
1704 if ( ! function_exists( 'learn_press_is_course_category' ) ) {
1705 function learn_press_is_course_category( $term = '' ) {
1706 return ( defined( 'LEARNPRESS_IS_CATEGORY' ) && LEARNPRESS_IS_CATEGORY ) || is_tax( 'course_category', $term );
1707 }
1708 }
1709
1710
1711 if ( ! function_exists( 'learn_press_is_course_tag' ) ) {
1712 function learn_press_is_course_tag( $term = '' ) {
1713 return ( defined( 'LEARNPRESS_IS_TAG' ) && LEARNPRESS_IS_TAG ) || is_tax( 'course_tag', $term );
1714 }
1715 }
1716
1717 if ( ! function_exists( 'learn_press_is_course' ) ) {
1718 function learn_press_is_course() {
1719 /**
1720 * @var WP_Query $wp_query
1721 */
1722 global $wp_query;
1723
1724 if ( $wp_query->get_queried_object() ) {
1725 return is_singular( array( LP_COURSE_CPT ) );
1726 } else {
1727 return false;
1728 }
1729 }
1730 }
1731
1732 if ( ! function_exists( 'learn_press_is_lesson' ) ) {
1733 function learn_press_is_lesson() {
1734 return is_singular( array( LP_LESSON_CPT ) );
1735 }
1736 }
1737
1738 if ( ! function_exists( 'learn_press_is_quiz' ) ) {
1739 function learn_press_is_quiz() {
1740 return is_singular( array( LP_QUIZ_CPT ) );
1741 }
1742 }
1743
1744 function lp_content_has_shortcode( $tag = '' ) {
1745 global $post;
1746
1747 return is_singular() && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
1748 }
1749
1750 /**
1751 * Returns true when viewing profile page.
1752 *
1753 * @return bool
1754 */
1755 function learn_press_is_profile() {
1756 $page_id = learn_press_get_page_id( 'profile' );
1757
1758 if ( $page_id && is_page( $page_id ) || lp_content_has_shortcode( 'learn_press_profile' ) ) {
1759 return true;
1760 }
1761
1762 return apply_filters( 'learn-press/is-profile', false );
1763 }
1764
1765 /**
1766 * Return true if user is in checking out page
1767 *
1768 * @return bool
1769 */
1770 function learn_press_is_checkout() {
1771 $page_id = learn_press_get_page_id( 'checkout' );
1772
1773 if ( $page_id && is_page( $page_id ) ) {
1774 return true;
1775 }
1776
1777 return apply_filters( 'learn-press/is-checkout', false );
1778 }
1779
1780 /**
1781 * Return register permalink
1782 *
1783 * @return mixed
1784 */
1785 function learn_press_get_register_url() {
1786 return apply_filters( 'learn_press_register_url', wp_registration_url() );
1787 }
1788
1789 /**
1790 * Add a new notice into queue
1791 *
1792 * @param string
1793 * @param string
1794 *
1795 * @return mixed
1796 */
1797 function learn_press_add_notice( $message, $type = 'updated' ) {
1798 LP_Admin_Notice::instance()->add( $message, $type );
1799 }
1800
1801 /**
1802 * Set user's cookie
1803 *
1804 * @param $name
1805 * @param $value
1806 * @param int $expire
1807 * @param bool $secure
1808 *
1809 * @editor tungnx
1810 * @version 1.0.2
1811 */
1812 function learn_press_setcookie( $name, $value, $expire = 0, $secure = false, $httponly = false ) {
1813 $secure = ( 'https' === parse_url( wp_login_url(), PHP_URL_SCHEME ) );
1814
1815 @setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, $httponly );
1816 }
1817
1818 /**
1819 * Clear cookie
1820 *
1821 * @param $name
1822 */
1823 function learn_press_remove_cookie( $name ) {
1824 setcookie( $name, '', time() - YEAR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN );
1825
1826 if ( array_key_exists( $name, $_COOKIE ) ) {
1827 unset( $_COOKIE[ $name ] );
1828 }
1829 }
1830
1831 /**
1832 * Filter the login url so third-party can be customize
1833 *
1834 * @param string $redirect
1835 *
1836 * @return mixed
1837 */
1838 function learn_press_get_login_url( $redirect = null ) {
1839 $url = wp_login_url( $redirect );
1840 $profile_page = learn_press_get_page_link( 'profile' );
1841
1842 if ( 'yes' === LP_Settings::instance()->get( 'enable_login_profile' ) && $profile_page ) {
1843 $parse_url = parse_url( $url );
1844 $url = $profile_page . ( ! empty( $parse_url['query'] ) ? '?' . $parse_url['query'] : '' );
1845 }
1846
1847 return apply_filters( 'learn-press/login-url', $url );
1848 }
1849
1850 /**
1851 * Add variable to an url by checking the permalink structure.
1852 *
1853 * @param string $name
1854 * @param string $value
1855 * @param string $url
1856 *
1857 * @return string
1858 */
1859 function learn_press_get_endpoint_url( $name, $value, $url ) {
1860 if ( ! $url ) {
1861 $url = get_permalink();
1862 }
1863
1864 // Map endpoint to options
1865 $name = isset( LearnPress::instance()->query_vars[ $name ] ) ? LearnPress::instance()->query_vars[ $name ] : $name;
1866
1867 if ( get_option( 'permalink_structure' ) ) {
1868 if ( strstr( $url, '?' ) ) {
1869 $query_string = '?' . parse_url( $url, PHP_URL_QUERY );
1870 $url = current( explode( '?', $url ) );
1871 } else {
1872 $query_string = '';
1873 }
1874 $url = trailingslashit( $url ) . ( $name ? $name . '/' : '' ) . $value . $query_string;
1875
1876 } else {
1877 $url = esc_url_raw( add_query_arg( $name, $value, $url ) );
1878 }
1879
1880 return apply_filters( 'learn_press_get_endpoint_url', esc_url_raw( $url ), $name, $value, $url );
1881 }
1882
1883 /**
1884 * Add all endpoints from settings to the pages.
1885 */
1886 function learn_press_add_endpoints() {
1887 $settings = LP_Settings::instance();
1888
1889 $endpoints = $settings->get_checkout_endpoints();
1890 if ( $endpoints ) {
1891 foreach ( $endpoints as $endpoint => $value ) {
1892 LearnPress::instance()->query_vars[ $endpoint ] = $value;
1893 add_rewrite_endpoint( $value, EP_PAGES );
1894 }
1895 }
1896
1897 $endpoints = $settings->get_profile_endpoints();
1898 if ( $endpoints ) {
1899 foreach ( $endpoints as $endpoint => $value ) {
1900 LearnPress::instance()->query_vars[ $endpoint ] = $value;
1901 add_rewrite_endpoint( $value, EP_PAGES );
1902 }
1903 }
1904
1905 $endpoints = $settings->get( 'quiz_endpoints' );
1906 if ( $endpoints ) {
1907 foreach ( $endpoints as $endpoint => $value ) {
1908 $endpoint = preg_replace( '!_!', '-', $endpoint );
1909 LearnPress::instance()->query_vars[ $endpoint ] = $value;
1910 add_rewrite_endpoint(
1911 $value, /*EP_ROOT | */
1912 EP_PAGES
1913 );
1914 }
1915 }
1916 }
1917
1918 add_action( 'init', 'learn_press_add_endpoints' );
1919
1920 function learn_press_is_yes( $value ) {
1921 return ( $value === 1 ) || ( $value === '1' ) || ( $value == 'yes' ) || ( $value == true ) || ( $value == 'on' );
1922 }
1923
1924 /**
1925 * @param mixed $value
1926 *
1927 * @return bool
1928 */
1929 function _is_false_value( $value ) {
1930 if ( is_numeric( $value ) ) {
1931 return $value == 0;
1932 } elseif ( is_string( $value ) ) {
1933 return ( empty( $value ) || is_null( $value ) || in_array( $value, array( 'no', 'off', 'false' ) ) );
1934 }
1935
1936 return ! ! $value;
1937 }
1938
1939 /**
1940 * Map the query vars from LP to query vars of WP core
1941 * when WP parse the requesting.
1942 */
1943 function learn_press_parse_request() {
1944 global $wp;
1945
1946 // Map query vars to their keys, or get them if endpoints are not supported
1947 foreach ( LearnPress::instance()->query_vars as $key => $var ) {
1948 if ( isset( $_GET[ $var ] ) ) {
1949 $wp->query_vars[ $key ] = LP_Helper::sanitize_params_submitted( $_GET[ $var ] ?? '' );
1950 } elseif ( isset( $wp->query_vars[ $var ] ) ) {
1951 $wp->query_vars[ $key ] = LP_Helper::sanitize_params_submitted( $wp->query_vars[ $var ] ?? '' );
1952 }
1953 }
1954 }
1955
1956 add_action( 'parse_request', 'learn_press_parse_request' );
1957
1958 if ( ! function_exists( 'learn_press_reset_auto_increment' ) ) {
1959 /**
1960 * Reset AUTO INCREMENT of the table.
1961 *
1962 * @param $table
1963 */
1964 function learn_press_reset_auto_increment( $table ) {
1965 global $wpdb;
1966 $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}$table AUTO_INCREMENT = %d", 1 ) );
1967 }
1968 }
1969
1970 /**
1971 * @param string $handle
1972 * @param bool $hash
1973 *
1974 * @return string
1975 * @depecated 4.1.7.2
1976 */
1977 /*function learn_press_get_log_file_path( $handle, $hash = false ) {
1978 if ( $hash ) {
1979 $hash = '-' . sanitize_file_name( wp_hash( $handle ) );
1980 }
1981
1982 return trailingslashit( LP_LOG_PATH ) . $handle . $hash . '.log';
1983 }*/
1984
1985 /**
1986 * Get the cart object in checkout page
1987 *
1988 * @return LP_Cart
1989 */
1990 function learn_press_get_checkout_cart() {
1991 return apply_filters( 'learn_press_checkout_cart', LearnPress::instance()->cart );
1992 }
1993
1994 /*function learn_press_front_scripts() {
1995 if ( is_admin() ) {
1996 return;
1997 }
1998 $js = array(
1999 'ajax' => admin_url( 'admin-ajax.php' ),
2000 'plugin_url' => LearnPress::instance()->plugin_url(),
2001 'siteurl' => home_url(),
2002 'current_url' => learn_press_get_current_url(),
2003 'localize' => array(
2004 'button_ok' => __( 'OK', 'learnpress' ),
2005 'button_cancel' => __( 'Cancel', 'learnpress' ),
2006 'button_yes' => __( 'Yes', 'learnpress' ),
2007 'button_no' => __( 'No', 'learnpress' ),
2008 ),
2009 );
2010 foreach ( $js as $k => $v ) {
2011 LP_Assets::add_param( $k, $v, array( 'learn-press-single-course', 'learn-press-global' ), 'LP_Settings' );
2012 }
2013 }
2014
2015 add_action( 'wp_print_scripts', 'learn_press_front_scripts' );*/
2016
2017 function learn_press_user_time( $time, $format = 'timestamp' ) {
2018 if ( is_string( $time ) ) {
2019 $time = @strtotime( $time );
2020 }
2021 $time = $time + ( get_option( 'gmt_offset' ) - $_COOKIE['timezone'] / 60 ) * HOUR_IN_SECONDS;
2022 switch ( $format ) {
2023 case 'timestamp':
2024 return $time;
2025 default:
2026 return date( 'Y-m-d H:i:s', $time );
2027 }
2028 }
2029
2030 function learn_press_get_current_version() {
2031 $data = get_plugin_data( LP_PLUGIN_FILE, $markup = true, $translate = true );
2032
2033 return $data['Version'];
2034 }
2035
2036 /**
2037 * Get current tab is displaying in user profile.
2038 * If there is no tab then get the first tab in
2039 * the list of tabs.
2040 *
2041 * @param bool $default
2042 *
2043 * @return mixed|string
2044 */
2045 function learn_press_get_current_profile_tab( $default = true ) {
2046 global $wp_query, $wp;
2047 $current = '';
2048
2049 if ( ! empty( $_REQUEST['tab'] ) ) {
2050 $current = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] );
2051 } elseif ( ! empty( $wp_query->query_vars['tab'] ) ) {
2052 $current = $wp_query->query_vars['tab'];
2053 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
2054 $current = $wp->query_vars['view'];
2055 } else {
2056 $tabs = learn_press_get_user_profile_tabs();
2057 if ( $default && $tabs ) {
2058 // Fixed for array_keys does not work with ArrayAccess instance
2059 if ( $tabs instanceof LP_Profile_Tabs ) {
2060 $tabs = $tabs->tabs();
2061 }
2062
2063 $tab_keys = array_keys( $tabs );
2064 $current = reset( $tab_keys );
2065 }
2066 }
2067
2068 return $current;
2069 }
2070
2071 add_action( 'init', 'learn_press_get_current_profile_tab' );
2072
2073 function learn_press_profile_tab_exists( $tab ) {
2074 $tabs = learn_press_get_user_profile_tabs();
2075
2076 if ( $tabs ) {
2077 return ! empty( $tabs[ $tab ] ) ? true : false;
2078 }
2079
2080 return false;
2081 }
2082
2083 /**
2084 * Replace the spacing with the + (plus) char.
2085 *
2086 * @param string $string
2087 *
2088 * @return string
2089 * @depecated 4.1.7.2
2090 */
2091 /*function _learn_press_urlencode( $string ) {
2092 return preg_replace( '/\s/', '+', $string );
2093 }*/
2094
2095 /**
2096 * Point the archive post type link to course page if current
2097 * post type is course and the page for displaying course is
2098 * setup.
2099 *
2100 * @param string $link
2101 * @param string $post_type
2102 *
2103 * @return string
2104 * @depecated 4.1.7.2
2105 */
2106 /*function learn_press_post_type_archive_link( $link, $post_type ) {
2107 if ( $post_type == LP_COURSE_CPT && learn_press_get_page_id( 'courses' ) ) {
2108 $link = learn_press_get_page_link( 'courses' );
2109 }
2110
2111 return $link;
2112 }*/
2113 //add_filter( 'post_type_archive_link', 'learn_press_post_type_archive_link', 10, 2 );
2114
2115 function learn_press_single_term_title( $prefix = '', $display = true ) {
2116 $term = get_queried_object();
2117
2118 if ( ! $term ) {
2119 return '';
2120 }
2121
2122 if ( learn_press_is_course_category() ) {
2123 $term_name = apply_filters( 'single_course_category_title', $term->name );
2124 } elseif ( learn_press_is_course_tag() ) {
2125 $term_name = apply_filters( 'single_course_tag_title', $term->name );
2126 } elseif ( learn_press_is_course_taxonomy() ) {
2127 $term_name = apply_filters( 'single_course_term_title', $term->name );
2128 } else {
2129 return single_term_title( $prefix, $display );
2130 }
2131
2132 if ( empty( $term_name ) ) {
2133 return single_term_title( $prefix, $display );
2134 }
2135
2136 if ( $display ) {
2137 echo wp_kses_post( $prefix . $term_name );
2138 }
2139
2140 return $prefix . $term_name;
2141 }
2142
2143 /**
2144 * Control the template file if user is searching course.
2145 * Use the template of archive course to display the
2146 * result if there is a flag in request to search course.
2147 *
2148 * @param string $template
2149 *
2150 * @return string
2151 */
2152 function learn_press_search_template( $template ) {
2153 if ( ! empty( $_REQUEST['ref'] ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course' ) {
2154 $template = learn_press_locate_template( 'archive-course.php' );
2155 }
2156
2157 return $template;
2158 }
2159
2160 /**
2161 * Auto enroll user to a course after an order is completed
2162 * if the option auto-enroll is turn on.
2163 *
2164 * @param int $order_id
2165 *
2166 * @return mixed
2167 * @editor tungnx
2168 */
2169 function learn_press_auto_enroll_user_to_courses( $order_id ) {
2170 _deprecated_function( __FUNCTION__, '4.1.3' );
2171 }
2172
2173 // add_action( 'learn_press_order_status_completed', 'learn_press_auto_enroll_user_to_courses' );
2174
2175 /**
2176 * Return true if enable cart
2177 *
2178 * @return bool
2179 */
2180 function learn_press_is_enable_cart() {
2181 return defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART == true;
2182 }
2183
2184 /**
2185 * Short way to get checkout object
2186 *
2187 * @param array
2188 *
2189 * @return LP_Checkout
2190 */
2191 function learn_press_get_checkout( $args = null ) {
2192 $checkout = LP_Checkout::instance();
2193
2194 if ( is_array( $args ) ) {
2195 foreach ( $args as $k => $v ) {
2196 $checkout->{$k} = $v;
2197 }
2198 }
2199
2200 return $checkout;
2201 }
2202
2203 if ( defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART ) {
2204 add_filter( 'learn_press_checkout_settings', '_learn_press_cart_settings', 10, 2 );
2205 function _learn_press_cart_settings( $settings, $class ) {
2206 $settings = array_merge(
2207 $settings,
2208 array(
2209 array(
2210 'title' => __( 'Cart', 'learnpress' ),
2211 'type' => 'title',
2212 ),
2213 array(
2214 'title' => __( 'Enable cart', 'learnpress' ),
2215 'desc' => __(
2216 'Check this option to enable user purchase multiple courses at one time.',
2217 'learnpress'
2218 ),
2219 'id' => $class->get_field_name( 'enable_cart' ),
2220 'default' => 'yes',
2221 'type' => 'checkbox',
2222 ),
2223 array(
2224 'title' => __( 'Add to cart redirect', 'learnpress' ),
2225 'desc' => __( 'Redirect to checkout immediately after adding course to cart.', 'learnpress' ),
2226 'id' => $class->get_field_name( 'redirect_after_add' ),
2227 'default' => 'yes',
2228 'type' => 'checkbox',
2229 ),
2230 array(
2231 'title' => __( 'AJAX add to cart', 'learnpress' ),
2232 'desc' => __( 'Using AJAX to add course to cart.', 'learnpress' ),
2233 'id' => $class->get_field_name( 'ajax_add_to_cart' ),
2234 'default' => 'no',
2235 'type' => 'checkbox',
2236 ),
2237 array(
2238 'title' => __( 'Cart page', 'learnpress' ),
2239 'id' => $class->get_field_name( 'cart_page_id' ),
2240 'default' => '',
2241 'type' => 'pages-dropdown',
2242 ),
2243 )
2244 );
2245
2246 return $settings;
2247 }
2248 } else {
2249 add_filter( 'learn_press_enable_cart', '_learn_press_enable_cart', 1000 );
2250 function _learn_press_enable_cart( $r ) {
2251 return false;
2252 }
2253
2254 add_filter( 'learn_press_get_template', '_learn_press_enroll_button', 1000, 5 );
2255 function _learn_press_enroll_button( $located, $template_name, $args, $template_path, $default_path ) {
2256 if ( $template_name == 'single-course/enroll-button.php' ) {
2257 $located = learn_press_locate_template(
2258 'single-course/enroll-button-new.php',
2259 $template_path,
2260 $default_path
2261 );
2262 }
2263
2264 return $located;
2265 }
2266 }
2267
2268 /**
2269 * Returns checkout url from setting
2270 *
2271 * @return string
2272 */
2273 function learn_press_get_checkout_url() {
2274 $checkout_url = learn_press_get_page_link( 'checkout' );
2275
2276 return apply_filters( 'learn_press_get_checkout_url', $checkout_url );
2277 }
2278
2279 /**
2280 * @return string
2281 */
2282 function learn_press_checkout_needs_payment() {
2283 return LearnPress::instance()->cart->needs_payment();
2284 }
2285
2286 /**
2287 * Return plugin basename
2288 *
2289 * @param string $filepath
2290 *
2291 * @return string
2292 */
2293 /*
2294 function learn_press_plugin_basename( $filepath ) {
2295 $file = str_replace( '\\', '/', $filepath );
2296 $file = preg_replace( '|/+|', '/', $file );
2297 $plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR );
2298 $plugin_dir = preg_replace( '|/+|', '/', $plugin_dir );
2299 $mu_plugin_dir = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
2300 $mu_plugin_dir = preg_replace( '|/+|', '/', $mu_plugin_dir );
2301 $sp_plugin_dir = dirname( $filepath );
2302 $sp_plugin_dir = dirname( $sp_plugin_dir );
2303 $sp_plugin_dir = str_replace( '\\', '/', $sp_plugin_dir );
2304 $sp_plugin_dir = preg_replace( '|/+|', '/', $sp_plugin_dir );
2305
2306 $file = preg_replace(
2307 '#^' . preg_quote( $sp_plugin_dir, '#' ) . '/|^' . preg_quote(
2308 $plugin_dir,
2309 '#'
2310 ) . '/|^' . preg_quote(
2311 $mu_plugin_dir,
2312 '#'
2313 ) . '/#',
2314 '',
2315 $file
2316 );
2317 $file = trim( $file, '/' );
2318
2319 return strtolower( $file );
2320 }*/
2321
2322 /**
2323 * Update log data for each LP version into wp option.
2324 *
2325 * @param string $version
2326 * @param mixed $data
2327 */
2328 function learn_press_update_log( $version, $data ) {
2329 $logs = get_option( 'learn_press_update_logs' );
2330 if ( ! $logs ) {
2331 $logs = array( $version => $data );
2332 } else {
2333 $logs[ $version ] = $data;
2334 }
2335 update_option( 'learn_press_update_logs', $logs );
2336 }
2337
2338 /**
2339 * Output variables to screen for debugging.
2340 */
2341 function learn_press_debug() {
2342 $args = func_get_args();
2343 $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
2344
2345 echo '<pre>';
2346 print_r( $debug );
2347 $arg = false;
2348
2349 if ( $args ) {
2350 foreach ( $args as $arg ) {
2351 echo "\n======LearnPress Debug=======\n";
2352 print_r( $arg );
2353 echo "\n=============================\n";
2354 }
2355 }
2356 echo '</pre>';
2357
2358 if ( true === $arg ) {
2359 die( __FUNCTION__ );
2360 }
2361 }
2362
2363 /**
2364 * Get current time to user for calculate remaining time of quiz.
2365 *
2366 * @return int
2367 * @depecated 4.1.7.2
2368 */
2369 /*function learn_press_get_current_time() {
2370 $current_time = apply_filters( 'learn_press_get_current_time', 0 );
2371
2372 if ( $current_time > 0 ) {
2373 return $current_time;
2374 }
2375
2376 $a = current_time( 'timestamp' );
2377 $b = time();
2378 $c = current_time( 'mysql' );
2379 $d = strtotime( $c );
2380
2381 if ( $d == $a ) {
2382 return $a;
2383 } else {
2384 return $b;
2385 }
2386 }*/
2387
2388 function learn_press_get_requested_post_type() {
2389 global $pagenow;
2390 if ( $pagenow == 'post-new.php' && ! empty( $_REQUEST['post_type'] ) ) {
2391 $post_type = LP_Helper::sanitize_params_submitted( $_REQUEST['post_type'] );
2392 } else {
2393 $post_id = learn_press_get_post();
2394 $post_type = learn_press_get_post_type( $post_id );
2395 }
2396
2397 return $post_type;
2398 }
2399
2400 /**
2401 * Get human string from grade slug.
2402 *
2403 * @param string $slug
2404 *
2405 * @return string
2406 */
2407 function learn_press_get_graduation_text( $slug ) {
2408 switch ( $slug ) {
2409 case 'passed':
2410 $text = esc_html__( 'Passed', 'learnpress' );
2411 break;
2412 case 'failed':
2413 $text = esc_html__( 'Failed', 'learnpress' );
2414 break;
2415 case 'in-progress':
2416 $text = esc_html__( 'In Progress', 'learnpress' );
2417 break;
2418 default:
2419 $text = $slug;
2420 }
2421
2422 return apply_filters( 'learn-press/get-graduation-text', $text, $slug );
2423 }
2424
2425 if ( ! function_exists( 'learn_press_is_negative_value' ) ) {
2426 function learn_press_is_negative_value( $value ) {
2427 $return = in_array( $value, array( 'no', 'off', 'false', '0' ) ) || ! $value || $value == '' || $value == null;
2428
2429 return $return;
2430 }
2431 }
2432
2433 /**
2434 * Filter to comment reply link to fix bug the link is invalid for
2435 * lesson or quiz.
2436 *
2437 * @param string $link
2438 * @param array $args
2439 * @param WP_Comment $comment
2440 * @param WP_Post $post
2441 *
2442 * @return string
2443 */
2444 function learn_press_comment_reply_link( $link, $args = array(), $comment = null, $post = null ) {
2445
2446 $post_type = learn_press_get_post_type( $post );
2447
2448 if ( ! learn_press_is_support_course_item_type( $post_type ) ) {
2449 return $link;
2450 }
2451
2452 $course_item = LP_Global::course_item();
2453
2454 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
2455 $link = sprintf(
2456 '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
2457 esc_url_raw( wp_login_url( get_permalink() ) ),
2458 $args['login_text']
2459 );
2460 } elseif ( $course_item ) {
2461 $onclick = sprintf(
2462 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
2463 $args['add_below'],
2464 $comment->comment_ID,
2465 $args['respond_id'],
2466 $post->ID
2467 );
2468
2469 $link = sprintf(
2470 "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
2471 esc_url_raw(
2472 add_query_arg(
2473 array(
2474 'replytocom' => $comment->comment_ID,
2475 ),
2476 $course_item->get_permalink()
2477 )
2478 ) . '#' . $args['respond_id'],
2479 $onclick,
2480 esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
2481 $args['reply_text']
2482 );
2483 }
2484
2485 return $link;
2486 }
2487
2488 add_filter( 'comment_reply_link', 'learn_press_comment_reply_link', 10, 4 );
2489
2490 /**
2491 * Sanitize content of tooltip
2492 *
2493 * @param string $tooltip
2494 * @param bool $html
2495 *
2496 * @return string
2497 */
2498 function learn_press_sanitize_tooltip( $tooltip, $html = false ) {
2499 if ( $html ) {
2500 $tooltip = htmlspecialchars(
2501 wp_kses(
2502 html_entity_decode( $tooltip ),
2503 array(
2504 'br' => array(),
2505 'em' => array(),
2506 'strong' => array(),
2507 'small' => array(),
2508 'span' => array(),
2509 'ul' => array(),
2510 'li' => array(),
2511 'ol' => array(),
2512 'p' => array(),
2513 )
2514 )
2515 );
2516 } else {
2517 $tooltip = esc_attr( $tooltip );
2518 }
2519
2520 return $tooltip;
2521 }
2522
2523 function learn_press_tooltip( $tooltip, $html = false ) {
2524 $tooltip = learn_press_sanitize_tooltip( $tooltip, $html );
2525 echo '<span class="learn-press-tooltip" data-tooltip="' . esc_attr( $tooltip ) . '"></span>';
2526 }
2527
2528 /**
2529 * Get timezone offset from wp settings.
2530 *
2531 * @return float|int
2532 * @since 3.0.0
2533 * @depecated 4.1.7.2
2534 */
2535 /*function learn_press_timezone_offset() {
2536 if ( $tz = get_option( 'timezone_string' ) ) {
2537 $timezone = new DateTimeZone( $tz );
2538
2539 return $timezone->getOffset( new DateTime( 'now' ) );
2540 } else {
2541 return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS;
2542 }
2543 }*/
2544
2545 /**
2546 * Get default static pages of LP.
2547 *
2548 * @return array
2549 *
2550 * @since 3.0.0
2551 */
2552 function learn_press_static_page_ids() {
2553 $pages = LP_Object_Cache::get( 'static-page-ids', 'learn-press' );
2554
2555 if ( false === $pages ) {
2556 $pages = array(
2557 'checkout' => learn_press_get_page_id( 'checkout' ),
2558 'courses' => learn_press_get_page_id( 'courses' ),
2559 'profile' => learn_press_get_page_id( 'profile' ),
2560 'become_a_teacher' => learn_press_get_page_id( 'become_a_teacher' ),
2561 );
2562
2563 foreach ( $pages as $name => $id ) {
2564 if ( ! get_post( $id ) ) {
2565 $pages[ $name ] = 0;
2566 }
2567 }
2568
2569 LP_Object_Cache::set( 'static-page-ids', $pages, 'learn-press' );
2570 }
2571
2572 return apply_filters( 'learn-press/static-page-ids', $pages );
2573 }
2574
2575 /**
2576 * Get default static pages of LP.
2577 *
2578 * @param bool $name - Optional. TRUE will return name only.
2579 *
2580 * @return array
2581 *
2582 * @since 3.0.0
2583 */
2584 function learn_press_static_pages( $name = false ) {
2585 $pages = apply_filters(
2586 'learn-press/static-pages',
2587 array(
2588 'checkout' => _x( 'Checkout', 'static-page-name', 'learnpress' ),
2589 'courses' => _x( 'Courses', 'static-page-name', 'learnpress' ),
2590 'profile' => _x( 'Profile', 'static-page-name', 'learnpress' ),
2591 'become_a_teacher' => _x( 'Become a Teacher', 'static-page-name', 'learnpress' ),
2592 )
2593 );
2594
2595 if ( $name ) {
2596 return array_keys( $pages );
2597 }
2598
2599 return $pages;
2600 }
2601
2602 /**
2603 * Callback function for sorting to array|object by key|prop priority.
2604 *
2605 * @param array|object $a
2606 * @param array|object $b
2607 *
2608 * @return int
2609 * @since 3.0.0
2610 */
2611 function learn_press_sort_list_by_priority_callback( $a, $b ) {
2612 $a_priority = null;
2613 $b_priority = null;
2614
2615 if ( is_array( $a ) && array_key_exists( 'priority', $a ) ) {
2616 $a_priority = $a['priority'];
2617 } elseif ( is_object( $a ) ) {
2618 if ( is_callable( array( $a, 'get_priority' ) ) ) {
2619 $a_priority = $a->get_priority();
2620 } elseif ( property_exists( $a, 'priority' ) ) {
2621 $a_priority = $a->priority;
2622 }
2623 }
2624
2625 if ( is_array( $b ) && array_key_exists( 'priority', $b ) ) {
2626 $b_priority = $b['priority'];
2627 } elseif ( is_object( $b ) ) {
2628 if ( is_callable( array( $b, 'get_priority' ) ) ) {
2629 $b_priority = $b->get_priority();
2630 } elseif ( property_exists( $b, 'priority' ) ) {
2631 $b_priority = $b->priority;
2632 }
2633 }
2634
2635 if ( $a_priority === $b_priority ) {
2636 return 0;
2637 }
2638
2639 return ( $a_priority < $b_priority ) ? - 1 : 1;
2640 }
2641
2642 /**
2643 * Localize date with custom format.
2644 *
2645 * @param string $timestamp
2646 * @param string $format
2647 * @param bool $gmt
2648 *
2649 * @return string
2650 * @since 3.0.0
2651 */
2652 function learn_press_date_i18n( $timestamp = '', $format = '', $gmt = false ) {
2653 if ( ! $format ) {
2654 $format = get_option( 'date_format' );
2655 }
2656
2657 return date_i18n( $format, $timestamp, $gmt );
2658 }
2659
2660 /**
2661 * Get item types of course support for blocking. Default is lp_lesson
2662 *
2663 * @return array
2664 * @since 3.0.0
2665 */
2666 function learn_press_get_block_course_item_types() {
2667 return apply_filters( 'learn-press/block-course-item-types', array( LP_LESSON_CPT, LP_QUIZ_CPT ) );
2668 }
2669
2670 /**
2671 * Get post type of a post from cache.
2672 * If there is no data stored in cache then
2673 * get it from WP API.
2674 *
2675 * @param int|WP_Post $post
2676 *
2677 * @return string
2678 * @since 3.1.0
2679 */
2680 function learn_press_get_post_type( $post ) {
2681 $post_types = LP_Object_Cache::get( 'post-types', 'learn-press' );
2682
2683 if ( false === $post_types ) {
2684 $post_types = array();
2685 }
2686
2687 if ( is_object( $post ) ) {
2688 $post_id = $post->ID;
2689 } else {
2690 $post_id = absint( $post );
2691 }
2692
2693 if ( empty( $post_types[ $post_id ] ) ) {
2694 $post_type = get_post_type( $post_id );
2695 $post_types[ $post_id ] = $post_type;
2696 LP_Object_Cache::set( 'post-types', $post_types, 'learn-press' );
2697 } else {
2698 $post_type = $post_types[ $post_id ];
2699 }
2700
2701 return $post_type;
2702 }
2703
2704 /**
2705 * Add post type of a post into cache
2706 *
2707 * @param int|array $id
2708 * @param string $type
2709 *
2710 * @since 3.1.0
2711 * @depecated 4.1.7.2
2712 */
2713 /*function learn_press_cache_add_post_type( $id, $type = '' ) {
2714 if ( false === ( $post_types = LP_Object_Cache::get( 'post-types', 'learn-press' ) ) ) {
2715 $post_types = array();
2716 }
2717
2718 if ( func_num_args() == 1 && is_array( $id ) ) {
2719 $post_types = $post_types + $id;
2720 } else {
2721 $post_types[ $id ] = $type;
2722 }
2723
2724 LP_Object_Cache::set( 'post-types', $post_types, 'learn-press' );
2725 }*/
2726
2727 /**
2728 * @depecated 4.1.7.2
2729 */
2730 /*function learn_press_has_option( $name ) {
2731 global $wpdb;
2732
2733 $query = $wpdb->prepare( "SELECT option_id FROM {$wpdb->options} WHERE option_name = %s", $name );
2734
2735 return $wpdb->get_var( $query ) > 0;
2736 }*/
2737
2738 /**
2739 * Update option to enable shuffle themes for ad.
2740 *
2741 * @since 3.2.1
2742 */
2743 function _learn_press_schedule_enable_shuffle_themes() {
2744 update_option( 'learn_press_ad_shuffle_themes', 'yes' );
2745 }
2746
2747 add_action( 'learn-press/schedule-enable-shuffle-themes', '_learn_press_schedule_enable_shuffle_themes' );
2748
2749 /**
2750 * @depecated 4.1.7.2
2751 */
2752 /*function learn_press_show_log() {
2753 if ( trim( LP_Request::get( 'show_log' ) ) === md5( AUTH_KEY ) ) {
2754 call_user_func_array( 'learn_press_debug', func_get_args() );
2755 }
2756 }*/
2757
2758 /**
2759 * @return array
2760 * @since 3.2.6
2761 */
2762 function learn_press_global_script_params() {
2763 $js = array(
2764 'ajax' => admin_url( 'admin-ajax.php' ),
2765 'plugin_url' => LearnPress::instance()->plugin_url(),
2766 'siteurl' => home_url(),
2767 'current_url' => learn_press_get_current_url(),
2768 'theme' => get_stylesheet(),
2769 'localize' => array(
2770 'button_ok' => __( 'OK', 'learnpress' ),
2771 'button_cancel' => __( 'Cancel', 'learnpress' ),
2772 'button_yes' => __( 'Yes', 'learnpress' ),
2773 'button_no' => __( 'No', 'learnpress' ),
2774 ),
2775 'root' => esc_url_raw( rest_url() ),
2776 'nonce' => wp_create_nonce( 'wp_rest' ),
2777 );
2778
2779 return $js;
2780 }
2781
2782 /**
2783 * Get url for setup cron job on server.
2784 *
2785 * @return string
2786 * @since 3.3.0
2787 * @depecated 4.1.7.2
2788 */
2789 /*function learn_press_get_cron_url() {
2790 $nonce = get_option( 'learnpress_cron_url_nonce' );
2791
2792 if ( ! $nonce ) {
2793 $nonce = md5( microtime( true ) );
2794 update_option( 'learnpress_cron_url_nonce', $nonce );
2795 }
2796 $url = add_query_arg(
2797 array(
2798 'lp-ajax' => 'cron',
2799 'sid' => $nonce,
2800 ),
2801 get_home_url()
2802 );
2803
2804 return $url;
2805 }*/
2806
2807 /**
2808 * Get courses expired.
2809 *
2810 * @return array
2811 * @since 3.3.0
2812 * @depecated 4.1.7.2
2813 */
2814 /*function learn_press_get_expired_courses() {
2815 global $wpdb;
2816
2817 $query = $wpdb->prepare(
2818 "
2819 SELECT X.*
2820 FROM(
2821 SELECT ui.*
2822 FROM {$wpdb->learnpress_user_items} ui
2823 LEFT JOIN {$wpdb->learnpress_user_items} uix
2824 ON ui.item_id = uix.item_id
2825 AND ui.user_id = uix.user_id
2826 AND ui.user_item_id < uix.user_item_id
2827 WHERE uix.user_item_id IS NULL
2828 ) X
2829 INNER JOIN {$wpdb->users} u ON u.ID = X.user_id
2830 INNER JOIN {$wpdb->posts} p ON p.ID = X.item_id
2831 WHERE X.item_type = %s
2832 AND X.status = %s
2833 #AND expiration_time_gmt <= UTC_TIMESTAMP()
2834 AND expiration_time <= UTC_TIMESTAMP()
2835 LIMIT 0, 10
2836 ",
2837 LP_COURSE_CPT,
2838 'enrolled'
2839 );
2840
2841 }*/
2842
2843 /**
2844 * Return list types of questions that support answer options.
2845 *
2846 * @return array
2847 * @since 3.3.0
2848 */
2849 function learn_press_get_question_support_answer_options() {
2850 $questions = learn_press_get_question_support_feature( 'answer-options' );
2851
2852 return apply_filters( 'learn-press/questions-support-answer-options', $questions );
2853 }
2854
2855 /**
2856 * Return list types of question that support a feature.
2857 *
2858 * @param string $feature
2859 *
2860 * @return array
2861 * @since 3.3.0
2862 */
2863 function learn_press_get_question_support_feature( $feature ) {
2864 $questions = array();
2865 $types = LP_Global::get_object_supports( 'question' );
2866
2867 if ( $types ) {
2868 foreach ( $types as $type => $features ) {
2869 if ( array_key_exists( $feature, $features ) ) {
2870 $questions[] = $type;
2871 }
2872 }
2873 }
2874
2875 return $questions;
2876 }
2877
2878 /**
2879 * Helper function to output html for rendering a 'circle progress bar'
2880 *
2881 * @param int $percent
2882 * @param int $width
2883 * @param int $border
2884 * @param string $color
2885 *
2886 * @since 3.3.0
2887 * @depecated 4.1.7.2
2888 */
2889 /*function learn_press_circle_progress_html( $percent = 0, $width = 32, $border = 4, $color = '' ) {
2890 $radius = $width / 2;
2891 $r = ( $width - $border ) / 2;
2892 $circumference = $r * 2 * pi();
2893 $offset = $circumference - $percent / 100 * $circumference;
2894
2895 printf(
2896 '<svg class="circle-progress-bar" width="%d" height="%d">
2897 <circle class="circle-progress-bar__circle"
2898 stroke="%s"
2899 stroke-width="%d"
2900 style="stroke-dasharray:%s %s; stroke-dashoffset:%s;"
2901 fill="transparent"
2902 r="%d" cx="%d" cy="%d"></circle>
2903 </svg>',
2904 $width,
2905 $width,
2906 $color,
2907 $border,
2908 $circumference,
2909 $circumference,
2910 $offset,
2911 $r,
2912 $radius,
2913 $radius
2914 );
2915 }*/
2916
2917 /**
2918 * @depecated 4.1.7.2
2919 */
2920 /*function learn_press_is_page( $page_name ) {
2921 $page_id = learn_press_get_page_id( $page_name );
2922
2923 return $page_id && is_page( $page_id );
2924 }*/
2925
2926 /**
2927 * Get end-date from start date with a duration.
2928 *
2929 * @param string|int $duration
2930 * @param string|int $start
2931 *
2932 * @return false|string
2933 * @since 3.x.x
2934 * @depecated 4.1.7.2
2935 */
2936 /*function learn_press_date_end_from( $duration, $start = '' ) {
2937 $format = 'Y-m-d H:i:s';
2938
2939 if ( ! $start ) {
2940 $start = time();
2941 } elseif ( ! is_numeric( $start ) ) {
2942 $start = strtotime( $start );
2943 }
2944
2945 // is LP duration format, e.g: 10 weeks
2946 if ( preg_match( '/^[0-9]+ [a-z]+$/', $duration ) ) {
2947 $duration = ( new LP_Duration( $duration ) )->get();
2948 } elseif ( ! is_numeric( $duration ) ) {
2949 // 10 days 5 hours ...
2950 $duration = strtotime( $duration, 0 );
2951 }
2952
2953 return date( $format, $start + $duration );
2954 }*/
2955
2956 function learn_press_cookie_get( $name, $namespace = 'LP' ) {
2957 if ( $namespace ) {
2958 $cookie = ! empty( $_COOKIE[ $namespace ] ) ? (array) json_decode( LP_Helper::sanitize_params_submitted( stripslashes( $_COOKIE[ $namespace ] ), 'html' ) ) : array();
2959 } else {
2960 $cookie = $_COOKIE;
2961 }
2962
2963 return $cookie[ $name ] ?? null;
2964 }
2965
2966 /**
2967 * Get default methods to evaluate course results.
2968 *
2969 * @param string $return - Optional. 'keys' will return keys instead of all.
2970 *
2971 * @return array
2972 * @since 3.x.x
2973 */
2974 function learn_press_course_evaluation_methods( $postid, $return = '', $final_quizz_passing = '' ) {
2975 $course_tip = '<span class="learn-press-tip">%s</span>';
2976 $final_quiz_btn = '<a href="#" class="lp-metabox-get-final-quiz" data-postid="' . $postid . '" data-loading="' . esc_attr__(
2977 'Loading...',
2978 'learnpress'
2979 ) . '">' . esc_html__( 'Get Passing Grade', 'learnpress' ) . '</a>';
2980
2981 $course_desc = array(
2982 'evaluate_lesson' => sprintf(
2983 '<p>%s<br/>%s</p>',
2984 __( 'Evaluate by the number of lessons completed per total number of lessons.', 'learnpress' ),
2985 __( 'E.g: Course has 10 lessons and user completed 5 lessons then the result = 5/10 (50.%)', 'learnpress' )
2986 ),
2987 'evaluate_final_quiz' => __(
2988 'Evaluate by result of final quiz in the course. You have to add a quiz to the end of the course.',
2989 'learnpress'
2990 ),
2991 'evaluate_quiz' => sprintf(
2992 '<p>%s<br/>%s</p>',
2993 __( 'Evaluate by the number of quizzes passed per total number of quizzes.', 'learnpress' ),
2994 __(
2995 'E.g: The course has 10 quizzes and the user passed 5 quizzes then the result = 5/10 (50%).',
2996 'learnpress'
2997 )
2998 ),
2999 'evaluate_questions' => sprintf(
3000 '<p>%s<br/>%s</p>',
3001 __( 'Evaluate by total number of correct answers per total number of questions.', 'learnpress' ),
3002 __(
3003 'E.g: Course has 10 questions. User correct 5 questions. Result is 5/10 (50%).',
3004 'learnpress'
3005 )
3006 ),
3007 'evaluate_mark' => __( 'Evaluate by total score achieved per total score of the questions.', 'learnpress' ),
3008 );
3009
3010 $methods = apply_filters(
3011 'learnpress/course-evaluation/methods',
3012 array(
3013 'evaluate_lesson' => __(
3014 'Evaluate via lessons',
3015 'learnpress'
3016 ) . learn_press_quick_tip( $course_desc['evaluate_lesson'], false ),
3017 'evaluate_final_quiz' => __( 'Evaluate via results of the final quiz', 'learnpress' ) . sprintf(
3018 $course_tip,
3019 $course_desc['evaluate_final_quiz']
3020 ) . $final_quiz_btn . $final_quizz_passing,
3021 'evaluate_quiz' => __( 'Evaluate via quizzes passed', 'learnpress' ) . sprintf(
3022 $course_tip,
3023 $course_desc['evaluate_quiz']
3024 ),
3025 'evaluate_questions' => __( 'Evaluate via questions', 'learnpress' ) . sprintf(
3026 $course_tip,
3027 $course_desc['evaluate_questions']
3028 ),
3029 'evaluate_mark' => __( 'Evaluate via mark', 'learnpress' ) . sprintf(
3030 $course_tip,
3031 $course_desc['evaluate_mark']
3032 ),
3033 )
3034 );
3035
3036 return apply_filters(
3037 'learn-press/course-evaluation-methods',
3038 $return === 'keys' ? array_keys( $methods ) : $methods,
3039 $return
3040 );
3041 }
3042
3043 /**
3044 * Convert time from GMT to local.
3045 *
3046 * @param string|int|LP_Datetime $gmt_time
3047 * @param string $format
3048 *
3049 * @return false|int|string
3050 * @since 4.0.0
3051 * @depecated 4.1.7.2
3052 */
3053 /*function learn_press_time_from_gmt( $gmt_time, $format = 'Y-m-d H:i:s' ) {
3054 if ( is_string( $gmt_time ) ) {
3055 $gmt_time = strtotime( $gmt_time );
3056 } elseif ( $gmt_time instanceof LP_Datetime ) {
3057 $gmt_time = strtotime( $gmt_time . '' );
3058 }
3059
3060 $current_time = $gmt_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
3061
3062 if ( $format ) {
3063 return date( $format, $current_time );
3064 }
3065
3066 return $current_time;
3067 }*/
3068
3069 /**
3070 * Get max retrying quiz allowed.
3071 *
3072 * @param int $quiz_id
3073 * @param int $course_id
3074 *
3075 * @return int
3076 * @since 4.0.0
3077 */
3078 function learn_press_get_quiz_max_retrying( $quiz_id = 0, $course_id = 0 ) {
3079 return apply_filters( 'learn-press/max-retry-quiz-allowed', 1, $quiz_id, $course_id );
3080 }
3081
3082 /**
3083 * Get max retrying course allowed.
3084 *
3085 * @param int $course_id
3086 *
3087 * @return int
3088 * @since 4.0.0
3089 * @depecated 4.1.7.2
3090 */
3091 /*function learn_press_get_course_max_retrying( $course_id ) {
3092 return apply_filters( 'learn-press/max-retry-course-allowed', 1, $course_id );
3093 }*/
3094
3095 /**
3096 * Get slug for status of course/lesson/quiz if user
3097 * completed/finished and graduation is passed.
3098 *
3099 * @param string $type
3100 *
3101 * @return string
3102 * @since 4.0.0
3103 */
3104 function learn_press_user_item_in_progress_slug( $type = '' ) {
3105 return apply_filters( 'learn-press/user-item-in-progress-slug', 'in-progress', $type );
3106 }
3107
3108 /**
3109 * Get slug for status of course/lesson/quiz if user
3110 * completed/finished and result is under-evaluation
3111 *
3112 * @param string $item - Optional. Type of item
3113 *
3114 * @return string
3115 * @since 4.0.0
3116 */
3117 function learn_press_user_item_under_evaluation_slug( $item = '' ) {
3118 return apply_filters( 'learn-press/user-item-under-evaluation-slug', 'in-progress', $item );
3119 }
3120
3121 function learn_press_is_enrolled_slug( $slug ) {
3122 return in_array(
3123 $slug,
3124 array(
3125 'in-progress',
3126 'enrolled',
3127 )
3128 );
3129 }
3130
3131 /**
3132 * @return array
3133 * @since 4.0.0
3134 */
3135 function lp_item_course_class( $class = array() ) {
3136 $classes = array_merge(
3137 $class,
3138 array( 'learn-press-courses' )
3139 );
3140 echo 'class="' . esc_attr( implode( ' ', apply_filters( 'lp_item_course_class', $classes ) ) ) . '"';
3141 }
3142
3143 //require_once dirname( __FILE__ ) . '/lp-custom-hooks.php';
3144
3145
3146 /**
3147 * Disable auto update
3148 *
3149 * @param $update
3150 * @param $item
3151 *
3152 * @return false
3153 * @author hungkv
3154 */
3155 /*function learnpress_disable_auto_update( $update, $item ) {
3156 $plugins = array( // Plugins to auto-update
3157 'learnpress',
3158 );
3159 // Auto-update specified plugins
3160 if ( in_array( $item->slug, $plugins ) ) {
3161 return false;
3162 }
3163 }
3164 add_filter( 'auto_update_plugin', 'learnpress_disable_auto_update', 10, 2 );*/
3165
3166 add_action(
3167 'in_plugin_update_message-learnpress/learnpress.php',
3168 function ( $plugin_data ) {
3169 version_update_warning( LEARNPRESS_VERSION, $plugin_data['new_version'] );
3170 }
3171 );
3172 /**
3173 * Custom message warning have new version
3174 *
3175 * @param $current_version
3176 * @param $new_version
3177 * @author hungkv
3178 */
3179 function version_update_warning( $current_version, $new_version ) {
3180 $current_version_minor_part = explode( '.', $current_version )[1];
3181 $new_version_minor_part = explode( '.', $new_version )[1];
3182 if ( $current_version_minor_part === $new_version_minor_part ) {
3183 return;
3184 }
3185
3186 $info = get_plugin_data( LP_PLUGIN_FILE );
3187 ?>
3188 <hr class="lp-update--warning__separator"/>
3189 <div class="lp-update--warning">
3190 <div>
3191 <div class="lp-update-warning__title">
3192 <?php echo esc_html__( 'Heads up, Please backup before upgrade!', 'learnpress' ); ?>
3193 </div>
3194 <div class="lp-update-warning__message">
3195 <?php echo 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' ); ?>
3196 <?php echo esc_html__( 'Learners require WordPress version ' . $info['Requires at least'] . ' or higher.', 'learnpress' ); ?>
3197 </div>
3198 </div>
3199 </div>
3200
3201 <?php
3202 }
3203
3204 // If profile content don't have shortcode profile.
3205 function lp_add_shortcode_profile() {
3206 global $post;
3207
3208 if ( learn_press_is_profile() && is_object( $post ) ) {
3209 if ( ! has_shortcode( $post->post_content, 'learn_press_profile' ) ) {
3210 $post->post_content .= '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->';
3211 }
3212
3213 wp_update_post( $post );
3214 }
3215 }
3216
3217 add_action( 'template_redirect', 'lp_add_shortcode_profile' );
3218
3219 /**
3220 * If Elementor Pro set Theme builder type "Archive", will not show content on page "Archive course"
3221 *
3222 * @editor tungnx
3223 * @author nhamdv
3224 *
3225 * @since 4.0.6
3226 * @version 1.0.1
3227 */
3228 add_filter(
3229 'elementor/theme/get_location_templates/template_id',
3230 function( $theme_template_id ) {
3231 $elementor_template_type = get_post_meta( $theme_template_id, '_elementor_template_type', true );
3232
3233 if ( in_array( $elementor_template_type, array( 'archive' ) ) ) {
3234 if ( LP_PAGE_COURSES === LP_Page_Controller::page_current() && class_exists( 'ElementorPro\Modules\ThemeBuilder\Conditions\Archive' ) ) {
3235 return false;
3236 }
3237 }
3238
3239 return $theme_template_id;
3240 }
3241 );
3242