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