Completed
Pull Request — master (#1749)
by Devin
06:21
created

process-donation.php ➔ give_get_purchase_cc_info()   C

Complexity

Conditions 12
Paths 2048

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 14
nc 2048
nop 0
dl 0
loc 17
rs 5.3132
c 0
b 0
f 0

How to fix   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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 );
49
50
	$is_ajax = isset( $_POST['give_ajax'] );
51
52
	// Process the login form
53
	if ( isset( $_POST['give_login_submit'] ) ) {
54
		give_process_form_login();
55
	}
56
57
	// Validate the user
58
	$user = give_get_donation_form_user( $valid_data );
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'] ) ? (float) apply_filters( 'give_donation_total', give_sanitize_amount( give_format_amount( $_POST['give-amount'] ) ) ) : '0.00';
103
	$purchase_key = strtolower( md5( $user['user_email'] . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'give', true ) ) );
104
105
	// Setup donation information
106
	$donation_data = array(
107
		'price'        => $price,
108
		'purchase_key' => $purchase_key,
109
		'user_email'   => $user['user_email'],
110
		'date'         => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
111
		'user_info'    => stripslashes_deep( $user_info ),
112
		'post_data'    => $_POST,
113
		'gateway'      => $valid_data['gateway'],
114
		'card_info'    => $valid_data['cc_info'],
115
	);
116
117
	// Add the user data for hooks
118
	$valid_data['user'] = $user;
119
120
	/**
121
	 * Fires before donation form gateway.
122
	 *
123
	 * Allow you to hook to donation form before the gateway.
124
	 *
125
	 * @since 1.0
126
	 *
127
	 * @param array $_POST Array of variables passed via the HTTP POST.
128
	 * @param array $user_info Array containing basic user information.
129
	 * @param bool|array $valid_data Validate fields.
130
	 */
131
	do_action( 'give_checkout_before_gateway', $_POST, $user_info, $valid_data );
132
133
	// Sanity check for price
134
	if ( ! $donation_data['price'] ) {
135
		// Revert to manual
136
		$donation_data['gateway'] = 'manual';
137
		$_POST['give-gateway']    = 'manual';
138
	}
139
140
	/**
141
	 * Allow the donation data to be modified before it is sent to the gateway.
142
	 *
143
	 * @since 1.7
144
	 */
145
	$donation_data = apply_filters( 'give_donation_data_before_gateway', $donation_data, $valid_data );
146
147
	// Setup the data we're storing in the donation session
148
	$session_data = $donation_data;
149
150
	// Make sure credit card numbers are never stored in sessions
151
	unset( $session_data['card_info']['card_number'] );
152
	unset( $session_data['post_data']['card_number'] );
153
154
	// Used for showing data to non logged-in users after donation, and for other plugins needing donation data.
155
	give_set_purchase_session( $session_data );
156
157
	// Send info to the gateway for payment processing
158
	give_send_to_gateway( $donation_data['gateway'], $donation_data );
159
	give_die();
160
161
}
162
163
add_action( 'give_purchase', 'give_process_donation_form' );
164
add_action( 'wp_ajax_give_process_donation', 'give_process_donation_form' );
165
add_action( 'wp_ajax_nopriv_give_process_donation', 'give_process_donation_form' );
166
167
168
/**
169
 * Verify that when a logged in user makes a donation that the email address used doesn't belong to a different customer.
170
 *
171
 * @since  1.7
172
 *
173
 * @param  array $valid_data Validated data submitted for the donation.
174
 * @param  array $post Additional $_POST data submitted
175
 *
176
 * @return void
177
 */
178
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...
179
180
	// Verify that the email address belongs to this customer.
181
	if ( is_user_logged_in() ) {
182
183
		$submitted_email    = $valid_data['logged_in_user']['user_email'];
184
		$customer = new Give_Customer( get_current_user_id(), true );
185
186
		// If this email address is not registered with this customer, see if it belongs to any other customer
187
		if (
188
			$submitted_email !== $customer->email
189
			&& ( is_array( $customer->emails ) && ! in_array( $submitted_email, $customer->emails ) )
190
		) {
191
			$found_customer = new Give_Customer( $submitted_email );
192
193
			if ( $found_customer->id > 0 ) {
194
				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.' ), $customer->email, $submitted_email ) );
195
			}
196
		}
197
	}
