forked from LearnPress/learnpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlp-core-functions.php
3887 lines (3390 loc) · 96.1 KB
/
lp-core-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* LearnPress Core Functions
* Define common functions for both front-end and back-end
*
* @author ThimPress
* @package LearnPress/Functions
* @version 1.0
*/
defined( 'ABSPATH' ) || exit;
function learnpress_gutenberg_disable_cpt( $can_edit, $post_type ) {
$post_types = array(
LP_COURSE_CPT => LP()->settings()->get( 'enable_gutenberg_course' ),
LP_LESSON_CPT => LP()->settings()->get( 'enable_gutenberg_lesson' ),
LP_QUIZ_CPT => LP()->settings()->get( 'enable_gutenberg_quiz' ),
LP_QUESTION_CPT => LP()->settings()->get( 'enable_gutenberg_question' ),
);
foreach ( $post_types as $key => $pt ) {
if ( $post_type === $key && $pt !== 'yes' ) {
$can_edit = false;
}
}
return $can_edit;
}
add_filter( 'use_block_editor_for_post_type', 'learnpress_gutenberg_disable_cpt', 10, 2 );
/**
* Get instance of a CURD class by type
*
* @param string $type
*
* @return bool|LP_Course_CURD|LP_User_CURD|LP_Quiz_CURD|LP_Question_CURD
*/
function learn_press_get_curd( $type ) {
$curds = array(
'user' => 'LP_User_CURD',
'course' => 'LP_Course_CURD',
'quiz' => 'LP_Quiz_CURD',
'question' => 'LP_Question_CURD',
);
$curd = false;
if ( ! empty( $curds[ $type ] ) && class_exists( $curds[ $type ] ) ) {
$curd = new $curds[ $type ]();
}
return apply_filters( 'learn-press/curd', $curd, $type, $curds );
}
if ( ! function_exists( 'lp_add_body_class' ) ) {
function lp_add_body_class( $classes ) {
$classes = (array) $classes;
if ( learn_press_is_profile() ) {
$classes[] = 'learnpress-profile';
} elseif ( learn_press_is_checkout() ) {
$classes[] = 'learnpress-checkout';
}
return $classes;
}
add_filter( 'body_class', 'lp_add_body_class' );
}
/**
* Short function to get name of a theme
*
* @param string $folder
*
* @return mixed|string
*/
function learn_press_get_theme_name( $folder ) {
$theme = wp_get_theme( $folder );
return ! empty( $theme['Name'] ) ? $theme['Name'] : '';
}
/**
* Clean.
*
* @param [type] $var
*
* @version 4.0.0
* @author Nhamdv <[email protected]>
*/
function learnpress_clean( $var ) {
if ( is_array( $var ) ) {
return array_map( 'learnpress_clean', $var );
} else {
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
}
}
/**
* Display HTML of element for building QuickTip JS.
*
* @param string $tip
* @param bool $echo
* @param array $options
*
* @return string
* @since 3.0.0
*/
function learn_press_quick_tip( $tip, $echo = true, $options = array() ) {
$atts = '';
if ( $options ) {
foreach ( $options as $k => $v ) {
$options[ $k ] = "data-{$k}=\"{$v}\"";
}
$atts = ' ' . implode( ' ', $options );
}
$tip = sprintf( '<span class="learn-press-tip" ' . $atts . '>%s</span>', $tip );
if ( $echo ) {
echo $tip;
}
return $tip;
}
/**
* Return TRUE if defined WP_DEBUG and is true or 1.
*
* @return bool
* @editor tungnx
* @todo comment this function - replace with LP_Debug::is_debug()
*/
function learn_press_is_debug() {
return LP_Debug::is_debug();
}
/**
* Get current post ID.
*
* @return int
*/
function learn_press_get_post() {
global $post;
$post_id = learn_press_get_request( 'post' );
if ( ! $post_id ) {
$post_id = ! empty( $post ) ? $post->ID : 0;
}
if ( empty( $post_id ) ) {
$post_id = learn_press_get_request( 'post_ID' );
}
return absint( $post_id );
}
/**
* Get the LearnPress plugin url
*
* @param string $sub_dir
*
* @return string
*/
function learn_press_plugin_url( $sub_dir = '' ) {
return LP()->plugin_url( $sub_dir );
}
/**
* Get the LearnPress plugin path.
*
* @param string $sub_dir
*
* @return string
*/
function learn_press_plugin_path( $sub_dir = '' ) {
return LP()->plugin_path( $sub_dir );
}
/**
* Includes file base on LearnPress path
*
* @param string $file
* @param string $folder
* @param bool $include_once
*
* @return bool
*/
function learn_press_include( $file, $folder = 'inc', $include_once = true ) {
$include = learn_press_plugin_path( "{$folder}/{$file}" );
if ( file_exists( $include ) ) {
if ( $include_once ) {
include_once $include;
} else {
include $include;
}
return true;
}
return false;
}
/**
* Get current IP of the user
*
* @return mixed
*/
function learn_press_get_ip() {
// Just get the headers if we can or else use the SERVER global
if ( function_exists( 'apache_request_headers' ) ) {
$headers = apache_request_headers();
} else {
$headers = $_SERVER;
}
// Get the forwarded IP if it exists
if ( array_key_exists( 'X-Forwarded-For', $headers ) &&
(
filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ||
filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) )
) {
$the_ip = $headers['X-Forwarded-For'];
} elseif (
array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) &&
(
filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ||
filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 )
)
) {
$the_ip = $headers['HTTP_X_FORWARDED_FOR'];
} else {
$the_ip = $_SERVER['REMOTE_ADDR'];
}
return esc_sql( $the_ip );
}
/**
* Get user agent.
*
* @return string
*/
function learn_press_get_user_agent() {
return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
}
/**
* Generate an unique string.
*
* @param string $prefix
*
* @return string
*/
function learn_press_uniqid( $prefix = '' ) {
$hash = str_replace( '.', '', microtime( true ) . uniqid() );
return apply_filters( 'learn-press/generate-hash', $prefix . $hash, $prefix );
}
function learn_press_random_value( $len = 8 ) {
return substr( md5( uniqid( mt_rand(), true ) ), 0, $len );
}
function learn_press_map_columns_format( $columns, $format ) {
$return = array();
foreach ( $columns as $k => $v ) {
if ( ! empty( $format[ $k ] ) ) {
$return[] = $format[ $k ];
} else {
$return[] = '%s'; // default is string
}
}
return $return;
}
/**
* Check to see if an endpoint is showing in current URL.
*
* @param bool $endpoint
*
* @return bool
*/
function learn_press_is_endpoint_url( $endpoint = false ) {
global $wp;
$endpoints = array();
if ( $endpoint !== false ) {
if ( ! isset( $endpoints[ $endpoint ] ) ) {
return false;
} else {
$endpoint_var = $endpoints[ $endpoint ];
}
return isset( $wp->query_vars[ $endpoint_var ] );
} else {
foreach ( $endpoints as $key => $value ) {
if ( isset( $wp->query_vars[ $key ] ) ) {
return true;
}
}
return false;
}
}
/**
* Get current URL user is viewing.
*
* @return string
*/
function learn_press_get_current_url() {
static $current_url;
if ( ! $current_url ) {
$url = untrailingslashit( $_SERVER['REQUEST_URI'] );
if ( ! preg_match( '!^https?!', $url ) ) {
$siteurl = trailingslashit( get_home_url() );
$home_query = '';
if ( strpos( $siteurl, '?' ) !== false ) {
$parts = explode( '?', $siteurl );
$home_query = $parts[1];
$siteurl = $parts[0];
}
if ( $home_query ) {
parse_str( untrailingslashit( $home_query ), $home_query );
$url = add_query_arg( $home_query, $url );
}
$segs1 = explode( '/', $siteurl );
$segs2 = explode( '/', $url );
if ( $removed = array_intersect( $segs1, $segs2 ) ) {
if ( $segs2 = array_diff( $segs2, $removed ) ) {
$current_url = $siteurl . join( '/', $segs2 );
if ( strpos( $current_url, '?' ) === false ) {
$current_url = trailingslashit( $current_url );
}
}
}
}
}
return $current_url;
}
/**
* Compares an url with current URL user is viewing
*
* @param string $url
*
* @return bool
*/
function learn_press_is_current_url( $url ) {
$current_url = learn_press_get_current_url();
return ( $current_url && $url ) && strcmp( $current_url, learn_press_sanitize_url( $url ) ) == 0;
}
/**
* Remove unneeded characters in an URL
*
* @param string $url
* @param bool $trailingslashit
*
* @return string
*/
function learn_press_sanitize_url( $url, $trailingslashit = true ) {
if ( $url ) {
preg_match( '!(https?://)?(.*)!', $url, $matches );
$url_without_http = $matches[2];
$url_without_http = preg_replace( '![/]+!', '/', $url_without_http );
$url = $matches[1] . $url_without_http;
return ( $trailingslashit &&
strpos( $url, '?' ) === false ) ? trailingslashit( $url ) : untrailingslashit( $url );
}
return $url;
}
/**
* Get all types of question supported
*
* @return mixed
*/
function learn_press_question_types() {
return LP_Question::get_types();
}
/**
* Get human name of question's type by slug
*
* @param string $slug
*
* @return array
*/
function learn_press_question_name_from_slug( $slug ) {
$types = learn_press_question_types();
$name = ! empty( $types[ $slug ] ) ? $types[ $slug ] : '';
return apply_filters( 'learn-press/question/slug-to-name', $name, $slug );
}
/**
* Get the post types which supported to insert into course's section
*
* @return array
*/
function learn_press_section_item_types() {
$types = array(
'lp_lesson' => esc_html__( 'Lesson', 'learnpress' ),
'lp_quiz' => esc_html__( 'Quiz', 'learnpress' ),
);
return apply_filters( 'learn-press/section/support-item-type', $types );
}
/**
* Enqueue js code to print out
*
* @param string $code
* @param bool $script_tag - wrap code between <script> tag
*/
function learn_press_enqueue_script( $code, $script_tag = false ) {
global $learn_press_queued_js, $learn_press_queued_js_tag;
if ( $script_tag ) {
if ( empty( $learn_press_queued_js_tag ) ) {
$learn_press_queued_js_tag = '';
}
$learn_press_queued_js_tag .= "\n" . $code . "\n";
} else {
if ( empty( $learn_press_queued_js ) ) {
$learn_press_queued_js = '';
}
$learn_press_queued_js .= "\n" . $code . "\n";
}
}
/**
* Get terms of a course by taxonomy.
* E.g: course_tag, course_category
*
* @param int $course_id
* @param string $taxonomy
* @param array $args
*
* @return array|mixed
*/
function learn_press_get_course_terms( $course_id, $taxonomy, $args = array() ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return array();
}
// Support ordering by parent
if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
$fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
$orderby = $args['orderby'];
// Unset for wp_get_post_terms
unset( $args['orderby'] );
unset( $args['fields'] );
$terms = wp_get_post_terms( $course_id, $taxonomy, $args );
switch ( $orderby ) {
case 'name_num':
usort( $terms, '_learn_press_get_course_terms_name_num_usort_callback' );
break;
case 'parent':
usort( $terms, '_learn_press_get_course_terms_parent_usort_callback' );
break;
}
switch ( $fields ) {
case 'names':
$terms = wp_list_pluck( $terms, 'name' );
break;
case 'ids':
$terms = wp_list_pluck( $terms, 'term_id' );
break;
case 'slugs':
$terms = wp_list_pluck( $terms, 'slug' );
break;
}
} elseif ( ! empty( $args['orderby'] ) && $args['orderby'] === 'menu_order' ) {
// wp_get_post_terms doesn't let us use custom sort order
$args['include'] = wp_get_post_terms( $course_id, $taxonomy, array( 'fields' => 'ids' ) );
if ( empty( $args['include'] ) ) {
$terms = array();
} else {
// This isn't needed for get_terms
unset( $args['orderby'] );
// Set args for get_terms
$args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
$args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0;
$args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names';
// Ensure slugs is valid for get_terms - slugs isn't supported
$args['fields'] = $args['fields'] === 'slugs' ? 'id=>slug' : $args['fields'];
$terms = get_terms( $taxonomy, $args );
}
} else {
$terms = wp_get_post_terms( $course_id, $taxonomy, $args );
}
// @deprecated
$terms = apply_filters( 'learn_press_get_course_terms', $terms, $course_id, $taxonomy, $args );
return apply_filters( 'learn-press/course/terms', $terms, $course_id, $taxonomy, $args );
}
/**
* Callback function for sorting terms of course by name.
*
* @param object $a
* @param object $b
*
* @return int
*/
function _learn_press_get_course_terms_name_num_usort_callback( $a, $b ) {
if ( $a->name + 0 === $b->name + 0 ) {
return 0;
}
return ( $a->name + 0 < $b->name + 0 ) ? - 1 : 1;
}
/**
* Callback function for sorting terms of course by parent.
*
* @param object $a
* @param object $b
*
* @return int
*/
function _learn_press_get_course_terms_parent_usort_callback( $a, $b ) {
if ( $a->parent === $b->parent ) {
return 0;
}
return ( $a->parent < $b->parent ) ? 1 : - 1;
}
/**
* Get posts by it's post-name (slug).
*
* @param string $name
* @param string $type
* @param bool $single
*
* @return array|bool|null|WP_Post
*/
function learn_press_get_post_by_name( $name, $type, $single = true ) {
$post_name = sanitize_title( $name );
$id = LP_Object_Cache::get( $type . '-' . $post_name, 'learn-press/post-names' );
if ( false === $id ) {
foreach ( array( $name, urldecode( $name ) ) as $_name ) {
$args = array(
'name' => $_name,
'post_type' => array( $type ),
);
$posts = get_posts( $args );
if ( $posts ) {
$post = $posts[0];
$id = $post->ID;
wp_cache_set( $id, $post, 'posts' );
LP_Object_Cache::set( $type . '-' . $name, $id, 'learn-press/post-names' );
break;
}
}
}
return $id ? get_post( $id ) : false;
}
/**
* Cache static pages
*
* @deprecated
*/
function learn_press_setup_pages() {
global $wpdb;
$page_ids = LP_Object_Cache::get( 'static-page-ids', 'learn-press' );
if ( false === $page_ids ) {
$pages = learn_press_static_pages( true );
$page_ids = array();
foreach ( $pages as $page ) {
$id = get_option( 'learn_press_' . $page . '_page_id' );
if ( absint( $id ) > 0 ) {
$page_ids[] = $id;
}
}
if ( ! $page_ids ) {
return;
}
$query = $wpdb->prepare(
"
SELECT ID, post_title, post_name, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_parent, post_type
FROM {$wpdb->posts}
WHERE %d AND ID IN(" . join( ',', $page_ids ) . ')
AND post_status <> %s
',
1,
'trash'
);
if ( ! $rows = $wpdb->get_results( $query ) ) {
return;
}
foreach ( $rows as $page ) {
$page = sanitize_post( $page, 'raw' );
wp_cache_add( $page->ID, $page, 'posts' );
}
}
}
function learn_press_get_course_item_object( $post_type ) {
switch ( $post_type ) {
case 'lp_quiz':
$class = 'LP_Quiz';
break;
case 'lp_lesson':
$class = 'LP_Lesson';
break;
case 'lp_question':
$class = 'LP_Question';
}
}
/**
* Print out js code in the queue
*/
function learn_press_print_script() {
global $learn_press_queued_js, $learn_press_queued_js_tag;
if ( ! empty( $learn_press_queued_js ) ) {
?>
<!-- LearnPress JavaScript -->
<script type="text/javascript">
jQuery(function ($) {
<?php
$learn_press_queued_js = wp_check_invalid_utf8( $learn_press_queued_js );
$learn_press_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $learn_press_queued_js );
$learn_press_queued_js = str_replace( "\r", '', $learn_press_queued_js );
echo $learn_press_queued_js;
?>
})
</script>
<?php
unset( $learn_press_queued_js );
}
if ( ! empty( $learn_press_queued_js_tag ) ) {
echo $learn_press_queued_js_tag;
}
}
add_action( 'wp_footer', 'learn_press_print_script' );
add_action( 'admin_footer', 'learn_press_print_script' );
/**
* @param string $str
* @param int $lines
*/
function learn_press_email_new_line( $lines = 1, $str = "\r\n" ) {
echo str_repeat( $str, $lines );
}
if ( ! function_exists( 'learn_press_is_ajax' ) ) {
function learn_press_is_ajax() {
return defined( 'LP_DOING_AJAX' ) && LP_DOING_AJAX && 'yes' != learn_press_get_request( 'noajax' );
}
}
/**
* Get page id from admin settings page
*
* @param string $name
*
* @return int
*/
function learn_press_get_page_id( string $name ): int {
$page_id = LP_Settings::instance()->get( "{$name}_page_id", false );
if ( function_exists( 'icl_object_id' ) ) {
$page_id = icl_object_id( $page_id, 'page', false, defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '' );
}
$page_id = (int) $page_id;
return apply_filters( 'learn_press_get_page_id', $page_id, $name );
}
/**
* display the seconds in time format h:i:s
*
* @param $seconds
* @param string $separator
*
* @return string
*/
function learn_press_seconds_to_time( $seconds, $separator = ':' ) {
return sprintf(
'%02d%s%02d%s%02d',
floor( $seconds / 3600 ),
$separator,
( $seconds / 60 ) % 60,
$separator,
$seconds % 60
);
}
/* nav */
if ( ! function_exists( 'learn_press_course_paging_nav' ) ) {
/**
* Display navigation to next/previous set of posts when applicable.
*
* @param array
*/
function learn_press_course_paging_nav( $args = array() ) {
learn_press_paging_nav(
array(
'num_pages' => $GLOBALS['wp_query']->max_num_pages,
'wrapper_class' => 'navigation pagination',
)
);
}
}
/* nav */
if ( ! function_exists( 'learn_press_paging_nav' ) ) {
function learn_press_paging_nav( $args = array() ) {
$args = wp_parse_args(
$args,
array(
'num_pages' => 0,
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
'wrapper_class' => 'learn-press-pagination',
'base' => false,
'format' => '',
'echo' => true,
)
);
if ( $args['num_pages'] < 2 ) {
return false;
}
$paged = $args['paged'];
$pagenum_link = html_entity_decode( $args['base'] === false ? get_pagenum_link() : $args['base'] );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos(
$pagenum_link,
'index.php'
) ? 'index.php/' : '';
$format .= $args['format'] ? $args['format'] : ( $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit(
'page/%#%',
'paged'
) : '?paged=%#%' );
$link_args = array(
'base' => $pagenum_link,
'format' => $format,
'total' => $args['num_pages'],
'current' => max( 1, $paged ),
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '<', 'learnpress' ),
'next_text' => __( '>', 'learnpress' ),
'type' => 'list',
);
// Set up paginated links.
$links = paginate_links( $link_args );
ob_start();
if ( $links ) {
?>
<div class="<?php echo $args['wrapper_class']; ?>">
<?php echo $links; ?>
</div>
<?php
}
$output = ob_get_clean();
if ( $args['echo'] ) {
echo $output;
}
return $output;
}
}
/**
* Get number of pages by rows and items per page.
*
* @param int $total
* @param int $limit
*
* @return int
*/
function learn_press_get_num_pages( $total, $limit = 10 ) {
$limit = $limit <= 0 ? 10 : $limit;
if ( $total <= $limit ) {
return 1;
}
$pages = absint( $total / $limit );
if ( $total % $limit != 0 ) {
$pages ++;
}
return $pages;
}
/**
* Get text
*
* @param $status_id
*
* @return mixed
*/
function learn_press_get_status_text( $status_id ) {
switch ( $status_id ) {
case 1:
$text = 'pending';
break;
case 2:
$text = 'complete';
break;
case - 1:
$text = 'cancel';
break;
case - 2:
$text = 'refund';
break;
default:
$text = 'on-hold';
}
return $text;
}
function learn_press_get_course_duration_support() {
return apply_filters(
'learn_press_course_duration_support',
array(
'minute' => esc_html__( 'Minute(s)', 'learnpress' ),
'hour' => esc_html__( 'Hour(s)', 'learnpress' ),
'day' => esc_html__( 'Day(s)', 'learnpress' ),
'week' => esc_html__( 'Week(s)', 'learnpress' ),
)
);
}
function learn_press_number_to_string_time( $number ) {
$str = $number;
if ( preg_match( '!([0-9.]+) (minute|hour|day|week)!', $number, $matches ) ) {
switch ( $matches[2] ) {
case 'hour':
$minute = $matches[1] * 60;
$str = sprintf( '%s hour %s minute', absint( $minute / 60 ), $minute % 60 );
break;
case 'day':
$hour = $matches[1] * 24;
$str = sprintf( '%s day %s hour', absint( $hour / 24 ), $hour % 24 );
break;
case 'week':
$day = $matches[1] * 7;
$str = sprintf( '%s week %s day', absint( $day / 7 ), $day % 7 );
break;
}
}
return $str;
}
function learn_press_human_time_to_seconds( $time, $default = '' ) {
$duration = learn_press_get_course_duration_support();
$duration_keys = array_keys( $duration );
if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $time, $matches ) ) {
$a1 = $matches[1][0];
$a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : '';
} else {
$a1 = absint( $time );
$a2 = '';
}
if ( $a2 ) {
$b = array(
'minute' => 60,
'hour' => 3600,
'day' => 3600 * 24,
'week' => 3600 * 24 * 7,
);
$a1 = $a1 * $b[ $a2 ];
}
return $a1;
}
/**
* Send email notification.
*
* @param string $to .
* @param string $action .
* @param array $vars .
*
* @return bool
*/
function learn_press_send_mail( $to = '', $action = '', $vars = array() ) {
$email_settings = LP_Settings::instance();
if ( ! $email_settings->get( $action . '.enable' ) ) {
return "The action {$action} doesnt support";
}
$user = get_user_by( 'email', $to );
$vars['log_in'] = apply_filters( 'learn_press_site_url', get_home_url() );
// Send email.
$email = new LP_Email();
$email->set_action( $action );
$email->parse_email( $vars );
$email->add_recipient( $to );
return $email->send();
}
/*
* Send email notification when a course be published
*/
function learn_press_publish_course( $new_status, $old_status, $post ) {
if ( $old_status == 'pending' && $new_status == 'publish' && $post->post_type == 'lp_course' ) {
$instructor = get_userdata( $post->post_author );
$mail_to = $instructor->user_email;
learn_press_send_mail(
$mail_to,
'published_course',
apply_filters(
'learn_press_vars_enrolled_course',
array(
'user_name' => $instructor->display_name,
'course_name' => $post->post_title,
'course_link' => get_permalink( $post->ID ),
),
$post,
$instructor
)
);
}
}
add_action( 'transition_post_status', 'learn_press_publish_course', 10, 3 );
/**
* @param $user_id
*