PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.1
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.3.1, at inc/lp-core-functions.php

3,230 lines 81.7 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 * @deprecated 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 * @deprecated 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 * @deprecated 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 // Only load on profile page.
2050 $page_profile_option = untrailingslashit( get_the_permalink( learn_press_get_page_id( 'profile' ) ) );
2051 $page_profile_option = str_replace( '/', '\/', $page_profile_option );
2052 $pattern = '/' . $page_profile_option . '/';
2053 if ( ! preg_match( $pattern, LP_Helper::getUrlCurrent() ) ) {
2054 return false;
2055 }
2056
2057 if ( ! empty( $_REQUEST['tab'] ) ) {
2058 $current = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] );
2059 } elseif ( ! empty( $wp_query->query_vars['tab'] ) ) {
2060 $current = $wp_query->query_vars['tab'];
2061 } elseif ( ! empty( $wp->query_vars['view'] ) ) {
2062 $current = $wp->query_vars['view'];
2063 } else {
2064 $tabs = learn_press_get_user_profile_tabs();
2065 if ( $default && $tabs ) {
2066 // Fixed for array_keys does not work with ArrayAccess instance
2067 if ( $tabs instanceof LP_Profile_Tabs ) {
2068 $tabs = $tabs->tabs();
2069 }
2070
2071 $tab_keys = array_keys( $tabs );
2072 $current = reset( $tab_keys );
2073 }
2074 }
2075
2076 return $current;
2077 }
2078
2079 add_action( 'init', 'learn_press_get_current_profile_tab' );
2080
2081 function learn_press_profile_tab_exists( $tab ) {
2082 $tabs = learn_press_get_user_profile_tabs();
2083
2084 if ( $tabs ) {
2085 return ! empty( $tabs[ $tab ] ) ? true : false;
2086 }
2087
2088 return false;
2089 }
2090
2091 /**
2092 * Replace the spacing with the + (plus) char.
2093 *
2094 * @param string $string
2095 *
2096 * @return string
2097 * @deprecated 4.1.7.2
2098 */
2099 /*function _learn_press_urlencode( $string ) {
2100 return preg_replace( '/\s/', '+', $string );
2101 }*/
2102
2103 /**
2104 * Point the archive post type link to course page if current
2105 * post type is course and the page for displaying course is
2106 * setup.
2107 *
2108 * @param string $link
2109 * @param string $post_type
2110 *
2111 * @return string
2112 * @deprecated 4.1.7.2
2113 */
2114 /*function learn_press_post_type_archive_link( $link, $post_type ) {
2115 if ( $post_type == LP_COURSE_CPT && learn_press_get_page_id( 'courses' ) ) {
2116 $link = learn_press_get_page_link( 'courses' );
2117 }
2118
2119 return $link;
2120 }*/
2121 //add_filter( 'post_type_archive_link', 'learn_press_post_type_archive_link', 10, 2 );
2122
2123 function learn_press_single_term_title( $prefix = '', $display = true ) {
2124 $term = get_queried_object();
2125
2126 if ( ! $term ) {
2127 return '';
2128 }
2129
2130 if ( learn_press_is_course_category() ) {
2131 $term_name = apply_filters( 'single_course_category_title', $term->name );
2132 } elseif ( learn_press_is_course_tag() ) {
2133 $term_name = apply_filters( 'single_course_tag_title', $term->name );
2134 } elseif ( learn_press_is_course_taxonomy() ) {
2135 $term_name = apply_filters( 'single_course_term_title', $term->name );
2136 } else {
2137 return single_term_title( $prefix, $display );
2138 }
2139
2140 if ( empty( $term_name ) ) {
2141 return single_term_title( $prefix, $display );
2142 }
2143
2144 if ( $display ) {
2145 echo wp_kses_post( $prefix . $term_name );
2146 }
2147
2148 return $prefix . $term_name;
2149 }
2150
2151 /**
2152 * Control the template file if user is searching course.
2153 * Use the template of archive course to display the
2154 * result if there is a flag in request to search course.
2155 *
2156 * @param string $template
2157 *
2158 * @return string
2159 */
2160 function learn_press_search_template( $template ) {
2161 if ( ! empty( $_REQUEST['ref'] ) && sanitize_text_field( $_REQUEST['ref'] ) == 'course' ) {
2162 $template = learn_press_locate_template( 'archive-course.php' );
2163 }
2164
2165 return $template;
2166 }
2167
2168 /**
2169 * Auto enroll user to a course after an order is completed
2170 * if the option auto-enroll is turn on.
2171 *
2172 * @param int $order_id
2173 *
2174 * @return mixed
2175 * @editor tungnx
2176 */
2177 function learn_press_auto_enroll_user_to_courses( $order_id ) {
2178 _deprecated_function( __FUNCTION__, '4.1.3' );
2179 }
2180
2181 // add_action( 'learn_press_order_status_completed', 'learn_press_auto_enroll_user_to_courses' );
2182
2183 /**
2184 * Return true if enable cart
2185 *
2186 * @return bool
2187 */
2188 function learn_press_is_enable_cart() {
2189 return defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART == true;
2190 }
2191
2192 /**
2193 * Short way to get checkout object
2194 *
2195 * @param array
2196 *
2197 * @return LP_Checkout
2198 */
2199 function learn_press_get_checkout( $args = null ) {
2200 $checkout = LP_Checkout::instance();
2201
2202 if ( is_array( $args ) ) {
2203 foreach ( $args as $k => $v ) {
2204 $checkout->{$k} = $v;
2205 }
2206 }
2207
2208 return $checkout;
2209 }
2210
2211 if ( defined( 'LP_ENABLE_CART' ) && LP_ENABLE_CART ) {
2212 add_filter( 'learn_press_checkout_settings', '_learn_press_cart_settings', 10, 2 );
2213 function _learn_press_cart_settings( $settings, $class ) {
2214 $settings = array_merge(
2215 $settings,
2216 array(
2217 array(
2218 'title' => __( 'Cart', 'learnpress' ),
2219 'type' => 'title',
2220 ),
2221 array(
2222 'title' => __( 'Enable cart', 'learnpress' ),
2223 'desc' => __(
2224 'Check this option to enable users to purchase multiple courses at one time.',
2225 'learnpress'
2226 ),
2227 'id' => $class->get_field_name( 'enable_cart' ),
2228 'default' => 'yes',
2229 'type' => 'checkbox',
2230 ),
2231 array(
2232 'title' => __( 'Add to cart redirect', 'learnpress' ),
2233 'desc' => __( 'Redirect to checkout immediately after adding the course to the cart.', 'learnpress' ),
2234 'id' => $class->get_field_name( 'redirect_after_add' ),
2235 'default' => 'yes',
2236 'type' => 'checkbox',
2237 ),
2238 array(
2239 'title' => __( 'AJAX add to cart', 'learnpress' ),
2240 'desc' => __( 'Using AJAX to add the course to the cart.', 'learnpress' ),
2241 'id' => $class->get_field_name( 'ajax_add_to_cart' ),
2242 'default' => 'no',
2243 'type' => 'checkbox',
2244 ),
2245 array(
2246 'title' => __( 'Cart page', 'learnpress' ),
2247 'id' => $class->get_field_name( 'cart_page_id' ),
2248 'default' => '',
2249 'type' => 'pages-dropdown',
2250 ),
2251 )
2252 );
2253
2254 return $settings;
2255 }
2256 } else {
2257 add_filter( 'learn_press_enable_cart', '_learn_press_enable_cart', 1000 );
2258 function _learn_press_enable_cart( $r ) {
2259 return false;
2260 }
2261
2262 add_filter( 'learn_press_get_template', '_learn_press_enroll_button', 1000, 5 );
2263 function _learn_press_enroll_button( $located, $template_name, $args, $template_path, $default_path ) {
2264 if ( $template_name == 'single-course/enroll-button.php' ) {
2265 $located = learn_press_locate_template(
2266 'single-course/enroll-button-new.php',
2267 $template_path,
2268 $default_path
2269 );
2270 }
2271
2272 return $located;
2273 }
2274 }
2275
2276 /**
2277 * Returns checkout url from setting
2278 *
2279 * @return string
2280 */
2281 function learn_press_get_checkout_url() {
2282 $checkout_url = learn_press_get_page_link( 'checkout' );
2283
2284 return apply_filters( 'learn_press_get_checkout_url', $checkout_url );
2285 }
2286
2287 /**
2288 * @return string
2289 */
2290 function learn_press_checkout_needs_payment() {
2291 return LearnPress::instance()->cart->needs_payment();
2292 }
2293
2294 /**
2295 * Return plugin basename
2296 *
2297 * @param string $filepath
2298 *
2299 * @return string
2300 */
2301 /*
2302 function learn_press_plugin_basename( $filepath ) {
2303 $file = str_replace( '\\', '/', $filepath );
2304 $file = preg_replace( '|/+|', '/', $file );
2305 $plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR );
2306 $plugin_dir = preg_replace( '|/+|', '/', $plugin_dir );
2307 $mu_plugin_dir = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
2308 $mu_plugin_dir = preg_replace( '|/+|', '/', $mu_plugin_dir );
2309 $sp_plugin_dir = dirname( $filepath );
2310 $sp_plugin_dir = dirname( $sp_plugin_dir );
2311 $sp_plugin_dir = str_replace( '\\', '/', $sp_plugin_dir );
2312 $sp_plugin_dir = preg_replace( '|/+|', '/', $sp_plugin_dir );
2313
2314 $file = preg_replace(
2315 '#^' . preg_quote( $sp_plugin_dir, '#' ) . '/|^' . preg_quote(
2316 $plugin_dir,
2317 '#'
2318 ) . '/|^' . preg_quote(
2319 $mu_plugin_dir,
2320 '#'
2321 ) . '/#',
2322 '',
2323 $file
2324 );
2325 $file = trim( $file, '/' );
2326
2327 return strtolower( $file );
2328 }*/
2329
2330 /**
2331 * Update log data for each LP version into wp option.
2332 *
2333 * @param string $version
2334 * @param mixed $data
2335 */
2336 function learn_press_update_log( $version, $data ) {
2337 $logs = get_option( 'learn_press_update_logs' );
2338 if ( ! $logs ) {
2339 $logs = array( $version => $data );
2340 } else {
2341 $logs[ $version ] = $data;
2342 }
2343 update_option( 'learn_press_update_logs', $logs );
2344 }
2345
2346 /**
2347 * Output variables to screen for debugging.
2348 */
2349 function learn_press_debug() {
2350 $args = func_get_args();
2351 $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
2352
2353 echo '<pre>';
2354 print_r( $debug );
2355 $arg = false;
2356
2357 if ( $args ) {
2358 foreach ( $args as $arg ) {
2359 echo "\n======LearnPress Debug=======\n";
2360 print_r( $arg );
2361 echo "\n=============================\n";
2362 }
2363 }
2364 echo '</pre>';
2365
2366 if ( true === $arg ) {
2367 die( __FUNCTION__ );
2368 }
2369 }
2370
2371 /**
2372 * Get current time to user for calculate remaining time of quiz.
2373 *
2374 * @return int
2375 * @deprecated 4.1.7.2
2376 */
2377 /*function learn_press_get_current_time() {
2378 $current_time = apply_filters( 'learn_press_get_current_time', 0 );
2379
2380 if ( $current_time > 0 ) {
2381 return $current_time;
2382 }
2383
2384 $a = current_time( 'timestamp' );
2385 $b = time();
2386 $c = current_time( 'mysql' );
2387 $d = strtotime( $c );
2388
2389 if ( $d == $a ) {
2390 return $a;
2391 } else {
2392 return $b;
2393 }
2394 }*/
2395
2396 function learn_press_get_requested_post_type() {
2397 global $pagenow;
2398 if ( $pagenow == 'post-new.php' && ! empty( $_REQUEST['post_type'] ) ) {
2399 $post_type = LP_Helper::sanitize_params_submitted( $_REQUEST['post_type'] );
2400 } else {
2401 $post_id = learn_press_get_post();
2402 $post_type = learn_press_get_post_type( $post_id );
2403 }
2404
2405 return $post_type;
2406 }
2407
2408 /**
2409 * Get human string from grade slug.
2410 *
2411 * @param string $slug
2412 *
2413 * @return string
2414 */
2415 function learn_press_get_graduation_text( $slug ) {
2416 switch ( $slug ) {
2417 case 'passed':
2418 $text = esc_html__( 'Passed', 'learnpress' );
2419 break;
2420 case 'failed':
2421 $text = esc_html__( 'Failed', 'learnpress' );
2422 break;
2423 case 'in-progress':
2424 $text = esc_html__( 'In Progress', 'learnpress' );
2425 break;
2426 default:
2427 $text = $slug;
2428 }
2429
2430 return apply_filters( 'learn-press/get-graduation-text', $text, $slug );
2431 }
2432
2433 if ( ! function_exists( 'learn_press_is_negative_value' ) ) {
2434 function learn_press_is_negative_value( $value ) {
2435 $return = in_array( $value, array( 'no', 'off', 'false', '0' ) ) || ! $value || $value == '' || $value == null;
2436
2437 return $return;
2438 }
2439 }
2440
2441 /**
2442 * Filter to comment reply link to fix bug the link is invalid for
2443 * lesson or quiz.
2444 *
2445 * @param string $link
2446 * @param array $args
2447 * @param WP_Comment $comment
2448 * @param WP_Post $post
2449 *
2450 * @return string
2451 */
2452 function learn_press_comment_reply_link( $link, $args = array(), $comment = null, $post = null ) {
2453
2454 $post_type = learn_press_get_post_type( $post );
2455
2456 if ( ! learn_press_is_support_course_item_type( $post_type ) ) {
2457 return $link;
2458 }
2459
2460 $course_item = LP_Global::course_item();
2461
2462 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
2463 $link = sprintf(
2464 '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
2465 esc_url_raw( wp_login_url( get_permalink() ) ),
2466 $args['login_text']
2467 );
2468 } elseif ( $course_item ) {
2469 $onclick = sprintf(
2470 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
2471 $args['add_below'],
2472 $comment->comment_ID,
2473 $args['respond_id'],
2474 $post->ID
2475 );
2476
2477 $link = sprintf(
2478 "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
2479 esc_url_raw(
2480 add_query_arg(
2481 array(
2482 'replytocom' => $comment->comment_ID,
2483 ),
2484 $course_item->get_permalink()
2485 )
2486 ) . '#' . $args['respond_id'],
2487 $onclick,
2488 esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
2489 $args['reply_text']
2490 );
2491 }
2492
2493 return $link;
2494 }
2495
2496 add_filter( 'comment_reply_link', 'learn_press_comment_reply_link', 10, 4 );
2497
2498 /**
2499 * Sanitize content of tooltip
2500 *
2501 * @param string $tooltip
2502 * @param bool $html
2503 *
2504 * @return string
2505 */
2506 function learn_press_sanitize_tooltip( $tooltip, $html = false ) {
2507 if ( $html ) {
2508 $tooltip = htmlspecialchars(
2509 wp_kses(
2510 html_entity_decode( $tooltip ),
2511 array(
2512 'br' => array(),
2513 'em' => array(),
2514 'strong' => array(),
2515 'small' => array(),
2516 'span' => array(),
2517 'ul' => array(),
2518 'li' => array(),
2519 'ol' => array(),
2520 'p' => array(),
2521 )
2522 )
2523 );
2524 } else {
2525 $tooltip = esc_attr( $tooltip );
2526 }
2527
2528 return $tooltip;
2529 }
2530
2531 function learn_press_tooltip( $tooltip, $html = false ) {
2532 $tooltip = learn_press_sanitize_tooltip( $tooltip, $html );
2533 echo '<span class="learn-press-tooltip" data-tooltip="' . esc_attr( $tooltip ) . '"></span>';
2534 }
2535
2536 /**
2537 * Get timezone offset from wp settings.
2538 *
2539 * @return float|int
2540 * @since 3.0.0
2541 * @deprecated 4.1.7.2
2542 */
2543 /*function learn_press_timezone_offset() {
2544 if ( $tz = get_option( 'timezone_string' ) ) {
2545 $timezone = new DateTimeZone( $tz );
2546
2547 return $timezone->getOffset( new DateTime( 'now' ) );
2548 } else {
2549 return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS;
2550 }
2551 }*/
2552
2553 /**
2554 * Get default static pages of LP.
2555 *
2556 * @return array
2557 *
2558 * @since 3.0.0
2559 */
2560 function learn_press_static_page_ids() {
2561 $pages = LP_Object_Cache::get( 'static-page-ids', 'learn-press' );
2562
2563 if ( false === $pages ) {
2564 $pages = array(
2565 'checkout' => learn_press_get_page_id( 'checkout' ),
2566 'courses' => learn_press_get_page_id( 'courses' ),
2567 'profile' => learn_press_get_page_id( 'profile' ),
2568 'become_a_teacher' => learn_press_get_page_id( 'become_a_teacher' ),
2569 );
2570
2571 foreach ( $pages as $name => $id ) {
2572 if ( ! get_post( $id ) ) {
2573 $pages[ $name ] = 0;
2574 }
2575 }
2576
2577 LP_Object_Cache::set( 'static-page-ids', $pages, 'learn-press' );
2578 }
2579
2580 return apply_filters( 'learn-press/static-page-ids', $pages );
2581 }
2582
2583 /**
2584 * Get default static pages of LP.
2585 *
2586 * @param bool $name - Optional. TRUE will return name only.
2587 *
2588 * @return array
2589 *
2590 * @since 3.0.0
2591 */
2592 function learn_press_static_pages( $name = false ) {
2593 $pages = apply_filters(
2594 'learn-press/static-pages',
2595 array(
2596 'checkout' => _x( 'Checkout', 'static-page-name', 'learnpress' ),
2597 'courses' => _x( 'Courses', 'static-page-name', 'learnpress' ),
2598 'profile' => _x( 'Profile', 'static-page-name', 'learnpress' ),
2599 'become_a_teacher' => _x( 'Become a Teacher', 'static-page-name', 'learnpress' ),
2600 )
2601 );
2602
2603 if ( $name ) {
2604 return array_keys( $pages );
2605 }
2606
2607 return $pages;
2608 }
2609
2610 /**
2611 * Callback function for sorting to array|object by key|prop priority.
2612 *
2613 * @param array|object $a
2614 * @param array|object $b
2615 *
2616 * @return int
2617 * @since 3.0.0
2618 */
2619 function learn_press_sort_list_by_priority_callback( $a, $b ) {
2620 $a_priority = null;
2621 $b_priority = null;
2622
2623 if ( is_array( $a ) && array_key_exists( 'priority', $a ) ) {
2624 $a_priority = $a['priority'];
2625 } elseif ( is_object( $a ) ) {
2626 if ( is_callable( array( $a, 'get_priority' ) ) ) {
2627 $a_priority = $a->get_priority();
2628 } elseif ( property_exists( $a, 'priority' ) ) {
2629 $a_priority = $a->priority;
2630 }
2631 }
2632
2633 if ( is_array( $b ) && array_key_exists( 'priority', $b ) ) {
2634 $b_priority = $b['priority'];
2635 } elseif ( is_object( $b ) ) {
2636 if ( is_callable( array( $b, 'get_priority' ) ) ) {
2637 $b_priority = $b->get_priority();
2638 } elseif ( property_exists( $b, 'priority' ) ) {
2639 $b_priority = $b->priority;
2640 }
2641 }
2642
2643 if ( $a_priority === $b_priority ) {
2644 return 0;
2645 }
2646
2647 return ( $a_priority < $b_priority ) ? - 1 : 1;
2648 }
2649
2650 /**
2651 * Localize date with custom format.
2652 *
2653 * @param string $timestamp
2654 * @param string $format
2655 * @param bool $gmt
2656 *
2657 * @return string
2658 * @since 3.0.0
2659 */
2660 function learn_press_date_i18n( $timestamp = '', $format = '', $gmt = false ) {
2661 if ( ! $format ) {
2662 $format = get_option( 'date_format' );
2663 }
2664
2665 return date_i18n( $format, $timestamp, $gmt );
2666 }
2667
2668 /**
2669 * Get item types of course support for blocking. Default is lp_lesson
2670 *
2671 * @return array
2672 * @since 3.0.0
2673 */
2674 function learn_press_get_block_course_item_types() {
2675 return apply_filters( 'learn-press/block-course-item-types', array( LP_LESSON_CPT, LP_QUIZ_CPT ) );
2676 }
2677
2678 /**
2679 * Get post type of a post from cache.
2680 * If there is no data stored in cache then
2681 * get it from WP API.
2682 *
2683 * @param int|WP_Post $post
2684 *
2685 * @return string
2686 * @since 3.1.0
2687 */
2688 function learn_press_get_post_type( $post ) {
2689 $post_types = LP_Object_Cache::get( 'post-types', 'learn-press' );
2690
2691 if ( false === $post_types ) {
2692 $post_types = array();
2693 }
2694
2695 if ( is_object( $post ) ) {
2696 $post_id = $post->ID;
2697 } else {
2698 $post_id = absint( $post );
2699 }
2700
2701 if ( empty( $post_types[ $post_id ] ) ) {
2702 $post_type = get_post_type( $post_id );
2703 $post_types[ $post_id ] = $post_type;
2704 LP_Object_Cache::set( 'post-types', $post_types, 'learn-press' );
2705 } else {
2706 $post_type = $post_types[ $post_id ];
2707 }
2708
2709 return $post_type;
2710 }
2711
2712 /**
2713 * Add post type of a post into cache
2714 *
2715 * @param int|array $id
2716 * @param string $type
2717 *
2718 * @since 3.1.0
2719 * @deprecated 4.1.7.2
2720 */
2721 /*function learn_press_cache_add_post_type( $id, $type = '' ) {
2722 if ( false === ( $post_types = LP_Object_Cache::get( 'post-types', 'learn-press' ) ) ) {
2723 $post_types = array();
2724 }
2725
2726 if ( func_num_args() == 1 && is_array( $id ) ) {
2727 $post_types = $post_types + $id;
2728 } else {
2729 $post_types[ $id ] = $type;
2730 }
2731
2732 LP_Object_Cache::set( 'post-types', $post_types, 'learn-press' );
2733 }*/
2734
2735 /**
2736 * @deprecated 4.1.7.2
2737 */
2738 /*function learn_press_has_option( $name ) {
2739 global $wpdb;
2740
2741 $query = $wpdb->prepare( "SELECT option_id FROM {$wpdb->options} WHERE option_name = %s", $name );
2742
2743 return $wpdb->get_var( $query ) > 0;
2744 }*/
2745
2746 /**
2747 * Update option to enable shuffle themes for ad.
2748 *
2749 * @since 3.2.1
2750 */
2751 function _learn_press_schedule_enable_shuffle_themes() {
2752 update_option( 'learn_press_ad_shuffle_themes', 'yes' );
2753 }
2754
2755 add_action( 'learn-press/schedule-enable-shuffle-themes', '_learn_press_schedule_enable_shuffle_themes' );
2756
2757 /**
2758 * @deprecated 4.1.7.2
2759 */
2760 /*function learn_press_show_log() {
2761 if ( trim( LP_Request::get( 'show_log' ) ) === md5( AUTH_KEY ) ) {
2762 call_user_func_array( 'learn_press_debug', func_get_args() );
2763 }
2764 }*/
2765
2766 /**
2767 * @return array
2768 * @since 3.2.6
2769 */
2770 function learn_press_global_script_params() {
2771 $js = array(
2772 'ajax' => admin_url( 'admin-ajax.php' ),
2773 'plugin_url' => LearnPress::instance()->plugin_url(),
2774 'siteurl' => home_url(),
2775 'current_url' => learn_press_get_current_url(),
2776 'theme' => get_stylesheet(),
2777 'localize' => array(
2778 'button_ok' => __( 'OK', 'learnpress' ),
2779 'button_cancel' => __( 'Cancel', 'learnpress' ),
2780 'button_yes' => __( 'Yes', 'learnpress' ),
2781 'button_no' => __( 'No', 'learnpress' ),
2782 ),
2783 'root' => esc_url_raw( rest_url() ),
2784 'nonce' => wp_create_nonce( 'wp_rest' ),
2785 );
2786
2787 return $js;
2788 }
2789
2790 /**
2791 * Get url for setup cron job on server.
2792 *
2793 * @return string
2794 * @since 3.3.0
2795 * @deprecated 4.1.7.2
2796 */
2797 /*function learn_press_get_cron_url() {
2798 $nonce = get_option( 'learnpress_cron_url_nonce' );
2799
2800 if ( ! $nonce ) {
2801 $nonce = md5( microtime( true ) );
2802 update_option( 'learnpress_cron_url_nonce', $nonce );
2803 }
2804 $url = add_query_arg(
2805 array(
2806 'lp-ajax' => 'cron',
2807 'sid' => $nonce,
2808 ),
2809 get_home_url()
2810 );
2811
2812 return $url;
2813 }*/
2814
2815 /**
2816 * Get courses expired.
2817 *
2818 * @return array
2819 * @since 3.3.0
2820 * @deprecated 4.1.7.2
2821 */
2822 /*function learn_press_get_expired_courses() {
2823 global $wpdb;
2824
2825 $query = $wpdb->prepare(
2826 "
2827 SELECT X.*
2828 FROM(
2829 SELECT ui.*
2830 FROM {$wpdb->learnpress_user_items} ui
2831 LEFT JOIN {$wpdb->learnpress_user_items} uix
2832 ON ui.item_id = uix.item_id
2833 AND ui.user_id = uix.user_id
2834 AND ui.user_item_id < uix.user_item_id
2835 WHERE uix.user_item_id IS NULL
2836 ) X
2837 INNER JOIN {$wpdb->users} u ON u.ID = X.user_id
2838 INNER JOIN {$wpdb->posts} p ON p.ID = X.item_id
2839 WHERE X.item_type = %s
2840 AND X.status = %s
2841 #AND expiration_time_gmt <= UTC_TIMESTAMP()
2842 AND expiration_time <= UTC_TIMESTAMP()
2843 LIMIT 0, 10
2844 ",
2845 LP_COURSE_CPT,
2846 'enrolled'
2847 );
2848
2849 }*/
2850
2851 /**
2852 * Return list types of questions that support answer options.
2853 *
2854 * @return array
2855 * @since 3.3.0
2856 */
2857 function learn_press_get_question_support_answer_options() {
2858 $questions = learn_press_get_question_support_feature( 'answer-options' );
2859
2860 return apply_filters( 'learn-press/questions-support-answer-options', $questions );
2861 }
2862
2863 /**
2864 * Return list types of question that support a feature.
2865 *
2866 * @param string $feature
2867 *
2868 * @return array
2869 * @since 3.3.0
2870 */
2871 function learn_press_get_question_support_feature( $feature ) {
2872 $questions = array();
2873 $types = LP_Global::get_object_supports( 'question' );
2874
2875 if ( $types ) {
2876 foreach ( $types as $type => $features ) {
2877 if ( array_key_exists( $feature, $features ) ) {
2878 $questions[] = $type;
2879 }
2880 }
2881 }
2882
2883 return $questions;
2884 }
2885
2886 /**
2887 * Helper function to output html for rendering a 'circle progress bar'
2888 *
2889 * @param int $percent
2890 * @param int $width
2891 * @param int $border
2892 * @param string $color
2893 *
2894 * @since 3.3.0
2895 * @deprecated 4.1.7.2
2896 */
2897 /*function learn_press_circle_progress_html( $percent = 0, $width = 32, $border = 4, $color = '' ) {
2898 $radius = $width / 2;
2899 $r = ( $width - $border ) / 2;
2900 $circumference = $r * 2 * pi();
2901 $offset = $circumference - $percent / 100 * $circumference;
2902
2903 printf(
2904 '<svg class="circle-progress-bar" width="%d" height="%d">
2905 <circle class="circle-progress-bar__circle"
2906 stroke="%s"
2907 stroke-width="%d"
2908 style="stroke-dasharray:%s %s; stroke-dashoffset:%s;"
2909 fill="transparent"
2910 r="%d" cx="%d" cy="%d"></circle>
2911 </svg>',
2912 $width,
2913 $width,
2914 $color,
2915 $border,
2916 $circumference,
2917 $circumference,
2918 $offset,
2919 $r,
2920 $radius,
2921 $radius
2922 );
2923 }*/
2924
2925 /**
2926 * @deprecated 4.1.7.2
2927 */
2928 /*function learn_press_is_page( $page_name ) {
2929 $page_id = learn_press_get_page_id( $page_name );
2930
2931 return $page_id && is_page( $page_id );
2932 }*/
2933
2934 /**
2935 * Get end-date from start date with a duration.
2936 *
2937 * @param string|int $duration
2938 * @param string|int $start
2939 *
2940 * @return false|string
2941 * @since 3.x.x
2942 * @deprecated 4.1.7.2
2943 */
2944 /*function learn_press_date_end_from( $duration, $start = '' ) {
2945 $format = 'Y-m-d H:i:s';
2946
2947 if ( ! $start ) {
2948 $start = time();
2949 } elseif ( ! is_numeric( $start ) ) {
2950 $start = strtotime( $start );
2951 }
2952
2953 // is LP duration format, e.g: 10 weeks
2954 if ( preg_match( '/^[0-9]+ [a-z]+$/', $duration ) ) {
2955 $duration = ( new LP_Duration( $duration ) )->get();
2956 } elseif ( ! is_numeric( $duration ) ) {
2957 // 10 days 5 hours ...
2958 $duration = strtotime( $duration, 0 );
2959 }
2960
2961 return date( $format, $start + $duration );
2962 }*/
2963
2964 function learn_press_cookie_get( $name, $namespace = 'LP' ) {
2965 if ( $namespace ) {
2966 $cookie = ! empty( $_COOKIE[ $namespace ] ) ? (array) json_decode( LP_Helper::sanitize_params_submitted( stripslashes( $_COOKIE[ $namespace ] ), 'html' ) ) : array();
2967 } else {
2968 $cookie = $_COOKIE;
2969 }
2970
2971 return $cookie[ $name ] ?? null;
2972 }
2973
2974 /**
2975 * Get default methods to evaluate course results.
2976 *
2977 * @param string $return - Optional. 'keys' will return keys instead of all.
2978 *
2979 * @return array
2980 * @since 3.x.x
2981 */
2982 function learn_press_course_evaluation_methods( $postid, $return = '', $final_quizz_passing = '' ) {
2983 $course_tip = '<span class="learn-press-tip">%s</span>';
2984 $final_quiz_btn = '<a href="#" class="lp-metabox-get-final-quiz" data-postid="' . $postid . '" data-loading="' . esc_attr__(
2985 'Loading...',
2986 'learnpress'
2987 ) . '">' . esc_html__( 'Get A Passing Grade', 'learnpress' ) . '</a>';
2988
2989 $course_desc = array(
2990 'evaluate_lesson' => sprintf(
2991 '<p>%s<br/>%s</p>',
2992 __( 'Evaluate by the number of completed lessons per the total number of lessons.', 'learnpress' ),
2993 __( 'E.g: If a course has 10 lessons and a user completes 5 lessons, then the result is 5/10 (50%).', 'learnpress' )
2994 ),
2995 'evaluate_final_quiz' => __(
2996 'Evaluate by the result of the final quiz in the course. You have to add a quiz at the end of the course.',
2997 'learnpress'
2998 ),
2999 'evaluate_quiz' => sprintf(
3000 '<p>%s<br/>%s</p>',
3001 __( 'Evaluate by the number of passed quizzes per the total number of quizzes.', 'learnpress' ),
3002 __(
3003 'E.g: If the course has 10 quizzes and the user passes 5 quizzes, then the result is 5/10 (50%).',
3004 'learnpress'
3005 )
3006 ),
3007 'evaluate_questions' => sprintf(
3008 '<p>%s<br/>%s</p>',
3009 __( 'Evaluate by the number of correct answers per the total number of questions.', 'learnpress' ),
3010 __(
3011 'E.g: If the course has 10 questions and the user corrects 5 questions, then the result is 5/10 (50%).',
3012 'learnpress'
3013 )
3014 ),
3015 'evaluate_mark' => __( 'Evaluate by the number of achieved scores per the total score of the questions.', 'learnpress' ),
3016 );
3017
3018 $methods = apply_filters(
3019 'learnpress/course-evaluation/methods',
3020 array(
3021 'evaluate_lesson' => __(
3022 'Evaluate via lessons',
3023 'learnpress'
3024 ) . learn_press_quick_tip( $course_desc['evaluate_lesson'], false ),
3025 'evaluate_final_quiz' => __( 'Evaluate via results of the final quiz', 'learnpress' ) . sprintf(
3026 $course_tip,
3027 $course_desc['evaluate_final_quiz']
3028 ) . $final_quiz_btn . $final_quizz_passing,
3029 'evaluate_quiz' => __( 'Evaluate via passed quizzes', 'learnpress' ) . sprintf(
3030 $course_tip,
3031 $course_desc['evaluate_quiz']
3032 ),
3033 'evaluate_questions' => __( 'Evaluate via questions', 'learnpress' ) . sprintf(
3034 $course_tip,
3035 $course_desc['evaluate_questions']
3036 ),
3037 'evaluate_mark' => __( 'Evaluate via mark', 'learnpress' ) . sprintf(
3038 $course_tip,
3039 $course_desc['evaluate_mark']
3040 ),
3041 )
3042 );
3043
3044 return apply_filters(
3045 'learn-press/course-evaluation-methods',
3046 $return === 'keys' ? array_keys( $methods ) : $methods,
3047 $return
3048 );
3049 }
3050
3051 /**
3052 * Convert time from GMT to local.
3053 *
3054 * @param string|int|LP_Datetime $gmt_time
3055 * @param string $format
3056 *
3057 * @return false|int|string
3058 * @since 4.0.0
3059 * @deprecated 4.1.7.2
3060 */
3061 /*function learn_press_time_from_gmt( $gmt_time, $format = 'Y-m-d H:i:s' ) {
3062 if ( is_string( $gmt_time ) ) {
3063 $gmt_time = strtotime( $gmt_time );
3064 } elseif ( $gmt_time instanceof LP_Datetime ) {
3065 $gmt_time = strtotime( $gmt_time . '' );
3066 }
3067
3068 $current_time = $gmt_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
3069
3070 if ( $format ) {
3071 return date( $format, $current_time );
3072 }
3073
3074 return $current_time;
3075 }*/
3076
3077 /**
3078 * Get max retrying quiz allowed.
3079 *
3080 * @param int $quiz_id
3081 * @param int $course_id
3082 *
3083 * @return int
3084 * @since 4.0.0
3085 */
3086 function learn_press_get_quiz_max_retrying( $quiz_id = 0, $course_id = 0 ) {
3087 return apply_filters( 'learn-press/max-retry-quiz-allowed', 1, $quiz_id, $course_id );
3088 }
3089
3090 /**
3091 * Get max retrying course allowed.
3092 *
3093 * @param int $course_id
3094 *
3095 * @return int
3096 * @since 4.0.0
3097 * @deprecated 4.1.7.2
3098 */
3099 /*function learn_press_get_course_max_retrying( $course_id ) {
3100 return apply_filters( 'learn-press/max-retry-course-allowed', 1, $course_id );
3101 }*/
3102
3103 /**
3104 * Get slug for status of course/lesson/quiz if user
3105 * completed/finished and graduation is passed.
3106 *
3107 * @param string $type
3108 *
3109 * @return string
3110 * @since 4.0.0
3111 */
3112 function learn_press_user_item_in_progress_slug( $type = '' ) {
3113 return apply_filters( 'learn-press/user-item-in-progress-slug', 'in-progress', $type );
3114 }
3115
3116 /**
3117 * Get slug for status of course/lesson/quiz if user
3118 * completed/finished and result is under-evaluation
3119 *
3120 * @param string $item - Optional. Type of item
3121 *
3122 * @return string
3123 * @since 4.0.0
3124 */
3125 function learn_press_user_item_under_evaluation_slug( $item = '' ) {
3126 return apply_filters( 'learn-press/user-item-under-evaluation-slug', 'in-progress', $item );
3127 }
3128
3129 function learn_press_is_enrolled_slug( $slug ) {
3130 return in_array(
3131 $slug,
3132 array(
3133 'in-progress',
3134 'enrolled',
3135 )
3136 );
3137 }
3138
3139 /**
3140 * @return array
3141 * @since 4.0.0
3142 */
3143 function lp_item_course_class( $class = array() ) {
3144 $classes = array_merge(
3145 $class,
3146 array( 'learn-press-courses' )
3147 );
3148 echo 'class="' . esc_attr( implode( ' ', apply_filters( 'lp_item_course_class', $classes ) ) ) . '"';
3149 }
3150
3151 //require_once dirname( __FILE__ ) . '/lp-custom-hooks.php';
3152
3153
3154 /**
3155 * Disable auto update
3156 *
3157 * @param $update
3158 * @param $item
3159 *
3160 * @return false
3161 * @author hungkv
3162 */
3163 /*function learnpress_disable_auto_update( $update, $item ) {
3164 $plugins = array( // Plugins to auto-update
3165 'learnpress',
3166 );
3167 // Auto-update specified plugins
3168 if ( in_array( $item->slug, $plugins ) ) {
3169 return false;
3170 }
3171 }
3172 add_filter( 'auto_update_plugin', 'learnpress_disable_auto_update', 10, 2 );*/
3173
3174 add_action(
3175 'in_plugin_update_message-learnpress/learnpress.php',
3176 function ( $plugin_data ) {
3177 if ( version_compare( $plugin_data['new_version'], LEARNPRESS_VERSION, '<=' ) ) {
3178 return;
3179 }
3180 ?>
3181 <hr/>
3182 <h3>
3183 <?php echo esc_html__( 'Heads up! Please backup before upgrading!', 'learnpress' ); ?>
3184 </h3>
3185 <div>
3186 <?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' ); ?>
3187 </div>
3188 <?php
3189 }
3190 );
3191
3192 // If profile content don't have shortcode profile.
3193 function lp_add_shortcode_profile() {
3194 global $post;
3195
3196 if ( learn_press_is_profile() && is_object( $post ) ) {
3197 if ( ! has_shortcode( $post->post_content, 'learn_press_profile' ) ) {
3198 $post->post_content .= '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->';
3199 }
3200
3201 wp_update_post( $post );
3202 }
3203 }
3204
3205 add_action( 'template_redirect', 'lp_add_shortcode_profile' );
3206
3207 /**
3208 * If Elementor Pro set Theme builder type "Archive", will not show content on page "Archive course"
3209 *
3210 * @editor tungnx
3211 * @author nhamdv
3212 *
3213 * @since 4.0.6
3214 * @version 1.0.1
3215 */
3216 add_filter(
3217 'elementor/theme/get_location_templates/template_id',
3218 function( $theme_template_id ) {
3219 $elementor_template_type = get_post_meta( $theme_template_id, '_elementor_template_type', true );
3220
3221 if ( in_array( $elementor_template_type, array( 'archive' ) ) ) {
3222 if ( LP_PAGE_COURSES === LP_Page_Controller::page_current() && class_exists( 'ElementorPro\Modules\ThemeBuilder\Conditions\Archive' ) ) {
3223 return false;
3224 }
3225 }
3226
3227 return $theme_template_id;
3228 }
3229 );
3230