| @@ -1,184 +1,184 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * FormHandler class | |
| 4 | - * | |
| 5 | - * @author: themeum | |
| 6 | - * @author_uri: https://themeum.com | |
| 7 | - * @package Tutor | |
| 8 | - * @since v.1.4.3 | |
| 9 | - */ | |
| 10 | - | |
| 11 | -namespace TUTOR; | |
| 12 | - | |
| 13 | - | |
| 14 | -if ( ! defined( 'ABSPATH' ) ) | |
| 15 | - exit; | |
| 16 | - | |
| 17 | - | |
| 18 | -class FormHandler { | |
| 19 | - | |
| 20 | - public function __construct() { | |
| 21 | - add_action('tutor_action_tutor_retrieve_password', array($this, 'tutor_retrieve_password')); | |
| 22 | - add_action('tutor_action_tutor_process_reset_password', array($this, 'tutor_process_reset_password')); | |
| 23 | - | |
| 24 | - add_action( 'tutor_reset_password_notification', array( $this, 'reset_password_notification' ), 10, 2 ); | |
| 25 | - add_filter( 'tutor_lostpassword_url', array( $this, 'lostpassword_url' ) ); | |
| 26 | - } | |
| 27 | - | |
| 28 | - public function tutor_retrieve_password(){ | |
| 29 | - tutils()->checking_nonce(); | |
| 30 | - | |
| 31 | - $login = sanitize_user( tutils()->array_get('user_login', $_POST)); | |
| 32 | - | |
| 33 | - if ( empty( $login ) ) { | |
| 34 | - tutor_flash_set('danger', __( 'Enter a username or email address.', 'tutor' )); | |
| 35 | - return false; | |
| 36 | - } else { | |
| 37 | - // Check on username first, as customers can use emails as usernames. | |
| 38 | - $user_data = get_user_by( 'login', $login ); | |
| 39 | - } | |
| 40 | - | |
| 41 | - // If no user found, check if it login is email and lookup user based on email. | |
| 42 | - if ( ! $user_data && is_email( $login ) && apply_filters( 'tutor_get_username_from_email', true ) ) { | |
| 43 | - $user_data = get_user_by( 'email', $login ); | |
| 44 | - } | |
| 45 | - | |
| 46 | - $errors = new \WP_Error(); | |
| 47 | - | |
| 48 | - do_action( 'lostpassword_post', $errors ); | |
| 49 | - | |
| 50 | - if ( $errors->get_error_code() ) { | |
| 51 | - tutor_flash_set('danger', $errors->get_error_message() ); | |
| 52 | - return false; | |
| 53 | - } | |
| 54 | - | |
| 55 | - if ( ! $user_data ) { | |
| 56 | - tutor_flash_set('danger', __( 'Invalid username or email.', 'tutor' ) ); | |
| 57 | - return false; | |
| 58 | - } | |
| 59 | - | |
| 60 | - if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { | |
| 61 | - tutor_flash_set('danger', __( 'Invalid username or email.', 'tutor' ) ); | |
| 62 | - return false; | |
| 63 | - } | |
| 64 | - | |
| 65 | - // Redefining user_login ensures we return the right case in the email. | |
| 66 | - $user_login = $user_data->user_login; | |
| 67 | - | |
| 68 | - do_action( 'retrieve_password', $user_login ); | |
| 69 | - | |
| 70 | - $allow = apply_filters( 'allow_password_reset', true, $user_data->ID ); | |
| 71 | - | |
| 72 | - if ( ! $allow ) { | |
| 73 | - tutor_flash_set('danger', __( 'Password reset is not allowed for this user', 'tutor' ) ); | |
| 74 | - return false; | |
| 75 | - } elseif ( is_wp_error( $allow ) ) { | |
| 76 | - tutor_flash_set('danger', $allow->get_error_message() ); | |
| 77 | - return false; | |
| 78 | - } | |
| 79 | - | |
| 80 | - // Get password reset key (function introduced in WordPress 4.4). | |
| 81 | - $key = get_password_reset_key($user_data); | |
| 82 | - | |
| 83 | - // Send email notification. | |
| 84 | - do_action( 'tutor_reset_password_notification', $user_login, $key ); | |
| 85 | - } | |
| 86 | - | |
| 87 | - public function reset_password_notification( $user_login = '', $reset_key = ''){ | |
| 88 | - $this->sendNotification($user_login, $reset_key); | |
| 89 | - | |
| 90 | - $html = "<h3>".__('Check your E-Mail', 'tutor')."</h3>"; | |
| 91 | - $html .= "<p>".__("We've sent an email to this account's email address. Click the link in the email to reset your password", 'tutor')."</p>"; | |
| 92 | - $html .= "<p>".__("If you don't see the email, check other places it might be, like your junk, spam, social, promotion or others folders.", 'tutor')."</p>"; | |
| 93 | - tutor_flash_set('success', $html); | |
| 94 | - } | |
| 95 | - | |
| 96 | - public function lostpassword_url($url){ | |
| 97 | - return tutils()->tutor_dashboard_url('retrieve-password'); | |
| 98 | - } | |
| 99 | - | |
| 100 | - public function tutor_process_reset_password(){ | |
| 101 | - tutils()->checking_nonce(); | |
| 102 | - | |
| 103 | - $reset_key = sanitize_text_field(tutils()->array_get('reset_key', $_POST)); | |
| 104 | - $user_id = (int) sanitize_text_field(tutils()->array_get('user_id', $_POST)); | |
| 105 | - $password = sanitize_text_field(tutils()->array_get('password', $_POST)); | |
| 106 | - $confirm_password = sanitize_text_field(tutils()->array_get('confirm_password', $_POST)); | |
| 107 | - | |
| 108 | - $user = get_user_by('ID', $user_id); | |
| 109 | - $user = check_password_reset_key( $reset_key, $user->user_login ); | |
| 110 | - | |
| 111 | - if ( is_wp_error( $user ) ) { | |
| 112 | - tutor_flash_set('danger', __( 'This key is invalid or has already been used. Please reset your password again if needed.', 'tutor') ); | |
| 113 | - return false; | |
| 114 | - } | |
| 115 | - | |
| 116 | - | |
| 117 | - if ( $user instanceof \WP_User ) { | |
| 118 | - if ( !$password ) { | |
| 119 | - tutor_flash_set('danger', __( 'Please enter your password.', 'tutor') ); | |
| 120 | - return false; | |
| 121 | - } | |
| 122 | - | |
| 123 | - if ( $password !== $confirm_password) { | |
| 124 | - tutor_flash_set('danger', __( 'Passwords do not match.', 'tutor') ); | |
| 125 | - return false; | |
| 126 | - } | |
| 127 | - | |
| 128 | - tutils()->reset_password($user, $password); | |
| 129 | - | |
| 130 | - do_action( 'tutor_user_reset_password', $user ); | |
| 131 | - | |
| 132 | - // Perform the login. | |
| 133 | - $creds = array('user_login' => $user->user_login, 'user_password' => $password, 'remember' => true); | |
| 134 | - $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() ); | |
| 135 | - | |
| 136 | - do_action( 'tutor_user_reset_password_login', $user ); | |
| 137 | - | |
| 138 | - wp_safe_redirect( tutils()->tutor_dashboard_url() ); | |
| 139 | - exit; | |
| 140 | - } | |
| 141 | - } | |
| 142 | - | |
| 143 | - /** | |
| 144 | - * @param $user_login | |
| 145 | - * @param $reset_key | |
| 146 | - * | |
| 147 | - * Send E-Mail notification | |
| 148 | - * We are sending directly right now, later we will introduce centralised E-Mail notification System... | |
| 149 | - */ | |
| 150 | - public function sendNotification($user_login, $reset_key){ | |
| 151 | - //Send the E-Mail to user | |
| 152 | - | |
| 153 | - $user_data = get_user_by( 'login', $user_login ); | |
| 154 | - | |
| 155 | - $variable = array( | |
| 156 | - 'user_login' => $user_login, | |
| 157 | - 'reset_key' => $reset_key, | |
| 158 | - 'user_id' => $user_data->ID, | |
| 159 | - ); | |
| 160 | - | |
| 161 | - $html = tutor_get_template_html('email.send-reset-password', $variable); | |
| 162 | - $subject = sprintf(__( 'Password Reset Request for %s', 'tutor' ), get_option( 'blogname' )); | |
| 163 | - | |
| 164 | - $header = 'Content-Type: text/html' . "\r\n"; | |
| 165 | - | |
| 166 | - add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 167 | - add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 168 | - | |
| 169 | - wp_mail($user_data->user_email, $subject, $html, $header); | |
| 170 | - | |
| 171 | - remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 172 | - remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 173 | - } | |
| 174 | - | |
| 175 | - public function get_from_address(){ | |
| 176 | - return apply_filters('tutor_email_from_address', get_tutor_option('email_from_address')); | |
| 177 | - } | |
| 178 | - | |
| 179 | - public function get_from_name(){ | |
| 180 | - return apply_filters('tutor_email_from_name', get_tutor_option('email_from_name')); | |
| 181 | - } | |
| 182 | - | |
| 183 | - | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * FormHandler class | |
| 4 | + * | |
| 5 | + * @author: themeum | |
| 6 | + * @author_uri: https://themeum.com | |
| 7 | + * @package Tutor | |
| 8 | + * @since v.1.4.3 | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace TUTOR; | |
| 12 | + | |
| 13 | + | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) | |
| 15 | + exit; | |
| 16 | + | |
| 17 | + | |
| 18 | +class FormHandler { | |
| 19 | + | |
| 20 | + public function __construct() { | |
| 21 | + add_action('tutor_action_tutor_retrieve_password', array($this, 'tutor_retrieve_password')); | |
| 22 | + add_action('tutor_action_tutor_process_reset_password', array($this, 'tutor_process_reset_password')); | |
| 23 | + | |
| 24 | + add_action( 'tutor_reset_password_notification', array( $this, 'reset_password_notification' ), 10, 2 ); | |
| 25 | + add_filter( 'tutor_lostpassword_url', array( $this, 'lostpassword_url' ) ); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public function tutor_retrieve_password(){ | |
| 29 | + tutils()->checking_nonce(); | |
| 30 | + | |
| 31 | + $login = sanitize_user( tutils()->array_get('user_login', $_POST)); | |
| 32 | + | |
| 33 | + if ( empty( $login ) ) { | |
| 34 | + tutor_flash_set('danger', __( 'Enter a username or email address.', 'tutor' )); | |
| 35 | + return false; | |
| 36 | + } else { | |
| 37 | + // Check on username first, as customers can use emails as usernames. | |
| 38 | + $user_data = get_user_by( 'login', $login ); | |
| 39 | + } | |
| 40 | + | |
| 41 | + // If no user found, check if it login is email and lookup user based on email. | |
| 42 | + if ( ! $user_data && is_email( $login ) && apply_filters( 'tutor_get_username_from_email', true ) ) { | |
| 43 | + $user_data = get_user_by( 'email', $login ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + $errors = new \WP_Error(); | |
| 47 | + | |
| 48 | + do_action( 'lostpassword_post', $errors ); | |
| 49 | + | |
| 50 | + if ( $errors->get_error_code() ) { | |
| 51 | + tutor_flash_set('danger', $errors->get_error_message() ); | |
| 52 | + return false; | |
| 53 | + } | |
| 54 | + | |
| 55 | + if ( ! $user_data ) { | |
| 56 | + tutor_flash_set('danger', __( 'Invalid username or email.', 'tutor' ) ); | |
| 57 | + return false; | |
| 58 | + } | |
| 59 | + | |
| 60 | + if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { | |
| 61 | + tutor_flash_set('danger', __( 'Invalid username or email.', 'tutor' ) ); | |
| 62 | + return false; | |
| 63 | + } | |
| 64 | + | |
| 65 | + // Redefining user_login ensures we return the right case in the email. | |
| 66 | + $user_login = $user_data->user_login; | |
| 67 | + | |
| 68 | + do_action( 'retrieve_password', $user_login ); | |
| 69 | + | |
| 70 | + $allow = apply_filters( 'allow_password_reset', true, $user_data->ID ); | |
| 71 | + | |
| 72 | + if ( ! $allow ) { | |
| 73 | + tutor_flash_set('danger', __( 'Password reset is not allowed for this user', 'tutor' ) ); | |
| 74 | + return false; | |
| 75 | + } elseif ( is_wp_error( $allow ) ) { | |
| 76 | + tutor_flash_set('danger', $allow->get_error_message() ); | |
| 77 | + return false; | |
| 78 | + } | |
| 79 | + | |
| 80 | + // Get password reset key (function introduced in WordPress 4.4). | |
| 81 | + $key = get_password_reset_key($user_data); | |
| 82 | + | |
| 83 | + // Send email notification. | |
| 84 | + do_action( 'tutor_reset_password_notification', $user_login, $key ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + public function reset_password_notification( $user_login = '', $reset_key = ''){ | |
| 88 | + $this->sendNotification($user_login, $reset_key); | |
| 89 | + | |
| 90 | + $html = "<h3>".__('Check your E-Mail', 'tutor')."</h3>"; | |
| 91 | + $html .= "<p>".__("We've sent an email to this account's email address. Click the link in the email to reset your password", 'tutor')."</p>"; | |
| 92 | + $html .= "<p>".__("If you don't see the email, check other places it might be, like your junk, spam, social, promotion or others folders.", 'tutor')."</p>"; | |
| 93 | + tutor_flash_set('success', $html); | |
| 94 | + } | |
| 95 | + | |
| 96 | + public function lostpassword_url($url){ | |
| 97 | + return tutils()->tutor_dashboard_url('retrieve-password'); | |
| 98 | + } | |
| 99 | + | |
| 100 | + public function tutor_process_reset_password(){ | |
| 101 | + tutils()->checking_nonce(); | |
| 102 | + | |
| 103 | + $reset_key = sanitize_text_field(tutils()->array_get('reset_key', $_POST)); | |
| 104 | + $user_id = (int) sanitize_text_field(tutils()->array_get('user_id', $_POST)); | |
| 105 | + $password = sanitize_text_field(tutils()->array_get('password', $_POST)); | |
| 106 | + $confirm_password = sanitize_text_field(tutils()->array_get('confirm_password', $_POST)); | |
| 107 | + | |
| 108 | + $user = get_user_by('ID', $user_id); | |
| 109 | + $user = check_password_reset_key( $reset_key, $user->user_login ); | |
| 110 | + | |
| 111 | + if ( is_wp_error( $user ) ) { | |
| 112 | + tutor_flash_set('danger', __( 'This key is invalid or has already been used. Please reset your password again if needed.', 'tutor') ); | |
| 113 | + return false; | |
| 114 | + } | |
| 115 | + | |
| 116 | + | |
| 117 | + if ( $user instanceof \WP_User ) { | |
| 118 | + if ( !$password ) { | |
| 119 | + tutor_flash_set('danger', __( 'Please enter your password.', 'tutor') ); | |
| 120 | + return false; | |
| 121 | + } | |
| 122 | + | |
| 123 | + if ( $password !== $confirm_password) { | |
| 124 | + tutor_flash_set('danger', __( 'Passwords do not match.', 'tutor') ); | |
| 125 | + return false; | |
| 126 | + } | |
| 127 | + | |
| 128 | + tutils()->reset_password($user, $password); | |
| 129 | + | |
| 130 | + do_action( 'tutor_user_reset_password', $user ); | |
| 131 | + | |
| 132 | + // Perform the login. | |
| 133 | + $creds = array('user_login' => $user->user_login, 'user_password' => $password, 'remember' => true); | |
| 134 | + $user = wp_signon( apply_filters( 'tutor_login_credentials', $creds ), is_ssl() ); | |
| 135 | + | |
| 136 | + do_action( 'tutor_user_reset_password_login', $user ); | |
| 137 | + | |
| 138 | + wp_safe_redirect( tutils()->tutor_dashboard_url() ); | |
| 139 | + exit; | |
| 140 | + } | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * @param $user_login | |
| 145 | + * @param $reset_key | |
| 146 | + * | |
| 147 | + * Send E-Mail notification | |
| 148 | + * We are sending directly right now, later we will introduce centralised E-Mail notification System... | |
| 149 | + */ | |
| 150 | + public function sendNotification($user_login, $reset_key){ | |
| 151 | + //Send the E-Mail to user | |
| 152 | + | |
| 153 | + $user_data = get_user_by( 'login', $user_login ); | |
| 154 | + | |
| 155 | + $variable = array( | |
| 156 | + 'user_login' => $user_login, | |
| 157 | + 'reset_key' => $reset_key, | |
| 158 | + 'user_id' => $user_data->ID, | |
| 159 | + ); | |
| 160 | + | |
| 161 | + $html = tutor_get_template_html('email.send-reset-password', $variable); | |
| 162 | + $subject = sprintf(__( 'Password Reset Request for %s', 'tutor' ), get_option( 'blogname' )); | |
| 163 | + | |
| 164 | + $header = 'Content-Type: text/html' . "\r\n"; | |
| 165 | + | |
| 166 | + add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 167 | + add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 168 | + | |
| 169 | + wp_mail($user_data->user_email, $subject, $html, $header); | |
| 170 | + | |
| 171 | + remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 172 | + remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 173 | + } | |
| 174 | + | |
| 175 | + public function get_from_address(){ | |
| 176 | + return apply_filters('tutor_email_from_address', get_tutor_option('email_from_address')); | |
| 177 | + } | |
| 178 | + | |
| 179 | + public function get_from_name(){ | |
| 180 | + return apply_filters('tutor_email_from_name', get_tutor_option('email_from_name')); | |
| 181 | + } | |
| 182 | + | |
| 183 | + | |
| 184 | 184 | } |