| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Student |
| 4 |
* |
| 5 |
* @package Tutor\Student |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace TUTOR; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Manage students |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Student { |
| 23 |
|
| 24 |
/** |
| 25 |
* Bagged error messages |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* |
| 29 |
* @var $error_msgs |
| 30 |
*/ |
| 31 |
protected $error_msgs = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* Handle Hooks |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
add_action( 'template_redirect', array( $this, 'register_student' ) ); |
| 40 |
add_filter( 'get_avatar_url', array( $this, 'filter_avatar' ), 10, 3 ); |
| 41 |
add_action( 'wp_ajax_tutor_social_profile', array( $this, 'tutor_social_profile' ) ); |
| 42 |
add_action( 'wp_ajax_tutor_profile_password_reset', array( $this, 'tutor_reset_password' ) ); |
| 43 |
add_action( 'wp_ajax_tutor_update_profile', array( $this, 'update_profile' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register new user and mark him as student |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function register_student() { |
| 54 |
if ( 'tutor_register_student' !== Input::post( 'tutor_action', '' ) || ! get_option( 'users_can_register', false ) ) { |
| 55 |
// Action must be register, and registration must be enabled in dashboard. |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Checking nonce. |
| 60 |
tutor_utils()->checking_nonce(); |
| 61 |
|
| 62 |
$required_fields = apply_filters( |
| 63 |
'tutor_student_registration_required_fields', |
| 64 |
array( |
| 65 |
'first_name' => __( 'First name field is required', 'tutor' ), |
| 66 |
'last_name' => __( 'Last name field is required', 'tutor' ), |
| 67 |
'email' => __( 'E-Mail field is required', 'tutor' ), |
| 68 |
'user_login' => __( 'User Name field is required', 'tutor' ), |
| 69 |
'password' => __( 'Password field is required', 'tutor' ), |
| 70 |
'password_confirmation' => __( 'Password Confirmation field is required', 'tutor' ), |
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
$validation_errors = array(); |
| 75 |
|
| 76 |
// Registration error push into validation_errors. |
| 77 |
$errors = apply_filters( 'registration_errors', new \WP_Error(), '', '' ); |
| 78 |
foreach ( $errors->errors as $key => $value ) { |
| 79 |
$validation_errors[ $key ] = $value[0]; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
foreach ( $required_fields as $required_key => $required_value ) { |
| 84 |
if ( empty( Input::post( $required_key, '' ) ) ) { |
| 85 |
$validation_errors[ $required_key ] = $required_value; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! filter_var( tutor_utils()->input_old( 'email' ), FILTER_VALIDATE_EMAIL ) ) { |
| 90 |
$validation_errors['email'] = __( 'Valid E-Mail is required', 'tutor' ); |
| 91 |
} |
| 92 |
if ( tutor_utils()->input_old( 'password' ) !== tutor_utils()->input_old( 'password_confirmation' ) ) { |
| 93 |
$validation_errors['password_confirmation'] = __( 'Your passwords should match each other. Please recheck.', 'tutor' ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( count( $validation_errors ) ) { |
| 97 |
$this->error_msgs = $validation_errors; |
| 98 |
add_filter( 'tutor_student_register_validation_errors', array( $this, 'tutor_student_form_validation_errors' ) ); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$first_name = sanitize_text_field( tutor_utils()->input_old( 'first_name' ) ); |
| 103 |
$last_name = sanitize_text_field( tutor_utils()->input_old( 'last_name' ) ); |
| 104 |
$email = sanitize_text_field( tutor_utils()->input_old( 'email' ) ); |
| 105 |
$user_login = sanitize_text_field( tutor_utils()->input_old( 'user_login' ) ); |
| 106 |
$password = sanitize_text_field( tutor_utils()->input_old( 'password' ) ); |
| 107 |
|
| 108 |
$userdata = array( |
| 109 |
'user_login' => $user_login, |
| 110 |
'user_email' => $email, |
| 111 |
'first_name' => $first_name, |
| 112 |
'last_name' => $last_name, |
| 113 |
'user_pass' => $password, |
| 114 |
); |
| 115 |
|
| 116 |
global $wpdb; |
| 117 |
$wpdb->query( 'START TRANSACTION' ); |
| 118 |
|
| 119 |
$user_id = wp_insert_user( $userdata ); |
| 120 |
$enroll_attempt = Input::post( 'tutor_course_enroll_attempt', '' ); |
| 121 |
|
| 122 |
if ( is_wp_error( $user_id ) ) { |
| 123 |
$this->error_msgs = $user_id->get_error_messages(); |
| 124 |
add_filter( 'tutor_student_register_validation_errors', array( $this, 'tutor_student_form_validation_errors' ) ); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$user = get_user_by( 'id', $user_id ); |
| 129 |
|
| 130 |
$is_req_email_verification = apply_filters( 'tutor_require_email_verification', false ); |
| 131 |
if ( $is_req_email_verification ) { |
| 132 |
do_action( 'tutor_send_verification_mail', $user, $enroll_attempt ); |
| 133 |
$reg_done = apply_filters( 'tutor_registration_done', true ); |
| 134 |
if ( ! $reg_done ) { |
| 135 |
$wpdb->query( 'ROLLBACK' ); |
| 136 |
return; |
| 137 |
} else { |
| 138 |
$wpdb->query( 'COMMIT' ); |
| 139 |
} |
| 140 |
} else { |
| 141 |
/** |
| 142 |
* Tutor Free - reqular student reg process. |
| 143 |
*/ |
| 144 |
$wpdb->query( 'COMMIT' ); |
| 145 |
|
| 146 |
wp_set_current_user( $user_id, $user->user_login ); |
| 147 |
wp_set_auth_cookie( $user_id ); |
| 148 |
|
| 149 |
do_action( 'tutor_after_student_signup', $user_id ); |
| 150 |
// since 1.9.8 do enroll if guest attempt to enroll. |
| 151 |
if ( ! empty( $enroll_attempt ) ) { |
| 152 |
do_action( 'tutor_do_enroll_after_login_if_attempt', $enroll_attempt, $user_id ); |
| 153 |
} |
| 154 |
|
| 155 |
// Redirect page. |
| 156 |
$redirect_page = tutor_utils()->array_get( 'redirect_to', $_REQUEST ); //phpcs:ignore |
| 157 |
if ( ! $redirect_page ) { |
| 158 |
$redirect_page = tutor_utils()->tutor_dashboard_url(); |
| 159 |
} |
| 160 |
wp_safe_redirect( apply_filters( 'tutor_student_register_redirect_url', $redirect_page, $user ) ); |
| 161 |
die(); |
| 162 |
} |
| 163 |
|
| 164 |
$registration_page = tutor_utils()->student_register_url(); |
| 165 |
wp_safe_redirect( $registration_page ); |
| 166 |
die(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get validation error messages |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* |
| 174 |
* @return mixed error messages |
| 175 |
*/ |
| 176 |
public function tutor_student_form_validation_errors() { |
| 177 |
return $this->error_msgs; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Update profile |
| 182 |
* |
| 183 |
* @since 1.0.0 |
| 184 |
* |
| 185 |
* @return void send wp_json response |
| 186 |
*/ |
| 187 |
public function update_profile() { |
| 188 |
// Checking nonce. |
| 189 |
tutor_utils()->checking_nonce(); |
| 190 |
|
| 191 |
$user_id = get_current_user_id(); |
| 192 |
do_action( 'tutor_profile_update_before', $user_id ); |
| 193 |
|
| 194 |
$first_name = sanitize_text_field( tutor_utils()->input_old( 'first_name' ) ); |
| 195 |
$last_name = sanitize_text_field( tutor_utils()->input_old( 'last_name' ) ); |
| 196 |
$phone_number = sanitize_text_field( tutor_utils()->input_old( 'phone_number' ) ); |
| 197 |
$tutor_profile_bio = wp_kses( Input::post( 'tutor_profile_bio', '', Input::TYPE_KSES_POST ), tutor_utils()->allowed_profile_bio_tags() ); |
| 198 |
$tutor_profile_job_title = sanitize_text_field( tutor_utils()->input_old( 'tutor_profile_job_title' ) ); |
| 199 |
$timezone = Input::post( 'timezone', '' ); |
| 200 |
|
| 201 |
$display_name = sanitize_text_field( tutor_utils()->input_old( 'display_name' ) ); |
| 202 |
|
| 203 |
$userdata = array( |
| 204 |
'ID' => $user_id, |
| 205 |
'first_name' => $first_name, |
| 206 |
'last_name' => $last_name, |
| 207 |
'display_name' => $display_name, |
| 208 |
); |
| 209 |
|
| 210 |
$user_id = wp_update_user( $userdata ); |
| 211 |
|
| 212 |
if ( ! is_wp_error( $user_id ) ) { |
| 213 |
update_user_meta( $user_id, User::PHONE_NUMBER_META, $phone_number ); |
| 214 |
update_user_meta( $user_id, User::PROFILE_BIO_META, $tutor_profile_bio ); |
| 215 |
update_user_meta( $user_id, User::PROFILE_JOB_TITLE_META, $tutor_profile_job_title ); |
| 216 |
update_user_meta( $user_id, User::TIMEZONE_META, $timezone ); |
| 217 |
} |
| 218 |
|
| 219 |
do_action( 'tutor_profile_update_after', $user_id ); |
| 220 |
|
| 221 |
wp_send_json_success( array( 'message' => __( 'Profile Updated', 'tutor' ) ) ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Filter Avatar, Change avatar URL with Tutor User Photo |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
* |
| 229 |
* @param string $url url. |
| 230 |
* @param mixed $id_or_email id or email. |
| 231 |
* @param array $args extra args. |
| 232 |
* |
| 233 |
* @return false|string |
| 234 |
*/ |
| 235 |
public function filter_avatar( $url, $id_or_email, $args ) { |
| 236 |
|
| 237 |
$finder = false; |
| 238 |
$is_id = is_numeric( $id_or_email ); |
| 239 |
|
| 240 |
if ( $is_id ) { |
| 241 |
$finder = absint( $id_or_email ); |
| 242 |
} elseif ( is_string( $id_or_email ) ) { |
| 243 |
$finder = $id_or_email; |
| 244 |
} elseif ( $id_or_email instanceof \WP_User ) { |
| 245 |
// User Object. |
| 246 |
$finder = $id_or_email->ID; |
| 247 |
} elseif ( $id_or_email instanceof \WP_Post ) { |
| 248 |
// Post Object. |
| 249 |
$finder = (int) $id_or_email->post_author; |
| 250 |
} elseif ( $id_or_email instanceof \WP_Comment ) { |
| 251 |
return $url; |
| 252 |
} |
| 253 |
|
| 254 |
if ( ! $finder ) { |
| 255 |
return $url; |
| 256 |
} |
| 257 |
|
| 258 |
$user = get_user_by( $is_id ? 'ID' : 'email', $finder ); |
| 259 |
|
| 260 |
if ( $user ) { |
| 261 |
$profile_photo = get_user_meta( $user->ID, '_tutor_profile_photo', true ); |
| 262 |
if ( $profile_photo ) { |
| 263 |
$size = isset( $args['size'] ) ? $args['size'] : 'thumbnail'; |
| 264 |
$url = wp_get_attachment_image_url( $profile_photo, $size ); |
| 265 |
} |
| 266 |
} |
| 267 |
return $url; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Password Rest |
| 272 |
* |
| 273 |
* @since 1.0.0 |
| 274 |
* |
| 275 |
* @return void send wp_json response |
| 276 |
*/ |
| 277 |
public function tutor_reset_password() { |
| 278 |
// Checking nonce. |
| 279 |
tutor_utils()->checking_nonce(); |
| 280 |
|
| 281 |
$user = wp_get_current_user(); |
| 282 |
|
| 283 |
$previous_password = Input::post( 'previous_password', '' ); |
| 284 |
$new_password = Input::post( 'new_password', '' ); |
| 285 |
$confirm_new_password = Input::post( 'confirm_new_password', '' ); |
| 286 |
|
| 287 |
$previous_password_checked = wp_check_password( $previous_password, $user->user_pass, $user->ID ); |
| 288 |
|
| 289 |
$validation_errors = array(); |
| 290 |
if ( ! $previous_password_checked ) { |
| 291 |
$validation_errors['incorrect_previous_password'] = __( 'Incorrect Previous Password', 'tutor' ); |
| 292 |
} |
| 293 |
if ( empty( $new_password ) ) { |
| 294 |
$validation_errors['new_password_required'] = __( 'New Password Required', 'tutor' ); |
| 295 |
} |
| 296 |
if ( empty( $confirm_new_password ) ) { |
| 297 |
$validation_errors['confirm_password_required'] = __( 'Confirm Password Required', 'tutor' ); |
| 298 |
} |
| 299 |
if ( $new_password !== $confirm_new_password ) { |
| 300 |
$validation_errors['password_not_matched'] = __( 'New password and confirm password does not matched', 'tutor' ); |
| 301 |
} |
| 302 |
|
| 303 |
if ( $previous_password_checked && ! empty( $new_password ) && $new_password === $confirm_new_password ) { |
| 304 |
wp_set_password( $new_password, $user->ID ); |
| 305 |
wp_send_json_success( array( 'message' => __( 'Password Changed', 'tutor' ) ) ); |
| 306 |
} |
| 307 |
|
| 308 |
$first_message = count( $validation_errors ) ? $validation_errors[ array_keys( $validation_errors )[0] ] : __( 'Something went wrong!', 'tutor' ); |
| 309 |
wp_send_json_error( array( 'message' => $first_message ) ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Handle social links |
| 314 |
* |
| 315 |
* @since 2.0.0 |
| 316 |
* |
| 317 |
* @return void |
| 318 |
*/ |
| 319 |
public function tutor_social_profile() { |
| 320 |
tutor_utils()->checking_nonce(); |
| 321 |
|
| 322 |
$user_id = get_current_user_id(); |
| 323 |
$tutor_user_social = tutor_utils()->tutor_user_social_icons(); |
| 324 |
|
| 325 |
foreach ( $tutor_user_social as $key => $social ) { |
| 326 |
$user_social_value = sanitize_text_field( tutor_utils()->input_old( $key ) ); |
| 327 |
if ( '' !== $user_social_value ) { |
| 328 |
update_user_meta( $user_id, $key, $user_social_value ); |
| 329 |
} else { |
| 330 |
delete_user_meta( $user_id, $key ); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
wp_send_json_success( array( 'message' => __( 'Social Profile Updated', 'tutor' ) ) ); |
| 335 |
die(); |
| 336 |
} |
| 337 |
} |
| 338 |
|