admin
8 years ago
api
8 years ago
deprecated
8 years ago
donors
8 years ago
emails
8 years ago
forms
8 years ago
gateways
8 years ago
libraries
8 years ago
payments
8 years ago
actions.php
8 years ago
ajax-functions.php
8 years ago
class-give-async-process.php
8 years ago
class-give-background-updater.php
8 years ago
class-give-cache.php
8 years ago
class-give-cli-commands.php
8 years ago
class-give-cron.php
8 years ago
class-give-db-donor-meta.php
8 years ago
class-give-db-donors.php
8 years ago
class-give-db-form-meta.php
8 years ago
class-give-db-logs-meta.php
8 years ago
class-give-db-logs.php
8 years ago
class-give-db-meta.php
8 years ago
class-give-db-payment-meta.php
8 years ago
class-give-db-sequential-ordering.php
8 years ago
class-give-db.php
8 years ago
class-give-donate-form.php
8 years ago
class-give-donor.php
8 years ago
class-give-email-access.php
8 years ago
class-give-gravatars.php
8 years ago
class-give-html-elements.php
8 years ago
class-give-license-handler.php
8 years ago
class-give-logging.php
8 years ago
class-give-roles.php
8 years ago
class-give-scripts.php
8 years ago
class-give-session.php
8 years ago
class-give-stats.php
8 years ago
class-give-template-loader.php
8 years ago
class-give-tooltips.php
8 years ago
class-give-translation.php
8 years ago
class-notices.php
8 years ago
country-functions.php
8 years ago
currency-functions.php
8 years ago
error-tracking.php
8 years ago
filters.php
8 years ago
formatting.php
8 years ago
import-functions.php
8 years ago
install.php
8 years ago
login-register.php
8 years ago
misc-functions.php
8 years ago
plugin-compatibility.php
8 years ago
post-types.php
8 years ago
price-functions.php
8 years ago
process-donation.php
8 years ago
shortcodes.php
8 years ago
template-functions.php
8 years ago
user-functions.php
8 years ago
process-donation.php
1330 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Process Donation |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Functions |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Process Donation Form |
| 19 | * |
| 20 | * Handles the donation form process. |
| 21 | * |
| 22 | * @access private |
| 23 | * @since 1.0 |
| 24 | * |
| 25 | * @return mixed |
| 26 | */ |
| 27 | function give_process_donation_form() { |
| 28 | |
| 29 | $is_ajax = isset( $_POST['give_ajax'] ); |
| 30 | |
| 31 | // Verify donation form nonce. |
| 32 | if( ! give_verify_donation_form_nonce() ) { |
| 33 | if( $is_ajax ) { |
| 34 | /** |
| 35 | * Fires when AJAX sends back errors from the donation form. |
| 36 | * |
| 37 | * @since 1.0 |
| 38 | */ |
| 39 | do_action( 'give_ajax_donation_errors' ); |
| 40 | |
| 41 | give_die(); |
| 42 | } else{ |
| 43 | give_send_back_to_checkout(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Fires before processing the donation form. |
| 49 | * |
| 50 | * @since 1.0 |
| 51 | */ |
| 52 | do_action( 'give_pre_process_donation' ); |
| 53 | |
| 54 | // Validate the form $_POST data. |
| 55 | $valid_data = give_donation_form_validate_fields(); |
| 56 | |
| 57 | /** |
| 58 | * Fires after validating donation form fields. |
| 59 | * |
| 60 | * Allow you to hook to donation form errors. |
| 61 | * |
| 62 | * @since 1.0 |
| 63 | * |
| 64 | * @param bool|array $valid_data Validate fields. |
| 65 | * @param array $deprecated Deprecated Since 2.0.2. Use $_POST instead. |
| 66 | */ |
| 67 | do_action( 'give_checkout_error_checks', $valid_data, $deprecated = $_POST ); |
| 68 | |
| 69 | // Process the login form. |
| 70 | if ( isset( $_POST['give_login_submit'] ) ) { |
| 71 | give_process_form_login(); |
| 72 | } |
| 73 | |
| 74 | // Validate the user. |
| 75 | $user = give_get_donation_form_user( $valid_data ); |
| 76 | |
| 77 | if ( false === $valid_data || give_get_errors() || ! $user ) { |
| 78 | if ( $is_ajax ) { |
| 79 | /** |
| 80 | * Fires when AJAX sends back errors from the donation form. |
| 81 | * |
| 82 | * @since 1.0 |
| 83 | */ |
| 84 | do_action( 'give_ajax_donation_errors' ); |
| 85 | give_die(); |
| 86 | } else { |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // If AJAX send back success to proceed with form submission. |
| 92 | if ( $is_ajax ) { |
| 93 | echo 'success'; |
| 94 | give_die(); |
| 95 | } |
| 96 | |
| 97 | // After AJAX: Setup session if not using php_sessions. |
| 98 | if ( ! Give()->session->use_php_sessions() ) { |
| 99 | // Double-check that set_cookie is publicly accessible. |
| 100 | // we're using a slightly modified class-wp-sessions.php. |
| 101 | $session_reflection = new ReflectionMethod( 'WP_Session', 'set_cookie' ); |
| 102 | if ( $session_reflection->isPublic() ) { |
| 103 | // Manually set the cookie. |
| 104 | Give()->session->init()->set_cookie(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Setup user information. |
| 109 | $user_info = array( |
| 110 | 'id' => $user['user_id'], |
| 111 | 'email' => $user['user_email'], |
| 112 | 'first_name' => $user['user_first'], |
| 113 | 'last_name' => $user['user_last'], |
| 114 | 'address' => $user['address'], |
| 115 | ); |
| 116 | |
| 117 | $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : ''; |
| 118 | |
| 119 | $price = isset( $_POST['give-amount'] ) ? |
| 120 | (float) apply_filters( 'give_donation_total', give_maybe_sanitize_amount( $_POST['give-amount'] ) ) : |
| 121 | '0.00'; |
| 122 | $purchase_key = strtolower( md5( $user['user_email'] . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'give', true ) ) ); |
| 123 | |
| 124 | // Setup donation information. |
| 125 | $donation_data = array( |
| 126 | 'price' => $price, |
| 127 | 'purchase_key' => $purchase_key, |
| 128 | 'user_email' => $user['user_email'], |
| 129 | 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), |
| 130 | 'user_info' => stripslashes_deep( $user_info ), |
| 131 | 'post_data' => give_clean( $_POST ), |
| 132 | 'gateway' => $valid_data['gateway'], |
| 133 | 'card_info' => $valid_data['cc_info'], |
| 134 | ); |
| 135 | |
| 136 | // Add the user data for hooks. |
| 137 | $valid_data['user'] = $user; |
| 138 | |
| 139 | /** |
| 140 | * Fires before donation form gateway. |
| 141 | * |
| 142 | * Allow you to hook to donation form before the gateway. |
| 143 | * |
| 144 | * @since 1.0 |
| 145 | * |
| 146 | * @param array $_POST Array of variables passed via the HTTP POST. |
| 147 | * @param array $user_info Array containing basic user information. |
| 148 | * @param bool|array $valid_data Validate fields. |
| 149 | */ |
| 150 | do_action( 'give_checkout_before_gateway', give_clean( $_POST ), $user_info, $valid_data ); |
| 151 | |
| 152 | // Sanity check for price. |
| 153 | if ( ! $donation_data['price'] ) { |
| 154 | // Revert to manual. |
| 155 | $donation_data['gateway'] = 'manual'; |
| 156 | $_POST['give-gateway'] = 'manual'; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Allow the donation data to be modified before it is sent to the gateway. |
| 161 | * |
| 162 | * @since 1.7 |
| 163 | */ |
| 164 | $donation_data = apply_filters( 'give_donation_data_before_gateway', $donation_data, $valid_data ); |
| 165 | |
| 166 | // Setup the data we're storing in the donation session. |
| 167 | $session_data = $donation_data; |
| 168 | |
| 169 | // Make sure credit card numbers are never stored in sessions. |
| 170 | unset( $session_data['card_info']['card_number'] ); |
| 171 | unset( $session_data['post_data']['card_number'] ); |
| 172 | |
| 173 | // Used for showing data to non logged-in users after donation, and for other plugins needing donation data. |
| 174 | give_set_purchase_session( $session_data ); |
| 175 | |
| 176 | // Send info to the gateway for payment processing. |
| 177 | give_send_to_gateway( $donation_data['gateway'], $donation_data ); |
| 178 | give_die(); |
| 179 | } |
| 180 | |
| 181 | add_action( 'give_purchase', 'give_process_donation_form' ); |
| 182 | add_action( 'wp_ajax_give_process_donation', 'give_process_donation_form' ); |
| 183 | add_action( 'wp_ajax_nopriv_give_process_donation', 'give_process_donation_form' ); |
| 184 | |
| 185 | /** |
| 186 | * Verify that when a logged in user makes a donation that the email address used doesn't belong to a different customer. |
| 187 | * |
| 188 | * @since 1.7 |
| 189 | * |
| 190 | * @param array $valid_data Validated data submitted for the donation. |
| 191 | * |
| 192 | * @return void |
| 193 | */ |
| 194 | function give_check_logged_in_user_for_existing_email( $valid_data ) { |
| 195 | |
| 196 | // Verify that the email address belongs to this customer. |
| 197 | if ( is_user_logged_in() ) { |
| 198 | |
| 199 | $submitted_email = $valid_data['logged_in_user']['user_email']; |
| 200 | $donor = new Give_Donor( get_current_user_id(), true ); |
| 201 | |
| 202 | // If this email address is not registered with this customer, see if it belongs to any other customer. |
| 203 | if ( |
| 204 | $submitted_email !== $donor->email |
| 205 | && ( is_array( $donor->emails ) && ! in_array( $submitted_email, $donor->emails ) ) |
| 206 | ) { |
| 207 | $found_donor = new Give_Donor( $submitted_email ); |
| 208 | |
| 209 | if ( $found_donor->id > 0 ) { |
| 210 | give_set_error( 'give-customer-email-exists', sprintf( __( 'You are logged in as %1$s, and are submitting a donation as %2$s, which is an existing donor. To ensure that the email address is tied to the correct donor, please submit this donation from a logged-out browser, or choose another email address.', 'give' ), $donor->email, $submitted_email ) ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | add_action( 'give_checkout_error_checks', 'give_check_logged_in_user_for_existing_email', 10, 1 ); |
| 217 | |
| 218 | /** |
| 219 | * Process the checkout login form |
| 220 | * |
| 221 | * @access private |
| 222 | * @since 1.0 |
| 223 | * @return void |
| 224 | */ |
| 225 | function give_process_form_login() { |
| 226 | $is_ajax = isset( $_POST['give_ajax'] ); |
| 227 | |
| 228 | $user_data = give_donation_form_validate_user_login(); |
| 229 | |
| 230 | if ( give_get_errors() || $user_data['user_id'] < 1 ) { |
| 231 | if ( $is_ajax ) { |
| 232 | /** |
| 233 | * Fires when AJAX sends back errors from the donation form. |
| 234 | * |
| 235 | * @since 1.0 |
| 236 | */ |
| 237 | ob_start(); |
| 238 | do_action( 'give_ajax_donation_errors' ); |
| 239 | $message = ob_get_contents(); |
| 240 | ob_end_clean(); |
| 241 | wp_send_json_error( $message ); |
| 242 | } else { |
| 243 | wp_redirect( $_SERVER['HTTP_REFERER'] ); |
| 244 | exit; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | give_log_user_in( $user_data['user_id'], $user_data['user_login'], $user_data['user_pass'] ); |
| 249 | |
| 250 | if ( $is_ajax ) { |
| 251 | $message = Give()->notices->print_frontend_notice( |
| 252 | sprintf( |
| 253 | /* translators: %s: user first name */ |
| 254 | esc_html__( 'Welcome %s! You have successfully logged into your account.', 'give' ), |
| 255 | ( ! empty( $user_data['user_first'] ) ) ? $user_data['user_first'] : $user_data['user_login'] |
| 256 | ), |
| 257 | false, |
| 258 | 'success' |
| 259 | ); |
| 260 | |
| 261 | wp_send_json_success( $message ); |
| 262 | } else { |
| 263 | wp_redirect( $_SERVER['HTTP_REFERER'] ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | add_action( 'wp_ajax_give_process_donation_login', 'give_process_form_login' ); |
| 268 | add_action( 'wp_ajax_nopriv_give_process_donation_login', 'give_process_form_login' ); |
| 269 | |
| 270 | /** |
| 271 | * Donation Form Validate Fields. |
| 272 | * |
| 273 | * @access private |
| 274 | * @since 1.0 |
| 275 | * @return bool|array |
| 276 | */ |
| 277 | function give_donation_form_validate_fields() { |
| 278 | |
| 279 | // Check if there is $_POST. |
| 280 | if ( empty( $_POST ) ) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | $form_id = ! empty( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : ''; |
| 285 | |
| 286 | // Start an array to collect valid data. |
| 287 | $valid_data = array( |
| 288 | 'gateway' => give_donation_form_validate_gateway(), // Gateway fallback (amount is validated here). |
| 289 | 'need_new_user' => false, // New user flag. |
| 290 | 'need_user_login' => false, // Login user flag. |
| 291 | 'logged_user_data' => array(), // Logged user collected data. |
| 292 | 'new_user_data' => array(), // New user collected data. |
| 293 | 'login_user_data' => array(), // Login user collected data. |
| 294 | 'guest_user_data' => array(), // Guest user collected data. |
| 295 | 'cc_info' => give_donation_form_validate_cc(),// Credit card info. |
| 296 | ); |
| 297 | |
| 298 | // Validate Honeypot First. |
| 299 | if ( ! empty( $_POST['give-honeypot'] ) ) { |
| 300 | give_set_error( 'invalid_honeypot', esc_html__( 'Honeypot field detected. Go away bad bot!', 'give' ) ); |
| 301 | } |
| 302 | |
| 303 | // Check spam detect. |
| 304 | if ( isset( $_POST['action'] ) |
| 305 | && give_is_setting_enabled( give_get_option( 'akismet_spam_protection' ) ) |
| 306 | && give_is_spam_donation() |
| 307 | ) { |
| 308 | give_set_error( 'invalid_donation', __( 'This donation has been flagged as spam. Please try again.', 'give' ) ); |
| 309 | } |
| 310 | |
| 311 | // Validate agree to terms. |
| 312 | if ( give_is_terms_enabled( $form_id ) ) { |
| 313 | give_donation_form_validate_agree_to_terms(); |
| 314 | } |
| 315 | |
| 316 | if ( is_user_logged_in() ) { |
| 317 | // Collect logged in user data. |
| 318 | $valid_data['logged_in_user'] = give_donation_form_validate_logged_in_user(); |
| 319 | } elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-register' && ! empty( $_POST['give_create_account'] ) ) { |
| 320 | // Set new user registration as required. |
| 321 | $valid_data['need_new_user'] = true; |
| 322 | // Validate new user data. |
| 323 | $valid_data['new_user_data'] = give_donation_form_validate_new_user(); |
| 324 | // Check if login validation is needed. |
| 325 | } elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-login' ) { |
| 326 | // Set user login as required. |
| 327 | $valid_data['need_user_login'] = true; |
| 328 | // Validate users login info. |
| 329 | $valid_data['login_user_data'] = give_donation_form_validate_user_login(); |
| 330 | } else { |
| 331 | // Not registering or logging in, so setup guest user data. |
| 332 | $valid_data['guest_user_data'] = give_donation_form_validate_guest_user(); |
| 333 | } |
| 334 | |
| 335 | // Return collected data. |
| 336 | return $valid_data; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Detect spam donation. |
| 341 | * |
| 342 | * @since 1.8.14 |
| 343 | * |
| 344 | * @return bool|mixed |
| 345 | */ |
| 346 | function give_is_spam_donation() { |
| 347 | $spam = false; |
| 348 | |
| 349 | $user_agent = (string) isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
| 350 | |
| 351 | if ( strlen( $user_agent ) < 2 ) { |
| 352 | $spam = true; |
| 353 | } |
| 354 | |
| 355 | // Allow developer to customized Akismet spam detect API call and it's response. |
| 356 | return apply_filters( 'give_spam', $spam ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Donation Form Validate Gateway |
| 361 | * |
| 362 | * Validate the gateway and donation amount. |
| 363 | * |
| 364 | * @access private |
| 365 | * @since 1.0 |
| 366 | * @return string |
| 367 | */ |
| 368 | function give_donation_form_validate_gateway() { |
| 369 | |
| 370 | $form_id = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0; |
| 371 | $amount = isset( $_REQUEST['give-amount'] ) ? give_maybe_sanitize_amount( $_REQUEST['give-amount'] ) : 0; |
| 372 | $gateway = give_get_default_gateway( $form_id ); |
| 373 | |
| 374 | // Check if a gateway value is present. |
| 375 | if ( ! empty( $_REQUEST['give-gateway'] ) ) { |
| 376 | |
| 377 | $gateway = sanitize_text_field( $_REQUEST['give-gateway'] ); |
| 378 | |
| 379 | // Is amount being donated in LIVE mode 0.00? If so, error: |
| 380 | if ( $amount == 0 && ! give_is_test_mode() ) { |
| 381 | |
| 382 | give_set_error( 'invalid_donation_amount', __( 'Please insert a valid donation amount.', 'give' ) ); |
| 383 | |
| 384 | } // End if(). |
| 385 | elseif ( ! give_verify_minimum_price( 'minimum' ) ) { |
| 386 | // translators: %s: minimum donation amount. |
| 387 | give_set_error( |
| 388 | 'invalid_donation_minimum', |
| 389 | sprintf( |
| 390 | /* translators: %s: minimum donation amount */ |
| 391 | __( 'This form has a minimum donation amount of %s.', 'give' ), |
| 392 | give_currency_filter( give_format_amount( give_get_form_minimum_price( $form_id ), array( 'sanitize' => false ) ) ) |
| 393 | ) |
| 394 | ); |
| 395 | |
| 396 | } // End if(). |
| 397 | elseif ( ! give_verify_minimum_price( 'maximum' ) ) { |
| 398 | // translators: %s: Maximum donation amount. |
| 399 | give_set_error( |
| 400 | 'invalid_donation_maximum', |
| 401 | sprintf( |
| 402 | /* translators: %s: Maximum donation amount */ |
| 403 | __( 'This form has a maximum donation amount of %s.', 'give' ), |
| 404 | give_currency_filter( give_format_amount( give_get_form_maximum_price( $form_id ), array( 'sanitize' => false ) ) ) |
| 405 | ) |
| 406 | ); |
| 407 | |
| 408 | } //Is this test mode zero donation? Let it through but set to manual gateway. |
| 409 | elseif ( $amount == 0 && give_is_test_mode() ) { |
| 410 | |
| 411 | $gateway = 'manual'; |
| 412 | |
| 413 | } //Check if this gateway is active. |
| 414 | elseif ( ! give_is_gateway_active( $gateway ) ) { |
| 415 | |
| 416 | give_set_error( 'invalid_gateway', __( 'The selected payment gateway is not enabled.', 'give' ) ); |
| 417 | |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return $gateway; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Donation Form Validate Minimum or Maximum Donation Amount |
| 427 | * |
| 428 | * @access private |
| 429 | * @since 1.3.6 |
| 430 | * @since 2.1 Added support for give maximum amount. |
| 431 | * |
| 432 | * @param string $amount_range Which amount needs to verify? minimum or maximum. |
| 433 | * |
| 434 | * @return bool |
| 435 | */ |
| 436 | function give_verify_minimum_price( $amount_range = 'minimum' ) { |
| 437 | |
| 438 | $amount = give_maybe_sanitize_amount( $_REQUEST['give-amount'] ); |
| 439 | $form_id = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0; |
| 440 | $price_id = isset( $_REQUEST['give-price-id'] ) ? $_REQUEST['give-price-id'] : null; |
| 441 | $variable_prices = give_has_variable_prices( $form_id ); |
| 442 | |
| 443 | if ( $variable_prices && in_array( $price_id, give_get_variable_price_ids( $form_id ) ) ) { |
| 444 | |
| 445 | $price_level_amount = give_get_price_option_amount( $form_id, $price_id ); |
| 446 | |
| 447 | if ( $price_level_amount == $amount ) { |
| 448 | return true; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | switch ( $amount_range ) { |
| 453 | case 'minimum' : |
| 454 | if ( give_get_form_minimum_price( $form_id ) > $amount ) { |
| 455 | return false; |
| 456 | } |
| 457 | break; |
| 458 | case 'maximum' : |
| 459 | if ( give_get_form_maximum_price( $form_id ) < $amount ) { |
| 460 | return false; |
| 461 | } |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Donation form validate agree to "Terms and Conditions". |
| 470 | * |
| 471 | * @access private |
| 472 | * @since 1.0 |
| 473 | * @return void |
| 474 | */ |
| 475 | function give_donation_form_validate_agree_to_terms() { |
| 476 | // Validate agree to terms. |
| 477 | if ( ! isset( $_POST['give_agree_to_terms'] ) || $_POST['give_agree_to_terms'] != 1 ) { |
| 478 | // User did not agree. |
| 479 | give_set_error( 'agree_to_terms', apply_filters( 'give_agree_to_terms_text', __( 'You must agree to the terms and conditions.', 'give' ) ) ); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Donation Form Required Fields. |
| 485 | * |
| 486 | * @access private |
| 487 | * @since 1.0 |
| 488 | * |
| 489 | * @param $form_id |
| 490 | * |
| 491 | * @return array |
| 492 | */ |
| 493 | function give_get_required_fields( $form_id ) { |
| 494 | |
| 495 | $payment_mode = give_get_chosen_gateway( $form_id ); |
| 496 | |
| 497 | $required_fields = array( |
| 498 | 'give_email' => array( |
| 499 | 'error_id' => 'invalid_email', |
| 500 | 'error_message' => __( 'Please enter a valid email address.', 'give' ), |
| 501 | ), |
| 502 | 'give_first' => array( |
| 503 | 'error_id' => 'invalid_first_name', |
| 504 | 'error_message' => __( 'Please enter your first name.', 'give' ), |
| 505 | ), |
| 506 | ); |
| 507 | |
| 508 | $require_address = give_require_billing_address( $payment_mode ); |
| 509 | |
| 510 | if ( $require_address ) { |
| 511 | $required_fields['card_address'] = array( |
| 512 | 'error_id' => 'invalid_card_address', |
| 513 | 'error_message' => __( 'Please enter your primary billing address.', 'give' ), |
| 514 | ); |
| 515 | $required_fields['card_zip'] = array( |
| 516 | 'error_id' => 'invalid_zip_code', |
| 517 | 'error_message' => __( 'Please enter your zip / postal code.', 'give' ), |
| 518 | ); |
| 519 | $required_fields['card_city'] = array( |
| 520 | 'error_id' => 'invalid_city', |
| 521 | 'error_message' => __( 'Please enter your billing city.', 'give' ), |
| 522 | ); |
| 523 | $required_fields['billing_country'] = array( |
| 524 | 'error_id' => 'invalid_country', |
| 525 | 'error_message' => __( 'Please select your billing country.', 'give' ), |
| 526 | ); |
| 527 | |
| 528 | |
| 529 | $required_fields['card_state'] = array( |
| 530 | 'error_id' => 'invalid_state', |
| 531 | 'error_message' => __( 'Please enter billing state / province / County.', 'give' ), |
| 532 | ); |
| 533 | |
| 534 | // Check if billing country already exists. |
| 535 | if ( ! empty( $_POST['billing_country'] ) ) { |
| 536 | // Get the value from $_POST. |
| 537 | $country = sanitize_text_field( $_POST['billing_country'] ); |
| 538 | |
| 539 | // Get the country list that does not required any states init. |
| 540 | $states_country = give_states_not_required_country_list(); |
| 541 | |
| 542 | // Check if states is empty or not. |
| 543 | if ( array_key_exists( $country, $states_country ) ) { |
| 544 | // If states is empty remove the required feilds of state in billing cart. |
| 545 | unset( $required_fields['card_state'] ); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if ( give_is_company_field_enabled( $form_id ) ) { |
| 551 | $form_option = give_get_meta( $form_id, '_give_company_field', true ); |
| 552 | $global_setting = give_get_option( 'company_field' ); |
| 553 | |
| 554 | $is_company_field_required = false; |
| 555 | |
| 556 | if ( ! empty( $form_option ) && give_is_setting_enabled( $form_option, array( 'required' ) ) ) { |
| 557 | $is_company_field_required = true; |
| 558 | |
| 559 | } elseif ( 'global' === $form_option && give_is_setting_enabled( $global_setting, array( 'required' ) ) ) { |
| 560 | $is_company_field_required = true; |
| 561 | |
| 562 | } elseif ( empty( $form_option ) && give_is_setting_enabled( $global_setting, array( 'required' ) ) ) { |
| 563 | $is_company_field_required = true; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | if( $is_company_field_required ) { |
| 568 | $required_fields['give_company_name'] = array( |
| 569 | 'error_id' => 'invalid_company', |
| 570 | 'error_message' => __( 'Please enter Company Name.', 'give' ), |
| 571 | ); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Filters the donation form required field. |
| 577 | * |
| 578 | * @since 1.7 |
| 579 | */ |
| 580 | $required_fields = apply_filters( 'give_donation_form_required_fields', $required_fields, $form_id ); |
| 581 | |
| 582 | return $required_fields; |
| 583 | |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Check if the Billing Address is required |
| 588 | * |
| 589 | * @since 1.0.1 |
| 590 | * |
| 591 | * @param string $payment_mode |
| 592 | * |
| 593 | * @return bool |
| 594 | */ |
| 595 | function give_require_billing_address( $payment_mode ) { |
| 596 | |
| 597 | $return = false; |
| 598 | |
| 599 | if ( isset( $_POST['billing_country'] ) || did_action( "give_{$payment_mode}_cc_form" ) || did_action( 'give_cc_form' ) ) { |
| 600 | $return = true; |
| 601 | } |
| 602 | |
| 603 | // Let payment gateways and other extensions determine if address fields should be required. |
| 604 | return apply_filters( 'give_require_billing_address', $return ); |
| 605 | |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Donation Form Validate Logged In User. |
| 610 | * |
| 611 | * @access private |
| 612 | * @since 1.0 |
| 613 | * @return array |
| 614 | */ |
| 615 | function give_donation_form_validate_logged_in_user() { |
| 616 | global $user_ID; |
| 617 | |
| 618 | $form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : ''; |
| 619 | |
| 620 | // Start empty array to collect valid user data. |
| 621 | $valid_user_data = array( |
| 622 | // Assume there will be errors. |
| 623 | 'user_id' => - 1, |
| 624 | ); |
| 625 | |
| 626 | // Verify there is a user_ID. |
| 627 | if ( $user_ID > 0 ) { |
| 628 | // Get the logged in user data. |
| 629 | $user_data = get_userdata( $user_ID ); |
| 630 | |
| 631 | // Validate Required Form Fields. |
| 632 | give_validate_required_form_fields( $form_id ); |
| 633 | |
| 634 | // Verify data. |
| 635 | if ( $user_data ) { |
| 636 | // Collected logged in user data. |
| 637 | $valid_user_data = array( |
| 638 | 'user_id' => $user_ID, |
| 639 | 'user_email' => isset( $_POST['give_email'] ) ? sanitize_email( $_POST['give_email'] ) : $user_data->user_email, |
| 640 | 'user_first' => isset( $_POST['give_first'] ) && ! empty( $_POST['give_first'] ) ? sanitize_text_field( $_POST['give_first'] ) : $user_data->first_name, |
| 641 | 'user_last' => isset( $_POST['give_last'] ) && ! empty( $_POST['give_last'] ) ? sanitize_text_field( $_POST['give_last'] ) : $user_data->last_name, |
| 642 | ); |
| 643 | |
| 644 | give_donation_form_validate_name_fields(); |
| 645 | |
| 646 | if ( ! is_email( $valid_user_data['user_email'] ) ) { |
| 647 | give_set_error( 'email_invalid', esc_html__( 'Invalid email.', 'give' ) ); |
| 648 | } |
| 649 | } else { |
| 650 | // Set invalid user error. |
| 651 | give_set_error( 'invalid_user', esc_html__( 'The user information is invalid.', 'give' ) ); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Return user data. |
| 656 | return $valid_user_data; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Donate Form Validate New User |
| 661 | * |
| 662 | * @access private |
| 663 | * @since 1.0 |
| 664 | * @return array |
| 665 | */ |
| 666 | function give_donation_form_validate_new_user() { |
| 667 | |
| 668 | $auto_generated_password = wp_generate_password(); |
| 669 | |
| 670 | // Default user data. |
| 671 | $default_user_data = array( |
| 672 | 'give-form-id' => '', |
| 673 | 'user_id' => - 1, // Assume there will be errors. |
| 674 | 'user_first' => '', |
| 675 | 'user_last' => '', |
| 676 | 'give_user_login' => false, |
| 677 | 'give_email' => false, |
| 678 | 'give_user_pass' => $auto_generated_password, |
| 679 | 'give_user_pass_confirm' => $auto_generated_password, |
| 680 | ); |
| 681 | |
| 682 | // Get user data. |
| 683 | $user_data = wp_parse_args( give_clean( $_POST ), $default_user_data ); |
| 684 | $registering_new_user = false; |
| 685 | $form_id = absint( $user_data['give-form-id'] ); |
| 686 | |
| 687 | give_donation_form_validate_name_fields(); |
| 688 | |
| 689 | // Start an empty array to collect valid user data. |
| 690 | $valid_user_data = array( |
| 691 | // Assume there will be errors. |
| 692 | 'user_id' => - 1, |
| 693 | |
| 694 | // Get first name. |
| 695 | 'user_first' => $user_data['give_first'], |
| 696 | |
| 697 | // Get last name. |
| 698 | 'user_last' => $user_data['give_last'], |
| 699 | |
| 700 | // Get Password. |
| 701 | 'user_pass' => $user_data['give_user_pass'], |
| 702 | ); |
| 703 | |
| 704 | // Validate Required Form Fields. |
| 705 | give_validate_required_form_fields( $form_id ); |
| 706 | |
| 707 | // Set Email as Username. |
| 708 | $valid_user_data['user_login'] = $user_data['give_email']; |
| 709 | |
| 710 | // Check if we have an email to verify. |
| 711 | if ( give_validate_user_email( $user_data['give_email'], $registering_new_user ) ) { |
| 712 | $valid_user_data['user_email'] = $user_data['give_email']; |
| 713 | } |
| 714 | |
| 715 | return $valid_user_data; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Donation Form Validate User Login |
| 720 | * |
| 721 | * @access private |
| 722 | * @since 1.0 |
| 723 | * @return array |
| 724 | */ |
| 725 | function give_donation_form_validate_user_login() { |
| 726 | |
| 727 | // Start an array to collect valid user data. |
| 728 | $valid_user_data = array( |
| 729 | // Assume there will be errors. |
| 730 | 'user_id' => - 1, |
| 731 | ); |
| 732 | |
| 733 | // Username. |
| 734 | if ( ! isset( $_POST['give_user_login'] ) || $_POST['give_user_login'] == '' ) { |
| 735 | give_set_error( 'must_log_in', __( 'You must register or login to complete your donation.', 'give' ) ); |
| 736 | |
| 737 | return $valid_user_data; |
| 738 | } |
| 739 | |
| 740 | // Get the user by login. |
| 741 | $user_data = get_user_by( 'login', strip_tags( $_POST['give_user_login'] ) ); |
| 742 | |
| 743 | // Check if user exists. |
| 744 | if ( $user_data ) { |
| 745 | // Get password. |
| 746 | $user_pass = isset( $_POST['give_user_pass'] ) ? $_POST['give_user_pass'] : false; |
| 747 | |
| 748 | // Check user_pass. |
| 749 | if ( $user_pass ) { |
| 750 | // Check if password is valid. |
| 751 | if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) { |
| 752 | // Incorrect password. |
| 753 | give_set_error( |
| 754 | 'password_incorrect', |
| 755 | sprintf( |
| 756 | '%1$s <a href="%2$s">%3$s</a>', |
| 757 | __( 'The password you entered is incorrect.', 'give' ), |
| 758 | wp_lostpassword_url( "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ), |
| 759 | __( 'Reset Password', 'give' ) |
| 760 | ) |
| 761 | ); |
| 762 | // All is correct. |
| 763 | } else { |
| 764 | |
| 765 | // Repopulate the valid user data array. |
| 766 | $valid_user_data = array( |
| 767 | 'user_id' => $user_data->ID, |
| 768 | 'user_login' => $user_data->user_login, |
| 769 | 'user_email' => $user_data->user_email, |
| 770 | 'user_first' => $user_data->first_name, |
| 771 | 'user_last' => $user_data->last_name, |
| 772 | 'user_pass' => $user_pass, |
| 773 | ); |
| 774 | } |
| 775 | } else { |
| 776 | // Empty password. |
| 777 | give_set_error( 'password_empty', __( 'Enter a password.', 'give' ) ); |
| 778 | } |
| 779 | } else { |
| 780 | // No username. |
| 781 | give_set_error( 'username_incorrect', __( 'The username you entered does not exist.', 'give' ) ); |
| 782 | }// End if(). |
| 783 | |
| 784 | return $valid_user_data; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Donation Form Validate Guest User |
| 789 | * |
| 790 | * @access private |
| 791 | * @since 1.0 |
| 792 | * @return array |
| 793 | */ |
| 794 | function give_donation_form_validate_guest_user() { |
| 795 | |
| 796 | $form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : ''; |
| 797 | |
| 798 | // Start an array to collect valid user data. |
| 799 | $valid_user_data = array( |
| 800 | // Set a default id for guests. |
| 801 | 'user_id' => 0, |
| 802 | ); |
| 803 | |
| 804 | give_donation_form_validate_name_fields(); |
| 805 | |
| 806 | // Get the guest email. |
| 807 | $guest_email = isset( $_POST['give_email'] ) ? $_POST['give_email'] : false; |
| 808 | |
| 809 | // Check email. |
| 810 | if ( $guest_email && strlen( $guest_email ) > 0 ) { |
| 811 | // Validate email. |
| 812 | if ( ! is_email( $guest_email ) ) { |
| 813 | // Invalid email. |
| 814 | give_set_error( 'email_invalid', __( 'Invalid email.', 'give' ) ); |
| 815 | } else { |
| 816 | // All is good to go. |
| 817 | $valid_user_data['user_email'] = $guest_email; |
| 818 | |
| 819 | // Get user_id from donor if exist. |
| 820 | $donor = new Give_Donor( $guest_email ); |
| 821 | if ( $donor->id && $donor->user_id ) { |
| 822 | $valid_user_data['user_id'] = $donor->user_id; |
| 823 | } |
| 824 | } |
| 825 | } else { |
| 826 | // No email. |
| 827 | give_set_error( 'email_empty', __( 'Enter an email.', 'give' ) ); |
| 828 | } |
| 829 | |
| 830 | // Validate Required Form Fields. |
| 831 | give_validate_required_form_fields( $form_id ); |
| 832 | |
| 833 | return $valid_user_data; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Register And Login New User |
| 838 | * |
| 839 | * @param array $user_data |
| 840 | * |
| 841 | * @access private |
| 842 | * @since 1.0 |
| 843 | * @return integer |
| 844 | */ |
| 845 | function give_register_and_login_new_user( $user_data = array() ) { |
| 846 | // Verify the array. |
| 847 | if ( empty( $user_data ) ) { |
| 848 | return - 1; |
| 849 | } |
| 850 | |
| 851 | if ( give_get_errors() ) { |
| 852 | return - 1; |
| 853 | } |
| 854 | |
| 855 | $user_args = apply_filters( 'give_insert_user_args', array( |
| 856 | 'user_login' => isset( $user_data['user_login'] ) ? $user_data['user_login'] : '', |
| 857 | 'user_pass' => isset( $user_data['user_pass'] ) ? $user_data['user_pass'] : '', |
| 858 | 'user_email' => isset( $user_data['user_email'] ) ? $user_data['user_email'] : '', |
| 859 | 'first_name' => isset( $user_data['user_first'] ) ? $user_data['user_first'] : '', |
| 860 | 'last_name' => isset( $user_data['user_last'] ) ? $user_data['user_last'] : '', |
| 861 | 'user_registered' => date( 'Y-m-d H:i:s' ), |
| 862 | 'role' => give_get_option( 'donor_default_user_role', 'give_donor' ), |
| 863 | ), $user_data ); |
| 864 | |
| 865 | // Insert new user. |
| 866 | $user_id = wp_insert_user( $user_args ); |
| 867 | |
| 868 | // Validate inserted user. |
| 869 | if ( is_wp_error( $user_id ) ) { |
| 870 | return - 1; |
| 871 | } |
| 872 | |
| 873 | // Allow themes and plugins to filter the user data. |
| 874 | $user_data = apply_filters( 'give_insert_user_data', $user_data, $user_args ); |
| 875 | |
| 876 | /** |
| 877 | * Fires after inserting user. |
| 878 | * |
| 879 | * @since 1.0 |
| 880 | * |
| 881 | * @param int $user_id User id. |
| 882 | * @param array $user_data Array containing user data. |
| 883 | */ |
| 884 | do_action( 'give_insert_user', $user_id, $user_data ); |
| 885 | |
| 886 | /** |
| 887 | * Filter allow user to alter if user when to login or not when user is register for the first time. |
| 888 | * |
| 889 | * @since 1.8.13 |
| 890 | * |
| 891 | * return bool True if login with registration and False if only want to register. |
| 892 | */ |
| 893 | if ( true === (bool) apply_filters( 'give_log_user_in_on_register', true ) ) { |
| 894 | // Login new user. |
| 895 | give_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] ); |
| 896 | } |
| 897 | |
| 898 | // Return user id. |
| 899 | return $user_id; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Get Donation Form User |
| 904 | * |
| 905 | * @param array $valid_data |
| 906 | * |
| 907 | * @access private |
| 908 | * @since 1.0 |
| 909 | * @return array|bool |
| 910 | */ |
| 911 | function give_get_donation_form_user( $valid_data = array() ) { |
| 912 | |
| 913 | // Initialize user. |
| 914 | $user = false; |
| 915 | $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 916 | |
| 917 | if ( $is_ajax ) { |
| 918 | // Do not create or login the user during the ajax submission (check for errors only). |
| 919 | return true; |
| 920 | } elseif ( is_user_logged_in() ) { |
| 921 | // Set the valid user as the logged in collected data. |
| 922 | $user = $valid_data['logged_in_user']; |
| 923 | } elseif ( $valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true ) { |
| 924 | // New user registration. |
| 925 | if ( $valid_data['need_new_user'] === true ) { |
| 926 | // Set user. |
| 927 | $user = $valid_data['new_user_data']; |
| 928 | // Register and login new user. |
| 929 | $user['user_id'] = give_register_and_login_new_user( $user ); |
| 930 | // User login |
| 931 | } elseif ( $valid_data['need_user_login'] === true && ! $is_ajax ) { |
| 932 | |
| 933 | /** |
| 934 | * The login form is now processed in the give_process_donation_login() function. |
| 935 | * This is still here for backwards compatibility. |
| 936 | * This also allows the old login process to still work if a user removes the checkout login submit button. |
| 937 | * |
| 938 | * This also ensures that the donor is logged in correctly if they click "Donation" instead of submitting the login form, meaning the donor is logged in during the donation process. |
| 939 | */ |
| 940 | // Set user. |
| 941 | $user = $valid_data['login_user_data']; |
| 942 | // Login user. |
| 943 | give_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] ); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // Check guest checkout. |
| 948 | if ( false === $user && false === give_logged_in_only( $_POST['give-form-id'] ) ) { |
| 949 | // Set user |
| 950 | $user = $valid_data['guest_user_data']; |
| 951 | } |
| 952 | |
| 953 | // Verify we have an user. |
| 954 | if ( false === $user || empty( $user ) ) { |
| 955 | // Return false. |
| 956 | return false; |
| 957 | } |
| 958 | |
| 959 | // Get user first name. |
| 960 | if ( ! isset( $user['user_first'] ) || strlen( trim( $user['user_first'] ) ) < 1 ) { |
| 961 | $user['user_first'] = isset( $_POST['give_first'] ) ? strip_tags( trim( $_POST['give_first'] ) ) : ''; |
| 962 | } |
| 963 | |
| 964 | // Get user last name. |
| 965 | if ( ! isset( $user['user_last'] ) || strlen( trim( $user['user_last'] ) ) < 1 ) { |
| 966 | $user['user_last'] = isset( $_POST['give_last'] ) ? strip_tags( trim( $_POST['give_last'] ) ) : ''; |
| 967 | } |
| 968 | |
| 969 | // Get the user's billing address details. |
| 970 | $user['address'] = array(); |
| 971 | $user['address']['line1'] = ! empty( $_POST['card_address'] ) ? give_clean( $_POST['card_address'] ) : false; |
| 972 | $user['address']['line2'] = ! empty( $_POST['card_address_2'] ) ? give_clean( $_POST['card_address_2'] ) : false; |
| 973 | $user['address']['city'] = ! empty( $_POST['card_city'] ) ? give_clean( $_POST['card_city'] ) : false; |
| 974 | $user['address']['state'] = ! empty( $_POST['card_state'] ) ? give_clean( $_POST['card_state'] ) : false; |
| 975 | $user['address']['zip'] = ! empty( $_POST['card_zip'] ) ? give_clean( $_POST['card_zip'] ) : false; |
| 976 | $user['address']['country'] = ! empty( $_POST['billing_country'] ) ? give_clean( $_POST['billing_country'] ) : false; |
| 977 | |
| 978 | if ( empty( $user['address']['country'] ) ) { |
| 979 | $user['address'] = false; |
| 980 | } // End if(). |
| 981 | |
| 982 | // Return valid user. |
| 983 | return $user; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Validates the credit card info. |
| 988 | * |
| 989 | * @access private |
| 990 | * @since 1.0 |
| 991 | * @return array |
| 992 | */ |
| 993 | function give_donation_form_validate_cc() { |
| 994 | |
| 995 | $card_data = give_get_donation_cc_info(); |
| 996 | |
| 997 | // Validate the card zip. |
| 998 | if ( ! empty( $card_data['card_zip'] ) ) { |
| 999 | if ( ! give_donation_form_validate_cc_zip( $card_data['card_zip'], $card_data['card_country'] ) ) { |
| 1000 | give_set_error( 'invalid_cc_zip', __( 'The zip / postal code you entered for your billing address is invalid.', 'give' ) ); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // Ensure no spaces. |
| 1005 | if ( ! empty( $card_data['card_number'] ) ) { |
| 1006 | $card_data['card_number'] = str_replace( '+', '', $card_data['card_number'] ); // no "+" signs |
| 1007 | $card_data['card_number'] = str_replace( ' ', '', $card_data['card_number'] ); // No spaces |
| 1008 | } |
| 1009 | |
| 1010 | // This should validate card numbers at some point too. |
| 1011 | return $card_data; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * Get credit card info. |
| 1016 | * |
| 1017 | * @access private |
| 1018 | * @since 1.0 |
| 1019 | * @return array |
| 1020 | */ |
| 1021 | function give_get_donation_cc_info() { |
| 1022 | |
| 1023 | $cc_info = array(); |
| 1024 | $cc_info['card_name'] = isset( $_POST['card_name'] ) ? sanitize_text_field( $_POST['card_name'] ) : ''; |
| 1025 | $cc_info['card_number'] = isset( $_POST['card_number'] ) ? sanitize_text_field( $_POST['card_number'] ) : ''; |
| 1026 | $cc_info['card_cvc'] = isset( $_POST['card_cvc'] ) ? sanitize_text_field( $_POST['card_cvc'] ) : ''; |
| 1027 | $cc_info['card_exp_month'] = isset( $_POST['card_exp_month'] ) ? sanitize_text_field( $_POST['card_exp_month'] ) : ''; |
| 1028 | $cc_info['card_exp_year'] = isset( $_POST['card_exp_year'] ) ? sanitize_text_field( $_POST['card_exp_year'] ) : ''; |
| 1029 | $cc_info['card_address'] = isset( $_POST['card_address'] ) ? sanitize_text_field( $_POST['card_address'] ) : ''; |
| 1030 | $cc_info['card_address_2'] = isset( $_POST['card_address_2'] ) ? sanitize_text_field( $_POST['card_address_2'] ) : ''; |
| 1031 | $cc_info['card_city'] = isset( $_POST['card_city'] ) ? sanitize_text_field( $_POST['card_city'] ) : ''; |
| 1032 | $cc_info['card_state'] = isset( $_POST['card_state'] ) ? sanitize_text_field( $_POST['card_state'] ) : ''; |
| 1033 | $cc_info['card_country'] = isset( $_POST['billing_country'] ) ? sanitize_text_field( $_POST['billing_country'] ) : ''; |
| 1034 | $cc_info['card_zip'] = isset( $_POST['card_zip'] ) ? sanitize_text_field( $_POST['card_zip'] ) : ''; |
| 1035 | |
| 1036 | // Return cc info. |
| 1037 | return $cc_info; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Validate zip code based on country code |
| 1042 | * |
| 1043 | * @since 1.0 |
| 1044 | * |
| 1045 | * @param int $zip |
| 1046 | * @param string $country_code |
| 1047 | * |
| 1048 | * @return bool|mixed |
| 1049 | */ |
| 1050 | function give_donation_form_validate_cc_zip( $zip = 0, $country_code = '' ) { |
| 1051 | $ret = false; |
| 1052 | |
| 1053 | if ( empty( $zip ) || empty( $country_code ) ) { |
| 1054 | return $ret; |
| 1055 | } |
| 1056 | |
| 1057 | $country_code = strtoupper( $country_code ); |
| 1058 | |
| 1059 | $zip_regex = array( |
| 1060 | 'AD' => 'AD\d{3}', |
| 1061 | 'AM' => '(37)?\d{4}', |
| 1062 | 'AR' => '^([A-Z]{1}\d{4}[A-Z]{3}|[A-Z]{1}\d{4}|\d{4})$', |
| 1063 | 'AS' => '96799', |
| 1064 | 'AT' => '\d{4}', |
| 1065 | 'AU' => '^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$', |
| 1066 | 'AX' => '22\d{3}', |
| 1067 | 'AZ' => '\d{4}', |
| 1068 | 'BA' => '\d{5}', |
| 1069 | 'BB' => '(BB\d{5})?', |
| 1070 | 'BD' => '\d{4}', |
| 1071 | 'BE' => '^[1-9]{1}[0-9]{3}$', |
| 1072 | 'BG' => '\d{4}', |
| 1073 | 'BH' => '((1[0-2]|[2-9])\d{2})?', |
| 1074 | 'BM' => '[A-Z]{2}[ ]?[A-Z0-9]{2}', |
| 1075 | 'BN' => '[A-Z]{2}[ ]?\d{4}', |
| 1076 | 'BR' => '\d{5}[\-]?\d{3}', |
| 1077 | 'BY' => '\d{6}', |
| 1078 | 'CA' => '^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$', |
| 1079 | 'CC' => '6799', |
| 1080 | 'CH' => '^[1-9][0-9][0-9][0-9]$', |
| 1081 | 'CK' => '\d{4}', |
| 1082 | 'CL' => '\d{7}', |
| 1083 | 'CN' => '\d{6}', |
| 1084 | 'CR' => '\d{4,5}|\d{3}-\d{4}', |
| 1085 | 'CS' => '\d{5}', |
| 1086 | 'CV' => '\d{4}', |
| 1087 | 'CX' => '6798', |
| 1088 | 'CY' => '\d{4}', |
| 1089 | 'CZ' => '\d{3}[ ]?\d{2}', |
| 1090 | 'DE' => '\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b', |
| 1091 | 'DK' => '^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$', |
| 1092 | 'DO' => '\d{5}', |
| 1093 | 'DZ' => '\d{5}', |
| 1094 | 'EC' => '([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?', |
| 1095 | 'EE' => '\d{5}', |
| 1096 | 'EG' => '\d{5}', |
| 1097 | 'ES' => '^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$', |
| 1098 | 'ET' => '\d{4}', |
| 1099 | 'FI' => '\d{5}', |
| 1100 | 'FK' => 'FIQQ 1ZZ', |
| 1101 | 'FM' => '(9694[1-4])([ \-]\d{4})?', |
| 1102 | 'FO' => '\d{3}', |
| 1103 | 'FR' => '^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$', |
| 1104 | 'GE' => '\d{4}', |
| 1105 | 'GF' => '9[78]3\d{2}', |
| 1106 | 'GL' => '39\d{2}', |
| 1107 | 'GN' => '\d{3}', |
| 1108 | 'GP' => '9[78][01]\d{2}', |
| 1109 | 'GR' => '\d{3}[ ]?\d{2}', |
| 1110 | 'GS' => 'SIQQ 1ZZ', |
| 1111 | 'GT' => '\d{5}', |
| 1112 | 'GU' => '969[123]\d([ \-]\d{4})?', |
| 1113 | 'GW' => '\d{4}', |
| 1114 | 'HM' => '\d{4}', |
| 1115 | 'HN' => '(?:\d{5})?', |
| 1116 | 'HR' => '\d{5}', |
| 1117 | 'HT' => '\d{4}', |
| 1118 | 'HU' => '\d{4}', |
| 1119 | 'ID' => '\d{5}', |
| 1120 | 'IE' => '((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?', |
| 1121 | 'IL' => '\d{5}', |
| 1122 | 'IN' => '^[1-9][0-9][0-9][0-9][0-9][0-9]$', // india |
| 1123 | 'IO' => 'BBND 1ZZ', |
| 1124 | 'IQ' => '\d{5}', |
| 1125 | 'IS' => '\d{3}', |
| 1126 | 'IT' => '^(V-|I-)?[0-9]{5}$', |
| 1127 | 'JO' => '\d{5}', |
| 1128 | 'JP' => '\d{3}-\d{4}', |
| 1129 | 'KE' => '\d{5}', |
| 1130 | 'KG' => '\d{6}', |
| 1131 | 'KH' => '\d{5}', |
| 1132 | 'KR' => '\d{3}[\-]\d{3}', |
| 1133 | 'KW' => '\d{5}', |
| 1134 | 'KZ' => '\d{6}', |
| 1135 | 'LA' => '\d{5}', |
| 1136 | 'LB' => '(\d{4}([ ]?\d{4})?)?', |
| 1137 | 'LI' => '(948[5-9])|(949[0-7])', |
| 1138 | 'LK' => '\d{5}', |
| 1139 | 'LR' => '\d{4}', |
| 1140 | 'LS' => '\d{3}', |
| 1141 | 'LT' => '\d{5}', |
| 1142 | 'LU' => '\d{4}', |
| 1143 | 'LV' => '\d{4}', |
| 1144 | 'MA' => '\d{5}', |
| 1145 | 'MC' => '980\d{2}', |
| 1146 | 'MD' => '\d{4}', |
| 1147 | 'ME' => '8\d{4}', |
| 1148 | 'MG' => '\d{3}', |
| 1149 | 'MH' => '969[67]\d([ \-]\d{4})?', |
| 1150 | 'MK' => '\d{4}', |
| 1151 | 'MN' => '\d{6}', |
| 1152 | 'MP' => '9695[012]([ \-]\d{4})?', |
| 1153 | 'MQ' => '9[78]2\d{2}', |
| 1154 | 'MT' => '[A-Z]{3}[ ]?\d{2,4}', |
| 1155 | 'MU' => '(\d{3}[A-Z]{2}\d{3})?', |
| 1156 | 'MV' => '\d{5}', |
| 1157 | 'MX' => '\d{5}', |
| 1158 | 'MY' => '\d{5}', |
| 1159 | 'NC' => '988\d{2}', |
| 1160 | 'NE' => '\d{4}', |
| 1161 | 'NF' => '2899', |
| 1162 | 'NG' => '(\d{6})?', |
| 1163 | 'NI' => '((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?', |
| 1164 | 'NL' => '^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$', |
| 1165 | 'NO' => '\d{4}', |
| 1166 | 'NP' => '\d{5}', |
| 1167 | 'NZ' => '\d{4}', |
| 1168 | 'OM' => '(PC )?\d{3}', |
| 1169 | 'PF' => '987\d{2}', |
| 1170 | 'PG' => '\d{3}', |
| 1171 | 'PH' => '\d{4}', |
| 1172 | 'PK' => '\d{5}', |
| 1173 | 'PL' => '\d{2}-\d{3}', |
| 1174 | 'PM' => '9[78]5\d{2}', |
| 1175 | 'PN' => 'PCRN 1ZZ', |
| 1176 | 'PR' => '00[679]\d{2}([ \-]\d{4})?', |
| 1177 | 'PT' => '\d{4}([\-]\d{3})?', |
| 1178 | 'PW' => '96940', |
| 1179 | 'PY' => '\d{4}', |
| 1180 | 'RE' => '9[78]4\d{2}', |
| 1181 | 'RO' => '\d{6}', |
| 1182 | 'RS' => '\d{5}', |
| 1183 | 'RU' => '\d{6}', |
| 1184 | 'SA' => '\d{5}', |
| 1185 | 'SE' => '^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$', |
| 1186 | 'SG' => '\d{6}', |
| 1187 | 'SH' => '(ASCN|STHL) 1ZZ', |
| 1188 | 'SI' => '\d{4}', |
| 1189 | 'SJ' => '\d{4}', |
| 1190 | 'SK' => '\d{3}[ ]?\d{2}', |
| 1191 | 'SM' => '4789\d', |
| 1192 | 'SN' => '\d{5}', |
| 1193 | 'SO' => '\d{5}', |
| 1194 | 'SZ' => '[HLMS]\d{3}', |
| 1195 | 'TC' => 'TKCA 1ZZ', |
| 1196 | 'TH' => '\d{5}', |
| 1197 | 'TJ' => '\d{6}', |
| 1198 | 'TM' => '\d{6}', |
| 1199 | 'TN' => '\d{4}', |
| 1200 | 'TR' => '\d{5}', |
| 1201 | 'TW' => '\d{3}(\d{2})?', |
| 1202 | 'UA' => '\d{5}', |
| 1203 | 'UK' => '^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$', |
| 1204 | 'US' => '^\d{5}([\-]?\d{4})?$', |
| 1205 | 'UY' => '\d{5}', |
| 1206 | 'UZ' => '\d{6}', |
| 1207 | 'VA' => '00120', |
| 1208 | 'VE' => '\d{4}', |
| 1209 | 'VI' => '008(([0-4]\d)|(5[01]))([ \-]\d{4})?', |
| 1210 | 'WF' => '986\d{2}', |
| 1211 | 'YT' => '976\d{2}', |
| 1212 | 'YU' => '\d{5}', |
| 1213 | 'ZA' => '\d{4}', |
| 1214 | 'ZM' => '\d{5}', |
| 1215 | ); |
| 1216 | |
| 1217 | if ( ! isset( $zip_regex[ $country_code ] ) || preg_match( '/' . $zip_regex[ $country_code ] . '/i', $zip ) ) { |
| 1218 | $ret = true; |
| 1219 | } |
| 1220 | |
| 1221 | return apply_filters( 'give_is_zip_valid', $ret, $zip, $country_code ); |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * Validate donation amount and auto set correct donation level id on basis of amount. |
| 1226 | * |
| 1227 | * Note: If amount does not match to donation level amount then level id will be auto select to first match level id on basis of amount. |
| 1228 | * |
| 1229 | * @param array $valid_data List of Valid Data. |
| 1230 | * |
| 1231 | * @return bool |
| 1232 | */ |
| 1233 | function give_validate_donation_amount( $valid_data ) { |
| 1234 | $data = $_POST; |
| 1235 | |
| 1236 | /* @var Give_Donate_Form $form */ |
| 1237 | $form = new Give_Donate_Form( $data['give-form-id'] ); |
| 1238 | |
| 1239 | $donation_level_matched = false; |
| 1240 | |
| 1241 | if ( $form->is_set_type_donation_form() ) { |
| 1242 | // Sanitize donation amount. |
| 1243 | $data['give-amount'] = give_maybe_sanitize_amount( $data['give-amount'] ); |
| 1244 | |
| 1245 | // Backward compatibility. |
| 1246 | if ( $form->is_custom_price( $data['give-amount'] ) ) { |
| 1247 | $_POST['give-price-id'] = 'custom'; |
| 1248 | } |
| 1249 | |
| 1250 | $donation_level_matched = true; |
| 1251 | |
| 1252 | } elseif ( $form->is_multi_type_donation_form() ) { |
| 1253 | |
| 1254 | // Bailout. |
| 1255 | if ( ! ( $variable_prices = $form->get_prices() ) ) { |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | // Sanitize donation amount. |
| 1260 | $data['give-amount'] = give_maybe_sanitize_amount( $data['give-amount'] ); |
| 1261 | |
| 1262 | if ( $data['give-amount'] === give_maybe_sanitize_amount( give_get_price_option_amount( $data['give-form-id'], $data['give-price-id'] ) ) ) { |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | if ( $form->is_custom_price( $data['give-amount'] ) ) { |
| 1267 | $_POST['give-price-id'] = 'custom'; |
| 1268 | } else { |
| 1269 | // Find correct donation level from all donation levels. |
| 1270 | foreach ( $variable_prices as $variable_price ) { |
| 1271 | // Sanitize level amount. |
| 1272 | $variable_price['_give_amount'] = give_maybe_sanitize_amount( $variable_price['_give_amount'] ); |
| 1273 | |
| 1274 | // Set first match donation level ID. |
| 1275 | if ( $data['give-amount'] === $variable_price['_give_amount'] ) { |
| 1276 | $_POST['give-price-id'] = $variable_price['_give_id']['level_id']; |
| 1277 | break; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | // If donation amount is not find in donation levels then check if form has custom donation feature enable or not. |
| 1283 | // If yes then set price id to custom if amount is greater then custom minimum amount (if any). |
| 1284 | if ( ! empty( $_POST['give-price-id'] ) ) { |
| 1285 | $donation_level_matched = true; |
| 1286 | } |
| 1287 | }// End if(). |
| 1288 | |
| 1289 | return ( $donation_level_matched ? true : false ); |
| 1290 | } |
| 1291 | |
| 1292 | add_action( 'give_checkout_error_checks', 'give_validate_donation_amount', 10, 1 ); |
| 1293 | |
| 1294 | /** |
| 1295 | * Validate Required Form Fields. |
| 1296 | * |
| 1297 | * @param int $form_id Form ID. |
| 1298 | * |
| 1299 | * @since 2.0 |
| 1300 | */ |
| 1301 | function give_validate_required_form_fields( $form_id ) { |
| 1302 | |
| 1303 | // Loop through required fields and show error messages. |
| 1304 | foreach ( give_get_required_fields( $form_id ) as $field_name => $value ) { |
| 1305 | |
| 1306 | // Clean Up Data of the input fields. |
| 1307 | $field_value = give_clean( $_POST[ $field_name ] ); |
| 1308 | |
| 1309 | // Check whether the required field is empty, then show the error message. |
| 1310 | if ( in_array( $value, give_get_required_fields( $form_id ) ) && empty( $field_value ) ) { |
| 1311 | give_set_error( $value['error_id'], $value['error_message'] ); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * Validates and checks if name fields don't contain email addresses. |
| 1318 | * |
| 1319 | * @since 2.1 |
| 1320 | * @return void |
| 1321 | */ |
| 1322 | function give_donation_form_validate_name_fields() { |
| 1323 | $is_first_name = is_email( $_POST['give_first'] ) ? true : false; |
| 1324 | $is_last_name = is_email( $_POST['give_last'] ) ? true : false; |
| 1325 | |
| 1326 | if ( $is_first_name || $is_last_name ) { |
| 1327 | give_set_error( 'invalid_name', esc_html__( '<First Name | Last Name> cannot contain email address.', 'give' ) ); |
| 1328 | } |
| 1329 | } |
| 1330 |