198
}
199
200
add_action( 'give_checkout_error_checks', 'give_check_logged_in_user_for_existing_email', 10, 2 );
201
202
/**
203
 * Process the checkout login form
204
 *
205
 * @access      private
206
 * @since       1.0
207
 * @return      void
208
 */
209
function give_process_form_login() {
210
211
	$is_ajax = isset( $_POST['give_ajax'] );
212
213
	$user_data = give_donation_form_validate_user_login();
214
215
	if ( give_get_errors() || $user_data['user_id'] < 1 ) {
216
		if ( $is_ajax ) {
217
			/**
218
			 * Fires when AJAX sends back errors from the donation form.
219
			 *
220
			 * @since 1.0
221
			 */
222
			do_action( 'give_ajax_donation_errors' );
223
			give_die();
224
		} else {
225
			wp_redirect( $_SERVER['HTTP_REFERER'] );
226
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_process_form_login() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
227
		}
228
	}
229
230
	give_log_user_in( $user_data['user_id'], $user_data['user_login'], $user_data['user_pass'] );
231
232
	if ( $is_ajax ) {
233
		echo 'success';
234
		give_die();
235
	} else {
236
		wp_redirect( $_SERVER['HTTP_REFERER'] );
237
	}
238
}
239
240
add_action( 'wp_ajax_give_process_donation_login', 'give_process_form_login' );
241
add_action( 'wp_ajax_nopriv_give_process_donation_login', 'give_process_form_login' );
242
243
/**
244
 * Donation Form Validate Fields.
245
 *
246
 * @access      private
247
 * @since       1.0
248
 * @return      bool|array
249
 */
250
function give_donation_form_validate_fields() {
251
252
	// Check if there is $_POST
253
	if ( empty( $_POST ) ) {
254
		return false;
255
	}
256
257
	$form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
258
259
	// Start an array to collect valid data
260
	$valid_data = array(
261
		'gateway'          => give_donation_form_validate_gateway(), // Gateway fallback (amount is validated here)
262
		'need_new_user'    => false,     // New user flag
263
		'need_user_login'  => false,     // Login user flag
264
		'logged_user_data' => array(),   // Logged user collected data
265
		'new_user_data'    => array(),   // New user collected data
266
		'login_user_data'  => array(),   // Login user collected data
267
		'guest_user_data'  => array(),   // Guest user collected data
268
		'cc_info'          => give_donation_form_validate_cc(),// Credit card info
269
	);
270
271
	// Validate Honeypot First
272
	if ( ! empty( $_POST['give-honeypot'] ) ) {
273
		give_set_error( 'invalid_honeypot', esc_html__( 'Honeypot field detected. Go away bad bot!', 'give' ) );
274
	}
275
276
	// Validate agree to terms
277
	if ( give_is_terms_enabled( $form_id ) ) {
278
		give_donation_form_validate_agree_to_terms();
279
	}
280
281
	// Stop processing donor registration, if donor registration is optional and donor can do guest checkout.
282
	// If registration form username field is empty that means donor do not want to registration instead want guest checkout.
283
	if (
284
		! give_logged_in_only( $form_id )
285
		&& isset( $_POST['give-purchase-var'] )
286
		&& $_POST['give-purchase-var'] == 'needs-to-register'
287
		&& empty( $_POST['give_user_login'] )
288
	) {
289
		unset( $_POST['give-purchase-var'] );
290
	}
291
292
	if ( is_user_logged_in() ) {
293
		// Collect logged in user data.
294
		$valid_data['logged_in_user'] = give_donation_form_validate_logged_in_user();
295
	} elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-register' ) {
296
		// Set new user registration as required.
297
		$valid_data['need_new_user'] = true;
298
		// Validate new user data.
299
		$valid_data['new_user_data'] = give_donation_form_validate_new_user();
300
		// Check if login validation is needed.
301
	} elseif ( isset( $_POST['give-purchase-var'] ) && $_POST['give-purchase-var'] == 'needs-to-login' ) {
302
		// Set user login as required.
303
		$valid_data['need_user_login'] = true;
304
		// Validate users login info.
305
		$valid_data['login_user_data'] = give_donation_form_validate_user_login();
306
	} else {
307
		// Not registering or logging in, so setup guest user data.
308
		$valid_data['guest_user_data'] = give_donation_form_validate_guest_user();
309
	}
