Test Failed
Pull Request — master (#2079)
by
unknown
08:04
created

process-donation.php ➔ is_spam_donation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Process Donation
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Process Donation Form
19
 *
20
 * Handles the donation form process.
21
 *
22
 * @access      private
23
 * @since       1.0
24
 * @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'    => $_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', $_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 = 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...
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 ( is_spam_donation() ) {
291
		give_set_error( 'invalid_donation', __( 'Spam detected. Go away bad bot!', 'give' ) );
292
	}
293
294
	// Validate agree to terms
295
	if ( give_is_terms_enabled( $form_id ) ) {
296
		give_donation_form_validate_agree_to_terms();
297
	}
298
299
	// Stop processing donor registration, if donor registration is optional and donor can do guest checkout.
300
	// If registration form username field is empty that means donor do not want to registration instead want guest checkout.
301
	if (
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
302
		! give_logged_in_only( $form_id )
303
		&& isset( $_POST['give-purchase-var'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
304
		&& $_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...
305
		&& empty( $_POST['give_user_login'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
306
	) {
307
		unset( $_POST['give-purchase-var'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
308
	}
309
310
	if ( is_user_logged_in() ) {
311
		// Collect logged in user data.
312
		$valid_data['logged_in_user'] = give_donation_form_validate_logged_in_user();
313
	} 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...
314
		// Set new user registration as required.
315
		$valid_data['need_new_user'] = true;
316
		// Validate new user data.
317
		$valid_data['new_user_data'] = give_donation_form_validate_new_user();
318
		// Check if login validation is needed.
319
	} 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...
320
		// Set user login as required.
321
		$valid_data['need_user_login'] = true;
322
		// Validate users login info.
323
		$valid_data['login_user_data'] = give_donation_form_validate_user_login();
324
	} else {
325
		// Not registering or logging in, so setup guest user data.
326
		$valid_data['guest_user_data'] = give_donation_form_validate_guest_user();
327
	}
328
329
	// Return collected data.
330
	return $valid_data;
331
}
332
333
/**
334
 * Detect spam donation.
335
 *
336
 * @since 1.8.15
337
 *
338
 * @return bool|mixed
339
 */
340
function is_spam_donation() {
341
	$spam = false;
342
343
	$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...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
344
345
	if ( strlen( $user_agent ) < 2 ) {
346
		$spam = true;
347
	}
348
349
	return apply_filters( 'give_spam', $spam );
350
}
351
352
/**
353
 * Donation Form Validate Gateway
354
 *
355
 * Validate the gateway and donation amount.
356
 *
357
 * @access      private
358
 * @since       1.0
359
 * @return      string
360
 */
361
function give_donation_form_validate_gateway() {
362
363
	$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...
364
	$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...
365
	$gateway = give_get_default_gateway( $form_id );
366
367
	// Check if a gateway value is present.
368
	if ( ! empty( $_REQUEST['give-gateway'] ) ) {
369
370
		$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...
371
372
		// Is amount being donated in LIVE mode 0.00? If so, error:
373
		if ( $amount == 0 && ! give_is_test_mode() ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
374
375
			give_set_error( 'invalid_donation_amount', __( 'Please insert a valid donation amount.', 'give' ) );
376
377
		} // End if().
378
		elseif ( ! give_verify_minimum_price() ) {
379
			// translators: %s: minimum donation amount.
380
			give_set_error(
381
				'invalid_donation_minimum',
382
				sprintf(
383
				/* 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...
384
					__( 'This form has a minimum donation amount of %s.', 'give' ),
385
					give_currency_filter( give_format_amount( give_get_form_minimum_price( $form_id ), array( 'sanitize' => false ) ) )
386
				)
387
			);
388
389
		} //Is this test mode zero donation? Let it through but set to manual gateway.
390
		elseif ( $amount == 0 && give_is_test_mode() ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
391
392
			$gateway = 'manual';
393
394
		} //Check if this gateway is active.
395
		elseif ( ! give_is_gateway_active( $gateway ) ) {
396
397
			give_set_error( 'invalid_gateway', __( 'The selected payment gateway is not enabled.', 'give' ) );
398
399
		}
400
	}
401
402
	return $gateway;
403
404
}
405
406
/**
407
 * Donation Form Validate Minimum Donation Amount
408
 *
409
 * @access      private
410
 * @since       1.3.6
411
 * @return      bool
412
 */
413
function give_verify_minimum_price() {
414
415
	$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...
416
	$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...
417
	$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...
418
	$variable_prices = give_has_variable_prices( $form_id );
419
420
	if ( $variable_prices && in_array( $price_id, give_get_variable_price_ids( $form_id ) ) ) {
421
422
		$price_level_amount = give_get_price_option_amount( $form_id, $price_id );
423
424
		if ( $price_level_amount == $amount ) {
425
			return true;
426
		}
427
	}
428
429
	if ( give_get_form_minimum_price( $form_id ) > $amount ) {
430
		return false;
431
	}
432
433
	return true;
434
}
435
436
/**
437
 * Donation form validate agree to "Terms and Conditions".
438
 *
439
 * @access      private
440
 * @since       1.0
441
 * @return      void
442
 */
443
function give_donation_form_validate_agree_to_terms() {
444
	// Validate agree to terms.
445
	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...
446
		// User did not agree.
447
		give_set_error( 'agree_to_terms', apply_filters( 'give_agree_to_terms_text', __( 'You must agree to the terms and conditions.', 'give' ) ) );
448
	}
449
}
450
451
/**
452
 * Donation Form Required Fields.
453
 *
454
 * @access      private
455
 * @since       1.0
456
 *
457
 * @param       $form_id
458
 *
459
 * @return      array
460
 */
461
function give_get_required_fields( $form_id ) {
462
463
	$payment_mode = give_get_chosen_gateway( $form_id );
464
465
	$required_fields = array(
466
		'give_email' => array(
467
			'error_id'      => 'invalid_email',
468
			'error_message' => __( 'Please enter a valid email address.', 'give' ),
469
		),
470
		'give_first' => array(
471
			'error_id'      => 'invalid_first_name',
472
			'error_message' => __( 'Please enter your first name.', 'give' ),
473
		),
474
	);
475
476
	$require_address = give_require_billing_address( $payment_mode );
477
478
	if ( $require_address ) {
479
		$required_fields['card_address']    = array(
480
			'error_id'      => 'invalid_card_address',
481
			'error_message' => __( 'Please enter your primary billing address.', 'give' ),
482
		);
483
		$required_fields['card_zip']        = array(
484
			'error_id'      => 'invalid_zip_code',
485
			'error_message' => __( 'Please enter your zip / postal code.', 'give' ),
486
		);
487
		$required_fields['card_city']       = array(
488
			'error_id'      => 'invalid_city',
489
			'error_message' => __( 'Please enter your billing city.', 'give' ),
490
		);
491
		$required_fields['billing_country'] = array(
492
			'error_id'      => 'invalid_country',
493
			'error_message' => __( 'Please select your billing country.', 'give' ),
494
		);
495
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
496
497
		$required_fields['card_state'] = array(
498
			'error_id'      => 'invalid_state',
499
			'error_message' => __( 'Please enter billing state / province / County.', 'give' ),
500
		);
501
502
		// Check if billing country alredy exists.
503
		if ( ! empty( $_POST['billing_country'] ) ) {
504
			// Get the value from $_POST.
505
			$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...
506
507
			// Get the country list that does not required any states init.
508
			$states_country = give_states_not_required_country_list();
509
510
			// Check if states is empty or not.
511
			if ( array_key_exists( $country, $states_country ) ) {
512
				// If states is empty remove the required feilds of state in billing cart.
513
				unset( $required_fields['card_state'] );
514
			}
515
		}
516
	}
517
518
	/**
519
	 * Filters the donation form required field.
520
	 *
521
	 * @since 1.7
522
	 */
523
	$required_fields = apply_filters( 'give_donation_form_required_fields', $required_fields, $form_id );
524
525
	return $required_fields;
526
527
}
528
529
/**
530
 * Check if the Billing Address is required
531
 *
532
 * @since  1.0.1
533
 *
534
 * @param string $payment_mode
535
 *
536
 * @return bool
537
 */
538
function give_require_billing_address( $payment_mode ) {
539
540
	$return = false;
541
542
	if ( isset( $_POST['billing_country'] ) || did_action( "give_{$payment_mode}_cc_form" ) || did_action( 'give_cc_form' ) ) {
543
		$return = true;
544
	}
545
546
	// Let payment gateways and other extensions determine if address fields should be required.
547
	return apply_filters( 'give_require_billing_address', $return );
548
549
}
550
551
/**
552
 * Donation Form Validate Logged In User.
553
 *
554
 * @access      private
555
 * @since       1.0
556
 * @return      array
557
 */
558
function give_donation_form_validate_logged_in_user() {
559
	global $user_ID;
560
561
	$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...
562
563
	// Start empty array to collect valid user data.
564
	$valid_user_data = array(
565
		// Assume there will be errors.
566
		'user_id' => - 1,
567
	);
568
569
	// Verify there is a user_ID.
570
	if ( $user_ID > 0 ) {
571
		// Get the logged in user data.
572
		$user_data = get_userdata( $user_ID );
573
574
		// Loop through required fields and show error messages.
575 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...
576
			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...
577
				give_set_error( $value['error_id'], $value['error_message'] );
578
			}
579
		}
580
581
		// Verify data.
582
		if ( $user_data ) {
583
			// Collected logged in user data.
584
			$valid_user_data = array(
585
				'user_id'    => $user_ID,
586
				'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...
587
				'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...
588
				'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...
589
			);
590
591
			if ( ! is_email( $valid_user_data['user_email'] ) ) {
592
				give_set_error( 'email_invalid', esc_html__( 'Invalid email.', 'give' ) );
593
			}
594
		} else {
595
			// Set invalid user error.
596
			give_set_error( 'invalid_user', esc_html__( 'The user information is invalid.', 'give' ) );
597
		}
598
	}
599
600
	// Return user data.
601
	return $valid_user_data;
602
}
603
604
/**
605
 * Donate Form Validate New User
606
 *
607
 * @access      private
608
 * @since       1.0
609
 * @return      array
610
 */
611
function give_donation_form_validate_new_user() {
612
	// Default user data.
613
	$default_user_data = array(
614
		'give-form-id'           => '',
615
		'user_id'                => - 1, // Assume there will be errors.
616
		'user_first'             => '',
617
		'user_last'              => '',
618
		'give_user_login'        => false,
619
		'give_email'             => false,
620
		'give_user_pass'         => false,
621
		'give_user_pass_confirm' => false,
622
	);
623
624
	// Get user data.
625
	$user_data            = wp_parse_args( array_map( 'trim', 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...
626
	$registering_new_user = false;
627
	$form_id              = absint( $user_data['give-form-id'] );
628
629
	// Start an empty array to collect valid user data.
630
	$valid_user_data = array(
631
		// Assume there will be errors.
632
		'user_id'    => - 1,
633
634
		// Get first name.
635
		'user_first' => $user_data['give_first'],
636
637
		// Get last name.
638
		'user_last'  => $user_data['give_last'],
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
	// Check if we have an username to register.
649
	if ( give_validate_username( $user_data['give_user_login'] ) ) {
650
		$registering_new_user          = true;
651
		$valid_user_data['user_login'] = $user_data['give_user_login'];
652
	}
653
654
	// Check if we have an email to verify.
655
	if ( give_validate_user_email( $user_data['give_email'], $registering_new_user ) ) {
656
		$valid_user_data['user_email'] = $user_data['give_email'];
657
	}
658
659
	// Check password.
660
	if ( give_validate_user_password( $user_data['give_user_pass'], $user_data['give_user_pass_confirm'], $registering_new_user ) ) {
661
		// All is good to go.
662
		$valid_user_data['user_pass'] = $user_data['give_user_pass'];
663
	}
664
665
	return $valid_user_data;
666
}
667
668
/**
669
 * Donation Form Validate User Login
670
 *
671
 * @access      private
672
 * @since       1.0
673
 * @return      array
674
 */
675
function give_donation_form_validate_user_login() {
676
677
	// Start an array to collect valid user data.
678
	$valid_user_data = array(
679
		// Assume there will be errors.
680
		'user_id' => - 1,
681
	);
682
683
	// Username.
684
	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...
685
		give_set_error( 'must_log_in', __( 'You must register or login to complete your donation.', 'give' ) );
686
687
		return $valid_user_data;
688
	}
689
690
	// Get the user by login.
691
	$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...
692
693
	// Check if user exists.
694
	if ( $user_data ) {
695
		// Get password.
696
		$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...
697
698
		// Check user_pass.
699
		if ( $user_pass ) {
700
			// Check if password is valid.
701
			if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) {
702
				// Incorrect password.
703
				give_set_error(
704
					'password_incorrect',
705
					sprintf(
706
						'%1$s <a href="%2$s">%3$s</a>',
707
						__( 'The password you entered is incorrect.', 'give' ),
708
						wp_lostpassword_url( "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ),
709
						__( 'Reset Password', 'give' )
710
					)
711
				);
712
				// All is correct.
713
			} else {
714
				// Repopulate the valid user data array.
715
				$valid_user_data = array(
716
					'user_id'    => $user_data->ID,
717
					'user_login' => $user_data->user_login,
718
					'user_email' => $user_data->user_email,
719
					'user_first' => $user_data->first_name,
720
					'user_last'  => $user_data->last_name,
721
					'user_pass'  => $user_pass,
722
				);
723
			}
724
		} else {
725
			// Empty password.
726
			give_set_error( 'password_empty', __( 'Enter a password.', 'give' ) );
727
		}
728
	} else {
729
		// No username.
730
		give_set_error( 'username_incorrect', __( 'The username you entered does not exist.', 'give' ) );
731
	}// End if().
732
733
	return $valid_user_data;
734
}
735
736
/**
737
 * Donation Form Validate Guest User
738
 *
739
 * @access  private
740
 * @since   1.0
741
 * @return  array
742
 */
743
function give_donation_form_validate_guest_user() {
744
745
	$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...
746
747
	// Start an array to collect valid user data.
748
	$valid_user_data = array(
749
		// Set a default id for guests.
750
		'user_id' => 0,
751
	);
752
753
	// Show error message if user must be logged in.
754
	if ( give_logged_in_only( $form_id ) ) {
755
		give_set_error( 'logged_in_only', __( 'You must be logged in to donate.', 'give' ) );
756
	}
757
758
	// Get the guest email.
759
	$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...
760
761
	// Check email.
762
	if ( $guest_email && strlen( $guest_email ) > 0 ) {
763
		// Validate email.
764
		if ( ! is_email( $guest_email ) ) {
765
			// Invalid email.
766
			give_set_error( 'email_invalid', __( 'Invalid email.', 'give' ) );
767
		} else {
768
			// All is good to go.
769
			$valid_user_data['user_email'] = $guest_email;
770
771
			// Get user_id from donor if exist.
772
			$donor = new Give_Donor( $guest_email );
773
			if ( $donor->id && $donor->user_id ) {
774
				$valid_user_data['user_id'] = $donor->user_id;
775
			}
776
		}
777
	} else {
778
		// No email.
779
		give_set_error( 'email_empty', __( 'Enter an email.', 'give' ) );
780
	}
781
782
	// Loop through required fields and show error messages.
783 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...
784
		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...
785
			give_set_error( $value['error_id'], $value['error_message'] );
786
		}
787
	}
788
789
	return $valid_user_data;
790
}
791
792
/**
793
 * Register And Login New User
794
 *
795
 * @param array $user_data
796
 *
797
 * @access  private
798
 * @since   1.0
799
 * @return  integer
800
 */
801
function give_register_and_login_new_user( $user_data = array() ) {
802
	// Verify the array.
803
	if ( empty( $user_data ) ) {
804
		return - 1;
805
	}
806
807
	if ( give_get_errors() ) {
808
		return - 1;
809
	}
810
811
	$user_args = apply_filters( 'give_insert_user_args', array(
812
		'user_login'      => isset( $user_data['user_login'] ) ? $user_data['user_login'] : '',
813
		'user_pass'       => isset( $user_data['user_pass'] ) ? $user_data['user_pass'] : '',
814
		'user_email'      => isset( $user_data['user_email'] ) ? $user_data['user_email'] : '',
815
		'first_name'      => isset( $user_data['user_first'] ) ? $user_data['user_first'] : '',
816
		'last_name'       => isset( $user_data['user_last'] ) ? $user_data['user_last'] : '',
817
		'user_registered' => date( 'Y-m-d H:i:s' ),
818
		'role'            => give_get_option( 'donor_default_user_role', 'give_donor' ),
819
	), $user_data );
820
821
	// Insert new user.
822
	$user_id = wp_insert_user( $user_args );
823
824
	// Validate inserted user.
825
	if ( is_wp_error( $user_id ) ) {
826
		return - 1;
827
	}
828
829
	// Allow themes and plugins to filter the user data.
830
	$user_data = apply_filters( 'give_insert_user_data', $user_data, $user_args );
831
832
	/**
833
	 * Fires after inserting user.
834
	 *
835
	 * @since 1.0
836
	 *
837
	 * @param int $user_id User id.
838
	 * @param array $user_data Array containing user data.
839
	 */
840
	do_action( 'give_insert_user', $user_id, $user_data );
841
842
	/**
843
	 * Filter allow user to alter if user when to login or not when user is register for the first time.
844
	 *
845
	 * @since 1.8.13
846
	 *
847
	 * return bool True if login with registration and False if only want to register.
848
	 */
849
	if ( true === (bool) apply_filters( 'give_log_user_in_on_register', true ) ) {
850
		// Login new user.
851
		give_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] );
852
	}
853
854
	// Return user id.
855
	return $user_id;
856
}
857
858
/**
859
 * Get Donation Form User
860
 *
861
 * @param array $valid_data
862
 *
863
 * @access  private
864
 * @since   1.0
865
 * @return  array|bool
866
 */
867
function give_get_donation_form_user( $valid_data = array() ) {
868
869
	// Initialize user.
870
	$user    = false;
871
	$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
872
873
	if ( $is_ajax ) {
874
		// Do not create or login the user during the ajax submission (check for errors only).
875
		return true;
876
	} elseif ( is_user_logged_in() ) {
877
		// Set the valid user as the logged in collected data.
878
		$user = $valid_data['logged_in_user'];
879
	} 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...
880
		// New user registration.
881
		if ( $valid_data['need_new_user'] === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
882
			// Set user.
883
			$user = $valid_data['new_user_data'];
884
			// Register and login new user.
885
			$user['user_id'] = give_register_and_login_new_user( $user );
886
			// User login
887
		} 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...
888
889
			/**
890
			 * The login form is now processed in the give_process_donation_login() function.
891
			 * This is still here for backwards compatibility.
892
			 * This also allows the old login process to still work if a user removes the checkout login submit button.
893
			 *
894
			 * 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.
895
			 */
896
			// Set user.
897
			$user = $valid_data['login_user_data'];
898
			// Login user.
899
			give_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] );
900
		}
901
	}
902
903
	// Check guest checkout.
904
	if ( false === $user && false === give_logged_in_only( $_POST['give-form-id'] ) ) {
905
		// Set user
906
		$user = $valid_data['guest_user_data'];
907
	}
908
909
	// Verify we have an user.
910
	if ( false === $user || empty( $user ) ) {
911
		// Return false.
912
		return false;
913
	}
914
915
	// Get user first name.
916 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...
917
		$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...
918
	}
