Addons.php
5 years ago
Admin.php
5 years ago
Ajax.php
5 years ago
Assets.php
5 years ago
Course.php
5 years ago
Course_Filter.php
5 years ago
Course_Settings_Tabs.php
5 years ago
Course_Widget.php
5 years ago
Custom_Validation.php
5 years ago
Dashboard.php
5 years ago
Delete_Enrollment_With_Order.php
5 years ago
Email.php
5 years ago
FormHandler.php
5 years ago
Frontend.php
5 years ago
Gutenberg.php
5 years ago
Instructor.php
5 years ago
Instructors_List.php
5 years ago
Lesson.php
5 years ago
Options.php
5 years ago
Post_types.php
5 years ago
Private_Course_Access.php
5 years ago
Q_and_A.php
5 years ago
Question_Answers_List.php
5 years ago
Quiz.php
5 years ago
Quiz_Attempts_List.php
5 years ago
RestAPI.php
5 years ago
Rewrite_Rules.php
5 years ago
Shortcode.php
5 years ago
Student.php
5 years ago
Students_List.php
5 years ago
Taxonomies.php
5 years ago
Template.php
5 years ago
Theme_Compatibility.php
5 years ago
Tools.php
5 years ago
Tutor.php
5 years ago
TutorEDD.php
5 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
5 years ago
Tutor_Setup.php
5 years ago
Upgrader.php
5 years ago
User.php
5 years ago
Utils.php
5 years ago
Video_Stream.php
5 years ago
Withdraw.php
5 years ago
Withdraw_Requests_List.php
5 years ago
WooCommerce.php
5 years ago
Instructor.php
293 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Instructor |
| 5 | * @package TUTOR |
| 6 | * |
| 7 | * @since v.1.0.0 |
| 8 | */ |
| 9 | |
| 10 | namespace TUTOR; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) |
| 13 | exit; |
| 14 | |
| 15 | |
| 16 | class Instructor { |
| 17 | |
| 18 | protected $error_msgs = ''; |
| 19 | public function __construct() { |
| 20 | add_action('template_redirect', array($this, 'register_instructor')); |
| 21 | add_action('template_redirect', array($this, 'apply_instructor')); |
| 22 | |
| 23 | //Add instructor from admin panel. |
| 24 | add_action('wp_ajax_tutor_add_instructor', array($this, 'add_new_instructor')); |
| 25 | |
| 26 | /** |
| 27 | * Instructor Approval |
| 28 | * Block Unblock |
| 29 | * |
| 30 | * @since v.1.5.3 |
| 31 | */ |
| 32 | add_action('wp_ajax_instructor_approval_action', array($this, 'instructor_approval_action')); |
| 33 | |
| 34 | /** |
| 35 | * Check if instructor can publish courses |
| 36 | * @since v.1.5.9 |
| 37 | */ |
| 38 | add_action('tutor_option_save_after', array($this, 'can_publish_tutor_courses')); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Register new user and mark him as instructor |
| 43 | * |
| 44 | * @since v.1.0.0 |
| 45 | */ |
| 46 | public function register_instructor(){ |
| 47 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_register_instructor' ){ |
| 48 | return; |
| 49 | } |
| 50 | //Checking nonce |
| 51 | tutor_utils()->checking_nonce(); |
| 52 | |
| 53 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 54 | 'first_name' => __('First name field is required', 'tutor'), |
| 55 | 'last_name' => __('Last name field is required', 'tutor'), |
| 56 | 'email' => __('E-Mail field is required', 'tutor'), |
| 57 | 'user_login' => __('User Name field is required', 'tutor'), |
| 58 | 'password' => __('Password field is required', 'tutor'), |
| 59 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 60 | )); |
| 61 | |
| 62 | $validation_errors = array(); |
| 63 | |
| 64 | /* |
| 65 | *registration_errors |
| 66 | *push into validation_errors |
| 67 | */ |
| 68 | $errors = apply_filters('registration_errors',new \WP_Error,'',''); |
| 69 | foreach ($errors->errors as $key => $value) |
| 70 | { |
| 71 | $validation_errors[$key] = $value[0]; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | foreach ($required_fields as $required_key => $required_value){ |
| 76 | if (empty($_POST[$required_key])){ |
| 77 | $validation_errors[$required_key] = $required_value; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 82 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 83 | } |
| 84 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 85 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 86 | } |
| 87 | |
| 88 | if (count($validation_errors)){ |
| 89 | $this->error_msgs = $validation_errors; |
| 90 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 95 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 96 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 97 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 98 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 99 | |
| 100 | $userdata = array( |
| 101 | 'user_login' => $user_login, |
| 102 | 'user_email' => $email, |
| 103 | 'first_name' => $first_name, |
| 104 | 'last_name' => $last_name, |
| 105 | //'role' => tutor()->instructor_role, |
| 106 | 'user_pass' => $password, |
| 107 | ); |
| 108 | |
| 109 | $user_id = wp_insert_user( $userdata ) ; |
| 110 | if ( ! is_wp_error($user_id)){ |
| 111 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 112 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 113 | |
| 114 | do_action('tutor_new_instructor_after', $user_id); |
| 115 | |
| 116 | $user = get_user_by( 'id', $user_id ); |
| 117 | if( $user ) { |
| 118 | wp_set_current_user( $user_id, $user->user_login ); |
| 119 | wp_set_auth_cookie( $user_id ); |
| 120 | } |
| 121 | }else{ |
| 122 | $this->error_msgs = $user_id->get_error_messages(); |
| 123 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 128 | die(); |
| 129 | } |
| 130 | |
| 131 | public function tutor_instructor_form_validation_errors(){ |
| 132 | return $this->error_msgs; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * |
| 137 | * Usage for instructor applying when a user already logged in |
| 138 | * |
| 139 | * @since v.1.0.0 |
| 140 | */ |
| 141 | public function apply_instructor(){ |
| 142 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ |
| 143 | return; |
| 144 | } |
| 145 | //Checking nonce |
| 146 | tutor_utils()->checking_nonce(); |
| 147 | |
| 148 | $user_id = get_current_user_id(); |
| 149 | if ($user_id){ |
| 150 | if (tutor_utils()->is_instructor()){ |
| 151 | die(__('Already applied for instructor', 'tutor')); |
| 152 | }else{ |
| 153 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 154 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 155 | |
| 156 | do_action('tutor_new_instructor_after', $user_id); |
| 157 | } |
| 158 | }else{ |
| 159 | die(__('Permission denied', 'tutor')); |
| 160 | } |
| 161 | |
| 162 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 163 | die(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | public function add_new_instructor(){ |
| 168 | tutils()->checking_nonce(); |
| 169 | |
| 170 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 171 | 'first_name' => __('First name field is required', 'tutor'), |
| 172 | 'last_name' => __('Last name field is required', 'tutor'), |
| 173 | 'email' => __('E-Mail field is required', 'tutor'), |
| 174 | 'user_login' => __('User Name field is required', 'tutor'), |
| 175 | 'phone_number' => __('Phone Number field is required', 'tutor'), |
| 176 | 'password' => __('Password field is required', 'tutor'), |
| 177 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 178 | )); |
| 179 | |
| 180 | $validation_errors = array(); |
| 181 | foreach ($required_fields as $required_key => $required_value){ |
| 182 | if (empty($_POST[$required_key])){ |
| 183 | $validation_errors[$required_key] = $required_value; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 188 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 189 | } |
| 190 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 191 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 192 | } |
| 193 | |
| 194 | if (count($validation_errors)){ |
| 195 | wp_send_json_error(array('errors' => $validation_errors)); |
| 196 | } |
| 197 | |
| 198 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 199 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 200 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 201 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 202 | $phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); |
| 203 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 204 | $tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); |
| 205 | |
| 206 | $userdata = apply_filters('add_new_instructor_data', array( |
| 207 | 'user_login' => $user_login, |
| 208 | 'user_email' => $email, |
| 209 | 'first_name' => $first_name, |
| 210 | 'last_name' => $last_name, |
| 211 | 'role' => tutor()->instructor_role, |
| 212 | 'user_pass' => $password, |
| 213 | )); |
| 214 | |
| 215 | do_action('tutor_add_new_instructor_before'); |
| 216 | |
| 217 | $user_id = wp_insert_user( $userdata ) ; |
| 218 | if ( ! is_wp_error($user_id)) { |
| 219 | update_user_meta($user_id, 'phone_number', $phone_number); |
| 220 | update_user_meta($user_id, 'description', $tutor_profile_bio); |
| 221 | update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); |
| 222 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 223 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); |
| 224 | |
| 225 | do_action('tutor_add_new_instructor_after', $user_id); |
| 226 | |
| 227 | wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); |
| 228 | } |
| 229 | |
| 230 | wp_send_json_error(array('errors' => $user_id)); |
| 231 | } |
| 232 | |
| 233 | public function instructor_approval_action(){ |
| 234 | tutils()->checking_nonce(); |
| 235 | |
| 236 | $instructor_id = (int) sanitize_text_field(tutils()->array_get('instructor_id', $_POST)); |
| 237 | $action = sanitize_text_field(tutils()->array_get('action_name', $_POST)); |
| 238 | |
| 239 | if( 'approve' === $action ) { |
| 240 | do_action('tutor_before_approved_instructor', $instructor_id); |
| 241 | |
| 242 | update_user_meta($instructor_id, '_tutor_instructor_status', 'approved'); |
| 243 | update_user_meta($instructor_id, '_tutor_instructor_approved', tutor_time()); |
| 244 | |
| 245 | $instructor = new \WP_User($instructor_id); |
| 246 | $instructor->add_role(tutor()->instructor_role); |
| 247 | |
| 248 | //TODO: send E-Mail to this user about instructor approval, should via hook |
| 249 | do_action('tutor_after_approved_instructor', $instructor_id); |
| 250 | } |
| 251 | |
| 252 | if( 'blocked' === $action ) { |
| 253 | do_action('tutor_before_blocked_instructor', $instructor_id); |
| 254 | update_user_meta($instructor_id, '_tutor_instructor_status', 'blocked'); |
| 255 | |
| 256 | $instructor = new \WP_User($instructor_id); |
| 257 | $instructor->remove_role(tutor()->instructor_role); |
| 258 | do_action('tutor_after_blocked_instructor', $instructor_id); |
| 259 | |
| 260 | //TODO: send E-Mail to this user about instructor blocked, should via hook |
| 261 | } |
| 262 | |
| 263 | if( 'remove-instructor' === $action) |
| 264 | { |
| 265 | $user = new \WP_User($instructor_id); |
| 266 | $user->remove_role(tutor()->instructor_role); |
| 267 | |
| 268 | tutor_utils()->remove_instructor_role($instructor_id); |
| 269 | update_user_meta($instructor_id, '_is_tutor_instructor_rejected', tutor_time()); |
| 270 | } |
| 271 | |
| 272 | wp_send_json_success(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Can instructor publish courses directly |
| 277 | * Fixed in Gutenberg @since v.1.5.9 |
| 278 | */ |
| 279 | |
| 280 | public function can_publish_tutor_courses(){ |
| 281 | $can_publish_course = (bool) tutor_utils()->get_option('instructor_can_publish_course'); |
| 282 | |
| 283 | $instructor_role = tutor()->instructor_role; |
| 284 | $instructor = get_role( $instructor_role ); |
| 285 | |
| 286 | if ($can_publish_course){ |
| 287 | $instructor->add_cap('publish_tutor_courses'); |
| 288 | }else{ |
| 289 | $instructor->remove_cap('publish_tutor_courses'); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | } |