PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.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.6.9.2, at inc/course/lp-course-functions.php

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