ld_course_progress.php
2.96 KB
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
<?php
/**
* LearnDash `[learndash_course_progress]` shortcode processing.
*
* @since 2.1.0
* @package LearnDash\Shortcodes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Builds the `[learndash_course_progress]` shortcode output.
*
* @since 2.1.0
*
* @global boolean $learndash_shortcode_used
*
* @param array $atts {
* An array of shortcode attributes.
*
* @type int $course_id Course ID. Default 0.
* @type int $user_id User ID. Default 0.
* @type boolean $array Whether to return array. Default false.
* }
* @param string $content The shortcode content. Default empty.
* @param string $shortcode_slug The shortcode slug. Default 'learndash_course_progress'.
*
* @return string|array The `learndash_course_progress` shortcode output.
*/
function learndash_course_progress( $atts = array(), $content = '', $shortcode_slug = 'learndash_course_progress' ) {
global $learndash_shortcode_used;
$learndash_shortcode_used = true;
$atts = shortcode_atts(
array(
'course_id' => 0,
'user_id' => 0,
'array' => false,
),
$atts
);
if ( empty( $atts['user_id'] ) ) {
if ( is_user_logged_in() ) {
$atts['user_id'] = get_current_user_id();
}
}
if ( empty( $atts['course_id'] ) ) {
$atts['course_id'] = learndash_get_course_id();
}
if ( ( empty( $atts['user_id'] ) ) || ( empty( $atts['course_id'] ) ) ) {
if ( $atts['array'] ) {
return array(
'percentage' => 0,
'completed' => 0,
'total' => 0,
);
} else {
return '';
}
}
/** This filter is documented in includes/shortcodes/ld_course_resume.php */
$atts = apply_filters( 'learndash_shortcode_atts', $atts, $shortcode_slug );
$completed = 0;
$total = 0;
$percentage = 0;
$course_progress = learndash_user_get_course_progress( $atts['user_id'], $atts['course_id'] );
if ( isset( $course_progress['completed'] ) ) {
$completed = absint( $course_progress['completed'] );
}
if ( isset( $course_progress['total'] ) ) {
$total = absint( $course_progress['total'] );
}
if ( ( isset( $course_progress['status'] ) ) && ( 'completed' === $course_progress['status'] ) ) {
$completed = $total;
}
if ( $total > 0 ) {
$percentage = intval( $completed * 100 / $total );
$percentage = ( $percentage > 100 ) ? 100 : $percentage;
}
// translators: placeholders: completed steps, total steps.
$message = sprintf( esc_html_x( '%1$d out of %2$d steps completed', 'placeholders: completed steps, total steps', 'learndash' ), $completed, $total );
if ( $atts['array'] ) {
return array(
'percentage' => $percentage,
'completed' => $completed,
'total' => $total,
);
}
return SFWD_LMS::get_template(
'course_progress_widget',
array(
'user_id' => $atts['user_id'],
'course_id' => $atts['course_id'],
'message' => $message,
'percentage' => $percentage,
'completed' => $completed,
'total' => $total,
)
);
}
add_shortcode( 'learndash_course_progress', 'learndash_course_progress', 10, 3 );