| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that models the Password Reset email. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Email_Password_Reset |
| 6 |
* @author 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.6.37 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Email_Password_Reset' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Password Reset Email |
| 21 |
* |
| 22 |
* @since 1.4.0 |
| 23 |
*/ |
| 24 |
class Charitable_Email_Password_Reset extends Charitable_Email { |
| 25 |
|
| 26 |
/** Email ID */ |
| 27 |
const ID = 'password_reset'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether the email allows you to define the email recipients. |
| 31 |
* |
| 32 |
* @since 1.4.0 |
| 33 |
* |
| 34 |
* @var boolean |
| 35 |
*/ |
| 36 |
protected $has_recipient_field = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* The Password Reset email is required. |
| 40 |
* |
| 41 |
* @since 1.4.0 |
| 42 |
* |
| 43 |
* @var boolean |
| 44 |
*/ |
| 45 |
protected $required = true; |
| 46 |
|
| 47 |
/** |
| 48 |
* The user data. |
| 49 |
* |
| 50 |
* @since 1.4.0 |
| 51 |
* |
| 52 |
* @var WP_User |
| 53 |
*/ |
| 54 |
protected $user; |
| 55 |
|
| 56 |
/** |
| 57 |
* Array of supported object types (campaigns, donations, donors, etc). |
| 58 |
* |
| 59 |
* @since 1.4.0 |
| 60 |
* |
| 61 |
* @var string[] |
| 62 |
*/ |
| 63 |
protected $object_types = array( 'user' ); |
| 64 |
|
| 65 |
/** |
| 66 |
* The reset link. |
| 67 |
* |
| 68 |
* @since 1.4.0 |
| 69 |
* |
| 70 |
* @var string|WP_Error |
| 71 |
*/ |
| 72 |
protected $reset_link; |
| 73 |
|
| 74 |
/** |
| 75 |
* Instantiate the email class, defining its key values. |
| 76 |
* |
| 77 |
* @since 1.4.0 |
| 78 |
* |
| 79 |
* @param mixed[] $objects Array containing a user object, |
| 80 |
* or nothing if this is a preview. |
| 81 |
*/ |
| 82 |
public function __construct( $objects = array() ) { |
| 83 |
parent::__construct( $objects ); |
| 84 |
|
| 85 |
/** |
| 86 |
* Filter the default email name. |
| 87 |
* |
| 88 |
* @since 1.4.0 |
| 89 |
* |
| 90 |
* @param string $name The email name. |
| 91 |
*/ |
| 92 |
$this->name = apply_filters( 'charitable_email_password_reset_name', __( 'User: Password Reset', 'charitable' ) ); |
| 93 |
|
| 94 |
$this->user = array_key_exists( 'user', $objects ) ? $objects['user'] : false; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the current email's ID. |
| 99 |
* |
| 100 |
* @since 1.4.0 |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public static function get_email_id() { |
| 105 |
return self::ID; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return the recipient for the email. |
| 110 |
* |
| 111 |
* @since 1.4.0 |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function get_recipient() { |
| 116 |
return $this->has_valid_user() ? $this->user->user_email : ''; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the custom email fields for this email. |
| 121 |
* |
| 122 |
* @since 1.5.0 |
| 123 |
* |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function email_fields() { |
| 127 |
/* If we are using the default WordPress login process, return an empty array. */ |
| 128 |
if ( wp_login_url() == charitable_get_permalink( 'login_page' ) ) { |
| 129 |
return array(); |
| 130 |
} |
| 131 |
|
| 132 |
$fields = array( |
| 133 |
'reset_link' => array( |
| 134 |
'description' => __( 'The link the user needs to click to reset their password', 'charitable' ), |
| 135 |
'preview' => add_query_arg( |
| 136 |
array( |
| 137 |
'key' => '123123123', |
| 138 |
'login' => 'adam123', |
| 139 |
), |
| 140 |
charitable_get_permalink( 'reset_password_page' ) |
| 141 |
), |
| 142 |
), |
| 143 |
); |
| 144 |
|
| 145 |
if ( $this->has_valid_user() ) { |
| 146 |
$fields = array_merge_recursive( |
| 147 |
$fields, |
| 148 |
array( |
| 149 |
'reset_link' => array( 'callback' => array( $this, 'get_reset_link' ) ), |
| 150 |
) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
return $fields; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return the reset link. |
| 159 |
* |
| 160 |
* @since 1.5.0 |
| 161 |
* |
| 162 |
* @return string|WP_Error|false If the reset key could not be generated, an error is returned. |
| 163 |
*/ |
| 164 |
public function get_reset_link() { |
| 165 |
if ( ! isset( $this->reset_link ) ) { |
| 166 |
$base_url = charitable_get_permalink( 'reset_password_page' ); |
| 167 |
$key = get_password_reset_key( $this->user ); |
| 168 |
|
| 169 |
if ( is_wp_error( $key ) ) { |
| 170 |
return $key; |
| 171 |
} |
| 172 |
|
| 173 |
$this->reset_link = esc_url_raw( |
| 174 |
add_query_arg( |
| 175 |
array( |
| 176 |
'key' => $key, |
| 177 |
'login' => rawurlencode( $this->user->user_login ), |
| 178 |
'charitable' => true, |
| 179 |
), |
| 180 |
$base_url |
| 181 |
) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
return $this->reset_link; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Return the default subject line for the email. |
| 190 |
* |
| 191 |
* @since 1.4.0 |
| 192 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
protected function get_default_subject() { |
| 196 |
return __( 'Password Reset for [charitable_email show=site_name]', 'charitable' ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Return the default headline for the email. |
| 201 |
* |
| 202 |
* @since 1.4.0 |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
protected function get_default_headline() { |
| 207 |
/** |
| 208 |
* Filter the default email headline. |
| 209 |
* |
| 210 |
* @since 1.4.0 |
| 211 |
* |
| 212 |
* @param string $headline The default headline. |
| 213 |
* @param Charitable_Email_Password_Reset $email This instance of `Charitable_Email_Password_Reset`. |
| 214 |
*/ |
| 215 |
return apply_filters( 'charitable_email_password_reset_default_headline', __( 'Reset your password', 'charitable' ), $this ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Return the default body for the email. |
| 220 |
* |
| 221 |
* @since 1.4.0 |
| 222 |
* |
| 223 |
* @return string |
| 224 |
*/ |
| 225 |
protected function get_default_body() { |
| 226 |
ob_start(); |
| 227 |
?> |
| 228 |
<p><?php esc_html_e( 'Someone requested that the password be reset for the following account:', 'charitable' ); ?></p> |
| 229 |
<p><?php esc_html_e( 'Username: [charitable_email show=user_login]', 'charitable' ); ?></p> |
| 230 |
<p><?php esc_html_e( 'If this was a mistake, just ignore this email and nothing will happen.', 'charitable' ); ?></p> |
| 231 |
<p><?php esc_html_e( 'To reset your password, visit the following address:', 'charitable' ); ?></p> |
| 232 |
<p><a href="[charitable_email show=reset_link]">[charitable_email show=reset_link]</a></p> |
| 233 |
<?php |
| 234 |
/** |
| 235 |
* Filter the default email body. |
| 236 |
* |
| 237 |
* @since 1.4.0 |
| 238 |
* |
| 239 |
* @param string $body The default body text. |
| 240 |
* @param Charitable_Email_Password_Reset $email This instance of `Charitable_Email_Password_Reset`. |
| 241 |
*/ |
| 242 |
return apply_filters( 'charitable_email_password_reset_default_body', ob_get_clean(), $this ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Check whether a user is set for the object instance. |
| 247 |
* |
| 248 |
* @since 1.5.0 |
| 249 |
* |
| 250 |
* @return boolean |
| 251 |
*/ |
| 252 |
protected function has_valid_user() { |
| 253 |
return isset( $this->user ) && is_a( $this->user, 'WP_User' ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
endif; |
| 258 |
|