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

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

1,079 lines 26.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions to manipulate with course, lesson, quiz, questions, etc...
4 *
5 * @author ThimPress
6 * @package LearnPress/Functions
7 * @version 1.0
8 */
9
10 defined( 'ABSPATH' ) || exit();
11
12 /**
13 * Get course current on single course or course by id
14 * Only use learn_press_get_course() on the single page
15 * Another page use learn_press_get_course(id)
16 *
17 * @param int $the_course
18 * @since 3.0.0
19 * @version 1.0.1
20 * @editor tungnx
21 * @return bool|LP_Course|mixed
22 */
23 function learn_press_get_course( $the_course = 0 ) {
24 $the_course = (int) $the_course;
25
26 if ( 0 === $the_course ) {
27 $the_course = get_the_ID() ? get_the_ID() : 0;
28 }
29
30 return LP_Course::get_course( $the_course );
31 }
32
33 /**
34 * @editor tungnx
35 * @modify 4.1.4.1 - comment - not use
36 */
37 /*function learn_press_get_course_by_id( $id ) {
38 if ( false !== ( $courses = LP_Object_Cache::get( 'object', 'learn-press/courses' ) ) ) {
39 return ! empty( $courses[ $id ] ) ? $courses[ $id ] : false;
40 }
41
42 return false;
43 }*/
44
45 /**
46 * Create nonce for course action.
47 * Return nonce created with format 'learn-press-$action-$course_id-course-$user_id'
48 *
49 * @param string $action [retake, purchase, enroll]
50 * @param int $course_id
51 * @param int $user_id
52 *
53 * @return string
54 * @since 3.0.0
55 * @depecated 4.1.6.9
56 */
57 /*function learn_press_create_course_action_nonce( $action, $course_id = 0, $user_id = 0 ) {
58 return LP_Nonce_Helper::create_course( $action, $course_id, $user_id );
59 }*/
60
61 /**
62 * Verify nonce for course action.
63 *
64 * @param string $nonce
65 * @param string $action
66 * @param int $course_id
67 * @param int $user_id
68 *
69 * @return bool
70 * @since 3.0.0
71 * @depecated 4.1.6.9
72 */
73 /*function learn_press_verify_course_action_nonce( $nonce, $action, $course_id = 0, $user_id = 0 ) {
74 return LP_Nonce_Helper::verify_course( $nonce, $action, $course_id, $user_id );
75 }*/
76
77 /**
78 * Get type of items are supported in course curriculum (post types).
79 * Default: [lp_lesson, lp_quiz]
80 *
81 * @return mixed
82 * @since 3.0.0
83 * @editor tungnx
84 * @version 1.0.1
85 * @return array
86 */
87 function learn_press_get_course_item_types( bool $return_only_value = true ): array {
88 return apply_filters(
89 'learn-press/course-item-type',
90 array( LP_LESSON_CPT, LP_QUIZ_CPT )
91 );
92 }
93
94 /**
95 * Get type of items can purchase on LP Order.
96 * Default: ['lp_course', 'lp_certificate']
97 *
98 * @return mixed
99 * @since 3.0.0
100 *
101 */
102 function learn_press_get_item_types_can_purchase() {
103 return apply_filters(
104 'learn-press/purchase/item-types/can-purchase',
105 array( LP_COURSE_CPT )
106 );
107 }
108
109 /**
110 * Get the courses that a item is assigned to
111 *
112 * @param $item
113 *
114 * @return mixed
115 * @Todo - tungnx need review code to rewrite.
116 */
117 function learn_press_get_item_courses( $item ) {
118 global $wpdb;
119 $query = $wpdb->prepare(
120 "
121 SELECT c.*
122 FROM {$wpdb->posts} c
123 INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id
124 INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id
125 WHERE si.item_id = %d
126 ",
127 $item
128 );
129
130 return $wpdb->get_results( $query );
131 }
132
133 function _learn_press_usort_terms_by_ID( $terms ) {
134 $version = get_bloginfo( 'version' );
135 if ( version_compare( $version, '4.7', '>=' ) ) {
136 $terms = wp_list_sort( $terms, 'term_id' );
137 } else {
138 usort( $terms, '_usort_terms_by_ID' );
139 }
140
141 return $terms;
142 }
143
144 function learn_press_course_post_type_link( $permalink, $post ) {
145 if ( $post->post_type !== 'lp_course' ) {
146 return $permalink;
147 }
148
149 // Abort early if the placeholder rewrite tag isn't in the generated URL
150 if ( false === strpos( $permalink, '%' ) ) {
151 return $permalink;
152 }
153
154 // Get the custom taxonomy terms in use by this post
155 $terms = get_the_terms( $post->ID, 'course_category' );
156
157 if ( ! empty( $terms ) ) {
158 $terms = _learn_press_usort_terms_by_ID( $terms ); // order by ID
159 $category_object = apply_filters(
160 'learn_press_course_post_type_link_course_category',
161 $terms[0],
162 $terms,
163 $post
164 );
165 $category_object = get_term( $category_object, 'course_category' );
166 $course_category = $category_object->slug;
167
168 if ( $parent = $category_object->parent ) {
169 $ancestors = get_ancestors( $category_object->term_id, 'course_category' );
170 foreach ( $ancestors as $ancestor ) {
171 $ancestor_object = get_term( $ancestor, 'course_category' );
172 $course_category = $ancestor_object->slug . '/' . $course_category;
173 }
174 }
175 } else {
176 // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
177 $course_category = _x( 'uncategorized', 'slug', 'learnpress' );
178 }
179
180 $find = array(
181 '%year%',
182 '%monthnum%',
183 '%day%',
184 '%hour%',
185 '%minute%',
186 '%second%',
187 '%post_id%',
188 '%category%',
189 '%course_category%',
190 );
191
192 $replace = array(
193 date_i18n( 'Y', strtotime( $post->post_date ) ),
194 date_i18n( 'm', strtotime( $post->post_date ) ),
195 date_i18n( 'd', strtotime( $post->post_date ) ),
196 date_i18n( 'H', strtotime( $post->post_date ) ),
197 date_i18n( 'i', strtotime( $post->post_date ) ),
198 date_i18n( 's', strtotime( $post->post_date ) ),
199 $post->ID,
200 $course_category,
201 $course_category,
202 );
203
204 $permalink = str_replace( $find, $replace, $permalink );
205
206 return $permalink;
207 }
208
209 add_filter( 'post_type_link', 'learn_press_course_post_type_link', 10, 2 );
210
211 function learn_press_item_meta_format( $item, $nonce = '' ) {
212 if ( current_theme_supports( 'post-formats' ) ) {
213 $format = get_post_format( $item );
214 if ( false === $format ) {
215 $format = 'standard';
216 }
217
218 // return false to hide post format
219 $format = apply_filters( 'learn_press_course_item_format', $format, $item );
220 if ( $format ) {
221 printf(
222 '<label for="post-format-0" class="post-format-icon post-format-%s" title="%s"></label>',
223 $format,
224 ucfirst( $format )
225 );
226 } else {
227 echo esc_html( $nonce );
228 }
229 }
230 }
231
232 function learn_press_course_item_format_exclude( $format, $item ) {
233 if ( learn_press_get_post_type( $item ) != LP_LESSON_CPT || ( $format == 'standard' ) ) {
234 $format = false;
235 }
236
237 return $format;
238 }
239
240 /**
241 * Get curriculum of a course
242 *
243 * @param $course_id
244 *
245 * @return mixed
246 * @version 1.0
247 *
248 */
249 function learn_press_get_course_curriculum( $course_id ) {
250 $course = learn_press_get_course( $course_id );
251
252 return $course->get_curriculum();
253 }
254
255 /**
256 * Verify course access
257 *
258 * @param int $course_id
259 * @param int $user_id
260 *
261 * @return boolean
262 * @deprecated 4.1.3
263 * @editor tungnx
264 */
265 function learn_press_is_enrolled_course( $course_id = null, $user_id = null ) {
266 _deprecated_function( __FUNCTION__, '4.1.3' );
267 if ( $course = learn_press_get_course( $course_id ) && $user = learn_press_get_user( $user_id ) ) {
268 return $user->has_enrolled_course( $course_id );
269 }
270
271 return false;
272 }
273
274 /**
275 * Detect if a course is free or not
276 *
277 * @param null $course_id
278 *
279 * @return bool
280 */
281 function learn_press_is_free_course( $course_id = null ) {
282 if ( ! $course_id ) {
283 $course_id = get_the_ID();
284 }
285
286 return learn_press_get_course( $course_id )->is_free();
287 }
288
289 /**
290 * get current status of user's course
291 *
292 * @param int $user_id
293 * @param int $course_id
294 *
295 * @return string
296 * @author Tunn
297 *
298 */
299 function learn_press_get_user_course_status( $user_id = null, $course_id = null ) {
300 $course = learn_press_get_course( $course_id );
301 $user = learn_press_get_user( $user_id );
302
303 if ( $course && $user ) {
304 return $user->get_course_status( $course_id );
305 }
306
307 return false;
308 }
309
310 /**
311 * Wrap function can-view-item of user object.
312 *
313 * @param int $item_id
314 * @param int $course_id
315 * @param int $user_id
316 *
317 * @return mixed
318 * @since 3.1.0
319 *
320 */
321 //function learn_press_can_view_item( $item_id, $course_id = 0, $user_id = 0 ) {
322 // if ( ! $user_id ) {
323 // $user_id = get_current_user_id();
324 // }
325 // $user = learn_press_get_user( $user_id );
326 //
327 // return $user->can_view_item( $item_id, $course_id );
328 //}
329
330 /**
331 * Get course setting is enroll required or public
332 *
333 * @param int $course_id
334 *
335 * @return boolean
336 * @since 0.9.5
337 *
338 */
339 function learn_press_course_enroll_required( $course_id = null ) {
340 $course_id = learn_press_get_course_id( $course_id );
341
342 $required = ( 'yes' == get_post_meta( $course_id, '_lpr_course_enrolled_require', true ) );
343
344 return apply_filters( 'learn_press_course_enroll_required', $required, $course_id );
345 }
346
347 function learn_press_get_all_courses( $args = array() ) {
348 $term = '';
349 $exclude = '';
350 is_array( $args ) && extract( $args );
351 $args = array(
352 'post_type' => array( 'lp_course' ),
353 'post_status' => 'publish',
354 'posts_per_page' => - 1,
355 's' => $term,
356 'fields' => 'ids',
357 'exclude' => $exclude,
358 );
359 $args = apply_filters( 'learn_press_get_courses_args', $args );
360 $posts = get_posts( $args );
361
362 return apply_filters( 'learn_press_get_courses', $posts, $args );
363 }
364
365 function learn_press_search_post_excerpt( $where = '' ) {
366 global $wp_the_query, $wpdb;
367
368 if ( empty( $wp_the_query->query_vars['s'] ) ) {
369 return $where;
370 }
371
372 $where = preg_replace(
373 "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
374 "post_title LIKE $1) OR ({$wpdb->posts}.post_excerpt LIKE $1",
375 $where
376 );
377
378 return $where;
379 }
380
381 // add_filter( 'posts_where', 'learn_press_search_post_excerpt' );
382
383 function learn_press_get_course_user( $course_id = null ) {
384 if ( ! $course_id ) {
385 $course_id = get_the_ID();
386 }
387
388 return learn_press_get_user( get_post_field( 'post_author', $course_id ) );
389 }
390
391 /**
392 * Get item types support in course curriculum.
393 *
394 * @param bool $keys
395 *
396 * @return mixed|null
397 */
398 function learn_press_course_get_support_item_types( $keys = false ) {
399 $types = array();
400 if ( ! empty( $GLOBALS['learn_press_course_support_item_types'] ) ) {
401 $types = $GLOBALS['learn_press_course_support_item_types'];
402 }
403
404 return apply_filters( 'learn-press/course-support-items', $keys ? array_keys( $types ) : $types, $keys );
405 }
406
407 /**
408 * Register new type of course item
409 *
410 * @param string $post_type - Usually is post type
411 * @param string $label - Name show for user
412 */
413 function learn_press_course_add_support_item_type( $post_type, $label = '' ) {
414 if ( empty( $GLOBALS['learn_press_course_support_item_types'] ) ) {
415 $GLOBALS['learn_press_course_support_item_types'] = array();
416 }
417 if ( func_num_args() == 1 && is_array( func_get_arg( 0 ) ) ) {
418 foreach ( func_get_arg( 0 ) as $type => $label ) {
419 learn_press_course_add_support_item_type( $type, $label );
420 }
421 } elseif ( func_num_args() == 2 ) {
422 $GLOBALS['learn_press_course_support_item_types'][ func_get_arg( 0 ) ] = func_get_arg( 1 );
423 }
424 }
425
426 /**
427 * Check if course is support an item's type.
428 *
429 * @param string $type
430 *
431 * @return bool
432 */
433 function learn_press_is_support_course_item_type( $type ) {
434 $types = learn_press_course_get_support_item_types();
435
436 if ( is_array( $type ) ) {
437 $support = true;
438 foreach ( $type as $t ) {
439 $support = $support && learn_press_is_support_course_item_type( $t );
440 }
441 } else {
442 $support = is_string( $type ) && $type && ! empty( $types[ $type ] );
443 }
444
445 return $support;
446 }
447
448 learn_press_course_add_support_item_type(
449 array(
450 'lp_lesson' => __( 'Lesson', 'learnpress' ),
451 'lp_quiz' => __( 'Quiz', 'learnpress' ),
452 )
453 );
454
455 function learn_press_add_course_item_feature( $type, $feature ) {
456 $features = array();
457 if ( ! empty( $GLOBALS['learn_press_course_item_features'] ) ) {
458 $features = $GLOBALS['learn_press_course_item_features'];
459 }
460
461 if ( empty( $features[ $type ] ) ) {
462 $features[ $type ] = array();
463 }
464
465 if ( array_search( $feature, $features ) === false ) {
466 $features[ $type ] = $feature;
467 }
468
469 $GLOBALS['learn_press_course_item_features'] = $features;
470 }
471
472
473 function learn_press_get_course_id() {
474 $course_id = 0;
475 if ( learn_press_is_course() ) {
476 $course_id = get_the_ID();
477 }
478
479 return absint( $course_id );
480 }
481
482 /**
483 * Get the permalink of a course
484 *
485 * @param int $course_id
486 *
487 * @return string
488 * @since 3.0.0
489 *
490 */
491 function learn_press_get_course_permalink( $course_id = 0 ) {
492 if ( $course = learn_press_get_course( $course_id ) ) {
493 return $course->get_permalink();
494 }
495
496 return false;
497 }
498
499
500 /**
501 * Get the permalink of a item in a course
502 *
503 * @param int $course_id
504 * @param int $item_id
505 *
506 * @return string
507 * @since 3.0.0
508 *
509 */
510 function learn_press_get_course_item_permalink( int $course_id = 0, int $item_id = 0 ): string {
511 $course = learn_press_get_course( $course_id );
512 if ( $course ) {
513 return $course->get_item_link( $item_id );
514 }
515
516 return '';
517 }
518
519
520 function learn_press_get_the_course() {
521 static $course;
522 if ( ! $course ) {
523 $course_id = get_the_ID();
524 if ( learn_press_get_post_type( $course ) == LP_COURSE_CPT ) {
525 $course = learn_press_get_course( $course_id );
526 }
527 }
528 if ( ! $course ) {
529 return new LP_Course( 0 );
530 }
531
532 return $course;
533 }
534
535 function learn_press_get_user_question_answer( $args = '' ) {
536 $args = wp_parse_args(
537 $args,
538 array(
539 'question_id' => 0,
540 'history_id' => 0,
541 'quiz_id' => 0,
542 'course_id' => 0,
543 'user_id' => get_current_user_id(),
544 )
545 );
546 $answered = null;
547 if ( $args['history_id'] ) {
548 $user_meta = learn_press_get_user_item_meta( $args['history_id'], 'question_answers', true );
549 if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) {
550 $answered = $user_meta[ $args['question_id'] ];
551 }
552 } elseif ( $args['quiz_id'] && $args['course_id'] ) {
553 $user = learn_press_get_user( $args['user_id'] );
554 $history = $user->get_quiz_results( $args['quiz_id'], $args['course_id'] );
555 if ( $history ) {
556 $user_meta = learn_press_get_user_item_meta( $history->history_id, 'question_answers', true );
557 if ( $user_meta && array_key_exists( $args['question_id'], $user_meta ) ) {
558 $answered = $user_meta[ $args['question_id'] ];
559 }
560 }
561 }
562
563 return $answered;
564 }
565
566 function need_to_updating() {
567 ob_start();
568 learn_press_display_message( 'This function need to updating' );
569
570 return ob_get_clean();
571 }
572
573 /* filter section item single course */
574 function learn_press_get_course_sections() {
575 return apply_filters(
576 'learn_press_get_course_sections',
577 array(
578 'lp_lesson',
579 'lp_quiz',
580 )
581 );
582 }
583
584 function lean_press_get_course_sections() {
585 _deprecated_function( __FUNCTION__, '2.1', 'learn_press_get_course_sections' );
586
587 return learn_press_get_course_sections();
588 }
589
590 if ( ! function_exists( 'learn_press_get_course_item_url' ) ) {
591 function learn_press_get_course_item_url( $course_id = null, $item_id = null ) {
592 $course = learn_press_get_course( $course_id );
593
594 return $course ? $course->get_item_link( $item_id ) : false;
595 }
596 }
597
598 /**
599 * Add filter to WP comment form of lesson or quiz to output ID of current course.
600 *
601 * @param $post_id
602 *
603 * @since 3.0.10
604 *
605 */
606 function learn_press_comment_post_item_course( $post_id ) {
607 $course = learn_press_get_course();
608 if ( ! $course ) {
609 return;
610 }
611
612 echo sprintf( '<input type="hidden" name="comment-post-item-course" value="%d" />', $course->get_id() );
613 }
614
615 add_action( 'comment_form', 'learn_press_comment_post_item_course' );
616
617 function learn_press_item_comment_link( $link, $comment, $args, $cpage ) {
618
619 $comment_post_ID = $comment->comment_post_ID;
620
621 /**
622 * Validate if comment post is an item of course
623 */
624 if ( ! learn_press_is_support_course_item_type( learn_press_get_post_type( $comment_post_ID ) ) ) {
625 return $link;
626 }
627
628 $post_id = 0;
629
630 /**
631 * Ensure there is a course
632 */
633 if ( empty( $_POST['comment-post-item-course'] ) ) {
634 $course = learn_press_get_course();
635 if ( $course ) {
636 $post_id = $course->get_id();
637 }
638 } else {
639 $post_id = absint( $_POST['comment-post-item-course'] );
640 }
641
642 $course = learn_press_get_course( $post_id );
643 if ( $course ) {
644 $link = str_replace( get_the_permalink( $comment_post_ID ), $course->get_item_link( $comment_post_ID ), $link );
645 }
646
647 return $link;
648 }
649
650 add_filter( 'get_comment_link', 'learn_press_item_comment_link', 100, 4 );
651
652 /**
653 * Fix redirection invalid when SG Cache is installed
654 *
655 * @param int $comment_id
656 * @param string $status
657 *
658 * @since 3.0.10
659 * @depecated 4.1.6.9
660 *
661 */
662 /*function learn_press_force_refresh_course( $comment_id, $status ) {
663
664 if ( empty( $_POST['comment-post-item-course'] ) ) {
665 return;
666 }
667
668 $course_id = absint( $_POST['comment-post-item-course'] );
669 $course = learn_press_get_course( $course_id );
670 $curd = new LP_Course_CURD();
671 $curd->load( $course );
672 }
673
674 add_action( 'comment_post', 'learn_press_force_refresh_course', 1000, 2 );*/
675
676 /**
677 * @editor tungnx | comment code
678 * @deprecated 3.2.7.5
679 */
680 /*if ( ! function_exists( 'learn_press_get_sample_link_course_item_url' ) ) {
681
682 function learn_press_get_sample_link_course_item_url( $item_id = null ) {
683
684 if ( ! $item_id ) {
685 return;
686 }
687
688
689 $permalink = get_the_permalink( $item_id );
690 $post_name = get_post_field( 'post_name', $item_id );
691
692 if ( '' != get_option( 'permalink_structure' ) ) {
693 $permalink = $post_name;
694 } else {
695 $key = preg_replace( '!lp_!', '', learn_press_get_post_type( $item_id ) );
696 $permalink = add_query_arg( array( $key => $post_name ), $permalink );
697 }
698
699 return $permalink;
700
701 }
702 }*/
703
704 /**
705 * @depecated 4.1.6.9
706 */
707 /*if ( ! function_exists( 'learn_press_get_nav_course_item_url' ) ) {
708 function learn_press_get_nav_course_item_url( $course_id = null, $item_id = null, $content_only = false ) {
709
710 $course = learn_press_get_course( $course_id );
711 $curriculum_items = $course->get_items();// LP_Helper::maybe_unserialize( $course->post->curriculum_items );
712 $index = array_search( $item_id, $curriculum_items );
713 $return = array(
714 'back' => '',
715 'next' => '',
716 );
717 if ( is_array( $curriculum_items ) ) {
718 if ( array_key_exists( $index - 1, $curriculum_items ) ) {
719 $back_item = get_post( $curriculum_items[ $index - 1 ] );
720 $return['back'] = array(
721 'id' => $back_item->ID,
722 'link' => $course->get_item_link( $curriculum_items[ $index - 1 ] ),
723 'title' => $back_item->post_title,
724 );
725 if ( $content_only ) {
726 $return['back']['link'] .= '?content-item-only=yes';
727 }
728 }
729 if ( array_key_exists( $index + 1, $curriculum_items ) ) {
730 $next_item = get_post( $curriculum_items[ $index + 1 ] );
731 $return['next'] = array(
732 'id' => $next_item->ID,
733 'link' => $course->get_item_link( $curriculum_items[ $index + 1 ] ),
734 'title' => $next_item->post_title,
735 );
736 if ( $content_only ) {
737 $return['next']['link'] .= '?content-item-only=yes';
738 }
739 }
740 }
741
742 return $return;
743 }
744 }*/
745
746 if ( ! function_exists( 'learn_press_edit_item_link' ) ) {
747 /**
748 * Displaying course items navigation
749 *
750 * @param null $item_id
751 * @param null $course_id
752 * @param bool $content_only
753 */
754 function learn_press_edit_item_link( $item_id = null, $course_id = null, $content_only = false ) {
755 $user = learn_press_get_current_user();
756 if ( $user->can_edit_item( $item_id, $course_id ) ) : ?>
757 <p class="edit-course-item-link">
758 <a href="<?php echo get_edit_post_link( $item_id ); ?>">
759 <?php
760 _e(
761 'Edit this item',
762 'learnpress'
763 );
764 ?>
765 </a>
766 </p>
767 <?php
768 endif;
769 }
770 }
771 /**
772 * Get course id of an item by id
773 *
774 * @depecated 4.1.6.9
775 */
776
777 if ( ! function_exists( 'learn_press_get_item_course_id' ) ) {
778
779 function learn_press_get_item_course_id( $post_id, $post_type ) {
780 _deprecated_function( __FUNCTION__, '4.1.6.9' );
781 /*global $wpdb;
782
783 // If the post is a course
784 if ( LP_COURSE_CPT == learn_press_get_post_type( $post_id ) ) {
785 return false;
786 }
787
788 if ( ! $post_types = learn_press_course_get_support_item_types( true ) ) {
789 return false;
790 }
791
792 if ( ! in_array( learn_press_get_post_type( $post_id ), $post_types ) ) {
793 return false;
794 }
795
796 $course_id = false;
797
798 if ( false !== ( $courses = LP_Object_Cache::get( 'item-course-ids', 'learn-press' ) ) ) {
799
800 foreach ( $courses as $course_id => $items ) {
801 if ( in_array( $post_id, $items ) ) {
802 break;
803 }
804 $course_id = false;
805 }
806 } else {
807 $courses = array();
808 }
809
810 if ( false === $course_id ) {
811 $query = $wpdb->prepare(
812 "
813 SELECT section.section_course_id
814 FROM {$wpdb->learnpress_sections} AS section
815 INNER JOIN {$wpdb->learnpress_section_items} AS item
816 ON item.section_id = section.section_id
817 WHERE item.item_id = %d
818 LIMIT 1
819 ",
820 $post_id
821 );
822
823 $course_id = apply_filters( 'learn-press/item-course-id', absint( $wpdb->get_var( $query ) ), $post_id );
824
825 if ( $course = learn_press_get_course( $course_id ) ) {
826 $courses[ $course_id ] = $course->get_items();
827 }
828
829 if ( empty( $courses[ $course_id ] ) ) {
830 $courses[ $course_id ] = array();
831 }
832
833 if ( ! in_array( $post_id, $courses[ $course_id ] ) ) {
834 $courses[ $course_id ][] = $post_id;
835 }
836 LP_Object_Cache::set( 'item-course-ids', $courses, 'learn-press' );
837
838 }
839
840 return $course_id;*/
841 }
842 }
843
844 /**
845 * @editor tungnx | comment code
846 * @deprecated 3.2.7.5
847 */
848 /*function learn_press_item_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) {
849 remove_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10 );
850
851 $return = sprintf(
852 '<a class="button" href="%s" target="_blank">%s</a>',
853 learn_press_get_preview_url( $post_id ),
854 __( 'Preview', 'learnpress' )
855 );
856
857 $return .= '<span>' . __( 'Permalink only available if the item is already assigned to a course.', 'learnpress' ) . '</span>';
858
859 return sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $return );
860 }*/
861
862 /**
863 * @editor tungnx | comment code
864 * @deprecated 3.2.7.5
865 */
866 /*if ( ! function_exists( 'learn_press_item_sample_permalink' ) ) {
867
868 function learn_press_item_sample_permalink( $permalink, $post_id, $title, $name, $post ) {
869 if ( ! in_array( $post->post_type, learn_press_course_get_support_item_types( true ) ) ) {
870 return $permalink;
871 }
872
873 $permalink[0] = str_replace( $post->post_name, '%pagename%', $permalink[0] );
874
875 if ( ! preg_match( '~^https?://~', $permalink[0] ) ) {
876 add_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10, 5 );
877 }
878
879 return $permalink;
880 }
881
882 }*/
883 //add_filter( 'get_sample_permalink', 'learn_press_item_sample_permalink', 10, 5 );
884
885 /**
886 * Get preview url for LP post type.
887 *
888 * @param int $post_id
889 *
890 * @return string
891 * @since 3.0.0
892 *
893 */
894 function learn_press_get_preview_url( $post_id ) {
895 return
896 esc_url_raw(
897 add_query_arg(
898 array(
899 'lp-preview' => $post_id,
900 '_wpnonce' => wp_create_nonce( 'lp-preview' ),
901 ),
902 trailingslashit( get_home_url() )
903 )
904 );
905 }
906
907 if ( ! function_exists( 'learn_press_course_item_type_link' ) ) {
908 /**
909 * Add filter to WP custom post-type-link to edit the link of item
910 * with the link of it's course.
911 *
912 * @updated 12 Nov 2018
913 *
914 * @param string $post_link
915 * @param WP_Post $post
916 * @param bool $leavename
917 * @param bool $sample
918 *
919 * @editor tungnx | comment code
920 * @return string
921 * @deprecated 3.2.7.4
922 */
923 /*function learn_press_course_item_type_link( $post_link, $post, $leavename, $sample ) {
924
925 remove_filter( 'post_type_link', 'learn_press_course_item_type_link', 10 );
926
927 $course = learn_press_get_course();
928
929 if ( ! $course && ( $course_id = learn_press_get_item_course( $post->ID ) ) ) {
930 $course = learn_press_get_course( $course_id );
931 }
932
933 if ( learn_press_is_support_course_item_type( $post->post_type ) ) {
934 // Check elementor installed and activated
935 if ( did_action( 'elementor/loaded' ) ) {
936 // do stuff for edit mode
937 if ( ! Elementor\Plugin::$instance->editor->is_edit_mode() ) {
938 if ( $course ) {
939 $post_link = $course->get_item_link( $post->ID );
940 } else {
941 $post_link = learn_press_get_sample_link_course_item_url( $post->ID );
942 }
943 }
944 } else {
945 if ( $course ) {
946 $post_link = $course->get_item_link( $post->ID );
947 } else {
948 $post_link = learn_press_get_sample_link_course_item_url( $post->ID );
949 }
950 }
951 }
952
953 add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 );
954
955 return $post_link;
956 }*/
957 }
958 //add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 );
959
960 /**
961 * Get grade course type can translate
962 *
963 * @param string $grade
964 * @param bool $echo
965 *
966 * @return mixed|void
967 */
968 function learn_press_course_grade_html( string $grade = '', bool $echo = true ) {
969 switch ( $grade ) {
970 case 'passed':
971 $html = __( 'Passed', 'learnpress' );
972 break;
973 case 'failed':
974 $html = __( 'Failed', 'learnpress' );
975 break;
976 case 'in-progress':
977 $html = __( 'In Progress', 'learnpress' );
978 break;
979 default:
980 $html = $grade;
981 break;
982 }
983
984 // @since 3.0.0
985 $html = apply_filters( 'learn-press/course/grade-html', $html, $grade );
986
987 if ( $echo ) {
988 echo wp_kses_post( $html );
989 }
990
991 return $html;
992 }
993
994 function learn_press_get_course_results_tooltip( $course_id ) {
995 $metabox = LP_Course_Post_Type::assessment_meta_box();
996 $options = $metabox['fields'][0]['options'];
997 $cr = get_post_meta( $course_id, '_lp_course_result', true );
998 $tooltip = ! empty( $options[ $cr ] ) ? $options[ $cr ] : false;
999 if ( $tooltip ) {
1000 if ( preg_match_all( '~<p.*>(.*)<\/p>~im', $tooltip, $matches ) ) {
1001 $tooltip = $matches[1][0];
1002 }
1003 }
1004
1005 return $tooltip;
1006 }
1007
1008 function learn_press_course_passing_condition( $value, $format, $course_id ) {
1009 $course = learn_press_get_course( $course_id );
1010 $quiz_id = $course->get_final_quiz();
1011 $evalution = get_post_meta( $course_id, '_lp_course_result', true );
1012
1013 if ( $quiz_id && $evalution === 'evaluate_final_quiz' ) {
1014 $quiz = learn_press_get_quiz( $quiz_id );
1015 $value = absint( $quiz->get_passing_grade() );
1016
1017 if ( $format ) {
1018 $value = "{$value}%";
1019 }
1020 }
1021
1022 return $value;
1023 }
1024
1025 add_filter( 'learn-press/course-passing-condition', 'learn_press_course_passing_condition', 10, 3 );
1026
1027 function learn_press_remove_query_var_enrolled_course( $redirect ) {
1028 return esc_url_raw( remove_query_arg( 'enroll-course', $redirect ) );
1029 }
1030
1031 add_filter( 'learn-press/enroll-course-redirect', 'learn_press_remove_query_var_enrolled_course' );
1032
1033 /**
1034 * Mark the user to know if they have just logged in
1035 * for some purpose.
1036 *
1037 * @since 3.0.0
1038 */
1039 function learn_press_mark_user_just_logged_in() {
1040 LearnPress::instance()->session->set( 'user_just_logged_in', 'yes' );
1041 }
1042
1043 add_action( 'wp_login', 'learn_press_mark_user_just_logged_in' );
1044
1045 function learn_press_translate_course_result_required( $course ) {
1046 if ( ! $course ) {
1047 return '';
1048 }
1049
1050 $passing_condition = $course->get_passing_condition();
1051
1052 $evaluate_type = $course->get_data( 'course_result', 'evaluate_lesson' );
1053 switch ( $evaluate_type ) {
1054 case 'evaluate_lesson':
1055 $label = esc_html__( 'lessons completed per total number of lessons.', 'learnpress' );
1056 break;
1057 case 'evaluate_quiz':
1058 $label = esc_html__( 'quizzes passed per total number of quizzes.', 'learnpress' );
1059 break;
1060 case 'evaluate_final_quiz':
1061 $label = esc_html__( 'Final Quiz', 'learnpress' );
1062 break;
1063 case 'evaluate_questions':
1064 $label = esc_html__( 'correct answers per total number of questions.', 'learnpress' );
1065 break;
1066 case 'evaluate_mark':
1067 $label = esc_html__( 'score achieved per total score of the questions.', 'learnpress' );
1068 break;
1069 default:
1070 $label = apply_filters( 'learnpress/message/evaluate/' . $evaluate_type, $evaluate_type );
1071 break;
1072 }
1073
1074 return apply_filters(
1075 'learnpress/message/evaluate',
1076 wp_sprintf( '%1$s %2$s %3$s', __( 'Require', 'learnpress' ), $passing_condition . '%', $label )
1077 );
1078 }
1079