| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that models the Email Verification email. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Email_Email_Verification |
| 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.5.0 |
| 10 |
* @version 1.6.37 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Email_Email_Verification' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Password Reset Email |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Email_Email_Verification extends Charitable_Email { |
| 25 |
|
| 26 |
/** Email ID */ |
| 27 |
const ID = 'email_verification'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether the email allows you to define the email recipients. |
| 31 |
* |
| 32 |
* @since 1.5.0 |
| 33 |
* |
| 34 |
* @var boolean |
| 35 |
*/ |
| 36 |
protected $has_recipient_field = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* The Email Verification email is required. |
| 40 |
* |
| 41 |
* @since 1.5.0 |
| 42 |
* |
| 43 |
* @var boolean |
| 44 |
*/ |
| 45 |
protected $required = true; |
| 46 |
|
| 47 |
/** |
| 48 |
* The user data. |
| 49 |
* |
| 50 |
* @since 1.5.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.5.0 |
| 60 |
* |
| 61 |
* @var string[] |
| 62 |
*/ |
| 63 |
protected $object_types = array( 'user' ); |
| 64 |
|
| 65 |
/** |
| 66 |
* The confirmation link. |
| 67 |
* |
| 68 |
* @since 1.5.0 |
| 69 |
* |
| 70 |
* @var string|WP_Error |
| 71 |
*/ |
| 72 |
protected $confirmation_url; |
| 73 |
|
| 74 |
/** |
| 75 |
* The URL to redirect to after verification. |
| 76 |
* |
| 77 |
* @since 1.5.0 |
| 78 |
* |
| 79 |
* @var string|WP_Error |
| 80 |
*/ |
| 81 |
protected $redirect_url; |
| 82 |
|
| 83 |
/** |
| 84 |
* Instantiate the email class, defining its key values. |
| 85 |
* |
| 86 |
* @since 1.5.0 |
| 87 |
* |
| 88 |
* @param mixed[] $objects Array containing a user object, or nothing if this is a preview. |
| 89 |
*/ |
| 90 |
public function __construct( $objects = array() ) { |
| 91 |
parent::__construct( $objects ); |
| 92 |
|
| 93 |
add_filter( 'charitable_settings_fields_emails_email_' . self::ID, array( $this, 'add_email_settings' ), 1 ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Filter the default email name. |
| 97 |
* |
| 98 |
* @since 1.5.0 |
| 99 |
* |
| 100 |
* @param string $name The email name. |
| 101 |
*/ |
| 102 |
$this->name = apply_filters( 'charitable_email_email_verification_name', __( 'User: Email Verification', 'charitable' ) ); |
| 103 |
|
| 104 |
$this->user = array_key_exists( 'user', $objects ) ? $objects['user'] : false; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the current email's ID. |
| 109 |
* |
| 110 |
* @since 1.5.0 |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public static function get_email_id() { |
| 115 |
return self::ID; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return the recipient for the email. |
| 120 |
* |
| 121 |
* @since 1.5.0 |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
public function get_recipient() { |
| 126 |
return $this->has_valid_user() ? $this->user->user_email : ''; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Add extra email settings. |
| 131 |
* |
| 132 |
* @since 1.5.7 |
| 133 |
* |
| 134 |
* @param array $settings Email settings. |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function add_email_settings( $settings ) { |
| 138 |
return array_merge( |
| 139 |
$settings, |
| 140 |
array( |
| 141 |
'send_after_registration' => array( |
| 142 |
'type' => 'radio', |
| 143 |
'title' => __( 'Send after Registration', 'charitable' ), |
| 144 |
'help' => __( 'Whether to send an email verification message immediately after registration. If this is not set, users are only prompted to verify their email when they try to access their donation history.', 'charitable' ), |
| 145 |
'priority' => 22, |
| 146 |
'class' => 'wide', |
| 147 |
'default' => 1, |
| 148 |
'options' => array( |
| 149 |
'1' => __( 'Yes', 'charitable' ), |
| 150 |
'0' => __( 'No', 'charitable' ), |
| 151 |
), |
| 152 |
), |
| 153 |
) |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return the custom email fields for this email. |
| 159 |
* |
| 160 |
* @since 1.5.0 |
| 161 |
* |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function email_fields() { |
| 165 |
$fields = array( |
| 166 |
'confirmation_url' => array( |
| 167 |
'description' => __( 'The link the user needs to verify their email address.', 'charitable' ), |
| 168 |
'preview' => add_query_arg( |
| 169 |
array( |
| 170 |
'key' => '123123123', |
| 171 |
'login' => 'adam123', |
| 172 |
), |
| 173 |
charitable_get_permalink( 'email_verification' ) |
| 174 |
), |
| 175 |
), |
| 176 |
); |
| 177 |
|
| 178 |
if ( $this->has_valid_user() ) { |
| 179 |
$fields = array_merge_recursive( |
| 180 |
$fields, |
| 181 |
array( |
| 182 |
'confirmation_url' => array( 'callback' => array( $this, 'get_confirmation_url' ) ), |
| 183 |
) |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
return $fields; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Return the reset link. |
| 192 |
* |
| 193 |
* @since 1.5.0 |
| 194 |
* |
| 195 |
* @return string|WP_Error If the reset key could not be generated, an error is returned. |
| 196 |
*/ |
| 197 |
public function get_confirmation_url() { |
| 198 |
if ( ! isset( $this->confirmation_url ) ) { |
| 199 |
$key = get_password_reset_key( $this->user ); |
| 200 |
|
| 201 |
if ( is_wp_error( $key ) ) { |
| 202 |
return $key; |
| 203 |
} |
| 204 |
|
| 205 |
$args = array( |
| 206 |
'key' => $key, |
| 207 |
'login' => rawurlencode( $this->user->user_login ), |
| 208 |
'charitable' => true, |
| 209 |
); |
| 210 |
|
| 211 |
$redirect_url = $this->get_redirect_url(); |
| 212 |
|
| 213 |
if ( $redirect_url ) { |
| 214 |
$args['redirect_to'] = $redirect_url; |
| 215 |
} |
| 216 |
|
| 217 |
$this->confirmation_url = esc_url_raw( add_query_arg( $args, charitable_get_permalink( 'email_verification' ) ) ); |
| 218 |
} |
| 219 |
|
| 220 |
return $this->confirmation_url; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Return the redirect URL. |
| 225 |
* |
| 226 |
* @since 1.5.0 |
| 227 |
* |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
public function get_redirect_url() { |
| 231 |
if ( ! isset( $this->redirect_url ) ) { |
| 232 |
$profile = charitable_get_option( 'profile_page', false ); |
| 233 |
$this->redirect_url = $profile ? get_permalink( $profile ) : false; |
| 234 |
} |
| 235 |
|
| 236 |
return $this->redirect_url; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Set the redirect URL. |
| 241 |
* |
| 242 |
* @since 1.5.0 |
| 243 |
* |
| 244 |
* @param string $redirect_url The URL. |
| 245 |
* @return void |
| 246 |
*/ |
| 247 |
public function set_redirect_url( $redirect_url ) { |
| 248 |
$this->redirect_url = $redirect_url; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Send the email, then record the time that it was sent. |
| 253 |
* |
| 254 |
* @since 1.6.32 |
| 255 |
* |
| 256 |
* @return boolean |
| 257 |
*/ |
| 258 |
public function send() { |
| 259 |
$sent = parent::send(); |
| 260 |
|
| 261 |
if ( $sent ) { |
| 262 |
update_user_meta( $this->user->ID, '_charitable_email_verification_email_send_time', time() ); |
| 263 |
} |
| 264 |
|
| 265 |
return $sent; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Checks whether the email was sent recently. |
| 270 |
* |
| 271 |
* @since 1.6.32 |
| 272 |
* |
| 273 |
* @return boolean |
| 274 |
*/ |
| 275 |
public function sent_recently() { |
| 276 |
$send_time = get_user_meta( $this->user->ID, '_charitable_email_verification_email_send_time', true ); |
| 277 |
|
| 278 |
if ( ! $send_time ) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Change the amount of minutes to wait before resending |
| 284 |
* the email (unless it's a force-resend). |
| 285 |
* |
| 286 |
* By default, this is set to wait 30 minutes. |
| 287 |
* |
| 288 |
* @since 1.6.32 |
| 289 |
* |
| 290 |
* @param boolean $seconds The number of seconds to wait. |
| 291 |
*/ |
| 292 |
$threshold = apply_filters( 'charitable_email_verification_resend_threshold_time', 1800 ); |
| 293 |
|
| 294 |
return time() - $send_time < $threshold; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Return the default subject line for the email. |
| 299 |
* |
| 300 |
* @since 1.5.0 |
| 301 |
* |
| 302 |
* @return string |
| 303 |
*/ |
| 304 |
protected function get_default_subject() { |
| 305 |
return __( 'Verify your email for [charitable_email show=site_name]', 'charitable' ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Return the default headline for the email. |
| 310 |
* |
| 311 |
* @since 1.5.0 |
| 312 |
* |
| 313 |
* @return string |
| 314 |
*/ |
| 315 |
protected function get_default_headline() { |
| 316 |
/** |
| 317 |
* Filter the default email headline. |
| 318 |
* |
| 319 |
* @since 1.5.0 |
| 320 |
* |
| 321 |
* @param string $headline The default headline. |
| 322 |
* @param Charitable_Email_Email_Verification $email This instance of `Charitable_Email_Email_Verification`. |
| 323 |
*/ |
| 324 |
return apply_filters( 'charitable_email_email_verification_default_headline', __( 'Verify your email address', 'charitable' ), $this ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Return the default body for the email. |
| 329 |
* |
| 330 |
* @since 1.5.0 |
| 331 |
* |
| 332 |
* @return string |
| 333 |
*/ |
| 334 |
protected function get_default_body() { |
| 335 |
ob_start(); |
| 336 |
?> |
| 337 |
<p><?php esc_html_e( 'Welcome to [charitable_email show=site_name]', 'charitable' ); ?></p> |
| 338 |
<p><?php esc_html_e( 'To complete your registration, please confirm your email address by clicking the link below:', 'charitable' ); ?></p> |
| 339 |
<p><a href="[charitable_email show=confirmation_url]">[charitable_email show=confirmation_url]</a></p> |
| 340 |
<?php |
| 341 |
/** |
| 342 |
* Filter the default email body. |
| 343 |
* |
| 344 |
* @since 1.5.0 |
| 345 |
* |
| 346 |
* @param string $body The default body. |
| 347 |
* @param Charitable_Email_Email_Verification $email This instance of `Charitable_Email_Email_Verification`. |
| 348 |
*/ |
| 349 |
return apply_filters( 'charitable_email_email_verification_default_body', ob_get_clean(), $this ); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Check whether a user is set for the object instance. |
| 354 |
* |
| 355 |
* @since 1.5.0 |
| 356 |
* |
| 357 |
* @return boolean |
| 358 |
*/ |
| 359 |
protected function has_valid_user() { |
| 360 |
return isset( $this->user ) && is_a( $this->user, 'WP_User' ); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
endif; |
| 365 |
|