PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / endpoints / class-charitable-email-verification-endpoint.php

class-charitable-email-verification-endpoint.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/endpoints/class-charitable-email-verification-endpoint.php

262 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email verification endpoint.
4 *
5 * @package Charitable/Classes/Charitable_Email_Verification_Endpoint
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.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 * @version 1.8.9.1
117 *
118 * @return false|string
119 */
120 public function get_redirect() {
121 $result = $this->get_verification_check_result();
122
123 /* After successful verification, set a notice and redirect. */
124 if ( is_a( $result, 'WP_User' ) ) {
125 charitable_get_notices()->add_success( __( 'Your email address has been verified.', 'charitable' ) );
126 charitable_get_session()->add_notices();
127
128 // phpcs:disable WordPress.Security.NonceVerification.Recommended
129 if ( array_key_exists( 'redirect_to', $_GET ) ) {
130 return sanitize_text_field( wp_unslash( $_GET['redirect_to'] ) );
131 }
132 // phpcs:enable WordPress.Security.NonceVerification.Recommended
133 }
134
135 return false;
136 }
137
138 /**
139 * Prepare the template for the endpoint.
140 *
141 * @since 1.6.55
142 *
143 * @return void
144 */
145 public function setup_template() {
146 new Charitable_Ghost_Page(
147 'email-verification-page',
148 array(
149 'title' => __( 'Email Verification', 'charitable' ),
150 'content' => '<!-- Silence is golden -->',
151 )
152 );
153 }
154
155 /**
156 * Return the template to display for this endpoint.
157 *
158 * @since 1.5.0
159 *
160 * @param string $template The default template.
161 * @return array
162 */
163 public function get_template( $template ) {
164 $profile = charitable_get_option( 'profile_page', false );
165 $templates = array( 'email-verification-page.php', 'page.php', 'singular.php', 'index.php' );
166
167 if ( $profile ) {
168 return charitable_splice_template( get_page_template_slug( $profile ), $templates );
169 }
170
171 return $templates;
172 }
173
174 /**
175 * Get the content to display for the endpoint.
176 *
177 * @since 1.5.0
178 *
179 * @param string $content The page content.
180 * @return string
181 */
182 public function get_content( $content ) {
183 $template = $this->is_verified() ? 'account/email-verified.php' : 'account/email-not-verified.php';
184
185 ob_start();
186
187 charitable_template(
188 $template,
189 array(
190 'result' => $this->get_verification_check_result(),
191 )
192 );
193
194 return ob_get_clean();
195 }
196
197 /**
198 * Checks whether the email address is verified.
199 *
200 * @since 1.5.0
201 *
202 * @return boolean
203 */
204 protected function is_verified() {
205 return is_a( $this->get_verification_check_result(), 'WP_User' );
206 }
207
208 /**
209 * Check whether a key and login were provided and are valid.
210 *
211 * @since 1.5.0
212 * @version 1.8.9.1
213 *
214 * @return false|WP_User|WP_Error False if the key or login are missing.
215 * WP_User if they are and the combo is valid.
216 * WP_Error in case of failure.
217 */
218 protected function get_verification_check_result() {
219 if ( ! isset( $this->verification_result ) ) {
220 // phpcs:disable WordPress.Security.NonceVerification.Recommended
221 if ( ! isset( $_GET['key'] ) || ! isset( $_GET['login'] ) ) {
222 $this->verification_result = false;
223 } else {
224 $key = sanitize_text_field( wp_unslash( $_GET['key'] ) );
225 $login = sanitize_text_field( wp_unslash( $_GET['login'] ) );
226 $this->verification_result = check_password_reset_key( $key, $login );
227 }
228 // phpcs:enable WordPress.Security.NonceVerification.Recommended
229
230 /* The user is logged in but the verification was for a different user. */
231 if ( is_user_logged_in() && get_current_user_id() !== $this->verification_result->ID ) {
232 $this->verification_result = false;
233 }
234
235 /* If everything checks out, mark the user as verified. */
236 if ( is_a( $this->verification_result, 'WP_User' ) ) {
237 // phpcs:disable WordPress.Security.NonceVerification.Recommended
238 $login = sanitize_text_field( wp_unslash( $_GET['login'] ) );
239 // phpcs:enable WordPress.Security.NonceVerification.Recommended
240 charitable_get_user( get_user_by( 'login', $login ) )->mark_as_verified( true );
241 }
242 }
243
244 return $this->verification_result;
245 }
246
247 /**
248 * Return the base page for the endpoint.
249 *
250 * @since 1.5.0
251 *
252 * @return string
253 */
254 protected function get_base_page() {
255 $profile = charitable_get_permalink( 'profile' );
256
257 return empty( $profile ) ? home_url() : $profile;
258 }
259 }
260
261 endif;
262