| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that manages the display and processing of the forgot password form. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Forgot_Password_Form |
| 6 |
* @author Rafe Colton, David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.4.0 |
| 10 |
* @version 1.5.1 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Forgot_Password_Form' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Forgot_Password_Form |
| 21 |
* |
| 22 |
* @since 1.4.0 |
| 23 |
*/ |
| 24 |
class Charitable_Forgot_Password_Form extends Charitable_Form { |
| 25 |
|
| 26 |
/** |
| 27 |
* Nonce action. |
| 28 |
* |
| 29 |
* @since 1.4.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $nonce_action = 'charitable_forgot_password'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Nonce name. |
| 37 |
* |
| 38 |
* @since 1.4.0 |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $nonce_name = '_charitable_forgot_password_nonce'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Form action. |
| 46 |
* |
| 47 |
* @since 1.4.0 |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $form_action = 'retrieve_password'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Create class object. |
| 55 |
* |
| 56 |
* @since 1.4.0 |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
$this->id = uniqid(); |
| 60 |
|
| 61 |
/* For backwards-compatibility */ |
| 62 |
add_action( 'charitable_form_field', array( $this, 'render_field' ), 10, 6 ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Forgot password fields to be displayed. |
| 67 |
* |
| 68 |
* @since 1.4.0 |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function get_fields() { |
| 73 |
$fields = apply_filters( |
| 74 |
'charitable_forgot_password_fields', |
| 75 |
array( |
| 76 |
'user_login' => array( |
| 77 |
'label' => __( 'Email Address', 'charitable' ), |
| 78 |
'type' => 'email', |
| 79 |
'required' => true, |
| 80 |
'priority' => 10, |
| 81 |
), |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
uasort( $fields, 'charitable_priority_sort' ); |
| 86 |
|
| 87 |
return $fields; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Send the password reset email. |
| 92 |
* |
| 93 |
* @since 1.4.0 |
| 94 |
* |
| 95 |
* @return bool|WP_Error True: when finish. WP_Error on error |
| 96 |
*/ |
| 97 |
public static function retrieve_password() { |
| 98 |
$form = new Charitable_Forgot_Password_Form(); |
| 99 |
|
| 100 |
if ( ! $form->validate_nonce() || ! $form->validate_honeypot() ) { |
| 101 |
charitable_get_notices()->add_error( __( 'Unfortunately, we were unable to verify your form submission. Please reload the page and try again.', 'charitable' ) ); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
if ( empty( $_POST['user_login'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 106 |
charitable_get_notices()->add_error( __( '<strong>ERROR</strong>: Enter a username or email address.', 'charitable' ) ); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
/* Sanitize and unslash the user input. */ |
| 111 |
$user_login = sanitize_text_field( wp_unslash( $_POST['user_login'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 112 |
|
| 113 |
if ( strpos( $user_login, '@' ) ) { |
| 114 |
$user = get_user_by( 'email', trim( $user_login ) ); |
| 115 |
} else { |
| 116 |
$login = trim( $user_login ); |
| 117 |
$user = get_user_by( 'login', $login ); |
| 118 |
} |
| 119 |
|
| 120 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- lostpassword_post is a WordPress core hook for password reset functionality. |
| 121 |
do_action( 'lostpassword_post' ); |
| 122 |
|
| 123 |
/* If we are missing user data, proceed no further. */ |
| 124 |
if ( ! $user ) { |
| 125 |
charitable_get_notices()->add_error( __( '<strong>ERROR</strong>: Invalid username or email.', 'charitable' ) ); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
/* Prepare the email. */ |
| 130 |
$email = new Charitable_Email_Password_Reset( array( 'user' => $user ) ); |
| 131 |
|
| 132 |
/* Make sure that the reset link was generated correctly. */ |
| 133 |
if ( is_wp_error( $email->get_reset_link() ) ) { |
| 134 |
charitable_get_notices()->add_errors_from_wp_error( $email->get_reset_link() ); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$sent = $email->send(); |
| 139 |
|
| 140 |
if ( ! $sent ) { |
| 141 |
charitable_get_notices()->add_error( __( 'We were unable to send your password reset email.', 'charitable' ) ); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
charitable_get_notices()->add_success( __( 'Your password reset request has been received. Please check your email for a link to reset your password.', 'charitable' ) ); |
| 146 |
|
| 147 |
charitable_get_session()->add_notices(); |
| 148 |
|
| 149 |
$redirect_url = esc_url_raw( charitable_get_permalink( 'login_page' ) ); |
| 150 |
|
| 151 |
wp_safe_redirect( $redirect_url ); |
| 152 |
|
| 153 |
exit(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
endif; |
| 158 |
|