919
920
	// Get user last name.
921 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...
922
		$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...
923
	}
924
925
	// Get the user's billing address details.
926
	$user['address']            = array();
927
	$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...
928
	$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...
929
	$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...
930
	$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...
931
	$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...
932
	$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...
933
934
	if ( empty( $user['address']['country'] ) ) {
935
		$user['address'] = false;
936
	} // End if().
937
938
	if ( ! empty( $user['user_id'] ) && $user['user_id'] > 0 && ! empty( $user['address'] ) ) {
939
		// Store the address in the user's meta so the donation form can be pre-populated with it on return donation.
940
		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...
941
	}
942
943
	// Return valid user.
944
	return $user;
945
}
946
947
/**
948
 * Validates the credit card info.
949
 *
950
 * @access  private
951
 * @since   1.0
952
 * @return  array
953
 */
954
function give_donation_form_validate_cc() {
955
956
	$card_data = give_get_donation_cc_info();
957
958
	// Validate the card zip.
959
	if ( ! empty( $card_data['card_zip'] ) ) {
960
		if ( ! give_donation_form_validate_cc_zip( $card_data['card_zip'], $card_data['card_country'] ) ) {
961
			give_set_error( 'invalid_cc_zip', __( 'The zip / postal code you entered for your billing address is invalid.', 'give' ) );
962
		}
963
	}
964
965
	// Ensure no spaces.
966
	if ( ! empty( $card_data['card_number'] ) ) {
967
		$card_data['card_number'] = str_replace( '+', '', $card_data['card_number'] ); // no "+" signs
968
		$card_data['card_number'] = str_replace( ' ', '', $card_data['card_number'] ); // No spaces
969
	}
970
971
	// This should validate card numbers at some point too.
972
	return $card_data;
973
}
974
975
/**
976
 * Get credit card info.
977
 *
978
 * @access  private
979
 * @since   1.0
980
 * @return  array
981
 */
