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