Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

process-donation.php ➔ give_validate_donation_amount()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 56
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 24
nop 2
dl 0
loc 56
rs 6.5481
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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