310
311
	// Return collected data.
312
	return $valid_data;
313
}
314
315
/**
316
 * Donation Form Validate Gateway
317
 *
318
 * Validate the gateway and donation amount.
319
 *
320
 * @access      private
321
 * @since       1.0
322
 * @return      string
323
 */
324
function give_donation_form_validate_gateway() {
325
326
	$form_id = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0;
327
	$amount  = isset( $_REQUEST['give-amount'] ) ? give_sanitize_amount( $_REQUEST['give-amount'] ) : 0;
328
	$gateway = give_get_default_gateway( $form_id );
329
330
	// Check if a gateway value is present.
331
	if ( ! empty( $_REQUEST['give-gateway'] ) ) {
332
333
		$gateway = sanitize_text_field( $_REQUEST['give-gateway'] );
334
335
		// Is amount being donated in LIVE mode 0.00? If so, error:
336
		if ( $amount == 0 && ! give_is_test_mode() ) {
337
338
			give_set_error( 'invalid_donation_amount', __( 'Please insert a valid donation amount.', 'give' ) );
339
340
		} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
341
		elseif ( ! give_verify_minimum_price() ) {
342
			// translators: %s: minimum donation amount.
343
			give_set_error(
344
				'invalid_donation_minimum',
345
				sprintf(
346
					/* translators: %s: minimum donation amount */
347
					__( 'This form has a minimum donation amount of %s.', 'give' ),
348
					give_currency_filter( give_format_amount( give_get_form_minimum_price( $form_id ) ) )
349
				)
350
			);
351
352
		} //Is this test mode zero donation? Let it through but set to manual gateway.
353
		elseif ( $amount == 0 && give_is_test_mode() ) {
354
355
			$gateway = 'manual';
356
357
		} //Check if this gateway is active.
358
		elseif ( ! give_is_gateway_active( $gateway ) ) {
359
360
			give_set_error( 'invalid_gateway', __( 'The selected payment gateway is not enabled.', 'give' ) );
361
362
		}
363
	}
364
365
	return $gateway;
366
367
}
368
369
/**
370
 * Donation Form Validate Minimum Donation Amount
371
 *
372
 * @access      private
373
 * @since       1.3.6
374
 * @return      bool
375
 */
376
function give_verify_minimum_price() {
377
378
	$amount          = give_sanitize_amount( $_REQUEST['give-amount'] );
379
	$form_id         = isset( $_REQUEST['give-form-id'] ) ? $_REQUEST['give-form-id'] : 0;
380
	$price_id        = isset( $_REQUEST['give-price-id'] ) ? $_REQUEST['give-price-id'] : null;
381
	$variable_prices = give_has_variable_prices( $form_id );
382
383
	if ( $variable_prices && in_array( $price_id, give_get_variable_price_ids( $form_id ) ) ) {
384
385
		$price_level_amount = give_get_price_option_amount( $form_id, $price_id );
386
387
		if ( $price_level_amount == $amount ) {
388
			return true;
389
		}
390
	}
391
392
	if ( give_get_form_minimum_price( $form_id ) > $amount ) {
393
		return false;
394
	}
395
396
	return true;
397
}
398
399
/**
400
 * Donation form validate agree to "Terms and Conditions".
401
 *
402
 * @access      private
403
 * @since       1.0
404
 * @return      void
405
 */
406
function give_donation_form_validate_agree_to_terms() {
407
	// Validate agree to terms.
408
	if ( ! isset( $_POST['give_agree_to_terms'] ) || $_POST['give_agree_to_terms'] != 1 ) {
409
		// User did not agree.
410
		give_set_error( 'agree_to_terms', apply_filters( 'give_agree_to_terms_text', __( 'You must agree to the terms and conditions.', 'give' ) ) );
411
	}
412
}
413
414
/**
415
 * Donation Form Required Fields.
416
 *
417
 * @access      private
418
 * @since       1.0
419
 *
420
 * @param       $form_id
421
 *
422
 * @return      array
423
 */
