-
Notifications
You must be signed in to change notification settings - Fork 199
/
class-sensei-learner-profiles.php
executable file
·297 lines (259 loc) · 7.82 KB
/
class-sensei-learner-profiles.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* All functionality pertaining to the learner profiles in Sensei.
*
* @package Views
* @author Automattic
*
* @since 1.4.0
*/
class Sensei_Learner_Profiles {
/**
* Constructor.
*
* @since 1.4.0
*/
public function __construct() {
// Setup permalink structure for learner profiles
add_action( 'init', array( $this, 'setup_permastruct' ) );
add_filter( 'wp_title', array( $this, 'page_title' ), 10, 2 );
// Scripts for frontend.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Set heading for courses section of learner profiles
add_action( 'sensei_learner_profile_info', array( $this, 'learner_profile_courses_heading' ), 30, 1 );
// Add class to body tag
add_filter( 'body_class', array( $this, 'learner_profile_body_class' ), 10, 1 );
}
/**
* Get the profile URL base.
*
* @return string
*/
private function get_profile_url_base() {
return (string) apply_filters( 'sensei_learner_profiles_url_base', __( 'learner', 'sensei-lms' ) );
}
/**
* Enqueue frontend JavaScripts.
*
* @since 3.0.0
* @access private
*/
public function enqueue_scripts() {
global $wp_query;
if ( ! Sensei_Utils::get_setting_as_flag( 'js_disable', 'sensei_settings_js_disable' ) &&
isset( $wp_query->query_vars['learner_profile'] ) ) {
wp_enqueue_script( Sensei()->token . '-user-dashboard' );
}
}
/**
* Setup permalink structure for learner profiles
*
* @since 1.4.0
* @return void
*/
public function setup_permastruct() {
if ( isset( Sensei()->settings->settings['learner_profile_enable'] )
&& Sensei()->settings->settings['learner_profile_enable'] ) {
add_rewrite_rule( '^' . $this->get_profile_url_base() . '/([^/]*)/?', 'index.php?learner_profile=$matches[1]', 'top' );
add_rewrite_tag( '%learner_profile%', '([^&]+)' );
}
}
/**
* Adding page title for course results page
*
* @param string $title Original title
* @param string $sep Seeparator string
* @return string Modified title
*/
public function page_title( $title, $sep = null ) {
global $wp_query;
if ( isset( $wp_query->query_vars['learner_profile'] ) ) {
$query_var = $wp_query->query_vars['learner_profile'];
$learner_user = Sensei_Learner::find_by_query_var( $query_var );
if ( false === $learner_user ) {
return $title;
}
$name = Sensei_Learner::get_full_name( $learner_user->ID );
/**
* Filter the title for the learner profile page.
*
* @hook sensei_learner_profile_courses_heading
*
* @param {string} $title The title.
* @return {string} The filtered title.
*/
$title = apply_filters(
'sensei_learner_profile_courses_heading',
// translators: Placeholder is the name of the student.
sprintf( __( 'Courses %s is taking', 'sensei-lms' ), $name )
) . ' ' . $sep . ' ';
}
return $title;
}
/**
* Get permalink for learner profile
*
* @since 1.4.0
* @param integer $user_id ID of user
* @return string The learner profile permalink
*/
public function get_permalink( $user_id = 0 ) {
$user = false;
if ( $user_id == 0 ) {
global $current_user;
wp_get_current_user();
$user = $current_user;
} else {
$user = get_userdata( $user_id );
}
$permalink = '';
if ( $user ) {
if ( get_option( 'permalink_structure' ) ) {
$permalink = trailingslashit( get_home_url() ) . $this->get_profile_url_base() . '/' . $user->user_nicename;
} else {
$permalink = trailingslashit( get_home_url() ) . '?learner_profile=' . $user->user_nicename;
}
}
/**
* This allows filtering of the Learner Profile permalinks.
*
* @since 1.9.13
*
* @hook sensei_learner_profile_permalink
*
* @param {string} $permalink The profile permalink.
* @param {object} $user The user object.
* @return {string} The filtered permalink.
*/
return apply_filters( 'sensei_learner_profile_permalink', $permalink, $user );
}
/**
* Set heading for courses section of learner profiles
*
* @since 1.4.0
* @param object $user Queried user object
* @return void
*/
public function learner_profile_courses_heading( $user ) {
if ( strlen( $user->first_name ) > 0 ) {
$name = $user->first_name;
} else {
$name = $user->display_name;
}
/**
* Filter the name for the heading in the courses section of the learner profile.
*
* @hook sensei_learner_profile_courses_heading_name
*
* @param {string} $name The name.
* @return {string} The filtered name.
*/
$name = apply_filters( 'sensei_learner_profile_courses_heading_name', $name );
/**
* Filter the title for the learner profile page.
*
* @hook sensei_learner_profile_courses_heading
*
* @param {string} $title The title.
* @return {string} The filtered title.
*/
$title = apply_filters(
'sensei_learner_profile_courses_heading',
// translators: Placeholder is the name of the student.
sprintf( __( 'Courses %s is taking', 'sensei-lms' ), $name )
);
echo '<h2>' . wp_kses_post( $title ) . '</h2>';
}
/**
* Load user info for learner profiles
*
* @since 1.4.0
* @param object $user Queried user object
* @return void
*/
public static function user_info( $user ) {
/**
* This hooke fires inside the Sensei_Learner_Profiles::user_info function.
* Just before the HTML is generated.
*
* @since 1.0.0
*
* @hook sensei_learner_profile_info
*
* @param {object} $user The user object.
*/
do_action( 'sensei_learner_profile_info', $user );
/**
* This filter runs inside the Sensei_Learner_Profiles::user_info function.
* Here you can change the user avatar.
*
* @since 1.0.0
*
* @hook sensei_learner_profile_info_avatar
*
* @param {false|string} $user_avatar The user avatar.
* @param {int} $user_id The user ID.
* @return {false|string} The filtered avatar.
*/
$learner_avatar = apply_filters( 'sensei_learner_profile_info_avatar', get_avatar( $user->ID, 120 ), $user->ID );
/**
* This filter runs inside the Sensei_Learner_Profiles::user_info function.
* Here you can change the learner profile user display name.
*
* @since 1.0.0
*
* @hook sensei_learner_profile_info_name
*
* @param {string} $user_display_name The user display name.
* @param {string} $user_id The user ID.
* @return {string} The filtered display name.
*/
$learner_name = apply_filters( 'sensei_learner_profile_info_name', $user->display_name, $user->ID );
/**
* This filter runs inside the Sensei_Learner_Profiles::user_info function.
* With this filter can change the users description on the learner user info
* output.
*
* @since 1.0.0
*
* @hook sensei_learner_profile_info_bio
*
* @param {string} $user_description The user description.
* @param {string} $user_id The user ID.
* @return {string} The filtered description.
*/
$learner_bio = apply_filters( 'sensei_learner_profile_info_bio', $user->description, $user->ID );
?>
<div id="learner-info">
<div class="learner-avatar"><?php echo wp_kses_post( $learner_avatar ); ?></div>
<div class="learner-content">
<h2><?php echo esc_html( $learner_name ); ?></h2>
<div class="description"><?php echo wp_kses_post( wpautop( $learner_bio ) ); ?></div>
</div>
</div>
<?php
}
/**
* Adding class to body tag
*
* @param array $classes Existing classes
* @return array Modified classes
*/
public function learner_profile_body_class( $classes ) {
global $wp_query;
if ( isset( $wp_query->query_vars['learner_profile'] ) ) {
$classes[] = 'learner-profile';
}
return $classes;
}
}
/**
* Class WooThemes_Sensei_Learner_Profiles
*
* @ignore only for backward compatibility
* @since 1.9.0
*/
class WooThemes_Sensei_Learner_Profiles extends Sensei_Learner_Profiles {}