982
function give_get_donation_cc_info() {
983
984
	$cc_info                   = array();
985
	$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...
986
	$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...
987
	$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...
988
	$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...
989
	$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...
990
	$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...
991
	$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...
992
	$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...
993
	$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...
994
	$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...
995
	$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...
996
997
	// Return cc info.
998
	return $cc_info;
999
}
1000
1001
/**
1002
 * Validate zip code based on country code
1003
 *
1004
 * @since  1.0
1005
 *
1006
 * @param int $zip
1007
 * @param string $country_code
1008
 *
1009
 * @return bool|mixed
1010
 */
1011
function give_donation_form_validate_cc_zip( $zip = 0, $country_code = '' ) {
1012
	$ret = false;
1013
1014
	if ( empty( $zip ) || empty( $country_code ) ) {
1015
		return $ret;
1016
	}
1017
1018
	$country_code = strtoupper( $country_code );
1019
1020
	$zip_regex = array(
1021
		'AD' => 'AD\d{3}',
1022
		'AM' => '(37)?\d{4}',
1023
		'AR' => '^([A-Z]{1}\d{4}[A-Z]{3}|[A-Z]{1}\d{4}|\d{4})$',
1024
		'AS' => '96799',
1025
		'AT' => '\d{4}',
1026
		'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})$',
1027
		'AX' => '22\d{3}',
1028
		'AZ' => '\d{4}',
1029
		'BA' => '\d{5}',
1030
		'BB' => '(BB\d{5})?',
1031
		'BD' => '\d{4}',
1032
		'BE' => '^[1-9]{1}[0-9]{3}$',
1033
		'BG' => '\d{4}',
1034
		'BH' => '((1[0-2]|[2-9])\d{2})?',
1035
		'BM' => '[A-Z]{2}[ ]?[A-Z0-9]{2}',
1036
		'BN' => '[A-Z]{2}[ ]?\d{4}',
1037
		'BR' => '\d{5}[\-]?\d{3}',
1038
		'BY' => '\d{6}',
1039
		'CA' => '^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$',
1040
		'CC' => '6799',
1041
		'CH' => '^[1-9][0-9][0-9][0-9]$',
1042
		'CK' => '\d{4}',
1043
		'CL' => '\d{7}',
1044
		'CN' => '\d{6}',
1045
		'CR' => '\d{4,5}|\d{3}-\d{4}',
1046
		'CS' => '\d{5}',
1047
		'CV' => '\d{4}',
1048
		'CX' => '6798',
1049
		'CY' => '\d{4}',
1050
		'CZ' => '\d{3}[ ]?\d{2}',
1051
		'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',
1052
		'DK' => '^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$',
1053
		'DO' => '\d{5}',
1054
		'DZ' => '\d{5}',
1055
		'EC' => '([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?',
1056
		'EE' => '\d{5}',
1057
		'EG' => '\d{5}',
1058
		'ES' => '^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$',
1059
		'ET' => '\d{4}',
1060
		'FI' => '\d{5}',
1061
		'FK' => 'FIQQ 1ZZ',
1062
		'FM' => '(9694[1-4])([ \-]\d{4})?',
1063
		'FO' => '\d{3}',
1064
		'FR' => '^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$',
1065
		'GE' => '\d{4}',
1066
		'GF' => '9[78]3\d{2}',
1067
		'GL' => '39\d{2}',
1068
		'GN' => '\d{3}',
1069
		'GP' => '9[78][01]\d{2}',
1070
		'GR' => '\d{3}[ ]?\d{2}',
1071
		'GS' => 'SIQQ 1ZZ',
1072
		'GT' => '\d{5}',
1073
		'GU' => '969[123]\d([ \-]\d{4})?',
1074
		'GW' => '\d{4}',
1075
		'HM' => '\d{4}',
1076
		'HN' => '(?:\d{5})?',
1077
		'HR' => '\d{5}',
1078
		'HT' => '\d{4}',
1079
		'HU' => '\d{4}',
1080
		'ID' => '\d{5}',
1081
		'IE' => '((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?',
1082
		'IL' => '\d{5}',
1083
		'IN' => '^[1-9][0-9][0-9][0-9][0-9][0-9]$', // india
1084
		'IO' => 'BBND 1ZZ',
1085
		'IQ' => '\d{5}',
1086
		'IS' => '\d{3}',
1087
		'IT' => '^(V-|I-)?[0-9]{5}$',
1088
		'JO' => '\d{5}',
1089
		'JP' => '\d{3}-\d{4}',
1090
		'KE' => '\d{5}',
1091
		'KG' => '\d{6}',
1092
		'KH' => '\d{5}',
1093
		'KR' => '\d{3}[\-]\d{3}',
1094
		'KW' => '\d{5}',
1095
		'KZ' => '\d{6}',
1096
		'LA' => '\d{5}',
1097
		'LB' => '(\d{4}([ ]?\d{4})?)?',
1098
		'LI' => '(948[5-9])|(949[0-7])',
1099
		'LK' => '\d{5}',
1100
		'LR' => '\d{4}',
1101
		'LS' => '\d{3}',
1102
		'LT' => '\d{5}',
1103
		'LU' => '\d{4}',
1104
		'LV' => '\d{4}',
1105
		'MA' => '\d{5}',
1106
		'MC' => '980\d{2}',
1107
		'MD' => '\d{4}',
1108
		'ME' => '8\d{4}',
1109
		'MG' => '\d{3}',
1110
		'MH' => '969[67]\d([ \-]\d{4})?',
1111
		'MK' => '\d{4}',
1112
		'MN' => '\d{6}',
1113
		'MP' => '9695[012]([ \-]\d{4})?',
1114
		'MQ' => '9[78]2\d{2}',
1115
		'MT' => '[A-Z]{3}[ ]?\d{2,4}',
1116
		'MU' => '(\d{3}[A-Z]{2}\d{3})?',
1117
		'MV' => '\d{5}',
1118
		'MX' => '\d{5}',
1119
		'MY' => '\d{5}',
1120
		'NC' => '988\d{2}',
1121
		'NE' => '\d{4}',
1122
		'NF' => '2899',
1123
		'NG' => '(\d{6})?',
1124
		'NI' => '((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?',
1125
		'NL' => '^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$',
1126
		'NO' => '\d{4}',
1127
		'NP' => '\d{5}',
1128
		'NZ' => '\d{4}',
1129
		'OM' => '(PC )?\d{3}',
1130
		'PF' => '987\d{2}',
1131
		'PG' => '\d{3}',
1132
		'PH' => '\d{4}',
1133
		'PK' => '\d{5}',
1134
		'PL' => '\d{2}-\d{3}',
1135
		'PM' => '9[78]5\d{2}',
1136
		'PN' => 'PCRN 1ZZ',
1137
		'PR' => '00[679]\d{2}([ \-]\d{4})?',
1138
		'PT' => '\d{4}([\-]\d{3})?',
1139
		'PW' => '96940',
1140
		'PY' => '\d{4}',
1141
		'RE' => '9[78]4\d{2}',
1142
		'RO' => '\d{6}',
1143
		'RS' => '\d{5}',
1144
		'RU' => '\d{6}',
1145
		'SA' => '\d{5}',
1146
		'SE' => '^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$',
1147
		'SG' => '\d{6}',
1148
		'SH' => '(ASCN|STHL) 1ZZ',
1149
		'SI' => '\d{4}',
1150
		'SJ' => '\d{4}',
1151
		'SK' => '\d{3}[ ]?\d{2}',
1152
		'SM' => '4789\d',
1153
		'SN' => '\d{5}',
1154
		'SO' => '\d{5}',
1155
		'SZ' => '[HLMS]\d{3}',
1156
		'TC' => 'TKCA 1ZZ',
1157
		'TH' => '\d{5}',
1158
		'TJ' => '\d{6}',
1159
		'TM' => '\d{6}',
1160
		'TN' => '\d{4}',
1161
		'TR' => '\d{5}',
1162
		'TW' => '\d{3}(\d{2})?',
1163
		'UA' => '\d{5}',
1164
		'UK' => '^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$',
1165
		'US' => '^\d{5}([\-]?\d{4})?$',
1166
		'UY' => '\d{5}',
1167
		'UZ' => '\d{6}',
1168
		'VA' => '00120',
1169
		'VE' => '\d{4}',
1170
		'VI' => '008(([0-4]\d)|(5[01]))([ \-]\d{4})?',
1171
		'WF' => '986\d{2}',
1172
		'YT' => '976\d{2}',
1173
		'YU' => '\d{5}',
1174
		'ZA' => '\d{4}',
1175
		'ZM' => '\d{5}',
1176
	);
1177
1178
	if ( ! isset( $zip_regex[ $country_code ] ) || preg_match( '/' . $zip_regex[ $country_code ] . '/i', $zip ) ) {
1179
		$ret = true;
1180
	}
1181
1182
	return apply_filters( 'give_is_zip_valid', $ret, $zip, $country_code );
1183
}
1184
1185
1186
/**
1187
 * Auto set correct donation level id on basis of amount.
1188
 *
1189
 * 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.
1190
 *
1191
 * @param array $valid_data
1192
 * @param array $data
1193
 *
1194
 * @return bool
1195
 */