424
function give_get_required_fields( $form_id ) {
425
426
	$payment_mode = give_get_chosen_gateway( $form_id );
427
428
	$required_fields = array(
429
		'give_email' => array(
430
			'error_id'      => 'invalid_email',
431
			'error_message' => __( 'Please enter a valid email address.', 'give' ),
432
		),
433
		'give_first' => array(
434
			'error_id'      => 'invalid_first_name',
435
			'error_message' => __( 'Please enter your first name.', 'give' ),
436
		),
437
	);
438
439
	$require_address = give_require_billing_address( $payment_mode );
440
441
	if ( $require_address ) {
442
		$required_fields['card_address']    = array(
443
			'error_id'      => 'invalid_card_address',
444
			'error_message' => __( 'Please enter your primary billing address.', 'give' ),
445
		);
446
		$required_fields['card_zip']        = array(
447
			'error_id'      => 'invalid_zip_code',
448
			'error_message' => __( 'Please enter your zip / postal code.', 'give' ),
449
		);
450
		$required_fields['card_city']       = array(
451
			'error_id'      => 'invalid_city',
452
			'error_message' => __( 'Please enter your billing city.', 'give' ),
453
		);
454
		$required_fields['billing_country'] = array(
455
			'error_id'      => 'invalid_country',
456
			'error_message' => __( 'Please select your billing country.', 'give' ),
457
		);
458
		$required_fields['card_state']      = array(
459
			'error_id'      => 'invalid_state',
460
			'error_message' => __( 'Please enter billing state / province.', 'give' ),
461
		);
462
	}
463
464
	/**
465
	 * Filters the donation form required field.
466
	 *
467
	 * @since 1.7
468
	 */
469
	$required_fields = apply_filters( 'give_donation_form_required_fields', $required_fields, $form_id );
470
471
	return $required_fields;
472
473
}
474
475
/**
476
 * Check if the Billing Address is required
477
 *
478
 * @since  1.0.1
479
 *
480
 * @param string $payment_mode
481
 *
482
 * @return bool
483
 */
484
function give_require_billing_address( $payment_mode ) {
485
486
	$return = false;
487
488
	if ( isset( $_POST['billing_country'] ) || did_action( "give_{$payment_mode}_cc_form" ) || did_action( 'give_cc_form' ) ) {
489
		$return = true;
490
	}
491
492
	// Let payment gateways and other extensions determine if address fields should be required.
493
	return apply_filters( 'give_require_billing_address', $return );
494
495
}
496
497
/**
498
 * Donation Form Validate Logged In User.
499
 *
500
 * @access      private
501
 * @since       1.0
502
 * @return      array
503
 */
504
function give_donation_form_validate_logged_in_user() {
505
	global $user_ID;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
506
507
	$form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
508
509
	// Start empty array to collect valid user data.
510
	$valid_user_data = array(
511
		// Assume there will be errors.
512
		'user_id' => - 1,
513
	);
514
515
	// Verify there is a user_ID.
516
	if ( $user_ID > 0 ) {
517
		// Get the logged in user data.
518
		$user_data = get_userdata( $user_ID );
519
520
		// Loop through required fields and show error messages.
521
		foreach ( give_get_required_fields( $form_id ) as $field_name => $value ) {
522
			if ( in_array( $value, give_get_required_fields( $form_id ) ) && empty( $_POST[ $field_name ] ) ) {
523
				give_set_error( $value['error_id'], $value['error_message'] );
524
			}
525
		}
526
527
		// Verify data.
528
		if ( $user_data ) {
529
			// Collected logged in user data.
530
			$valid_user_data = array(
531
				'user_id'    => $user_ID,
532
				'user_email' => isset( $_POST['give_email'] ) ? sanitize_email( $_POST['give_email'] ) : $user_data->user_email,
533
				'user_first' => isset( $_POST['give_first'] ) && ! empty( $_POST['give_first'] ) ? sanitize_text_field( $_POST['give_first'] ) : $user_data->first_name,
534
				'user_last'  => isset( $_POST['give_last'] ) && ! empty( $_POST['give_last'] ) ? sanitize_text_field( $_POST['give_last'] ) : $user_data->last_name,
535
			);
536
537
			if ( ! is_email( $valid_user_data['user_email'] ) ) {
538
				give_set_error( 'email_invalid', esc_html__( 'Invalid email.', 'give' ) );
539
			}
540
		} else {
541
			// Set invalid user error.
542
			give_set_error( 'invalid_user', esc_html__( 'The user information is invalid.', 'give' ) );
543
		}
544
	}
545
546
	// Return user data.
547
	return $valid_user_data;
548
}
549
550
/**
551
 * Donate Form Validate New User
552
 *
553
 * @access      private
554
 * @since       1.0
555
 * @return      array
556
 */
