Addons.php
7 years ago
Admin.php
7 years ago
Ajax.php
7 years ago
Assets.php
7 years ago
Course.php
7 years ago
Gutenberg.php
7 years ago
Instructor.php
7 years ago
Instructors_List.php
7 years ago
Lesson.php
7 years ago
Options.php
7 years ago
Post_types.php
7 years ago
Q_and_A.php
7 years ago
Question.php
7 years ago
Question_Answers_List.php
7 years ago
Quiz.php
7 years ago
Quiz_Attempts_List.php
7 years ago
Rewrite_Rules.php
7 years ago
Shortcode.php
7 years ago
Student.php
7 years ago
Students_List.php
7 years ago
Taxonomies.php
7 years ago
Template.php
7 years ago
Theme_Compatibility.php
7 years ago
Tools.php
7 years ago
Tutor.php
7 years ago
TutorEDD.php
7 years ago
Tutor_Base.php
7 years ago
Tutor_List_Table.php
7 years ago
User.php
7 years ago
Utils.php
7 years ago
Video_Stream.php
7 years ago
Withdraw.php
7 years ago
Withdraw_Requests_List.php
7 years ago
WooCommerce.php
7 years ago
Instructor.php
193 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 | |
| 13 | class Instructor { |
| 14 | |
| 15 | protected $error_msgs = ''; |
| 16 | public function __construct() { |
| 17 | add_action('template_redirect', array($this, 'register_instructor')); |
| 18 | add_action('template_redirect', array($this, 'apply_instructor')); |
| 19 | |
| 20 | //Add instructor from admin panel. |
| 21 | add_action('wp_ajax_tutor_add_instructor', array($this, 'add_new_instructor')); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Register new user and mark him as instructor |
| 26 | * |
| 27 | * @since v.1.0.0 |
| 28 | */ |
| 29 | public function register_instructor(){ |
| 30 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_register_instructor' ){ |
| 31 | return; |
| 32 | } |
| 33 | //Checking nonce |
| 34 | tutor_utils()->checking_nonce(); |
| 35 | |
| 36 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 37 | 'first_name' => __('First name field is required', 'tutor'), |
| 38 | 'last_name' => __('Last name field is required', 'tutor'), |
| 39 | 'email' => __('E-Mail field is required', 'tutor'), |
| 40 | 'user_login' => __('User Name field is required', 'tutor'), |
| 41 | 'password' => __('Password field is required', 'tutor'), |
| 42 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 43 | )); |
| 44 | |
| 45 | $validation_errors = array(); |
| 46 | foreach ($required_fields as $required_key => $required_value){ |
| 47 | if (empty($_POST[$required_key])){ |
| 48 | $validation_errors[$required_key] = $required_value; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 53 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 54 | } |
| 55 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 56 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 57 | } |
| 58 | |
| 59 | if (count($validation_errors)){ |
| 60 | $this->error_msgs = $validation_errors; |
| 61 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 66 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 67 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 68 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 69 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 70 | |
| 71 | $userdata = array( |
| 72 | 'user_login' => $user_login, |
| 73 | 'user_email' => $email, |
| 74 | 'first_name' => $first_name, |
| 75 | 'last_name' => $last_name, |
| 76 | //'role' => tutor()->instructor_role, |
| 77 | 'user_pass' => $password, |
| 78 | ); |
| 79 | |
| 80 | $user_id = wp_insert_user( $userdata ) ; |
| 81 | if ( ! is_wp_error($user_id)){ |
| 82 | update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 83 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 84 | |
| 85 | $user = get_user_by( 'id', $user_id ); |
| 86 | if( $user ) { |
| 87 | wp_set_current_user( $user_id, $user->user_login ); |
| 88 | wp_set_auth_cookie( $user_id ); |
| 89 | } |
| 90 | }else{ |
| 91 | $this->error_msgs = $user_id->get_error_messages(); |
| 92 | add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 97 | die(); |
| 98 | } |
| 99 | |
| 100 | public function tutor_instructor_form_validation_errors(){ |
| 101 | return $this->error_msgs; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * |
| 106 | * Usage for instructor applying when a user already logged in |
| 107 | * |
| 108 | * @since v.1.0.0 |
| 109 | */ |
| 110 | public function apply_instructor(){ |
| 111 | if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ |
| 112 | return; |
| 113 | } |
| 114 | //Checking nonce |
| 115 | tutor_utils()->checking_nonce(); |
| 116 | |
| 117 | $user_id = get_current_user_id(); |
| 118 | if ($user_id){ |
| 119 | if (tutor_utils()->is_instructor()){ |
| 120 | die(__('Already applied for instructor', 'tutor')); |
| 121 | }else{ |
| 122 | update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 123 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 124 | } |
| 125 | }else{ |
| 126 | die(__('Permission denied', 'tutor')); |
| 127 | } |
| 128 | |
| 129 | wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 130 | die(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | public function add_new_instructor(){ |
| 135 | $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 136 | 'first_name' => __('First name field is required', 'tutor'), |
| 137 | 'last_name' => __('Last name field is required', 'tutor'), |
| 138 | 'email' => __('E-Mail field is required', 'tutor'), |
| 139 | 'user_login' => __('User Name field is required', 'tutor'), |
| 140 | 'phone_number' => __('Phone Number field is required', 'tutor'), |
| 141 | 'password' => __('Password field is required', 'tutor'), |
| 142 | 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 143 | )); |
| 144 | |
| 145 | $validation_errors = array(); |
| 146 | foreach ($required_fields as $required_key => $required_value){ |
| 147 | if (empty($_POST[$required_key])){ |
| 148 | $validation_errors[$required_key] = $required_value; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 153 | $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 154 | } |
| 155 | if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 156 | $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 157 | } |
| 158 | |
| 159 | if (count($validation_errors)){ |
| 160 | wp_send_json_error(array('errors' => $validation_errors)); |
| 161 | } |
| 162 | |
| 163 | $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 164 | $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 165 | $email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 166 | $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 167 | $phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); |
| 168 | $password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 169 | $tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); |
| 170 | |
| 171 | $userdata = array( |
| 172 | 'user_login' => $user_login, |
| 173 | 'user_email' => $email, |
| 174 | 'first_name' => $first_name, |
| 175 | 'last_name' => $last_name, |
| 176 | 'role' => tutor()->instructor_role, |
| 177 | 'user_pass' => $password, |
| 178 | ); |
| 179 | $user_id = wp_insert_user( $userdata ) ; |
| 180 | if ( ! is_wp_error($user_id)) { |
| 181 | update_user_meta($user_id, 'phone_number', $phone_number); |
| 182 | update_user_meta($user_id, 'description', $tutor_profile_bio); |
| 183 | update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); |
| 184 | update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 185 | update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); |
| 186 | |
| 187 | wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); |
| 188 | } |
| 189 | |
| 190 | wp_send_json_error(array('errors' => $user_id)); |
| 191 | } |
| 192 | |
| 193 | } |