Test Failed
Push — issues/2610 ( 4e0645 )
by Ravinder
08:18
created

actions.php ➔ give_donation_form_nonce()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateway Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Gateways
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
 * Processes gateway select on checkout. Only for users without ajax / javascript
19
 *
20
 * @since 1.0
21
 *
22
 * @param $data
23
 */
24
function give_process_gateway_select( $data ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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...
25
	if ( isset( $_POST['gateway_submit'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
26
		wp_redirect( esc_url( add_query_arg( 'payment-mode', $_POST['payment-mode'] ) ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
27
		exit;
28
	}
29
}
30
31
add_action( 'give_gateway_select', 'give_process_gateway_select' );
32
33
/**
34
 * Loads a payment gateway via AJAX.
35
 *
36
 * @since 1.0
37
 *
38
 * @return void
39
 */
40
function give_load_ajax_gateway() {
41
	if ( isset( $_POST['give_payment_mode'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
42
		/**
43
		 * Fire to render donation form.
44
		 *
45
		 * @since 1.7
46
		 */
47
		do_action( 'give_donation_form', $_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-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
48
49
		exit();
50
	}
51
}
52
53
add_action( 'wp_ajax_give_load_gateway', 'give_load_ajax_gateway' );
54
add_action( 'wp_ajax_nopriv_give_load_gateway', 'give_load_ajax_gateway' );
55
56
/**
57
 * Create wp nonce using Ajax call.
58
 * 
59
 * Use give_donation_form_nonce() js fn to create nonce.
60
 *
61
 * @since 2.0
62
 *
63
 * @return void
64
 */
65
function give_donation_form_nonce() {
66
	if ( isset( $_POST['give_form_id'] ) ) {
67
68
		// Get donation form id.
69
		$form_id = is_numeric( $_POST['give_form_id'] ) ? absint( $_POST['give_form_id'] ) : 0;
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...
70
71
		// Send nonce json data.
72
		wp_send_json_success( wp_create_nonce( "donation_form_nonce_{$form_id}" ) );
73
	}
74
}
75
76
add_action( 'wp_ajax_give_donation_form_nonce', 'give_donation_form_nonce' );
77
add_action( 'wp_ajax_nopriv_give_donation_form_nonce', 'give_donation_form_nonce' );
78
79
/**
80
 * Sets an error within the donation form if no gateways are enabled.
81
 *
82
 * @since 1.0
83
 *
84
 * @return void
85
 */
86
function give_no_gateway_error() {
87
	$gateways = give_get_enabled_payment_gateways();
88
89
	if ( empty( $gateways ) ) {
90
		give_set_error( 'no_gateways', esc_html__( 'You must enable a payment gateway to use Give.', 'give' ) );
91
	} else {
92
		give_unset_error( 'no_gateways' );
93
	}
94
}
95
96
add_action( 'init', 'give_no_gateway_error' );
97