557
function give_donation_form_validate_new_user() {
558
	// Default user data.
559
	$default_user_data = array(
560
		'give-form-id'           => '',
561
		'user_id'                => - 1, // Assume there will be errors.
562
		'user_first'             => '',
563
		'user_last'              => '',
564
		'give_user_login'        => false,
565
		'give_email'             => false,
566
		'give_user_pass'         => false,
567
		'give_user_pass_confirm' => false,
568
	);
569
570
	// Get user data.
571
	$user_data            = wp_parse_args( array_map( 'trim', give_clean( $_POST ) ), $default_user_data );
572
	$registering_new_user = false;
573
	$form_id              = absint( $user_data['give-form-id'] );
574
575
	// Start an empty array to collect valid user data.
576
	$valid_user_data = array(
577
		// Assume there will be errors.
578
		'user_id'    => - 1,
579
580
		// Get first name.
581
		'user_first' => $user_data['give_first'],
582
583
		// Get last name.
584
		'user_last'  => $user_data['give_last'],
585
	);
586
587
	// Loop through required fields and show error messages.
588
	foreach ( give_get_required_fields( $form_id ) as $field_name => $value ) {
589
		if ( in_array( $value, give_get_required_fields( $form_id ) ) && empty( $_POST[ $field_name ] ) ) {
590
			give_set_error( $value['error_id'], $value['error_message'] );
591
		}
592
	}
593
594
	// Check if we have an username to register.
595
	if ( give_validate_username( $user_data['give_user_login'] ) ) {
596
		$registering_new_user          = true;
597
		$valid_user_data['user_login'] = $user_data['give_user_login'];
598
	}
599
600
	// Check if we have an email to verify.
601
	if ( give_validate_user_email( $user_data['give_email'], $registering_new_user ) ) {
602
		$valid_user_data['user_email'] = $user_data['give_email'];
603
	}
604
605
	// Check password.
606
	if ( give_validate_user_password( $user_data['give_user_pass'], $user_data['give_user_pass_confirm'], $registering_new_user ) ) {
607
		// All is good to go.
608
		$valid_user_data['user_pass'] = $user_data['give_user_pass'];
609
	}
610
611
	return $valid_user_data;
612
}
613
614
/**
615
 * Donation Form Validate User Login
616
 *
617
 * @access      private
618
 * @since       1.0
619
 * @return      array
620
 */
