-
Notifications
You must be signed in to change notification settings - Fork 199
/
class-sensei-guest-user.php
515 lines (462 loc) · 12.6 KB
/
class-sensei-guest-user.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
* Guest User
*
* Handles operations related to allowing guest users take a course.
*
* @package Sensei\Frontend
* @since 1.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Sensei Guest User Class.
*
* @author Automattic
*
* @since 4.11.0
* @package Core
*/
class Sensei_Guest_User {
/**
* Name of the Role for Guest Users.
*
* @since 4.11.0
*
* @var string
*/
const ROLE = 'guest_student';
/**
* Guest user login name prefix.
*
* @since 4.11.0
*
* @var string
*/
const LOGIN_PREFIX = 'sensei_guest_';
/**
* Email domain used for guest users.
*
* @since 4.14.0
*
* @var string
*/
const EMAIL_DOMAIN = 'guest.senseilms';
/**
* Guest user id.
*
* @since 4.11.0
*
* @var int
*/
private $guest_user_id = 0;
/**
* Meta key for course open access setting.
*
* @since 4.11.0
*
* @var string
*/
const COURSE_OPEN_ACCESS_META = '_open_access';
/**
* List of actions to create a guest user for if the course is open access.
*
* @var array[] {
* @type string $field Form field.
* @type string $nonce Nonce field.
* @type bool $enrol Whether to enrol the guest user before this action.
* }
*/
protected $supported_actions = [
// Take course.
[
'field' => 'course_start',
'nonce' => 'woothemes_sensei_start_course_noonce',
'enrol' => false,
],
// Lesson complete.
[
'field' => 'quiz_action',
'nonce' => 'woothemes_sensei_complete_lesson_noonce',
'enrol' => true,
],
// Quiz complete.
[
'field' => 'quiz_complete',
'nonce' => 'woothemes_sensei_complete_quiz_nonce',
'enrol' => true,
],
// Quiz save.
[
'field' => 'quiz_save',
'nonce' => 'woothemes_sensei_save_quiz_nonce',
'enrol' => true,
],
// Quiz pagination. (Saves answers on the page).
[
'field' => 'quiz_target_page',
'nonce' => 'sensei_quiz_page_change_nonce',
'enrol' => true,
],
];
/**
* Sensei_Guest_User constructor.
*
* @since 4.11.0
*/
public function __construct() {
add_action( 'init', [ $this, 'log_guest_user_out_before_all_actions' ], 8 );
add_action( 'wp', [ $this, 'init' ], 1 );
}
/**
* Initialize guest user feature.
*
* @since 4.11.0
*/
public function init() {
/**
* Enable or disable 'open access course' feature.
*
* @hook sensei_feature_open_access_courses
* @since 4.11.0
*
* @param {bool} $enable Enable feature. Default true.
*
* @return {bool} Wether to enable feature.
*/
if ( ! apply_filters( 'sensei_feature_open_access_courses', true ) ) {
return;
}
add_action( 'wp', [ $this, 'log_in_guest_user_if_in_open_course' ], 8 );
add_action( 'wp', [ $this, 'create_guest_user_and_login_for_open_course' ], 9 );
add_action( 'sensei_is_enrolled', [ $this, 'open_course_always_enrolled' ], 10, 3 );
add_action( 'sensei_can_access_course_content', [ $this, 'open_course_enable_course_access' ], 10, 2 );
add_action( 'sensei_can_user_manually_enrol', [ $this, 'open_course_user_can_manualy_enroll' ], 10, 2 );
add_filter( 'sensei_send_emails', [ $this, 'skip_sensei_email' ] );
$this->create_guest_student_role_if_not_exists();
}
/**
* Log out the guest user before any action, some actions like Log in Form does not work if guest user is logged in
* even after setting current user to 0 by 'wp' hook.
*
* @since 4.11.0
*/
public function log_guest_user_out_before_all_actions() {
if (
is_user_logged_in() &&
self::is_current_user_guest()
) {
$this->guest_user_id = get_current_user_id();
wp_set_current_user( 0 );
}
}
/**
* Filter enrolment check to always return true if the course is open access.
*
* @since 4.11.0
*
* @param bool $is_enrolled Initial value.
* @param int $user_id User ID. Unused.
* @param int $course_id Course ID.
*
* @return bool
*/
public function open_course_always_enrolled( $is_enrolled, $user_id, $course_id ) {
$in_course_content = is_singular( [ 'lesson', 'quiz' ] );
return ( $in_course_content && $this->is_course_open_access( $course_id ) ) ? true : $is_enrolled;
}
/**
* Filter manual enrolment check to always allow users to manually enrol if the course is open access.
*
* @since 4.11.0
*
* @param bool $can_enroll Initial value.
* @param int $course_id Course ID.
*
* @return bool
*/
public function open_course_user_can_manualy_enroll( $can_enroll, $course_id ) {
$is_user_enrolled = is_user_logged_in() && Sensei_Course::is_user_enrolled( $course_id, get_current_user_id() );
return $this->is_course_open_access( $course_id ) ? ! $is_user_enrolled : $can_enroll;
}
/**
* Filter course access check to always return true if the course is open access.
*
* @since 4.11.0
*
* @param bool $can_view_course_content Initial value.
* @param int $course_id Course ID.
*
* @return bool
*/
public function open_course_enable_course_access( $can_view_course_content, $course_id ) {
return $this->is_course_open_access( $course_id ) ? true : $can_view_course_content;
}
/**
* Create a guest user for open access courses if no user is logged in.
*
* @since 4.11.0
* @access private
*/
public function create_guest_user_and_login_for_open_course() {
global $post;
$course_id = Sensei_Utils::get_current_course();
if ( empty( $course_id ) || is_user_logged_in() || ! $this->is_course_open_access( $course_id ) || post_password_required( $post->ID ) ) {
return;
}
$current_action = $this->get_current_action();
// Conditionally create Guest Student user and set role for open course.
if ( $current_action ) {
$user_id = self::create_guest_user();
$this->login_user( $user_id );
$this->recreate_nonce( $current_action );
if ( $current_action['enrol'] ) {
$this->enrol_user( $user_id, $course_id );
}
}
}
/**
* Sets current guest user to none if out of open course context.
*
* @since 4.11.0
* @access private
*/
public function log_in_guest_user_if_in_open_course() {
if (
! is_user_logged_in() &&
$this->is_open_course_related_action() &&
$this->guest_user_id > 0
) {
wp_set_current_user( $this->guest_user_id );
}
}
/**
* Checks if the action is related to an open course or a lesson or a quiz that belongs to an open course.
*
* @since 4.11.0
* @access private
* @return boolean
*/
private function is_open_course_related_action() {
if ( ! is_singular( [ 'course', 'lesson', 'quiz' ] ) ) {
return false;
}
return $this->is_course_open_access( Sensei_Utils::get_current_course() );
}
/**
* Check if the course is open access.
*
* @param int $course_id ID of the course.
*
* @since 4.11.0
* @return boolean|mixed
*/
private function is_course_open_access( $course_id ) {
$is_open_access = get_post_meta( $course_id, self::COURSE_OPEN_ACCESS_META, true );
/**
* Filter if the given course has open access turned on.
*
* @hook sensei_course_open_access
* @since 4.11.0
*
* @param {bool} $is_open_access Open access setting value.
* @param {int} $course_id Course ID.
*
* @return {bool} Open access setting value.
*/
return apply_filters( 'sensei_course_open_access', $is_open_access, $course_id );
}
/**
* Checks if the current user is a guest.
*
* @since 4.11.0
* @access private
*/
private static function is_current_user_guest() {
$user = wp_get_current_user();
return self::is_guest_user( $user );
}
/**
* Recreate nonce after logging in user invalidates existing one.
*
* @since 4.11.0
*
* @param array $action Action to recreate nonce for.
*/
private function recreate_nonce( $action ) {
$nonce = $action['nonce'];
$_POST[ $nonce ] = wp_create_nonce( $nonce );
}
/**
* Create a user with Guest Student role .
*
* @since 4.11.0
* @return int
*/
public static function create_guest_user() {
$user_count = Sensei_Utils::get_user_count_for_role( self::ROLE ) + 1;
$user_name = self::LOGIN_PREFIX . wp_rand( 10000000, 99999999 ) . '_' . $user_count;
return Sensei_Temporary_User::create_user(
[
'user_pass' => wp_generate_password(),
'user_login' => $user_name,
'user_email' => $user_name . '@' . self::EMAIL_DOMAIN,
'display_name' => 'Guest Student ' . str_pad( $user_count, 3, '0', STR_PAD_LEFT ),
'role' => self::ROLE,
]
);
}
/**
* Delete a guest user and remove their course progress.
*
* @param int $user_id User ID.
*
* @return void
*/
public static function delete_guest_user( $user_id ): void {
if ( ! $user_id || ! self::is_guest_user( $user_id ) ) {
return;
}
$course_ids = Sensei_Learner::instance()->get_enrolled_courses_query(
$user_id,
[
'posts_per_page' => -1,
'fields' => 'ids',
]
)->posts;
foreach ( $course_ids as $course_id ) {
Sensei_Utils::sensei_remove_user_from_course( $course_id, $user_id );
}
Sensei_Temporary_User::delete_user( $user_id );
}
/**
* Log a user in.
*
* @param int $user_id ID of the user.
*
* @since 4.11.0
*/
private function login_user( $user_id ) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, true );
}
/**
* Manually enrol the new user in the course.
*
* @since 4.11.0
*
* @param int $user_id User ID.
* @param int $course_id Course ID.
*/
private function enrol_user( $user_id, $course_id ) {
if ( ! Sensei_Course::can_current_user_manually_enrol( $course_id )
|| ! Sensei_Course::is_prerequisite_complete( $course_id ) ) {
return; // Error message?
}
$course_enrolment = Sensei_Course_Enrolment::get_course_instance( $course_id );
$course_enrolment->enrol( $user_id );
}
/**
* Create the Guest Student role if it does not exist.
*
* @since 4.11.0
*/
private function create_guest_student_role_if_not_exists() {
// Check if the Guest Student role exists.
$guest_role = get_role( self::ROLE );
// If Guest Student is not a valid WordPress role create it.
if ( ! is_a( $guest_role, 'WP_Role' ) ) {
// Create the role.
add_role( self::ROLE, __( 'Guest Student', 'sensei-lms' ) );
}
}
/**
* Determine if the current requests is for a supported action.
*
* @since 4.11.0
*
* @return string[]|null
*/
private function get_current_action() {
/**
* Filters the list of supported actions for Guest Users.
*
* @since 4.11
*
* @hook sensei_guest_user_supported_actions
*
* @param {array} List of supported actions for guest users.
* @return {array} List of supported actions for guest users.
*/
$supported_actions = apply_filters( 'sensei_guest_user_supported_actions', $this->supported_actions );
foreach ( $supported_actions as $action ) {
if ( $this->is_action( $action['field'], $action['nonce'] ) ) {
return $action;
}
}
return null;
}
/**
* Determines if the request is for an action submitting the given form field and nonce.
*
* @since 4.11.0
*
* @param string $field Form field name for the action.
* @param string $nonce Nonce name for the action.
*
* @return boolean
*/
private function is_action( $field, $nonce ) {
return isset( $_POST[ $field ] )
&& isset( $_POST[ $nonce ] )
&& wp_verify_nonce( wp_unslash( $_POST[ $nonce ] ), $nonce ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verification
}
/**
* Prevent Sensei emails related to guest user actions.
*
* @access private
* @since 4.11.0
*
* @param boolean $send_email Whether to send the email.
*
* @return boolean Whether to send the email.
*/
public function skip_sensei_email( $send_email ) {
return self::is_current_user_guest() ? false : $send_email;
}
/**
* Prevent emails related to the guest user from being dispatched via wp_mail.
*
* @access private
* @since 4.14.0
*
* @param bool|null $return Null if we should send the email, a boolean if not.
* @param array $atts Email attributes.
* @return bool|null Null if we should send the email, a boolean if not.
*/
public static function skip_wp_mail( $return, $atts ) {
if ( self::is_current_user_guest() ) {
// If this e-mail is being dispatched while the current user is a guest, just... don't send it.
return false;
}
if ( Sensei_Temporary_User::should_block_email( $atts, self::EMAIL_DOMAIN ) ) {
// If this e-mail is being dispatched to a guest user, don't send it.
return false;
}
return $return;
}
/**
* Check if the given user is a guest user.
*
* @param WP_User|int $user User object or ID.
*
* @return bool
*/
private static function is_guest_user( $user ): bool {
if ( is_numeric( $user ) ) {
$user = get_user_by( 'ID', $user );
}
return in_array( self::ROLE, (array) $user->roles, true );
}
}