Addons.php
6 years ago
Admin.php
6 years ago
Ajax.php
6 years ago
Assets.php
6 years ago
Course.php
6 years ago
Course_Settings_Tabs.php
6 years ago
Course_Widget.php
6 years ago
Dashboard.php
6 years ago
Email.php
6 years ago
FormHandler.php
6 years ago
Frontend.php
6 years ago
Gutenberg.php
6 years ago
Instructor.php
6 years ago
Instructors_List.php
6 years ago
Lesson.php
6 years ago
Options.php
6 years ago
Post_types.php
6 years ago
Q_and_A.php
6 years ago
Question_Answers_List.php
6 years ago
Quiz.php
6 years ago
Quiz_Attempts_List.php
6 years ago
RestAPI.php
6 years ago
Rewrite_Rules.php
6 years ago
Shortcode.php
6 years ago
Student.php
6 years ago
Students_List.php
6 years ago
Taxonomies.php
6 years ago
Template.php
6 years ago
Theme_Compatibility.php
6 years ago
Tools.php
6 years ago
Tutor.php
6 years ago
TutorEDD.php
6 years ago
Tutor_Base.php
6 years ago
Tutor_List_Table.php
6 years ago
Upgrader.php
6 years ago
User.php
6 years ago
Utils.php
6 years ago
Video_Stream.php
6 years ago
Withdraw.php
6 years ago
Withdraw_Requests_List.php
6 years ago
WooCommerce.php
6 years ago
Instructor.php
244 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 | /** |
| 36 | * Register new user and mark him as instructor |
| 37 | * |
| 38 | * @since v.1.0.0 |
| 39 | */ |
| 40 | public function register_instructor(){ |
| 41 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_register_instructor' ){ |
| 42 | return; |
| 43 | } |
| 44 | //Checking nonce |
| 45 | tutor_utils()->checking_nonce(); |
| 46 | |
| 47 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 48 | 'first_name' => __('First name field is required', 'tutor'), |
| 49 | 'last_name' => __('Last name field is required', 'tutor'), |
| 50 | 'email' => __('E-Mail field is required', 'tutor'), |
| 51 | 'user_login' => __('User Name field is required', 'tutor'), |
| 52 | 'password' => __('Password field is required', 'tutor'), |
| 53 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 54 | )); |
| 55 | |
| 56 | $validation_errors = array(); |
| 57 | foreach ($required_fields as $required_key => $required_value){ |
| 58 | if (empty($_POST[$required_key])){ |
| 59 | $validation_errors[$required_key] = $required_value; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 64 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 65 | } |
| 66 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 67 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 68 | } |
| 69 | |
| 70 | if (count($validation_errors)){ |
| 71 | $this->error_msgs = $validation_errors; |
| 72 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 77 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 78 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 79 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 80 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 81 | |
| 82 | $userdata = array( |
| 83 | 'user_login' => $user_login, |
| 84 | 'user_email' => $email, |
| 85 | 'first_name' => $first_name, |
| 86 | 'last_name' => $last_name, |
| 87 | //'role' => tutor()->instructor_role, |
| 88 | 'user_pass' => $password, |
| 89 | ); |
| 90 | |
| 91 | $user_id = wp_insert_user( $userdata ) ; |
| 92 | if ( ! is_wp_error($user_id)){ |
| 93 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 94 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 95 | |
| 96 | $user = get_user_by( 'id', $user_id ); |
| 97 | if( $user ) { |
| 98 | wp_set_current_user( $user_id, $user->user_login ); |
| 99 | wp_set_auth_cookie( $user_id ); |
| 100 | } |
| 101 | }else{ |
| 102 | $this->error_msgs = $user_id->get_error_messages(); |
| 103 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 108 | die(); |
| 109 | } |
| 110 | |
| 111 | public function tutor_instructor_form_validation_errors(){ |
| 112 | return $this->error_msgs; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * |
| 117 | * Usage for instructor applying when a user already logged in |
| 118 | * |
| 119 | * @since v.1.0.0 |
| 120 | */ |
| 121 | public function apply_instructor(){ |
| 122 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ |
| 123 | return; |
| 124 | } |
| 125 | //Checking nonce |
| 126 | tutor_utils()->checking_nonce(); |
| 127 | |
| 128 | $user_id = get_current_user_id(); |
| 129 | if ($user_id){ |
| 130 | if (tutor_utils()->is_instructor()){ |
| 131 | die(__('Already applied for instructor', 'tutor')); |
| 132 | }else{ |
| 133 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 134 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 135 | } |
| 136 | }else{ |
| 137 | die(__('Permission denied', 'tutor')); |
| 138 | } |
| 139 | |
| 140 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 141 | die(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | public function add_new_instructor(){ |
| 146 | tutils()->checking_nonce(); |
| 147 | |
| 148 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 149 | 'first_name' => __('First name field is required', 'tutor'), |
| 150 | 'last_name' => __('Last name field is required', 'tutor'), |
| 151 | 'email' => __('E-Mail field is required', 'tutor'), |
| 152 | 'user_login' => __('User Name field is required', 'tutor'), |
| 153 | 'phone_number' => __('Phone Number field is required', 'tutor'), |
| 154 | 'password' => __('Password field is required', 'tutor'), |
| 155 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 156 | )); |
| 157 | |
| 158 | $validation_errors = array(); |
| 159 | foreach ($required_fields as $required_key => $required_value){ |
| 160 | if (empty($_POST[$required_key])){ |
| 161 | $validation_errors[$required_key] = $required_value; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 166 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 167 | } |
| 168 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 169 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 170 | } |
| 171 | |
| 172 | if (count($validation_errors)){ |
| 173 | wp_send_json_error(array('errors' => $validation_errors)); |
| 174 | } |
| 175 | |
| 176 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 177 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 178 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 179 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 180 | $phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); |
| 181 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 182 | $tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); |
| 183 | |
| 184 | $userdata = apply_filters('add_new_instructor_data', array( |
| 185 | 'user_login' => $user_login, |
| 186 | 'user_email' => $email, |
| 187 | 'first_name' => $first_name, |
| 188 | 'last_name' => $last_name, |
| 189 | 'role' => tutor()->instructor_role, |
| 190 | 'user_pass' => $password, |
| 191 | )); |
| 192 | |
| 193 | do_action('tutor_add_new_instructor_before'); |
| 194 | |
| 195 | $user_id = wp_insert_user( $userdata ) ; |
| 196 | if ( ! is_wp_error($user_id)) { |
| 197 | update_user_meta($user_id, 'phone_number', $phone_number); |
| 198 | update_user_meta($user_id, 'description', $tutor_profile_bio); |
| 199 | update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); |
| 200 | update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); |
| 201 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); |
| 202 | |
| 203 | do_action('tutor_add_new_instructor_after', $user_id); |
| 204 | |
| 205 | wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); |
| 206 | } |
| 207 | |
| 208 | wp_send_json_error(array('errors' => $user_id)); |
| 209 | } |
| 210 | |
| 211 | public function instructor_approval_action(){ |
| 212 | tutils()->checking_nonce(); |
| 213 | |
| 214 | $instructor_id = (int) sanitize_text_field(tutils()->array_get('instructor_id', $_POST)); |
| 215 | $action = sanitize_text_field(tutils()->array_get('action_name', $_POST)); |
| 216 | |
| 217 | if( 'approve' === $action ) { |
| 218 | do_action('tutor_before_approved_instructor', $instructor_id); |
| 219 | |
| 220 | update_user_meta($instructor_id, '_tutor_instructor_status', 'approved'); |
| 221 | update_user_meta($instructor_id, '_tutor_instructor_approved', tutor_time()); |
| 222 | |
| 223 | $instructor = new \WP_User($instructor_id); |
| 224 | $instructor->add_role(tutor()->instructor_role); |
| 225 | |
| 226 | //TODO: send E-Mail to this user about instructor approval, should via hook |
| 227 | do_action('tutor_after_approved_instructor', $instructor_id); |
| 228 | } |
| 229 | |
| 230 | if( 'blocked' === $action ) { |
| 231 | do_action('tutor_before_blocked_instructor', $instructor_id); |
| 232 | update_user_meta($instructor_id, '_tutor_instructor_status', 'blocked'); |
| 233 | |
| 234 | $instructor = new \WP_User($instructor_id); |
| 235 | $instructor->remove_role(tutor()->instructor_role); |
| 236 | do_action('tutor_after_blocked_instructor', $instructor_id); |
| 237 | |
| 238 | //TODO: send E-Mail to this user about instructor blocked, should via hook |
| 239 | } |
| 240 | |
| 241 | wp_send_json_success(); |
| 242 | } |
| 243 | |
| 244 | } |