| 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 |
'phone_number' => __('Phone Number field is required', 'tutor'), |
| 42 |
'password' => __('Password field is required', 'tutor'), |
| 43 |
'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 44 |
)); |
| 45 |
|
| 46 |
$validation_errors = array(); |
| 47 |
foreach ($required_fields as $required_key => $required_value){ |
| 48 |
if (empty($_POST[$required_key])){ |
| 49 |
$validation_errors[$required_key] = $required_value; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 54 |
$validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 55 |
} |
| 56 |
if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 57 |
$validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 58 |
} |
| 59 |
|
| 60 |
if (count($validation_errors)){ |
| 61 |
$this->error_msgs = $validation_errors; |
| 62 |
add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 67 |
$last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 68 |
$email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 69 |
$user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 70 |
$phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); |
| 71 |
$password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 72 |
$tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); |
| 73 |
|
| 74 |
$userdata = array( |
| 75 |
'user_login' => $user_login, |
| 76 |
'user_email' => $email, |
| 77 |
'first_name' => $first_name, |
| 78 |
'last_name' => $last_name, |
| 79 |
//'role' => tutor()->instructor_role, |
| 80 |
'user_pass' => $password, |
| 81 |
); |
| 82 |
|
| 83 |
$user_id = wp_insert_user( $userdata ) ; |
| 84 |
if ( ! is_wp_error($user_id)){ |
| 85 |
update_user_meta($user_id, 'phone_number', $phone_number); |
| 86 |
update_user_meta($user_id, 'description', $tutor_profile_bio); |
| 87 |
update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); |
| 88 |
|
| 89 |
update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 90 |
update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 91 |
|
| 92 |
$user = get_user_by( 'id', $user_id ); |
| 93 |
if( $user ) { |
| 94 |
wp_set_current_user( $user_id, $user->user_login ); |
| 95 |
wp_set_auth_cookie( $user_id ); |
| 96 |
} |
| 97 |
}else{ |
| 98 |
$this->error_msgs = $user_id->get_error_messages(); |
| 99 |
add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 104 |
die(); |
| 105 |
} |
| 106 |
|
| 107 |
public function tutor_instructor_form_validation_errors(){ |
| 108 |
return $this->error_msgs; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* |
| 113 |
* Usage for instructor applying when a user already logged in |
| 114 |
* |
| 115 |
* @since v.1.0.0 |
| 116 |
*/ |
| 117 |
public function apply_instructor(){ |
| 118 |
if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ |
| 119 |
return; |
| 120 |
} |
| 121 |
//Checking nonce |
| 122 |
tutor_utils()->checking_nonce(); |
| 123 |
|
| 124 |
$user_id = get_current_user_id(); |
| 125 |
if ($user_id){ |
| 126 |
if (tutor_utils()->is_instructor()){ |
| 127 |
die(__('Already applied for instructor', 'tutor')); |
| 128 |
}else{ |
| 129 |
update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 130 |
update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); |
| 131 |
} |
| 132 |
}else{ |
| 133 |
die(__('Permission denied', 'tutor')); |
| 134 |
} |
| 135 |
|
| 136 |
wp_redirect(tutor_utils()->input_old('_wp_http_referer')); |
| 137 |
die(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public function add_new_instructor(){ |
| 142 |
$required_fields = apply_filters('tutor_instructor_registration_required_fields', array( |
| 143 |
'first_name' => __('First name field is required', 'tutor'), |
| 144 |
'last_name' => __('Last name field is required', 'tutor'), |
| 145 |
'email' => __('E-Mail field is required', 'tutor'), |
| 146 |
'user_login' => __('User Name field is required', 'tutor'), |
| 147 |
'phone_number' => __('Phone Number field is required', 'tutor'), |
| 148 |
'password' => __('Password field is required', 'tutor'), |
| 149 |
'password_confirmation' => __('Password Confirmation field is required', 'tutor'), |
| 150 |
)); |
| 151 |
|
| 152 |
$validation_errors = array(); |
| 153 |
foreach ($required_fields as $required_key => $required_value){ |
| 154 |
if (empty($_POST[$required_key])){ |
| 155 |
$validation_errors[$required_key] = $required_value; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { |
| 160 |
$validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); |
| 161 |
} |
| 162 |
if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ |
| 163 |
$validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); |
| 164 |
} |
| 165 |
|
| 166 |
if (count($validation_errors)){ |
| 167 |
wp_send_json_error(array('errors' => $validation_errors)); |
| 168 |
} |
| 169 |
|
| 170 |
$first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); |
| 171 |
$last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); |
| 172 |
$email = sanitize_text_field(tutor_utils()->input_old('email')); |
| 173 |
$user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); |
| 174 |
$phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); |
| 175 |
$password = sanitize_text_field(tutor_utils()->input_old('password')); |
| 176 |
$tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); |
| 177 |
|
| 178 |
$userdata = array( |
| 179 |
'user_login' => $user_login, |
| 180 |
'user_email' => $email, |
| 181 |
'first_name' => $first_name, |
| 182 |
'last_name' => $last_name, |
| 183 |
'role' => tutor()->instructor_role, |
| 184 |
'user_pass' => $password, |
| 185 |
); |
| 186 |
$user_id = wp_insert_user( $userdata ) ; |
| 187 |
if ( ! is_wp_error($user_id)) { |
| 188 |
update_user_meta($user_id, 'phone_number', $phone_number); |
| 189 |
update_user_meta($user_id, 'description', $tutor_profile_bio); |
| 190 |
update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); |
| 191 |
update_user_meta($user_id, '_is_tutor_instructor', time()); |
| 192 |
update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); |
| 193 |
|
| 194 |
wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); |
| 195 |
} |
| 196 |
|
| 197 |
wp_send_json_error(array('errors' => $user_id)); |
| 198 |
} |
| 199 |
|
| 200 |
} |