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

1,051 lines 26.0 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 * @deprecated 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 * @deprecated 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 * @deprecated 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 * @deprecated 4.1.6.9
678 */
679 /*if ( ! function_exists( 'learn_press_get_nav_course_item_url' ) ) {
680 function learn_press_get_nav_course_item_url( $course_id = null, $item_id = null, $content_only = false ) {
681
682 $course = learn_press_get_course( $course_id );
683 $curriculum_items = $course->get_items();// LP_Helper::maybe_unserialize( $course->post->curriculum_items );
684 $index = array_search( $item_id, $curriculum_items );
685 $return = array(
686 'back' => '',
687 'next' => '',
688 );
689 if ( is_array( $curriculum_items ) ) {
690 if ( array_key_exists( $index - 1, $curriculum_items ) ) {
691 $back_item = get_post( $curriculum_items[ $index - 1 ] );
692 $return['back'] = array(
693 'id' => $back_item->ID,
694 'link' => $course->get_item_link( $curriculum_items[ $index - 1 ] ),
695 'title' => $back_item->post_title,
696 );
697 if ( $content_only ) {
698 $return['back']['link'] .= '?content-item-only=yes';
699 }
700 }
701 if ( array_key_exists( $index + 1, $curriculum_items ) ) {
702 $next_item = get_post( $curriculum_items[ $index + 1 ] );
703 $return['next'] = array(
704 'id' => $next_item->ID,
705 'link' => $course->get_item_link( $curriculum_items[ $index + 1 ] ),
706 'title' => $next_item->post_title,
707 );
708 if ( $content_only ) {
709 $return['next']['link'] .= '?content-item-only=yes';
710 }
711 }
712 }
713
714 return $return;
715 }
716 }*/
717
718 if ( ! function_exists( 'learn_press_edit_item_link' ) ) {
719 /**
720 * Displaying course items navigation
721 *
722 * @param null $item_id
723 * @param null $course_id
724 * @param bool $content_only
725 */
726 function learn_press_edit_item_link( $item_id = null, $course_id = null, $content_only = false ) {
727 $user = learn_press_get_current_user();
728 if ( $user->can_edit_item( $item_id, $course_id ) ) : ?>
729 <p class="edit-course-item-link">
730 <a href="<?php echo get_edit_post_link( $item_id ); ?>">
731 <?php
732 _e(
733 'Edit this item',
734 'learnpress'
735 );
736 ?>
737 </a>
738 </p>
739 <?php
740 endif;
741 }
742 }
743 /**
744 * Get course id of an item by id
745 *
746 * @deprecated 4.1.6.9
747 */
748
749 if ( ! function_exists( 'learn_press_get_item_course_id' ) ) {
750
751 function learn_press_get_item_course_id( $post_id, $post_type ) {
752 _deprecated_function( __FUNCTION__, '4.1.6.9' );
753 /*global $wpdb;
754
755 // If the post is a course
756 if ( LP_COURSE_CPT == learn_press_get_post_type( $post_id ) ) {
757 return false;
758 }
759
760 if ( ! $post_types = learn_press_course_get_support_item_types( true ) ) {
761 return false;
762 }
763
764 if ( ! in_array( learn_press_get_post_type( $post_id ), $post_types ) ) {
765 return false;
766 }
767
768 $course_id = false;
769
770 if ( false !== ( $courses = LP_Object_Cache::get( 'item-course-ids', 'learn-press' ) ) ) {
771
772 foreach ( $courses as $course_id => $items ) {
773 if ( in_array( $post_id, $items ) ) {
774 break;
775 }
776 $course_id = false;
777 }
778 } else {
779 $courses = array();
780 }
781
782 if ( false === $course_id ) {
783 $query = $wpdb->prepare(
784 "
785 SELECT section.section_course_id
786 FROM {$wpdb->learnpress_sections} AS section
787 INNER JOIN {$wpdb->learnpress_section_items} AS item
788 ON item.section_id = section.section_id
789 WHERE item.item_id = %d
790 LIMIT 1
791 ",
792 $post_id
793 );
794
795 $course_id = apply_filters( 'learn-press/item-course-id', absint( $wpdb->get_var( $query ) ), $post_id );
796
797 if ( $course = learn_press_get_course( $course_id ) ) {
798 $courses[ $course_id ] = $course->get_items();
799 }
800
801 if ( empty( $courses[ $course_id ] ) ) {
802 $courses[ $course_id ] = array();
803 }
804
805 if ( ! in_array( $post_id, $courses[ $course_id ] ) ) {
806 $courses[ $course_id ][] = $post_id;
807 }
808 LP_Object_Cache::set( 'item-course-ids', $courses, 'learn-press' );
809
810 }
811
812 return $course_id;*/
813 }
814 }
815
816 /**
817 * @editor tungnx | comment code
818 * @deprecated 3.2.7.5
819 */
820 /*function learn_press_item_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) {
821 remove_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10 );
822
823 $return = sprintf(
824 '<a class="button" href="%s" target="_blank">%s</a>',
825 learn_press_get_preview_url( $post_id ),
826 __( 'Preview', 'learnpress' )
827 );
828
829 $return .= '<span>' . __( 'Permalink only available if the item is already assigned to a course.', 'learnpress' ) . '</span>';
830
831 return sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $return );
832 }*/
833
834 /**
835 * @editor tungnx | comment code
836 * @deprecated 3.2.7.5
837 */
838 /*if ( ! function_exists( 'learn_press_item_sample_permalink' ) ) {
839
840 function learn_press_item_sample_permalink( $permalink, $post_id, $title, $name, $post ) {
841 if ( ! in_array( $post->post_type, learn_press_course_get_support_item_types( true ) ) ) {
842 return $permalink;
843 }
844
845 $permalink[0] = str_replace( $post->post_name, '%pagename%', $permalink[0] );
846
847 if ( ! preg_match( '~^https?://~', $permalink[0] ) ) {
848 add_filter( 'get_sample_permalink_html', 'learn_press_item_sample_permalink_html', 10, 5 );
849 }
850
851 return $permalink;
852 }
853
854 }*/
855 //add_filter( 'get_sample_permalink', 'learn_press_item_sample_permalink', 10, 5 );
856
857 /**
858 * Get preview url for LP post type.
859 *
860 * @param int $post_id
861 *
862 * @return string
863 * @since 3.0.0
864 *
865 */
866 function learn_press_get_preview_url( $post_id ) {
867 return
868 esc_url_raw(
869 add_query_arg(
870 array(
871 'lp-preview' => $post_id,
872 '_wpnonce' => wp_create_nonce( 'lp-preview' ),
873 ),
874 trailingslashit( get_home_url() )
875 )
876 );
877 }
878
879 if ( ! function_exists( 'learn_press_course_item_type_link' ) ) {
880 /**
881 * Add filter to WP custom post-type-link to edit the link of item
882 * with the link of it's course.
883 *
884 * @updated 12 Nov 2018
885 *
886 * @param string $post_link
887 * @param WP_Post $post
888 * @param bool $leavename
889 * @param bool $sample
890 *
891 * @editor tungnx | comment code
892 * @return string
893 * @deprecated 3.2.7.4
894 */
895 /*function learn_press_course_item_type_link( $post_link, $post, $leavename, $sample ) {
896
897 remove_filter( 'post_type_link', 'learn_press_course_item_type_link', 10 );
898
899 $course = learn_press_get_course();
900
901 if ( ! $course && ( $course_id = learn_press_get_item_course( $post->ID ) ) ) {
902 $course = learn_press_get_course( $course_id );
903 }
904
905 if ( learn_press_is_support_course_item_type( $post->post_type ) ) {
906 // Check elementor installed and activated
907 if ( did_action( 'elementor/loaded' ) ) {
908 // do stuff for edit mode
909 if ( ! Elementor\Plugin::$instance->editor->is_edit_mode() ) {
910 if ( $course ) {
911 $post_link = $course->get_item_link( $post->ID );
912 } else {
913 $post_link = learn_press_get_sample_link_course_item_url( $post->ID );
914 }
915 }
916 } else {
917 if ( $course ) {
918 $post_link = $course->get_item_link( $post->ID );
919 } else {
920 $post_link = learn_press_get_sample_link_course_item_url( $post->ID );
921 }
922 }
923 }
924
925 add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 );
926
927 return $post_link;
928 }*/
929 }
930 //add_filter( 'post_type_link', 'learn_press_course_item_type_link', 10, 4 );
931
932 /**
933 * Get grade course type can translate
934 *
935 * @param string $grade
936 * @param bool $echo
937 *
938 * @return mixed|void
939 */
940 function learn_press_course_grade_html( string $grade = '', bool $echo = true ) {
941 switch ( $grade ) {
942 case 'passed':
943 $html = __( 'Passed', 'learnpress' );
944 break;
945 case 'failed':
946 $html = __( 'Failed', 'learnpress' );
947 break;
948 case 'in-progress':
949 $html = __( 'In Progress', 'learnpress' );
950 break;
951 default:
952 $html = $grade;
953 break;
954 }
955
956 // @since 3.0.0
957 $html = apply_filters( 'learn-press/course/grade-html', $html, $grade );
958
959 if ( $echo ) {
960 echo wp_kses_post( $html );
961 }
962
963 return $html;
964 }
965
966 function learn_press_get_course_results_tooltip( $course_id ) {
967 $metabox = LP_Course_Post_Type::assessment_meta_box();
968 $options = $metabox['fields'][0]['options'];
969 $cr = get_post_meta( $course_id, '_lp_course_result', true );
970 $tooltip = ! empty( $options[ $cr ] ) ? $options[ $cr ] : false;
971 if ( $tooltip ) {
972 if ( preg_match_all( '~<p.*>(.*)<\/p>~im', $tooltip, $matches ) ) {
973 $tooltip = $matches[1][0];
974 }
975 }
976
977 return $tooltip;
978 }
979
980 function learn_press_course_passing_condition( $value, $format, $course_id ) {
981 $course = learn_press_get_course( $course_id );
982 $quiz_id = $course->get_final_quiz();
983 $evalution = get_post_meta( $course_id, '_lp_course_result', true );
984
985 if ( $quiz_id && $evalution === 'evaluate_final_quiz' ) {
986 $quiz = learn_press_get_quiz( $quiz_id );
987 $value = absint( $quiz->get_passing_grade() );
988
989 if ( $format ) {
990 $value = "{$value}%";
991 }
992 }
993
994 return $value;
995 }
996
997 add_filter( 'learn-press/course-passing-condition', 'learn_press_course_passing_condition', 10, 3 );
998
999 function learn_press_remove_query_var_enrolled_course( $redirect ) {
1000 return esc_url_raw( remove_query_arg( 'enroll-course', $redirect ) );
1001 }
1002
1003 add_filter( 'learn-press/enroll-course-redirect', 'learn_press_remove_query_var_enrolled_course' );
1004
1005 /**
1006 * Mark the user to know if they have just logged in
1007 * for some purpose.
1008 *
1009 * @since 3.0.0
1010 */
1011 function learn_press_mark_user_just_logged_in() {
1012 LearnPress::instance()->session->set( 'user_just_logged_in', 'yes' );
1013 }
1014
1015 add_action( 'wp_login', 'learn_press_mark_user_just_logged_in' );
1016
1017 function learn_press_translate_course_result_required( $course ) {
1018 if ( ! $course ) {
1019 return '';
1020 }
1021
1022 $passing_condition = $course->get_passing_condition();
1023
1024 $evaluate_type = $course->get_data( 'course_result', 'evaluate_lesson' );
1025 switch ( $evaluate_type ) {
1026 case 'evaluate_lesson':
1027 $label = esc_html__( 'completed lessons per the total number of lessons.', 'learnpress' );
1028 break;
1029 case 'evaluate_quiz':
1030 $label = esc_html__( 'passed quizzes per the total number of quizzes.', 'learnpress' );
1031 break;
1032 case 'evaluate_final_quiz':
1033 $label = esc_html__( 'Final Quiz', 'learnpress' );
1034 break;
1035 case 'evaluate_questions':
1036 $label = esc_html__( 'correct answers per the total number of questions.', 'learnpress' );
1037 break;
1038 case 'evaluate_mark':
1039 $label = esc_html__( 'score achieved per the total score of the questions.', 'learnpress' );
1040 break;
1041 default:
1042 $label = apply_filters( 'learnpress/message/evaluate/' . $evaluate_type, $evaluate_type );
1043 break;
1044 }
1045
1046 return apply_filters(
1047 'learnpress/message/evaluate',
1048 wp_sprintf( '%1$s %2$s %3$s', __( 'Require', 'learnpress' ), $passing_condition . '%', $label )
1049 );
1050 }
1051