Test Failed
Push — issues/2610 ( 4e0645...7de36d )
by Ravinder
14:50
created

process-donation.php ➔ give_donation_form_validate_new_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 0
dl 0
loc 49
rs 9.2258
c 0
b 0
f 0
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'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
29
30
	// Verify donation form nonce.
31
	if(  ! give_verify_donation_form_nonce() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space before "!"; 2 found
Loading history...
32
		if( $is_ajax ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
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{
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
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 $_POST Array of variables passed via the HTTP POST.
65
	 */
66
	do_action( 'give_checkout_error_checks', $valid_data, $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
67
68
	// Process the login form.
69
	if ( isset( $_POST['give_login_submit'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
70
		give_process_form_login();
71
	}
72
73
	// Validate the user.
74
	$user = give_get_donation_form_user( $valid_data );
0 ignored issues
show
Security Bug introduced by
It seems like $valid_data defined by give_donation_form_validate_fields() on line 54 can also be of type false; however, give_get_donation_form_user() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method set_cookie cannot be called on Give()->session->init() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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'] ) ?
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
119
		(float) apply_filters( 'give_donation_total', give_maybe_sanitize_amount( $_POST['give-amount'] ) ) :
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
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 ),
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
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 );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
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
/**
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
 * @param  array $post Additional $_POST data submitted
192
 *
193
 * @return void
194
 */
195
function give_check_logged_in_user_for_existing_email( $valid_data, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
196
197
	// Verify that the email address belongs to this customer.
198
	if ( is_user_logged_in() ) {
199
200
		$submitted_email = $valid_data['logged_in_user']['user_email'];
201
		$donor           = new Give_Donor( get_current_user_id(), true );
202
203
		// If this email address is not registered with this customer, see if it belongs to any other customer.
204
		if (
205
			$submitted_email !== $donor->email
206
			&& ( is_array( $donor->emails ) && ! in_array( $submitted_email, $donor->emails ) )
207
		) {
208
			$found_donor = new Give_Donor( $submitted_email );
209
210
			if ( $found_donor->id > 0 ) {
211
				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 ) );
212
			}
213
		}
214
	}
215
}
216
217
add_action( 'give_checkout_error_checks', 'give_check_logged_in_user_for_existing_email', 10, 2 );
218
219
/**
220
 * Process the checkout login form
221
 *
222
 * @access      private
223
 * @since       1.0
224
 * @return      void
225
 */
226
function give_process_form_login() {
227
	$is_ajax = isset( $_POST['give_ajax'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
228
229
	$user_data = give_donation_form_validate_user_login();
230
231
	if ( give_get_errors() || $user_data['user_id'] < 1 ) {
232
		if ( $is_ajax ) {
233
			/**
234
			 * Fires when AJAX sends back errors from the donation form.
235
			 *
236
			 * @since 1.0
237
			 */
238
			ob_start();
239
			do_action( 'give_ajax_donation_errors' );
240
			$message = ob_get_contents();
241
			ob_end_clean();
242
			wp_send_json_error( $message );
243
		} else {
244
			wp_redirect( $_SERVER['HTTP_REFERER'] );
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_SERVER
Loading history...
245
			exit;
246
		}
247
	}
248
249
	give_log_user_in( $user_data['user_id'], $user_data['user_login'], $user_data['user_pass'] );
250
251
	if ( $is_ajax ) {
252
		$message = Give()->notices->print_frontend_notice(
253
			sprintf(
254
			/* translators: %s: user first name */
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 12.
Loading history...
255
				esc_html__( 'Welcome %s! You have successfully logged into your account.', 'give' ),
256
				( ! empty( $user_data['user_first'] ) ) ? $user_data['user_first'] : $user_data['user_login']
257
			),
258
			false,
259
			'success'
260
		);
261
262
		wp_send_json_success( $message );
263
	} else {
264
		wp_redirect( $_SERVER['HTTP_REFERER'] );
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_SERVER
Loading history...
265
	}
266
}
267
268
add_action( 'wp_ajax_give_process_donation_login', 'give_process_form_login' );
269
add_action( 'wp_ajax_nopriv_give_process_donation_login', 'give_process_form_login' );
270
271
/**
272
 * Donation Form Validate Fields.
273
 *
274
 * @access      private
275
 * @since       1.0
276
 * @return      bool|array
277
 */
278
function give_donation_form_validate_fields() {
279
280
	// Check if there is $_POST.
281
	if ( empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
282
		return false;
283
	}
284
285
	$form_id = ! empty( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
286
287
	// Start an array to collect valid data.
288
	$valid_data = array(
289
		'gateway'          => give_donation_form_validate_gateway(), // Gateway fallback (amount is validated here).
290
		'need_new_user'    => false,     // New user flag.
291
		'need_user_login'  => false,     // Login user flag.
292
		'logged_user_data' => array(),   // Logged user collected data.
293
		'new_user_data'    => array(),   // New user collected data.
294
		'login_user_data'  => array(),   // Login user collected data.
295
		'guest_user_data'  => array(),   // Guest user collected data.
296
		'cc_info'          => give_donation_form_validate_cc(),// Credit card info.
297
	);
298
299
	// Validate Honeypot First.
300
	if ( ! empty( $_POST['give-honeypot'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
301
		give_set_error( 'invalid_honeypot', esc_html__( 'Honeypot field detected. Go away bad bot!', 'give' ) );
302
	}
303
304
	// Check spam detect.
305
	if ( isset( $_POST['action'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
306
	     && give_is_setting_enabled( give_get_option( 'akismet_spam_protection' ) )
307
	     && give_is_spam_donation()
308
	) {
309
		give_set_error( 'invalid_donation', __( 'This donation has been flagged as spam. Please try again.', 'give' ) );
310
	}
311
312
	// Validate agree to terms.
313
	if ( give_is_terms_enabled( $form_id ) ) {
314
		give_donation_form_validate_agree_to_terms();
315
	}
316
317
	if ( is_user_logged_in() ) {
318
		// Collect logged in user data.
319
		$valid_data['logged_in_user'] = give_donation_form_validate_logged_in_user();
320
	} elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-register' && ! empty( $_POST['give_create_account'] ) ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
321
		// Set new user registration as required.
322
		$valid_data['need_new_user'] = true;
323
		// Validate new user data.
324
		$valid_data['new_user_data'] = give_donation_form_validate_new_user();
325
		// Check if login validation is needed.
326
	} elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-login' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
327
		// Set user login as required.
328
		$valid_data['need_user_login'] = true;
329
		// Validate users login info.
330
		$valid_data['login_user_data'] = give_donation_form_validate_user_login();
331
	} else {
332
		// Not registering or logging in, so setup guest user data.
333
		$valid_data['guest_user_data'] = give_donation_form_validate_guest_user();
334
	}
335
336
	// Return collected data.
337
	return $valid_data;
338
}
339
340
/**
341
 * Detect spam donation.
342
 *
343
 * @since 1.8.14
344
 *
345
 * @return bool|mixed
346
 */
347
function give_is_spam_donation() {
348
	$spam = false;
349
350
	$user_agent = (string) isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
0 ignored issues
show
introduced by
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
351
352
	if ( strlen( $user_agent ) < 2 ) {
353
		$spam = true;
354
	}
355
356
	// Allow developer to customized Akismet spam detect API call and it's response.
357
	return apply_filters( 'give_spam', $spam );
358
}
359
360
/**
361
 * Donation Form Validate Gateway
362
 *
363
 * Validate the gateway and donation amount.
364
 *
365
 * @access      private
366
 * @since       1.0
367
 * @return      string
368
 */
369
function give_donation_form_validate_gateway() {
370
371
	$form_id = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
372
	$amount  = isset( $_REQUEST['give-amount'] ) ? give_maybe_sanitize_amount( $_REQUEST['give-amount'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
373
	$gateway = give_get_default_gateway( $form_id );
374
375
	// Check if a gateway value is present.
376
	if ( ! empty( $_REQUEST['give-gateway'] ) ) {
377
378
		$gateway = sanitize_text_field( $_REQUEST['give-gateway'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
379
380
		// Is amount being donated in LIVE mode 0.00? If so, error:
381
		if ( $amount == 0 && ! give_is_test_mode() ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
382
383
			give_set_error( 'invalid_donation_amount', __( 'Please insert a valid donation amount.', 'give' ) );
384
385
		} // End if().
386
		elseif ( ! give_verify_minimum_price() ) {
387
			// translators: %s: minimum donation amount.
388
			give_set_error(
389
				'invalid_donation_minimum',
390
				sprintf(
391
				/* translators: %s: minimum donation amount */
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 16.
Loading history...
392
					__( 'This form has a minimum donation amount of %s.', 'give' ),
393
					give_currency_filter( give_format_amount( give_get_form_minimum_price( $form_id ), array( 'sanitize' => false ) ) )
0 ignored issues
show
Documentation introduced by
give_get_form_minimum_price($form_id) is of type false|double, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
394
				)
395
			);
396
397
		} //Is this test mode zero donation? Let it through but set to manual gateway.
398
		elseif ( $amount == 0 && give_is_test_mode() ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
399
400
			$gateway = 'manual';
401
402
		} //Check if this gateway is active.
403
		elseif ( ! give_is_gateway_active( $gateway ) ) {
404
405
			give_set_error( 'invalid_gateway', __( 'The selected payment gateway is not enabled.', 'give' ) );
406
407
		}
408
	}
409
410
	return $gateway;
411
412
}
413
414
/**
415
 * Donation Form Validate Minimum Donation Amount
416
 *
417
 * @access      private
418
 * @since       1.3.6
419
 * @return      bool
420
 */
421
function give_verify_minimum_price() {
422
423
	$amount          = give_maybe_sanitize_amount( $_REQUEST['give-amount'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_REQUEST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
424
	$form_id         = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
425
	$price_id        = isset( $_REQUEST['give-price-id'] ) ? $_REQUEST['give-price-id'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
426
	$variable_prices = give_has_variable_prices( $form_id );
427
428
	if ( $variable_prices && in_array( $price_id, give_get_variable_price_ids( $form_id ) ) ) {
429
430
		$price_level_amount = give_get_price_option_amount( $form_id, $price_id );
431
432
		if ( $price_level_amount == $amount ) {
433
			return true;
434
		}
435
	}
436
437
	if ( give_get_form_minimum_price( $form_id ) > $amount ) {
438
		return false;
439
	}
440
441
	return true;
442
}
443
444
/**
445
 * Donation form validate agree to "Terms and Conditions".
446
 *
447
 * @access      private
448
 * @since       1.0
449
 * @return      void
450
 */
451
function give_donation_form_validate_agree_to_terms() {
452
	// Validate agree to terms.
453
	if ( ! isset( $_POST['give_agree_to_terms'] ) || $_POST['give_agree_to_terms'] != 1 ) {
0 ignored issues
show
introduced by
Found "!= 1". Use Yoda Condition checks, you must
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
454
		// User did not agree.
455
		give_set_error( 'agree_to_terms', apply_filters( 'give_agree_to_terms_text', __( 'You must agree to the terms and conditions.', 'give' ) ) );
456
	}
457
}
458
459
/**
460
 * Donation Form Required Fields.
461
 *
462
 * @access      private
463
 * @since       1.0
464
 *
465
 * @param       $form_id
466
 *
467
 * @return      array
468
 */
469
function give_get_required_fields( $form_id ) {
470
471
	$payment_mode = give_get_chosen_gateway( $form_id );
472
473
	$required_fields = array(
474
		'give_email' => array(
475
			'error_id'      => 'invalid_email',
476
			'error_message' => __( 'Please enter a valid email address.', 'give' ),
477
		),
478
		'give_first' => array(
479
			'error_id'      => 'invalid_first_name',
480
			'error_message' => __( 'Please enter your first name.', 'give' ),
481
		),
482
	);
483
484
	$require_address = give_require_billing_address( $payment_mode );
485
486
	if ( $require_address ) {
487
		$required_fields['card_address']    = array(
488
			'error_id'      => 'invalid_card_address',
489
			'error_message' => __( 'Please enter your primary billing address.', 'give' ),
490
		);
491
		$required_fields['card_zip']        = array(
492
			'error_id'      => 'invalid_zip_code',
493
			'error_message' => __( 'Please enter your zip / postal code.', 'give' ),
494
		);
495
		$required_fields['card_city']       = array(
496
			'error_id'      => 'invalid_city',
497
			'error_message' => __( 'Please enter your billing city.', 'give' ),
498
		);
499
		$required_fields['billing_country'] = array(
500
			'error_id'      => 'invalid_country',
501
			'error_message' => __( 'Please select your billing country.', 'give' ),
502
		);
503
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
504
505
		$required_fields['card_state'] = array(
506
			'error_id'      => 'invalid_state',
507
			'error_message' => __( 'Please enter billing state / province / County.', 'give' ),
508
		);
509
510
		// Check if billing country already exists.
511
		if ( ! empty( $_POST['billing_country'] ) ) {
512
			// Get the value from $_POST.
513
			$country = sanitize_text_field( $_POST['billing_country'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
514
515
			// Get the country list that does not required any states init.
516
			$states_country = give_states_not_required_country_list();
517
518
			// Check if states is empty or not.
519
			if ( array_key_exists( $country, $states_country ) ) {
520
				// If states is empty remove the required feilds of state in billing cart.
521
				unset( $required_fields['card_state'] );
522
			}
523
		}
524
	}
525
526
	/**
527
	 * Filters the donation form required field.
528
	 *
529
	 * @since 1.7
530
	 */
531
	$required_fields = apply_filters( 'give_donation_form_required_fields', $required_fields, $form_id );
532
533
	return $required_fields;
534
535
}
536
537
/**
538
 * Check if the Billing Address is required
539
 *
540
 * @since  1.0.1
541
 *
542
 * @param string $payment_mode
543
 *
544
 * @return bool
545
 */
546
function give_require_billing_address( $payment_mode ) {
547
548
	$return = false;
549
550
	if ( isset( $_POST['billing_country'] ) || did_action( "give_{$payment_mode}_cc_form" ) || did_action( 'give_cc_form' ) ) {
551
		$return = true;
552
	}
553
554
	// Let payment gateways and other extensions determine if address fields should be required.
555
	return apply_filters( 'give_require_billing_address', $return );
556
557
}
558
559
/**
560
 * Donation Form Validate Logged In User.
561
 *
562
 * @access      private
563
 * @since       1.0
564
 * @return      array
565
 */
566
function give_donation_form_validate_logged_in_user() {
567
	global $user_ID;
568
569
	$form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
570
571
	// Start empty array to collect valid user data.
572
	$valid_user_data = array(
573
		// Assume there will be errors.
574
		'user_id' => - 1,
575
	);
576
577
	// Verify there is a user_ID.
578
	if ( $user_ID > 0 ) {
579
		// Get the logged in user data.
580
		$user_data = get_userdata( $user_ID );
581
582
		// Validate Required Form Fields.
583
		give_validate_required_form_fields( $form_id );
584
585
		// Verify data.
586
		if ( $user_data ) {
587
			// Collected logged in user data.
588
			$valid_user_data = array(
589
				'user_id'    => $user_ID,
590
				'user_email' => isset( $_POST['give_email'] ) ? sanitize_email( $_POST['give_email'] ) : $user_data->user_email,
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
591
				'user_first' => isset( $_POST['give_first'] ) && ! empty( $_POST['give_first'] ) ? sanitize_text_field( $_POST['give_first'] ) : $user_data->first_name,
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
592
				'user_last'  => isset( $_POST['give_last'] ) && ! empty( $_POST['give_last'] ) ? sanitize_text_field( $_POST['give_last'] ) : $user_data->last_name,
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
593
			);
594
595
			if ( ! is_email( $valid_user_data['user_email'] ) ) {
596
				give_set_error( 'email_invalid', esc_html__( 'Invalid email.', 'give' ) );
597
			}
598
		} else {
599
			// Set invalid user error.
600
			give_set_error( 'invalid_user', esc_html__( 'The user information is invalid.', 'give' ) );
601
		}
602
	}
603
604
	// Return user data.
605
	return $valid_user_data;
606
}
607
608
/**
609
 * Donate Form Validate New User
610
 *
611
 * @access      private
612
 * @since       1.0
613
 * @return      array
614
 */
615
function give_donation_form_validate_new_user() {
616
617
	$auto_generated_password = wp_generate_password();
618
619
	// Default user data.
620
	$default_user_data = array(
621
		'give-form-id'           => '',
622
		'user_id'                => - 1, // Assume there will be errors.
623
		'user_first'             => '',
624
		'user_last'              => '',
625
		'give_user_login'        => false,
626
		'give_email'             => false,
627
		'give_user_pass'         => $auto_generated_password,
628
		'give_user_pass_confirm' => $auto_generated_password,
629
	);
630
631
	// Get user data.
632
	$user_data            = wp_parse_args( give_clean( $_POST ), $default_user_data );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
633
	$registering_new_user = false;
634
	$form_id              = absint( $user_data['give-form-id'] );
635
636
	// Start an empty array to collect valid user data.
637
	$valid_user_data = array(
638
		// Assume there will be errors.
639
		'user_id'    => - 1,
640
641
		// Get first name.
642
		'user_first' => $user_data['give_first'],
643
644
		// Get last name.
645
		'user_last'  => $user_data['give_last'],
646
647
		// Get Password.
648
		'user_pass'  => $user_data['give_user_pass'],
649
	);
650
651
	// Validate Required Form Fields.
652
	give_validate_required_form_fields( $form_id );
653
654
	// Set Email as Username.
655
	$valid_user_data['user_login'] = $user_data['give_email'];
656
657
	// Check if we have an email to verify.
658
	if ( give_validate_user_email( $user_data['give_email'], $registering_new_user ) ) {
659
		$valid_user_data['user_email'] = $user_data['give_email'];
660
	}
661
662
	return $valid_user_data;
663
}
664
665
/**
666
 * Donation Form Validate User Login
667
 *
668
 * @access      private
669
 * @since       1.0
670
 * @return      array
671
 */
672
function give_donation_form_validate_user_login() {
673
674
	// Start an array to collect valid user data.
675
	$valid_user_data = array(
676
		// Assume there will be errors.
677
		'user_id' => - 1,
678
	);
679
680
	// Username.
681
	if ( ! isset( $_POST['give_user_login'] ) || $_POST['give_user_login'] == '' ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
682
		give_set_error( 'must_log_in', __( 'You must register or login to complete your donation.', 'give' ) );
683
684
		return $valid_user_data;
685
	}
686
687
	// Get the user by login.
688
	$user_data = get_user_by( 'login', strip_tags( $_POST['give_user_login'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
689
690
	// Check if user exists.
691
	if ( $user_data ) {
692
		// Get password.
693
		$user_pass = isset( $_POST['give_user_pass'] ) ? $_POST['give_user_pass'] : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
694
695
		// Check user_pass.
696
		if ( $user_pass ) {
697
			// Check if password is valid.
698
			if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) {
699
				// Incorrect password.
700
				give_set_error(
701
					'password_incorrect',
702
					sprintf(
703
						'%1$s <a href="%2$s">%3$s</a>',
704
						__( 'The password you entered is incorrect.', 'give' ),
705
						wp_lostpassword_url( "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ),
706
						__( 'Reset Password', 'give' )
707
					)
708
				);
709
				// All is correct.
710
			} else {
711
712
				// Repopulate the valid user data array.
713
				$valid_user_data = array(
714
					'user_id'    => $user_data->ID,
715
					'user_login' => $user_data->user_login,
716
					'user_email' => $user_data->user_email,
717
					'user_first' => $user_data->first_name,
718
					'user_last'  => $user_data->last_name,
719
					'user_pass'  => $user_pass,
720
				);
721
			}
722
		} else {
723
			// Empty password.
724
			give_set_error( 'password_empty', __( 'Enter a password.', 'give' ) );
725
		}
726
	} else {
727
		// No username.
728
		give_set_error( 'username_incorrect', __( 'The username you entered does not exist.', 'give' ) );
729
	}// End if().
730
731
	return $valid_user_data;
732
}
733
734
/**
735
 * Donation Form Validate Guest User
736
 *
737
 * @access  private
738
 * @since   1.0
739
 * @return  array
740
 */
741
function give_donation_form_validate_guest_user() {
742
743
	$form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
744
745
	// Start an array to collect valid user data.
746
	$valid_user_data = array(
747
		// Set a default id for guests.
748
		'user_id' => 0,
749
	);
750
751
	// Get the guest email.
752
	$guest_email = isset( $_POST['give_email'] ) ? $_POST['give_email'] : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
753
754
	// Check email.
755
	if ( $guest_email && strlen( $guest_email ) > 0 ) {
756
		// Validate email.
757
		if ( ! is_email( $guest_email ) ) {
758
			// Invalid email.
759
			give_set_error( 'email_invalid', __( 'Invalid email.', 'give' ) );
760
		} else {
761
			// All is good to go.
762
			$valid_user_data['user_email'] = $guest_email;
763
764
			// Get user_id from donor if exist.
765
			$donor = new Give_Donor( $guest_email );
766
			if ( $donor->id && $donor->user_id ) {
767
				$valid_user_data['user_id'] = $donor->user_id;
768
			}
769
		}
770
	} else {
771
		// No email.
772
		give_set_error( 'email_empty', __( 'Enter an email.', 'give' ) );
773
	}
774
775
	// Validate Required Form Fields.
776
	give_validate_required_form_fields( $form_id );
777
778
	return $valid_user_data;
779
}
780
781
/**
782
 * Register And Login New User
783
 *
784
 * @param array $user_data
785
 *
786
 * @access  private
787
 * @since   1.0
788
 * @return  integer
789
 */
790
function give_register_and_login_new_user( $user_data = array() ) {
791
	// Verify the array.
792
	if ( empty( $user_data ) ) {
793
		return - 1;
794
	}
795
796
	if ( give_get_errors() ) {
797
		return - 1;
798
	}
799
800
	$user_args = apply_filters( 'give_insert_user_args', array(
801
		'user_login'      => isset( $user_data['user_login'] ) ? $user_data['user_login'] : '',
802
		'user_pass'       => isset( $user_data['user_pass'] ) ? $user_data['user_pass'] : '',
803
		'user_email'      => isset( $user_data['user_email'] ) ? $user_data['user_email'] : '',
804
		'first_name'      => isset( $user_data['user_first'] ) ? $user_data['user_first'] : '',
805
		'last_name'       => isset( $user_data['user_last'] ) ? $user_data['user_last'] : '',
806
		'user_registered' => date( 'Y-m-d H:i:s' ),
807
		'role'            => give_get_option( 'donor_default_user_role', 'give_donor' ),
808
	), $user_data );
809
810
	// Insert new user.
811
	$user_id = wp_insert_user( $user_args );
812
813
	// Validate inserted user.
814
	if ( is_wp_error( $user_id ) ) {
815
		return - 1;
816
	}
817
818
	// Allow themes and plugins to filter the user data.
819
	$user_data = apply_filters( 'give_insert_user_data', $user_data, $user_args );
820
821
	/**
822
	 * Fires after inserting user.
823
	 *
824
	 * @since 1.0
825
	 *
826
	 * @param int $user_id User id.
827
	 * @param array $user_data Array containing user data.
828
	 */
829
	do_action( 'give_insert_user', $user_id, $user_data );
830
831
	/**
832
	 * Filter allow user to alter if user when to login or not when user is register for the first time.
833
	 *
834
	 * @since 1.8.13
835
	 *
836
	 * return bool True if login with registration and False if only want to register.
837
	 */
838
	if ( true === (bool) apply_filters( 'give_log_user_in_on_register', true ) ) {
839
		// Login new user.
840
		give_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] );
841
	}
842
843
	// Return user id.
844
	return $user_id;
845
}
846
847
/**
848
 * Get Donation Form User
849
 *
850
 * @param array $valid_data
851
 *
852
 * @access  private
853
 * @since   1.0
854
 * @return  array|bool
855
 */
856
function give_get_donation_form_user( $valid_data = array() ) {
857
858
	// Initialize user.
859
	$user    = false;
860
	$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
861
862
	if ( $is_ajax ) {
863
		// Do not create or login the user during the ajax submission (check for errors only).
864
		return true;
865
	} elseif ( is_user_logged_in() ) {
866
		// Set the valid user as the logged in collected data.
867
		$user = $valid_data['logged_in_user'];
868
	} elseif ( $valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
869
		// New user registration.
870
		if ( $valid_data['need_new_user'] === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
871
			// Set user.
872
			$user = $valid_data['new_user_data'];
873
			// Register and login new user.
874
			$user['user_id'] = give_register_and_login_new_user( $user );
875
			// User login
876
		} elseif ( $valid_data['need_user_login'] === true && ! $is_ajax ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
877
878
			/**
879
			 * The login form is now processed in the give_process_donation_login() function.
880
			 * This is still here for backwards compatibility.
881
			 * This also allows the old login process to still work if a user removes the checkout login submit button.
882
			 *
883
			 * 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.
884
			 */
885
			// Set user.
886
			$user = $valid_data['login_user_data'];
887
			// Login user.
888
			give_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] );
889
		}
890
	}
891
892
	// Check guest checkout.
893
	if ( false === $user && false === give_logged_in_only( $_POST['give-form-id'] ) ) {
894
		// Set user
895
		$user = $valid_data['guest_user_data'];
896
	}
897
898
	// Verify we have an user.
899
	if ( false === $user || empty( $user ) ) {
900
		// Return false.
901
		return false;
902
	}
903
904
	// Get user first name.
905 View Code Duplication
	if ( ! isset( $user['user_first'] ) || strlen( trim( $user['user_first'] ) ) < 1 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
906
		$user['user_first'] = isset( $_POST['give_first'] ) ? strip_tags( trim( $_POST['give_first'] ) ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
907
	}
908
909
	// Get user last name.
910 View Code Duplication
	if ( ! isset( $user['user_last'] ) || strlen( trim( $user['user_last'] ) ) < 1 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
911
		$user['user_last'] = isset( $_POST['give_last'] ) ? strip_tags( trim( $_POST['give_last'] ) ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
912
	}
913
914
	// Get the user's billing address details.
915
	$user['address']            = array();
916
	$user['address']['line1']   = ! empty( $_POST['card_address'] ) ? give_clean( $_POST['card_address'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
917
	$user['address']['line2']   = ! empty( $_POST['card_address_2'] ) ? give_clean( $_POST['card_address_2'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
918
	$user['address']['city']    = ! empty( $_POST['card_city'] ) ? give_clean( $_POST['card_city'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
919
	$user['address']['state']   = ! empty( $_POST['card_state'] ) ? give_clean( $_POST['card_state'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
920
	$user['address']['zip']     = ! empty( $_POST['card_zip'] ) ? give_clean( $_POST['card_zip'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
921
	$user['address']['country'] = ! empty( $_POST['billing_country'] ) ? give_clean( $_POST['billing_country'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
922
923
	if ( empty( $user['address']['country'] ) ) {
924
		$user['address'] = false;
925
	} // End if().
926
927
	// Return valid user.
928
	return $user;
929
}
930
931
/**
932
 * Validates the credit card info.
933
 *
934
 * @access  private
935
 * @since   1.0
936
 * @return  array
937
 */
938
function give_donation_form_validate_cc() {
939
940
	$card_data = give_get_donation_cc_info();
941
942
	// Validate the card zip.
943
	if ( ! empty( $card_data['card_zip'] ) ) {
944
		if ( ! give_donation_form_validate_cc_zip( $card_data['card_zip'], $card_data['card_country'] ) ) {
945
			give_set_error( 'invalid_cc_zip', __( 'The zip / postal code you entered for your billing address is invalid.', 'give' ) );
946
		}
947
	}
948
949
	// Ensure no spaces.
950
	if ( ! empty( $card_data['card_number'] ) ) {
951
		$card_data['card_number'] = str_replace( '+', '', $card_data['card_number'] ); // no "+" signs
952
		$card_data['card_number'] = str_replace( ' ', '', $card_data['card_number'] ); // No spaces
953
	}
954
955
	// This should validate card numbers at some point too.
956
	return $card_data;
957
}
958
959
/**
960
 * Get credit card info.
961
 *
962
 * @access  private
963
 * @since   1.0
964
 * @return  array
965
 */
966
function give_get_donation_cc_info() {
967
968
	$cc_info                   = array();
969
	$cc_info['card_name']      = isset( $_POST['card_name'] ) ? sanitize_text_field( $_POST['card_name'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
970
	$cc_info['card_number']    = isset( $_POST['card_number'] ) ? sanitize_text_field( $_POST['card_number'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
971
	$cc_info['card_cvc']       = isset( $_POST['card_cvc'] ) ? sanitize_text_field( $_POST['card_cvc'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
972
	$cc_info['card_exp_month'] = isset( $_POST['card_exp_month'] ) ? sanitize_text_field( $_POST['card_exp_month'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
973
	$cc_info['card_exp_year']  = isset( $_POST['card_exp_year'] ) ? sanitize_text_field( $_POST['card_exp_year'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
974
	$cc_info['card_address']   = isset( $_POST['card_address'] ) ? sanitize_text_field( $_POST['card_address'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
975
	$cc_info['card_address_2'] = isset( $_POST['card_address_2'] ) ? sanitize_text_field( $_POST['card_address_2'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
976
	$cc_info['card_city']      = isset( $_POST['card_city'] ) ? sanitize_text_field( $_POST['card_city'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
977
	$cc_info['card_state']     = isset( $_POST['card_state'] ) ? sanitize_text_field( $_POST['card_state'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
978
	$cc_info['card_country']   = isset( $_POST['billing_country'] ) ? sanitize_text_field( $_POST['billing_country'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
979
	$cc_info['card_zip']       = isset( $_POST['card_zip'] ) ? sanitize_text_field( $_POST['card_zip'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
980
981
	// Return cc info.
982
	return $cc_info;
983
}
984
985
/**
986
 * Validate zip code based on country code
987
 *
988
 * @since  1.0
989
 *
990
 * @param int $zip
991
 * @param string $country_code
992
 *
993
 * @return bool|mixed
994
 */
995
function give_donation_form_validate_cc_zip( $zip = 0, $country_code = '' ) {
996
	$ret = false;
997
998
	if ( empty( $zip ) || empty( $country_code ) ) {
999
		return $ret;
1000
	}
1001
1002
	$country_code = strtoupper( $country_code );
1003
1004
	$zip_regex = array(
1005
		'AD' => 'AD\d{3}',
1006
		'AM' => '(37)?\d{4}',
1007
		'AR' => '^([A-Z]{1}\d{4}[A-Z]{3}|[A-Z]{1}\d{4}|\d{4})$',
1008
		'AS' => '96799',
1009
		'AT' => '\d{4}',
1010
		'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})$',
1011
		'AX' => '22\d{3}',
1012
		'AZ' => '\d{4}',
1013
		'BA' => '\d{5}',
1014
		'BB' => '(BB\d{5})?',
1015
		'BD' => '\d{4}',
1016
		'BE' => '^[1-9]{1}[0-9]{3}$',
1017
		'BG' => '\d{4}',
1018
		'BH' => '((1[0-2]|[2-9])\d{2})?',
1019
		'BM' => '[A-Z]{2}[ ]?[A-Z0-9]{2}',
1020
		'BN' => '[A-Z]{2}[ ]?\d{4}',
1021
		'BR' => '\d{5}[\-]?\d{3}',
1022
		'BY' => '\d{6}',
1023
		'CA' => '^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$',
1024
		'CC' => '6799',
1025
		'CH' => '^[1-9][0-9][0-9][0-9]$',
1026
		'CK' => '\d{4}',
1027
		'CL' => '\d{7}',
1028
		'CN' => '\d{6}',
1029
		'CR' => '\d{4,5}|\d{3}-\d{4}',
1030
		'CS' => '\d{5}',
1031
		'CV' => '\d{4}',
1032
		'CX' => '6798',
1033
		'CY' => '\d{4}',
1034
		'CZ' => '\d{3}[ ]?\d{2}',
1035
		'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',
1036
		'DK' => '^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$',
1037
		'DO' => '\d{5}',
1038
		'DZ' => '\d{5}',
1039
		'EC' => '([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?',
1040
		'EE' => '\d{5}',
1041
		'EG' => '\d{5}',
1042
		'ES' => '^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$',
1043
		'ET' => '\d{4}',
1044
		'FI' => '\d{5}',
1045
		'FK' => 'FIQQ 1ZZ',
1046
		'FM' => '(9694[1-4])([ \-]\d{4})?',
1047
		'FO' => '\d{3}',
1048
		'FR' => '^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$',
1049
		'GE' => '\d{4}',
1050
		'GF' => '9[78]3\d{2}',
1051
		'GL' => '39\d{2}',
1052
		'GN' => '\d{3}',
1053
		'GP' => '9[78][01]\d{2}',
1054
		'GR' => '\d{3}[ ]?\d{2}',
1055
		'GS' => 'SIQQ 1ZZ',
1056
		'GT' => '\d{5}',
1057
		'GU' => '969[123]\d([ \-]\d{4})?',
1058
		'GW' => '\d{4}',
1059
		'HM' => '\d{4}',
1060
		'HN' => '(?:\d{5})?',
1061
		'HR' => '\d{5}',
1062
		'HT' => '\d{4}',
1063
		'HU' => '\d{4}',
1064
		'ID' => '\d{5}',
1065
		'IE' => '((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?',
1066
		'IL' => '\d{5}',
1067
		'IN' => '^[1-9][0-9][0-9][0-9][0-9][0-9]$', // india
1068
		'IO' => 'BBND 1ZZ',
1069
		'IQ' => '\d{5}',
1070
		'IS' => '\d{3}',
1071
		'IT' => '^(V-|I-)?[0-9]{5}$',
1072
		'JO' => '\d{5}',
1073
		'JP' => '\d{3}-\d{4}',
1074
		'KE' => '\d{5}',
1075
		'KG' => '\d{6}',
1076
		'KH' => '\d{5}',
1077
		'KR' => '\d{3}[\-]\d{3}',
1078
		'KW' => '\d{5}',
1079
		'KZ' => '\d{6}',
1080
		'LA' => '\d{5}',
1081
		'LB' => '(\d{4}([ ]?\d{4})?)?',
1082
		'LI' => '(948[5-9])|(949[0-7])',
1083
		'LK' => '\d{5}',
1084
		'LR' => '\d{4}',
1085
		'LS' => '\d{3}',
1086
		'LT' => '\d{5}',
1087
		'LU' => '\d{4}',
1088
		'LV' => '\d{4}',
1089
		'MA' => '\d{5}',
1090
		'MC' => '980\d{2}',
1091
		'MD' => '\d{4}',
1092
		'ME' => '8\d{4}',
1093
		'MG' => '\d{3}',
1094
		'MH' => '969[67]\d([ \-]\d{4})?',
1095
		'MK' => '\d{4}',
1096
		'MN' => '\d{6}',
1097
		'MP' => '9695[012]([ \-]\d{4})?',
1098
		'MQ' => '9[78]2\d{2}',
1099
		'MT' => '[A-Z]{3}[ ]?\d{2,4}',
1100
		'MU' => '(\d{3}[A-Z]{2}\d{3})?',
1101
		'MV' => '\d{5}',
1102
		'MX' => '\d{5}',
1103
		'MY' => '\d{5}',
1104
		'NC' => '988\d{2}',
1105
		'NE' => '\d{4}',
1106
		'NF' => '2899',
1107
		'NG' => '(\d{6})?',
1108
		'NI' => '((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?',
1109
		'NL' => '^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$',
1110
		'NO' => '\d{4}',
1111
		'NP' => '\d{5}',
1112
		'NZ' => '\d{4}',
1113
		'OM' => '(PC )?\d{3}',
1114
		'PF' => '987\d{2}',
1115
		'PG' => '\d{3}',
1116
		'PH' => '\d{4}',
1117
		'PK' => '\d{5}',
1118
		'PL' => '\d{2}-\d{3}',
1119
		'PM' => '9[78]5\d{2}',
1120
		'PN' => 'PCRN 1ZZ',
1121
		'PR' => '00[679]\d{2}([ \-]\d{4})?',
1122
		'PT' => '\d{4}([\-]\d{3})?',
1123
		'PW' => '96940',
1124
		'PY' => '\d{4}',
1125
		'RE' => '9[78]4\d{2}',
1126
		'RO' => '\d{6}',
1127
		'RS' => '\d{5}',
1128
		'RU' => '\d{6}',
1129
		'SA' => '\d{5}',
1130
		'SE' => '^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$',
1131
		'SG' => '\d{6}',
1132
		'SH' => '(ASCN|STHL) 1ZZ',
1133
		'SI' => '\d{4}',
1134
		'SJ' => '\d{4}',
1135
		'SK' => '\d{3}[ ]?\d{2}',
1136
		'SM' => '4789\d',
1137
		'SN' => '\d{5}',
1138
		'SO' => '\d{5}',
1139
		'SZ' => '[HLMS]\d{3}',
1140
		'TC' => 'TKCA 1ZZ',
1141
		'TH' => '\d{5}',
1142
		'TJ' => '\d{6}',
1143
		'TM' => '\d{6}',
1144
		'TN' => '\d{4}',
1145
		'TR' => '\d{5}',
1146
		'TW' => '\d{3}(\d{2})?',
1147
		'UA' => '\d{5}',
1148
		'UK' => '^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$',
1149
		'US' => '^\d{5}([\-]?\d{4})?$',
1150
		'UY' => '\d{5}',
1151
		'UZ' => '\d{6}',
1152
		'VA' => '00120',
1153
		'VE' => '\d{4}',
1154
		'VI' => '008(([0-4]\d)|(5[01]))([ \-]\d{4})?',
1155
		'WF' => '986\d{2}',
1156
		'YT' => '976\d{2}',
1157
		'YU' => '\d{5}',
1158
		'ZA' => '\d{4}',
1159
		'ZM' => '\d{5}',
1160
	);
1161
1162
	if ( ! isset( $zip_regex[ $country_code ] ) || preg_match( '/' . $zip_regex[ $country_code ] . '/i', $zip ) ) {
1163
		$ret = true;
1164
	}
1165
1166
	return apply_filters( 'give_is_zip_valid', $ret, $zip, $country_code );
1167
}
1168
1169
1170
/**
1171
 * Validate donation amount and auto set correct donation level id on basis of amount.
1172
 *
1173
 * 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.
1174
 *
1175
 * @param array $valid_data List of Valid Data.
1176
 * @param array $data       List of Posted Data.
1177
 *
1178
 * @return bool
1179
 */
1180
function give_validate_donation_amount( $valid_data, $data ) {
0 ignored issues
show
Unused Code introduced by
The parameter $valid_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1181
	/* @var Give_Donate_Form $form */
1182
	$form = new Give_Donate_Form( $data['give-form-id'] );
1183
1184
	$donation_level_matched = false;
1185
1186
	if ( $form->is_set_type_donation_form() ) {
1187
		// Sanitize donation amount.
1188
		$data['give-amount'] = give_maybe_sanitize_amount( $data['give-amount'] );
1189
1190
		// Backward compatibility.
1191
		if ( $form->is_custom_price( $data['give-amount'] ) ) {
1192
			$_POST['give-price-id'] = 'custom';
1193
		}
1194
1195
		$donation_level_matched = true;
1196
1197
	} elseif ( $form->is_multi_type_donation_form() ) {
1198
1199
		// Bailout.
1200
		if ( ! ( $variable_prices = $form->get_prices() ) ) {
1201
			return false;
1202
		}
1203
1204
		// Sanitize donation amount.
1205
		$data['give-amount'] = give_maybe_sanitize_amount( $data['give-amount'] );
1206
1207
		if ( $data['give-amount'] === give_maybe_sanitize_amount( give_get_price_option_amount( $data['give-form-id'], $data['give-price-id'] ) ) ) {
1208
			return true;
1209
		}
1210
1211
		if ( $form->is_custom_price( $data['give-amount'] ) ) {
1212
			$_POST['give-price-id'] = 'custom';
1213
		} else {
1214
			// Find correct donation level from all donation levels.
1215
			foreach ( $variable_prices as $variable_price ) {
1216
				// Sanitize level amount.
1217
				$variable_price['_give_amount'] = give_maybe_sanitize_amount( $variable_price['_give_amount'] );
1218
1219
				// Set first match donation level ID.
1220
				if ( $data['give-amount'] === $variable_price['_give_amount'] ) {
1221
					$_POST['give-price-id'] = $variable_price['_give_id']['level_id'];
1222
					break;
1223
				}
1224
			}
1225
		}
1226
1227
		// If donation amount is not find in donation levels then check if form has custom donation feature enable or not.
1228
		// If yes then set price id to custom if amount is greater then custom minimum amount (if any).
1229
		if ( ! empty( $_POST['give-price-id'] ) ) {
1230
			$donation_level_matched = true;
1231
		}
1232
	}// End if().
1233
1234
	return ( $donation_level_matched ? true : false );
1235
}
1236
1237
add_action( 'give_checkout_error_checks', 'give_validate_donation_amount', 10, 2 );
1238
1239
/**
1240
 * Validate Required Form Fields.
1241
 *
1242
 * @param int $form_id Form ID.
1243
 *
1244
 * @since 2.0
1245
 */
1246
function give_validate_required_form_fields( $form_id ) {
1247
1248
	// Loop through required fields and show error messages.
1249
	foreach ( give_get_required_fields( $form_id ) as $field_name => $value ) {
1250
1251
		// Clean Up Data of the input fields.
1252
		$field_value = give_clean( $_POST[ $field_name ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
1253
1254
		// Check whether the required field is empty, then show the error message.
1255
		if ( in_array( $value, give_get_required_fields( $form_id ) ) && empty( $field_value ) ) {
1256
			give_set_error( $value['error_id'], $value['error_message'] );
1257
		}
1258
	}
1259
}
1260