ld_student.php
4.42 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* LearnDash `[student]` shortcode processing.
*
* @since 2.1.0
*
* @package LearnDash\Shortcodes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Builds the `[student]` shortcode output.
*
* Shortcode to display content to users that have access to current course ID.
*
* @global boolean $learndash_shortcode_used
*
* @since 2.1.0
*
* @param array $atts {
* An array of shortcode attributes.
*
* @type int $course_id Course ID. Default current course ID.
* @type int $user_id User ID. Default current user ID.
* @type string $content The shortcode content. Default null.
* @type boolean $autop Whether to replace line breaks with paragraph elements. Default true.
* }
* @param string $content The shortcode content. Default empty.
* @param string $shortcode_slug The shortcode slug. Default 'student'.
*
* @return string The `student` shortcode output.
*/
function learndash_student_check_shortcode( $atts = array(), $content = '', $shortcode_slug = 'student' ) {
global $learndash_shortcode_used;
if ( ( ! empty( $content ) ) && ( is_user_logged_in() ) ) {
if ( ! is_array( $atts ) ) {
if ( ! empty( $atts ) ) {
$atts = array( $atts );
} else {
$atts = array();
}
}
$defaults = array(
'course_id' => '',
'group_id' => '',
'user_id' => get_current_user_id(),
'content' => $content,
'autop' => true,
);
$atts = wp_parse_args( $atts, $defaults );
/** This filter is documented in includes/shortcodes/ld_course_resume.php */
$atts = apply_filters( 'learndash_shortcode_atts', $atts, $shortcode_slug );
if ( ( true === $atts['autop'] ) || ( 'true' === $atts['autop'] ) || ( '1' === $atts['autop'] ) ) {
$atts['autop'] = true;
} else {
$atts['autop'] = false;
}
if ( ! empty( $atts['course_id'] ) ) {
if ( learndash_get_post_type_slug( 'course' ) !== get_post_type( $atts['course_id'] ) ) {
$atts['course_id'] = 0;
}
}
if ( ! empty( $atts['group_id'] ) ) {
if ( learndash_get_post_type_slug( 'group' ) !== get_post_type( $atts['group_id'] ) ) {
$atts['group_id'] = 0;
}
}
// If 'course_id' and 'group_id' are empty we check if we are showing a Course or Group related post type.
if ( ( empty( $atts['course_id'] ) ) && ( empty( $atts['group_id'] ) ) ) {
$viewed_post_id = (int) get_the_ID();
if ( ! empty( $viewed_post_id ) ) {
if ( in_array( get_post_type( $viewed_post_id ), learndash_get_post_types( 'course' ), true ) ) {
$atts['course_id'] = learndash_get_course_id( $viewed_post_id );
} elseif ( get_post_type( $viewed_post_id ) === learndash_get_post_type_slug( 'group' ) ) {
$atts['group_id'] = $viewed_post_id;
}
}
}
/**
* Filters student shortcode attributes.
*
* @param array $attributes An array of student shortcode attributes.
*/
$atts = apply_filters( 'learndash_student_shortcode_atts', $atts );
$atts['user_id'] = absint( $atts['user_id'] );
$atts['group_id'] = absint( $atts['group_id'] );
$atts['course_id'] = absint( $atts['course_id'] );
$view_content = false;
if ( ( ! empty( $atts['user_id'] ) ) && ( get_current_user_id() === $atts['user_id'] ) ) {
if ( ! empty( $atts['course_id'] ) ) {
// The reason we are doing this check is because 'sfwd_lms_has_access' will return true if the course does not exist.
// This needs to be changed to return some other value because true signals the calling function that all is well.
$course_id = learndash_get_course_id( $atts['course_id'] );
if ( (int) $course_id === (int) $atts['course_id'] ) {
if ( sfwd_lms_has_access( $atts['course_id'], $atts['user_id'] ) ) {
$view_content = true;
}
}
} elseif ( ! empty( $atts['group_id'] ) ) {
if ( learndash_is_user_in_group( $atts['user_id'], $atts['group_id'] ) ) {
$view_content = true;
}
}
}
if ( $view_content ) {
$learndash_shortcode_used = true;
$atts['content'] = do_shortcode( $atts['content'] );
$shortcode_out = SFWD_LMS::get_template(
'learndash_course_student_message',
array(
'shortcode_atts' => $atts,
),
false
);
if ( ! empty( $shortcode_out ) ) {
$content = '<div class="learndash-wrapper learndash-wrap learndash-shortcode-wrap">' . $shortcode_out . '</div>';
}
} else {
$content = '';
}
}
if ( ! is_user_logged_in() ) {
$content = '';
}
return $content;
}
add_shortcode( 'student', 'learndash_student_check_shortcode' );