PluginProbe
Tutor LMS – eLearning and online course solution / 2.0.1
Tutor LMS – eLearning and online course solution v2.0.1
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / Instructor.php

Instructor.php in Tutor LMS – eLearning and online course solution 2.0.1, at classes/Instructor.php

333 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Instructor
5 *
6 * @package TUTOR
7 *
8 * @since v.1.0.0
9 */
10
11 namespace TUTOR;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17
18 class Instructor {
19
20
21 protected $error_msgs = '';
22 public function __construct() {
23 add_action( 'template_redirect', array( $this, 'register_instructor' ) );
24 add_action( 'template_redirect', array( $this, 'apply_instructor' ) );
25
26 // Add instructor from admin panel.
27 add_action( 'wp_ajax_tutor_add_instructor', array( $this, 'add_new_instructor' ) );
28
29 /**
30 * Instructor Approval
31 * Block Unblock
32 *
33 * @since v.1.5.3
34 */
35 add_action( 'wp_ajax_instructor_approval_action', array( $this, 'instructor_approval_action' ) );
36
37 /**
38 * Check if instructor can publish courses
39 *
40 * @since v.1.5.9
41 */
42 add_action( 'tutor_option_save_after', array( $this, 'can_publish_tutor_courses' ) );
43
44 /**
45 * Hide instructor rejection message
46 *
47 * @since v1.9.2
48 */
49 add_action( 'wp_loaded', array( $this, 'hide_instructor_notice' ) );
50 }
51
52 /**
53 * Register new user and mark him as instructor
54 *
55 * @since v.1.0.0
56 */
57 public function register_instructor() {
58 if ( ! isset( $_POST['tutor_action'] ) || $_POST['tutor_action'] !== 'tutor_register_instructor' ) {
59 return;
60 }
61 // Checking nonce
62 tutor_utils()->checking_nonce();
63
64 $required_fields = apply_filters(
65 'tutor_instructor_registration_required_fields',
66 array(
67 'first_name' => __( 'First name field is required', 'tutor' ),
68 'last_name' => __( 'Last name field is required', 'tutor' ),
69 'email' => __( 'E-Mail field is required', 'tutor' ),
70 'user_login' => __( 'User Name field is required', 'tutor' ),
71 'password' => __( 'Password field is required', 'tutor' ),
72 'password_confirmation' => __( 'Password Confirmation field is required', 'tutor' ),
73 )
74 );
75
76 $validation_errors = array();
77
78 /*
79 *registration_errors
80 *push into validation_errors
81 */
82 $errors = apply_filters( 'registration_errors', new \WP_Error(), '', '' );
83 foreach ( $errors->errors as $key => $value ) {
84 $validation_errors[ $key ] = $value[0];
85 }
86
87 foreach ( $required_fields as $required_key => $required_value ) {
88 if ( empty( $_POST[ $required_key ] ) ) {
89 $validation_errors[ $required_key ] = $required_value;
90 }
91 }
92
93 if ( ! filter_var( tutor_utils()->input_old( 'email' ), FILTER_VALIDATE_EMAIL ) ) {
94 $validation_errors['email'] = __( 'Valid E-Mail is required', 'tutor' );
95 }
96 if ( tutor_utils()->input_old( 'password' ) !== tutor_utils()->input_old( 'password_confirmation' ) ) {
97 $validation_errors['password_confirmation'] = __( 'Your passwords should match each other. Please recheck.', 'tutor' );
98 }
99
100 if ( count( $validation_errors ) ) {
101 $this->error_msgs = $validation_errors;
102 add_filter( 'tutor_instructor_register_validation_errors', array( $this, 'tutor_instructor_form_validation_errors' ) );
103 return;
104 }
105
106 $first_name = sanitize_text_field( tutor_utils()->input_old( 'first_name' ) );
107 $last_name = sanitize_text_field( tutor_utils()->input_old( 'last_name' ) );
108 $email = sanitize_text_field( tutor_utils()->input_old( 'email' ) );
109 $user_login = sanitize_text_field( tutor_utils()->input_old( 'user_login' ) );
110 $password = sanitize_text_field( tutor_utils()->input_old( 'password' ) );
111
112 $userdata = array(
113 'user_login' => $user_login,
114 'user_email' => $email,
115 'first_name' => $first_name,
116 'last_name' => $last_name,
117 // 'role' => tutor()->instructor_role,
118 'user_pass' => $password,
119 );
120
121 $user_id = wp_insert_user( $userdata );
122 if ( ! is_wp_error( $user_id ) ) {
123 update_user_meta( $user_id, '_is_tutor_instructor', tutor_time() );
124 update_user_meta( $user_id, '_tutor_instructor_status', apply_filters( 'tutor_initial_instructor_status', 'pending' ) );
125
126 do_action( 'tutor_new_instructor_after', $user_id );
127
128 $user = get_user_by( 'id', $user_id );
129 if ( $user ) {
130 wp_set_current_user( $user_id, $user->user_login );
131 wp_set_auth_cookie( $user_id );
132 }
133 } else {
134 $this->error_msgs = $user_id->get_error_messages();
135 add_filter( 'tutor_instructor_register_validation_errors', array( $this, 'tutor_instructor_form_validation_errors' ) );
136 return;
137 }
138
139 wp_redirect( tutor_utils()->input_old( '_wp_http_referer' ) );
140 die();
141 }
142
143 public function tutor_instructor_form_validation_errors() {
144 return $this->error_msgs;
145 }
146
147 /**
148 *
149 * Usage for instructor applying when a user already logged in
150 *
151 * @since v.1.0.0
152 */
153 public function apply_instructor() {
154 if ( ! isset( $_POST['tutor_action'] ) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ) {
155 return;
156 }
157 // Checking nonce
158 tutor_utils()->checking_nonce();
159
160 $user_id = get_current_user_id();
161 if ( $user_id ) {
162 if ( tutor_utils()->is_instructor() ) {
163 die( __( 'Already applied for instructor', 'tutor' ) );
164 } else {
165 update_user_meta( $user_id, '_is_tutor_instructor', tutor_time() );
166 update_user_meta( $user_id, '_tutor_instructor_status', apply_filters( 'tutor_initial_instructor_status', 'pending' ) );
167
168 do_action( 'tutor_new_instructor_after', $user_id );
169 }
170 } else {
171 die( __( 'Permission denied', 'tutor' ) );
172 }
173
174 wp_redirect( tutor_utils()->input_old( '_wp_http_referer' ) );
175 die();
176 }
177
178
179 public function add_new_instructor() {
180 tutor_utils()->checking_nonce();
181
182 // Only admin should be able to add instructor
183 if ( ! current_user_can( 'manage_options' ) || ! get_option( 'users_can_register', false ) ) {
184 wp_send_json_error();
185 }
186
187 $required_fields = apply_filters(
188 'tutor_instructor_registration_required_fields',
189 array(
190 'first_name' => __( 'First name field is required', 'tutor' ),
191 'last_name' => __( 'Last name field is required', 'tutor' ),
192 'email' => __( 'E-Mail field is required', 'tutor' ),
193 'user_login' => __( 'User Name field is required', 'tutor' ),
194 'phone_number' => __( 'Phone Number field is required', 'tutor' ),
195 'password' => __( 'Password field is required', 'tutor' ),
196 'password_confirmation' => __( 'Your passwords should match each other. Please recheck.', 'tutor' ),
197 )
198 );
199
200 $validation_errors = array();
201 foreach ( $required_fields as $required_key => $required_value ) {
202 if ( empty( $_POST[ $required_key ] ) ) {
203 $validation_errors[ $required_key ] = $required_value;
204 }
205 }
206
207 if ( ! filter_var( tutor_utils()->input_old( 'email' ), FILTER_VALIDATE_EMAIL ) ) {
208 $validation_errors['email'] = __( 'Valid E-Mail is required', 'tutor' );
209 }
210 if ( tutor_utils()->input_old( 'password' ) !== tutor_utils()->input_old( 'password_confirmation' ) ) {
211 $validation_errors['password_confirmation'] = __( 'Your passwords should match each other. Please recheck.', 'tutor' );
212 }
213
214 if ( count( $validation_errors ) ) {
215 wp_send_json_error( array( 'errors' => $validation_errors ) );
216 }
217
218 $first_name = sanitize_text_field( tutor_utils()->input_old( 'first_name' ) );
219 $last_name = sanitize_text_field( tutor_utils()->input_old( 'last_name' ) );
220 $email = sanitize_text_field( tutor_utils()->input_old( 'email' ) );
221 $user_login = sanitize_text_field( tutor_utils()->input_old( 'user_login' ) );
222 $phone_number = sanitize_text_field( tutor_utils()->input_old( 'phone_number' ) );
223 $password = sanitize_text_field( tutor_utils()->input_old( 'password' ) );
224 $tutor_profile_bio = wp_kses_post( tutor_utils()->input_old( 'tutor_profile_bio' ) );
225 $tutor_profile_job_title = sanitize_text_field( tutor_utils()->input_old( 'tutor_profile_job_title' ) );
226
227 $userdata = apply_filters(
228 'add_new_instructor_data',
229 array(
230 'user_login' => $user_login,
231 'user_email' => $email,
232 'first_name' => $first_name,
233 'last_name' => $last_name,
234 'role' => tutor()->instructor_role,
235 'user_pass' => $password,
236 )
237 );
238
239 do_action( 'tutor_add_new_instructor_before' );
240
241 $user_id = wp_insert_user( $userdata );
242 if ( ! is_wp_error( $user_id ) ) {
243 update_user_meta( $user_id, 'phone_number', $phone_number );
244 update_user_meta( $user_id, 'description', $tutor_profile_bio );
245 update_user_meta( $user_id, '_tutor_profile_bio', $tutor_profile_bio );
246 update_user_meta( $user_id, '_tutor_profile_job_title', $tutor_profile_job_title );
247 update_user_meta( $user_id, '_is_tutor_instructor', tutor_time() );
248 update_user_meta( $user_id, '_tutor_instructor_status', apply_filters( 'tutor_initial_instructor_status', 'approved' ) );
249
250 do_action( 'tutor_add_new_instructor_after', $user_id );
251
252 wp_send_json_success( array( 'msg' => __( 'Instructor has been added successfully', 'tutor' ) ) );
253 }
254
255 wp_send_json_error( array( 'errors' => $user_id ) );
256 }
257
258 public function instructor_approval_action() {
259 tutor_utils()->checking_nonce();
260
261 if ( ! current_user_can( 'manage_options' ) ) {
262 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
263 }
264
265 $instructor_id = (int) sanitize_text_field( tutor_utils()->array_get( 'instructor_id', $_POST ) );
266 $action = sanitize_text_field( tutor_utils()->array_get( 'action_name', $_POST ) );
267
268 if ( 'approve' === $action ) {
269 do_action( 'tutor_before_approved_instructor', $instructor_id );
270
271 update_user_meta( $instructor_id, '_tutor_instructor_status', 'approved' );
272 update_user_meta( $instructor_id, '_tutor_instructor_approved', tutor_time() );
273
274 $instructor = new \WP_User( $instructor_id );
275 $instructor->add_role( tutor()->instructor_role );
276
277 // Send E-Mail to this user about instructor approval via hook
278 do_action( 'tutor_after_approved_instructor', $instructor_id );
279 }
280
281 if ( 'blocked' === $action ) {
282 do_action( 'tutor_before_blocked_instructor', $instructor_id );
283 update_user_meta( $instructor_id, '_tutor_instructor_status', 'blocked' );
284
285 $instructor = new \WP_User( $instructor_id );
286 $instructor->remove_role( tutor()->instructor_role );
287 do_action( 'tutor_after_blocked_instructor', $instructor_id );
288
289 // TODO: send E-Mail to this user about instructor blocked, should via hook
290 }
291
292 if ( 'remove-instructor' === $action ) {
293 do_action( 'tutor_before_rejected_instructor', $instructor_id );
294
295 $user = new \WP_User( $instructor_id );
296 $user->remove_role( tutor()->instructor_role );
297
298 tutor_utils()->remove_instructor_role( $instructor_id );
299 update_user_meta( $instructor_id, '_is_tutor_instructor_rejected', tutor_time() );
300 update_user_meta( $instructor_id, 'tutor_instructor_show_rejection_message', true );
301
302 // Send E-Mail to this user about instructor rejection via hook
303 do_action( 'tutor_after_rejected_instructor', $instructor_id );
304 }
305
306 wp_send_json_success();
307 }
308
309 public function hide_instructor_notice() {
310 if ( isset( $_GET['tutor_action'] ) && $_GET['tutor_action'] == 'hide_instructor_notice' ) {
311 delete_user_meta( get_current_user_id(), 'tutor_instructor_show_rejection_message' );
312 }
313 }
314
315 /**
316 * Can instructor publish courses directly
317 * Fixed in Gutenberg @since v.1.5.9
318 */
319
320 public function can_publish_tutor_courses() {
321 $can_publish_course = (bool) tutor_utils()->get_option( 'instructor_can_publish_course' );
322
323 $instructor_role = tutor()->instructor_role;
324 $instructor = get_role( $instructor_role );
325
326 if ( $can_publish_course ) {
327 $instructor->add_cap( 'publish_tutor_courses' );
328 } else {
329 $instructor->remove_cap( 'publish_tutor_courses' );
330 }
331 }
332 }
333