621
function give_donation_form_validate_user_login() {
622
623
	// Start an array to collect valid user data.
624
	$valid_user_data = array(
625
		// Assume there will be errors.
626
		'user_id' => - 1,
627
	);
628
629
	// Username.
630
	if ( ! isset( $_POST['give_user_login'] ) || $_POST['give_user_login'] == '' ) {
631
		give_set_error( 'must_log_in', __( 'You must register or login to complete your donation.', 'give' ) );
632
633
		return $valid_user_data;
634
	}
635
636
	// Get the user by login.
637
	$user_data = get_user_by( 'login', strip_tags( $_POST['give_user_login'] ) );
638
639
	// Check if user exists.
640
	if ( $user_data ) {
641
		// Get password.
642
		$user_pass = isset( $_POST['give_user_pass'] ) ? $_POST['give_user_pass'] : false;
643
644
		// Check user_pass.
645
		if ( $user_pass ) {
646
			// Check if password is valid.
647
			if ( ! wp_check_password( $user_pass, $user_data->user_pass, $user_data->ID ) ) {
648
				// Incorrect password.
649
				give_set_error(
650
					'password_incorrect',
651
					sprintf(
652
						'%1$s <a href="%2$s">%3$s</a>',
653
						__( 'The password you entered is incorrect.', 'give' ),
654
						wp_lostpassword_url( "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ),
655
						__( 'Reset Password', 'give' )
656
					)
657
				);
658
				// All is correct.
659
			} else {
660
				// Repopulate the valid user data array.
661
				$valid_user_data = array(
662
					'user_id'    => $user_data->ID,
663
					'user_login' => $user_data->user_login,
664
					'user_email' => $user_data->user_email,
665
					'user_first' => $user_data->first_name,
666
					'user_last'  => $user_data->last_name,
667
					'user_pass'  => $user_pass,
668
				);
669
			}
670
		} else {
671
			// Empty password.
672
			give_set_error( 'password_empty', __( 'Enter a password.', 'give' ) );
673
		}
674
	} else {
675
		// No username.
676
		give_set_error( 'username_incorrect', __( 'The username you entered does not exist.', 'give' ) );
677
	}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
678
679
	return $valid_user_data;
680
}
681
682
/**
683
 * Donation Form Validate Guest User
684
 *
685
 * @access  private
686
 * @since   1.0
687
 * @return  array
688
 */
689
function give_donation_form_validate_guest_user() {
690
691
	$form_id = isset( $_POST['give-form-id'] ) ? $_POST['give-form-id'] : '';
692
693
	// Start an array to collect valid user data.
694
	$valid_user_data = array(
695
		// Set a default id for guests.
696
		'user_id' => 0,
697
	);
698
699
	// Show error message if user must be logged in.
700
	if ( give_logged_in_only( $form_id ) ) {
701
		give_set_error( 'logged_in_only', __( 'You must be logged in to donate.', 'give' ) );
702
	}
703
704
	// Get the guest email.
705
	$guest_email = isset( $_POST['give_email'] ) ? $_POST['give_email'] : false;
706
707
	// Check email.
708
	if ( $guest_email && strlen( $guest_email ) > 0 ) {
709
		// Validate email.
710
		if ( ! is_email( $guest_email ) ) {
711
			// Invalid email.
712
			give_set_error( 'email_invalid', __( 'Invalid email.', 'give' ) );
713
		} else {
714
			// All is good to go.
715
			$valid_user_data['user_email'] = $guest_email;
716
717
			// Get user_id from donor if exist.
718
			$donor = new Give_Customer( $guest_email );
719
			if ( $donor->id && $donor->user_id ) {
720
				$valid_user_data['user_id'] = $donor->user_id;
721
			}
722
		}
723
	} else {
724
		// No email.
725
		give_set_error( 'email_empty', __( 'Enter an email.', 'give' ) );
726
	}
727
728
	// Loop through required fields and show error messages.
729
	foreach ( give_get_required_fields( $form_id ) as $field_name => $value ) {
730
		if ( in_array( $value, give_get_required_fields( $form_id ) ) && empty( $_POST[ $field_name ] ) ) {
731
			give_set_error( $value['error_id'], $value['error_message'] );
732
		}
733
	}
734
735
	return $valid_user_data;
736
}
737
738
/**
739
 * Register And Login New User
740
 *
741
 * @param array $user_data
742
 *
743
 * @access  private
744
 * @since   1.0
745
 * @return  integer
746
 */
747
function give_register_and_login_new_user( $user_data = array() ) {
748
	// Verify the array.
749
	if ( empty( $user_data ) ) {
750
		return - 1;
751
	}
752
753
	if ( give_get_errors() ) {
754
		return - 1;
755
	}
756
757
	$user_args = apply_filters( 'give_insert_user_args', array(
758
		'user_login'      => isset( $user_data['user_login'] ) ? $user_data['user_login'] : '',
759
		'user_pass'       => isset( $user_data['user_pass'] ) ? $user_data['user_pass'] : '',
760
		'user_email'      => isset( $user_data['user_email'] ) ? $user_data['user_email'] : '',
761
		'first_name'      => isset( $user_data['user_first'] ) ? $user_data['user_first'] : '',
762
		'last_name'       => isset( $user_data['user_last'] ) ? $user_data['user_last'] : '',
763
		'user_registered' => date( 'Y-m-d H:i:s' ),
764
		'role'            => get_option( 'default_role' ),
765
	), $user_data );
766
767
	// Insert new user.
768
	$user_id = wp_insert_user( $user_args );
769
770
	// Validate inserted user.
771
	if ( is_wp_error( $user_id ) ) {
772
		return - 1;
773
	}
774
775
	// Allow themes and plugins to filter the user data.
776
	$user_data = apply_filters( 'give_insert_user_data', $user_data, $user_args );
777
778
	/**
779
	 * Fires after inserting user.
780
	 *
781
	 * @since 1.0
782
	 *
783
	 * @param int $user_id User id.
784
	 * @param array $user_data Array containing user data.
785
	 */
786
	do_action( 'give_insert_user', $user_id, $user_data );
787
788
	// Login new user.
789
	give_log_user_in( $user_id, $user_data['user_login'], $user_data['user_pass'] );
790
791
	// Return user id.
792
	return $user_id;
793
}
794
795
/**
796
 * Get Donation Form User
797
 *
798
 * @param array $valid_data
799
 *
800
 * @access  private
801
 * @since   1.0
802
 * @return  array|bool
803
 */
804
function give_get_donation_form_user( $valid_data = array() ) {
805
806
	// Initialize user.
807
	$user    = false;
808
	$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
809
810
	if ( $is_ajax ) {
811
		// Do not create or login the user during the ajax submission (check for errors only).
812
		return true;
813
	} elseif ( is_user_logged_in() ) {
814
		// Set the valid user as the logged in collected data.
815
		$user = $valid_data['logged_in_user'];
816
	} elseif ( $valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true ) {
817
		// New user registration.
818
		if ( $valid_data['need_new_user'] === true ) {
819
			// Set user.
820
			$user = $valid_data['new_user_data'];
821
			// Register and login new user.
822
			$user['user_id'] = give_register_and_login_new_user( $user );
823
			// User login
824
		} elseif ( $valid_data['need_user_login'] === true && ! $is_ajax ) {
825
826
			/**
827
			 * The login form is now processed in the give_process_donation_login() function.
828
			 * This is still here for backwards compatibility.
829
			 * This also allows the old login process to still work if a user removes the checkout login submit button.
830
			 *
831
			 * 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.
832
			 */
833
			// Set user.
834
			$user = $valid_data['login_user_data'];
835
			// Login user.
836
			give_log_user_in( $user['user_id'], $user['user_login'], $user['user_pass'] );
837
		}
838
	}
839
840
	// Check guest checkout.
841
	if ( false === $user && false === give_logged_in_only( $_POST['give-form-id'] ) ) {
842
		// Set user
843
		$user = $valid_data['guest_user_data'];
844
	}
845
846
	// Verify we have an user.
847
	if ( false === $user || empty( $user ) ) {
848
		// Return false.
849
		return false;
850
	}
851
852
	// Get user first name.
853
	if ( ! isset( $user['user_first'] ) || strlen( trim( $user['user_first'] ) ) < 1 ) {
854
		$user['user_first'] = isset( $_POST['give_first'] ) ? strip_tags( trim( $_POST['give_first'] ) ) : '';
855
	}
856
857
	// Get user last name.
858
	if ( ! isset( $user['user_last'] ) || strlen( trim( $user['user_last'] ) ) < 1 ) {
859
		$user['user_last'] = isset( $_POST['give_last'] ) ? strip_tags( trim( $_POST['give_last'] ) ) : '';
860
	}
861
862
	// Get the user's billing address details.
863
	$user['address']            = array();
864
	$user['address']['line1']   = ! empty( $_POST['card_address'] ) ? sanitize_text_field( $_POST['card_address'] ) : false;
865
	$user['address']['line2']   = ! empty( $_POST['card_address_2'] ) ? sanitize_text_field( $_POST['card_address_2'] ) : false;
866
	$user['address']['city']    = ! empty( $_POST['card_city'] ) ? sanitize_text_field( $_POST['card_city'] ) : false;
867
	$user['address']['state']   = ! empty( $_POST['card_state'] ) ? sanitize_text_field( $_POST['card_state'] ) : false;
868
	$user['address']['country'] = ! empty( $_POST['billing_country'] ) ? sanitize_text_field( $_POST['billing_country'] ) : false;
869
	$user['address']['zip']     = ! empty( $_POST['card_zip'] ) ? sanitize_text_field( $_POST['card_zip'] ) : false;
870
871
	if ( empty( $user['address']['country'] ) ) {
872
		$user['address'] = false;
873
	} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1184
1185
	return ( $donation_level_matched ? true : false );
1186
}
1187
1188
add_action( 'give_checkout_error_checks', 'give_validate_multi_donation_form_level', 10, 2 );
1189