| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email verification endpoint. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Email_Verification_Endpoint |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, 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.55 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Email_Verification_Endpoint' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Email_Verification_Endpoint |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Email_Verification_Endpoint extends Charitable_Endpoint { |
| 25 |
|
| 26 |
/** Endpoint ID. */ |
| 27 |
const ID = 'email_verification'; |
| 28 |
|
| 29 |
/** Set priority */ |
| 30 |
const PRIORITY = 9; |
| 31 |
|
| 32 |
/** |
| 33 |
* The verification result. |
| 34 |
* |
| 35 |
* @since 1.5.0 |
| 36 |
* |
| 37 |
* @var false|WP_User|WP_Error |
| 38 |
*/ |
| 39 |
protected $verification_result; |
| 40 |
|
| 41 |
/** |
| 42 |
* Object instantiation. |
| 43 |
* |
| 44 |
* @since 1.5.4 |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
$this->cacheable = false; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Return the endpoint ID. |
| 52 |
* |
| 53 |
* @since 1.5.0 |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public static function get_endpoint_id() { |
| 58 |
return self::ID; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add rewrite rules for the endpoint. |
| 63 |
* |
| 64 |
* @since 1.5.0 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function setup_rewrite_rules() { |
| 69 |
add_rewrite_endpoint( 'email_verification', EP_PERMALINK | EP_ROOT ); |
| 70 |
add_rewrite_rule( '(.?.+?)(?:/([0-9]+))?/email-verification/?$', 'index.php?pagename=$matches[1]&page=$matches[2]&email_verification=1', 'top' ); |
| 71 |
add_rewrite_rule( 'email-verification/?$', 'index.php?&email_verification=1', 'top' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Return the endpoint URL. |
| 76 |
* |
| 77 |
* @since 1.5.0 |
| 78 |
* |
| 79 |
* @global WP_Rewrite $wp_rewrite |
| 80 |
* @param array $args Mixed arguments. |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function get_page_url( $args = array() ) { |
| 84 |
global $wp_rewrite; |
| 85 |
|
| 86 |
$base = $this->get_base_page(); |
| 87 |
|
| 88 |
/* Get the base URL. */ |
| 89 |
if ( $wp_rewrite->using_permalinks() ) { |
| 90 |
return trailingslashit( $base ) . 'email-verification/'; |
| 91 |
} |
| 92 |
|
| 93 |
return esc_url_raw( add_query_arg( array( 'email_verification' => 1 ), $base ) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Return whether we are currently viewing the endpoint. |
| 98 |
* |
| 99 |
* @since 1.5.0 |
| 100 |
* |
| 101 |
* @global WP_Query $wp_query |
| 102 |
* @param array $args Mixed set of arguments. |
| 103 |
* @return boolean |
| 104 |
*/ |
| 105 |
public function is_page( $args = array() ) { |
| 106 |
global $wp_query; |
| 107 |
|
| 108 |
return $wp_query->is_main_query() |
| 109 |
&& array_key_exists( 'email_verification', $wp_query->query_vars ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* If the page should redirect, return the URL it should redirect to. |
| 114 |
* |
| 115 |
* @since 1.6.26 |
| 116 |
* |
| 117 |
* @return false|string |
| 118 |
*/ |
| 119 |
public function get_redirect() { |
| 120 |
$result = $this->get_verification_check_result(); |
| 121 |
|
| 122 |
/* After successful verification, set a notice and redirect. */ |
| 123 |
if ( is_a( $result, 'WP_User' ) ) { |
| 124 |
charitable_get_notices()->add_success( __( 'Your email address has been verified.', 'charitable' ) ); |
| 125 |
charitable_get_session()->add_notices(); |
| 126 |
|
| 127 |
if ( array_key_exists( 'redirect_to', $_GET ) ) { |
| 128 |
return $_GET['redirect_to']; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Prepare the template for the endpoint. |
| 137 |
* |
| 138 |
* @since 1.6.55 |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function setup_template() { |
| 143 |
new Charitable_Ghost_Page( |
| 144 |
'email-verification-page', |
| 145 |
array( |
| 146 |
'title' => __( 'Email Verification', 'charitable' ), |
| 147 |
'content' => '<!-- Silence is golden -->', |
| 148 |
) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Return the template to display for this endpoint. |
| 154 |
* |
| 155 |
* @since 1.5.0 |
| 156 |
* |
| 157 |
* @param string $template The default template. |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function get_template( $template ) { |
| 161 |
$profile = charitable_get_option( 'profile_page', false ); |
| 162 |
$templates = array( 'email-verification-page.php', 'page.php', 'singular.php', 'index.php' ); |
| 163 |
|
| 164 |
if ( $profile ) { |
| 165 |
return charitable_splice_template( get_page_template_slug( $profile ), $templates ); |
| 166 |
} |
| 167 |
|
| 168 |
return $templates; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the content to display for the endpoint. |
| 173 |
* |
| 174 |
* @since 1.5.0 |
| 175 |
* |
| 176 |
* @param string $content The page content. |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function get_content( $content ) { |
| 180 |
$template = $this->is_verified() ? 'account/email-verified.php' : 'account/email-not-verified.php'; |
| 181 |
|
| 182 |
ob_start(); |
| 183 |
|
| 184 |
charitable_template( |
| 185 |
$template, |
| 186 |
array( |
| 187 |
'result' => $this->get_verification_check_result(), |
| 188 |
) |
| 189 |
); |
| 190 |
|
| 191 |
return ob_get_clean(); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Checks whether the email address is verified. |
| 196 |
* |
| 197 |
* @since 1.5.0 |
| 198 |
* |
| 199 |
* @return boolean |
| 200 |
*/ |
| 201 |
protected function is_verified() { |
| 202 |
return is_a( $this->get_verification_check_result(), 'WP_User' ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Check whether a key and login were provided and are valid. |
| 207 |
* |
| 208 |
* @since 1.5.0 |
| 209 |
* |
| 210 |
* @return false|WP_User|WP_Error False if the key or login are missing. |
| 211 |
* WP_User if they are and the combo is valid. |
| 212 |
* WP_Error in case of failure. |
| 213 |
*/ |
| 214 |
protected function get_verification_check_result() { |
| 215 |
if ( ! isset( $this->verification_result ) ) { |
| 216 |
if ( ! isset( $_GET['key'] ) || ! isset( $_GET['login'] ) ) { |
| 217 |
$this->verification_result = false; |
| 218 |
} else { |
| 219 |
$this->verification_result = check_password_reset_key( wp_unslash( $_GET['key'] ), wp_unslash( $_GET['login'] ) ); |
| 220 |
} |
| 221 |
|
| 222 |
/* The user is logged in but the verification was for a different user. */ |
| 223 |
if ( is_user_logged_in() && get_current_user_id() !== $this->verification_result->ID ) { |
| 224 |
$this->verification_result = false; |
| 225 |
} |
| 226 |
|
| 227 |
/* If everything checks out, mark the user as verified. */ |
| 228 |
if ( is_a( $this->verification_result, 'WP_User' ) ) { |
| 229 |
charitable_get_user( get_user_by( 'login', $_GET['login'] ) )->mark_as_verified( true ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $this->verification_result; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Return the base page for the endpoint. |
| 238 |
* |
| 239 |
* @since 1.5.0 |
| 240 |
* |
| 241 |
* @return string |
| 242 |
*/ |
| 243 |
protected function get_base_page() { |
| 244 |
$profile = charitable_get_permalink( 'profile' ); |
| 245 |
|
| 246 |
return empty( $profile ) ? home_url() : $profile; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
endif; |
| 251 |
|