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