PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.2
1.8.13.1 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 All 212 releases
charitable / includes / forms / class-charitable-registration-form.php

class-charitable-registration-form.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.2, at includes/forms/class-charitable-registration-form.php

350 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that manages the display and processing of the registration form.
4 *
5 * @package Charitable/Classes/Charitable_Registration_Form
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.0.0
10 * @version 1.6.51
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Registration_Form' ) ) :
19
20 /**
21 * Charitable_Registration_Form
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Registration_Form extends Charitable_Form {
26
27 /**
28 * Shortcode parameters.
29 *
30 * @since 1.0.0
31 *
32 * @var array
33 */
34 protected $shortcode_args;
35
36 /**
37 * Nonce action.
38 *
39 * @since 1.0.0
40 *
41 * @var string
42 */
43 protected $nonce_action = 'charitable_user_registration';
44
45 /**
46 * Nonce name.
47 *
48 * @since 1.0.0
49 *
50 * @var string
51 */
52 protected $nonce_name = '_charitable_user_registration_nonce';
53
54 /**
55 * Action to be executed upon form submission.
56 *
57 * @since 1.0.0
58 *
59 * @var string
60 */
61 protected $form_action = 'save_registration';
62
63 /**
64 * The current donor.
65 *
66 * @since 1.0.0
67 *
68 * @var Charitable_Donor
69 */
70 protected $donor;
71
72 /**
73 * Create class object.
74 *
75 * @since 1.0.0
76 *
77 * @param array $args User-defined shortcode attributes.
78 */
79 public function __construct( $args = array() ) {
80 $this->id = uniqid();
81 $this->shortcode_args = $args;
82
83 /* For backwards-compatibility */
84 add_action( 'charitable_form_field', array( $this, 'render_field' ), 10, 6 );
85 }
86
87 /**
88 * Return the arguments passed to the shortcode.
89 *
90 * @since 1.4.0
91 *
92 * @return mixed[]
93 */
94 public function get_shortcode_args() {
95 return $this->shortcode_args;
96 }
97
98 /**
99 * Profile fields to be displayed.
100 *
101 * @since 1.0.0
102 * @version 1.8.9.1
103 *
104 * @return array
105 */
106 public function get_fields() {
107 // phpcs:disable WordPress.Security.NonceVerification.Missing
108 $fields = array(
109 'user_email' => array(
110 'label' => __( 'Email', 'charitable' ),
111 'type' => 'email',
112 'required' => true,
113 'priority' => 4,
114 'value' => isset( $_POST['user_email'] ) ? sanitize_text_field( wp_unslash( $_POST['user_email'] ) ) : '',
115 ),
116 'user_login' => array(
117 'label' => __( 'Username', 'charitable' ),
118 'type' => 'text',
119 'priority' => 6,
120 'required' => true,
121 'value' => isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '',
122 ),
123 'user_pass' => array(
124 'label' => __( 'Password', 'charitable' ),
125 'type' => 'password',
126 'priority' => 8,
127 'required' => true,
128 'value' => isset( $_POST['user_pass'] ) ? sanitize_text_field( wp_unslash( $_POST['user_pass'] ) ) : '',
129 ),
130 );
131 // phpcs:enable WordPress.Security.NonceVerification.Missing
132
133 $fields = $this->maybe_add_terms_conditions_fields( $fields );
134
135 /**
136 * Filter the user registration fields.
137 *
138 * @since 1.0.0
139 *
140 * @param array $fields The registered fields.
141 */
142 $fields = apply_filters( 'charitable_user_registration_fields', $fields );
143
144 uasort( $fields, 'charitable_priority_sort' );
145
146 return $fields;
147 }
148
149 /**
150 * Maybe add terms and conditions fields to the form.
151 *
152 * @since 1.6.2
153 * @version 1.8.9.1
154 *
155 * @param array $fields The registered form fields.
156 * @return array
157 */
158 public function maybe_add_terms_conditions_fields( $fields ) {
159 if ( charitable_is_privacy_policy_activated() ) {
160 $fields['privacy_policy_text'] = array(
161 'type' => 'content',
162 'content' => '<p class="charitable-privacy-policy-text">' . charitable_get_privacy_policy_field_text() . '</p>',
163 'priority' => 20,
164 );
165 }
166
167 if ( charitable_is_contact_consent_activated() ) {
168 // phpcs:disable WordPress.Security.NonceVerification.Missing
169 $checked = isset( $_POST['contact_consent'] ) && ! empty( sanitize_text_field( wp_unslash( $_POST['contact_consent'] ) ) );
170 // phpcs:enable WordPress.Security.NonceVerification.Missing
171 $fields['contact_consent'] = array(
172 'type' => 'checkbox',
173 'label' => charitable_get_option( 'contact_consent_label', __( 'Yes, I am happy for you to contact me via email or phone.', 'charitable' ) ),
174 'priority' => 22,
175 'required' => false,
176 'checked' => $checked,
177 );
178 }
179
180 if ( charitable_is_terms_and_conditions_activated() ) {
181 $fields['terms_text'] = array(
182 'type' => 'content',
183 'content' => '<div class="charitable-terms-text">' . charitable_get_terms_and_conditions() . '</div>',
184 'priority' => 24,
185 );
186
187 $fields['accept_terms'] = array(
188 'type' => 'checkbox',
189 'label' => charitable_get_terms_and_conditions_field_label(),
190 'priority' => 28,
191 'required' => true,
192 'data_type' => 'meta',
193 );
194 }
195
196 return $fields;
197 }
198
199 /**
200 * Return the hidden fields for this form.
201 *
202 * @since 1.5.0
203 *
204 * @return array
205 */
206 public function get_hidden_fields() {
207 $fields = parent::get_hidden_fields();
208 $redirect = $this->get_redirect_url();
209
210 if ( false !== $redirect ) {
211 $fields['redirect_to'] = $redirect;
212 }
213
214 return $fields;
215 }
216
217 /**
218 * Update registration after form submission.
219 *
220 * @since 1.0.0
221 * @version 1.8.1.15
222 *
223 * @return void
224 */
225 public static function save_registration() {
226 $form = new Charitable_Registration_Form();
227
228 if ( ! $form->validate_nonce() || ! $form->validate_honeypot() ) {
229 charitable_get_notices()->add_error( __( 'Unfortunately, we were unable to verify your form submission. Please reload the page and try again.', 'charitable' ) );
230 return;
231 }
232
233 $fields = $form->get_fields();
234 $valid = $form->check_required_fields( $fields );
235
236 if ( ! $valid ) {
237 return;
238 }
239
240 $submitted = apply_filters( 'charitable_registration_values', $form->get_submitted_values(), $fields, $form );
241
242 if ( ! isset( $submitted['user_email'] ) || ! is_email( $submitted['user_email'] ) ) {
243 charitable_get_notices()->add_error(
244 sprintf(
245 /* translators: %s: submitted email address */
246 __( '%s is not a valid email address.', 'charitable' ),
247 $submitted['user_email']
248 )
249 );
250
251 return false;
252 }
253
254 // Remove any ID fields that may have been submitted.
255 if ( array_key_exists( 'ID', $submitted ) ) {
256 unset( $submitted['ID'] );
257 }
258
259 $user = new Charitable_User();
260 $user_id = $user->update_profile( $submitted, array_keys( $fields ) );
261
262 /**
263 * If the user was successfully created, redirect to the login redirect URL.
264 * If there was a problem, this simply falls through and keeps the user on the
265 * registration page.
266 */
267 if ( $user_id ) {
268
269 /* Maybe send an email verification email. */
270 if ( charitable_get_option( array( 'emails_email_verification', 'send_after_registration' ), 1 ) ) {
271
272 /* If the confirmation link is generated correctly and the email is sent, set a notice. */
273 if ( Charitable_User_Management::get_instance()->send_verification_email( $user ) ) {
274 $message = sprintf(
275 /* translators: %s: email verification link */
276 __( 'We have sent you an email to confirm your email address. Haven\'t received the email? <a href="%s">Click here to send it again.</a>', 'charitable' ),
277 esc_url_raw( charitable_get_email_verification_link( $user, false, true ) )
278 );
279
280 charitable_get_notices()->add_success( $message );
281 charitable_get_session()->add_notices();
282 }
283 }
284
285 wp_safe_redirect( charitable_get_login_redirect_url() );
286 exit();
287 }
288 }
289
290 /**
291 * Return the link to the login page, or false if we are not going to display it.
292 *
293 * @since 1.4.2
294 * @version 1.8.9.1
295 *
296 * @return false|string
297 */
298 public function get_login_link() {
299 if ( false == $this->shortcode_args['login_link_text'] || 'false' == $this->shortcode_args['login_link_text'] ) {
300 return false;
301 }
302
303 $login_link = charitable_get_permalink( 'login_page' );
304
305 if ( charitable_get_permalink( 'registration_page' ) === $login_link ) {
306 return false;
307 }
308
309 // phpcs:disable WordPress.Security.NonceVerification.Recommended
310 if ( isset( $_GET['redirect_to'] ) ) {
311 $redirect_to = sanitize_text_field( wp_unslash( $_GET['redirect_to'] ) );
312 $login_link = add_query_arg( 'redirect_to', $redirect_to, $login_link );
313 }
314 // phpcs:enable WordPress.Security.NonceVerification.Recommended
315
316 return sprintf(
317 '<a href="%1$s">%2$s</a>',
318 esc_url( $login_link ),
319 $this->shortcode_args['login_link_text']
320 );
321 }
322
323 /**
324 * Get the redirect URL.
325 *
326 * @since 1.5.0
327 * @version 1.8.9.1
328 *
329 * @return string|false
330 */
331 protected function get_redirect_url() {
332 $redirect = false;
333
334 // phpcs:disable WordPress.Security.NonceVerification.Recommended
335 if ( isset( $_GET['redirect_to'] ) ) {
336 $redirect_to = sanitize_text_field( wp_unslash( $_GET['redirect_to'] ) );
337 if ( strlen( $redirect_to ) ) {
338 $redirect = $redirect_to;
339 }
340 } elseif ( isset( $this->shortcode_args['redirect'] ) && strlen( $this->shortcode_args['redirect'] ) ) {
341 $redirect = $this->shortcode_args['redirect'];
342 }
343 // phpcs:enable WordPress.Security.NonceVerification.Recommended
344
345 return $redirect;
346 }
347 }
348
349 endif;
350