| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Manage Form | |
| 3 | + * FormHandler class | |
| 4 | 4 | * |
| 5 | + * @author: themeum | |
| 6 | + * @link: https://themeum.com | |
| 5 | 7 | * @package Tutor |
| 6 | - * @author Themeum <support@themeum.com> | |
| 7 | - * @link https://themeum.com | |
| 8 | - * @since 1.4.3 | |
| 8 | + * @since v.1.4.3 | |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace TUTOR; |
| 12 | 12 | |
| @@ -13,49 +13,22 @@ | ||
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | -/** | |
| 18 | - * FormHandler class | |
| 19 | - * | |
| 20 | - * @since 1.4.3 | |
| 21 | - */ | |
| 17 | + | |
| 22 | 18 | class FormHandler { |
| 23 | 19 | |
| 24 | - /** | |
| 25 | - * Constructor | |
| 26 | - * | |
| 27 | - * @since 1.4.3 | |
| 28 | - * @return void | |
| 29 | - */ | |
| 30 | 20 | public function __construct() { |
| 31 | 21 | add_action( 'tutor_action_tutor_retrieve_password', array( $this, 'tutor_retrieve_password' ) ); |
| 32 | 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 ); | |
| 33 | 25 | add_filter( 'tutor_lostpassword_url', array( $this, 'lostpassword_url' ) ); |
| 34 | 26 | } |
| 35 | 27 | |
| 36 | - /** | |
| 37 | - * Retrieve Password | |
| 38 | - * | |
| 39 | - * @since 1.4.3 | |
| 40 | - * | |
| 41 | - * @return void|bool | |
| 42 | - */ | |
| 43 | 28 | public function tutor_retrieve_password() { |
| 44 | 29 | tutils()->checking_nonce(); |
| 45 | 30 | |
| 46 | - /** | |
| 47 | - * To check spam or other logic before form process. | |
| 48 | - * | |
| 49 | - * @since 2.1.10 | |
| 50 | - */ | |
| 51 | - $before_form_process = apply_filters( 'tutor_before_retrieve_password_form_process', null ); | |
| 52 | - if ( is_wp_error( $before_form_process ) ) { | |
| 53 | - tutor_flash_set( 'danger', $before_form_process->get_error_message() ); | |
| 54 | - return false; | |
| 55 | - } | |
| 56 | - | |
| 57 | - //phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 58 | 31 | $login = sanitize_user( tutils()->array_get( 'user_login', $_POST ) ); |
| 59 | 32 | |
| 60 | 33 | if ( empty( $login ) ) { |
| 61 | 34 | tutor_flash_set( 'danger', __( 'Enter a username or email address.', 'tutor' ) ); |
| @@ -103,43 +76,35 @@ | ||
| 103 | 76 | tutor_flash_set( 'danger', $allow->get_error_message() ); |
| 104 | 77 | return false; |
| 105 | 78 | } |
| 106 | 79 | |
| 107 | - $errors = retrieve_password(); | |
| 108 | - if ( is_wp_error( $errors ) ) { | |
| 109 | - tutor_flash_set( 'danger', $errors->get_error_message() ); | |
| 110 | - return false; | |
| 111 | - } | |
| 80 | + // Get password reset key (function introduced in WordPress 4.4). | |
| 81 | + $key = get_password_reset_key( $user_data ); | |
| 112 | 82 | |
| 113 | - $html = '<p> ' . __( "We've sent an email to this account's email address. Click the link in the email to reset your password. If you don't see the email, check other places it might be, like your junk, spam, social, promotion or others folders.", 'tutor' ) . '</p>'; | |
| 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>'; | |
| 114 | 93 | tutor_flash_set( 'success', $html ); |
| 115 | 94 | } |
| 116 | 95 | |
| 117 | - /** | |
| 118 | - * Get lost password URL | |
| 119 | - * | |
| 120 | - * @since 1.4.3 | |
| 121 | - * | |
| 122 | - * @param string $url URL. | |
| 123 | - * @return string | |
| 124 | - */ | |
| 125 | 96 | public function lostpassword_url( $url ) { |
| 126 | 97 | return tutils()->tutor_dashboard_url( 'retrieve-password' ); |
| 127 | 98 | } |
| 128 | 99 | |
| 129 | - /** | |
| 130 | - * Handle reset password request | |
| 131 | - * | |
| 132 | - * @since 1.4.3 | |
| 133 | - * @return void|bool | |
| 134 | - */ | |
| 135 | 100 | public function tutor_process_reset_password() { |
| 136 | 101 | tutils()->checking_nonce(); |
| 137 | 102 | |
| 138 | - $reset_key = Input::post( 'reset_key' ); | |
| 139 | - $user_id = Input::post( 'user_id', 0, Input::TYPE_INT ); | |
| 140 | - $password = Input::post( 'password' ); | |
| 141 | - $confirm_password = Input::post( 'confirm_password' ); | |
| 103 | + $reset_key = sanitize_text_field( tutils()->array_get( 'reset_key', $_POST ) ); | |
| 104 | + $user_id = (int) 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 ) ); | |
| 142 | 107 | |
| 143 | 108 | $user = get_user_by( 'ID', $user_id ); |
| 144 | 109 | $user = check_password_reset_key( $reset_key, $user->user_login ); |
| 145 | 110 | |
| @@ -178,16 +143,46 @@ | ||
| 178 | 143 | } |
| 179 | 144 | } |
| 180 | 145 | |
| 181 | 146 | /** |
| 147 | + * Send Password Reset E-Mail to user. | |
| 148 | + * We are sending directly right now, later we will introduce centralised E-Mail notification System... | |
| 149 | + * | |
| 150 | + * @param $user_login login username. | |
| 151 | + * @param $reset_key password reset key. | |
| 152 | + */ | |
| 153 | + public function sendNotification( $user_login, $reset_key ) { | |
| 154 | + | |
| 155 | + $user_data = get_user_by( 'login', $user_login ); | |
| 156 | + | |
| 157 | + $variable = array( | |
| 158 | + 'user_login' => $user_login, | |
| 159 | + 'reset_key' => $reset_key, | |
| 160 | + 'user_id' => $user_data->ID, | |
| 161 | + ); | |
| 162 | + | |
| 163 | + $html = tutor_get_template_html( 'email.send-reset-password', $variable ); | |
| 164 | + $subject = sprintf( __( 'Password Reset Request for %s', 'tutor' ), get_option( 'blogname' ) ); | |
| 165 | + | |
| 166 | + $header = 'Content-Type: text/html' . "\r\n"; | |
| 167 | + | |
| 168 | + add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 169 | + add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 170 | + | |
| 171 | + wp_mail( $user_data->user_email, $subject, $html, $header ); | |
| 172 | + | |
| 173 | + remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); | |
| 174 | + remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 182 | 178 | * Get e-mail from address |
| 183 | 179 | * |
| 184 | - * @since 1.4.3 | |
| 185 | 180 | * @return string |
| 186 | 181 | */ |
| 187 | 182 | public function get_from_address() { |
| 188 | 183 | $from_address = get_tutor_option( 'email_from_address' ); |
| 189 | - $default = ! $from_address ? get_option( 'admin_email' ) : $from_address; | |
| 184 | + $default = !$from_address ? get_option( 'admin_email' ) : $from_address; | |
| 190 | 185 | return apply_filters( 'tutor_email_from_address', $default ); |
| 191 | 186 | } |
| 192 | 187 | |
| 193 | 188 | /** |
| @@ -192,14 +187,13 @@ | ||
| 192 | 187 | |
| 193 | 188 | /** |
| 194 | 189 | * Get e-mail from name |
| 195 | 190 | * |
| 196 | - * @since 1.4.3 | |
| 197 | 191 | * @return string |
| 198 | 192 | */ |
| 199 | 193 | public function get_from_name() { |
| 200 | 194 | $from_name = get_tutor_option( 'email_from_name' ); |
| 201 | - $default = ! $from_name ? get_option( 'blogname' ) : $from_name; | |
| 195 | + $default = !$from_name ? get_option( 'blogname' ) : $from_name; | |
| 202 | 196 | return apply_filters( 'tutor_email_from_name', $default ); |
| 203 | 197 | } |
| 204 | 198 | |
| 205 | 199 | } |