1196
function give_validate_multi_donation_form_level( $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...
1197
	/* @var Give_Donate_Form $form */
1198
	$form = new Give_Donate_Form( $data['give-form-id'] );
1199
1200
	$donation_level_matched = false;
1201
1202
	if ( $form->is_multi_type_donation_form() ) {
1203
1204
		// Bailout.
1205
		if ( ! ( $variable_prices = $form->get_prices() ) ) {
1206
			return false;
1207
		}
1208
1209
		// Sanitize donation amount.
1210
		$data['give-amount'] = give_maybe_sanitize_amount( $data['give-amount'] );
1211
1212
		if ( $data['give-amount'] === give_maybe_sanitize_amount( give_get_price_option_amount( $data['give-form-id'], $data['give-price-id'] ) ) ) {
1213
			return true;
1214
		}
1215
1216
		// Find correct donation level from all donation levels.
1217
		foreach ( $variable_prices as $variable_price ) {
1218
			// Sanitize level amount.
1219
			$variable_price['_give_amount'] = give_maybe_sanitize_amount( $variable_price['_give_amount'] );
1220
1221
			// Set first match donation level ID.
1222
			if ( $data['give-amount'] === $variable_price['_give_amount'] ) {
1223
				$_POST['give-price-id'] = $variable_price['_give_id']['level_id'];
1224
				$donation_level_matched = true;
1225
				break;
1226
			}
1227
		}
1228
1229
		// If donation amount is not find in donation levels then check if form has custom donation feature enable or not.
1230
		// If yes then set price id to custom if amount is greater then custom minimum amount (if any).
1231
		if (
1232
			! $donation_level_matched
1233
			&& ( give_is_setting_enabled( give_get_meta( $data['give-form-id'], '_give_custom_amount', true ) ) )
1234
		) {
1235
			// Sanitize custom minimum amount.
1236
			$custom_minimum_amount = give_maybe_sanitize_amount( give_get_meta( $data['give-form-id'], '_give_custom_amount_minimum', true ) );
1237
1238
			if ( $data['give-amount'] >= $custom_minimum_amount ) {
1239
				$_POST['give-price-id'] = 'custom';
1240
				$donation_level_matched = true;
1241
			}
1242
		}
1243
	}// End if().
1244
1245
	return ( $donation_level_matched ? true : false );
1246
}
1247
1248
add_action( 'give_checkout_error_checks', 'give_validate_multi_donation_form_level', 10, 2 );
1249