Completed
Pull Request — master (#984)
by Devin
28:24
created

functions.php ➔ give_get_chosen_gateway()   C

Complexity

Conditions 11
Paths 54

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 17.0498

Importance

Changes 0
Metric Value
cc 11
eloc 18
nc 54
nop 1
dl 0
loc 27
ccs 12
cts 19
cp 0.6316
crap 17.0498
rs 5.2653
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 23 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
 * Gateway Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Gateways
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Returns a list of all available gateways.
19
 *
20
 * @since 1.0
21
 * @return array $gateways All the available gateways
22
 */
23
function give_get_payment_gateways() {
24
	// Default, built-in gateways
25
	$gateways = array(
26
		'paypal' => array(
27 44
			'admin_label'    => esc_html__( 'PayPal Standard', 'give' ),
28 44
			'checkout_label' => esc_html__( 'PayPal', 'give' ),
29 44
			'supports'       => array( 'buy_now' )
30 44
		),
31
		'manual' => array(
32 44
			'admin_label'    => esc_html__( 'Test Payment', 'give' ),
33 44
			'checkout_label' => esc_html__( 'Test Payment', 'give' )
34 44
		),
35 44
	);
36
37 44
	return apply_filters( 'give_payment_gateways', $gateways );
38
39
}
40
41
/**
42
 * Returns a list of all enabled gateways.
43
 *
44
 * @since 1.0
45
 * @return array $gateway_list All the available gateways
46
 */
47
function give_get_enabled_payment_gateways() {
48
49 44
	$gateways = give_get_payment_gateways();
50
51 44
	$enabled = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' );
52
53 44
	$gateway_list = array();
54
55 44
	foreach ( $gateways as $key => $gateway ) {
56 44
		if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) {
57 44
			$gateway_list[ $key ] = $gateway;
58 44
		}
59 44
	}
60
61
	// Set order of payment gateway in list.
62 44
	$gateway_list = give_get_ordered_payment_gateways( $gateway_list );
63
64 44
	return apply_filters( 'give_enabled_payment_gateways', $gateway_list );
65
}
66
67
/**
68
 * Checks whether a specified gateway is activated.
69
 *
70
 * @since 1.0
71
 *
72
 * @param string $gateway Name of the gateway to check for
73
 *
74
 * @return boolean true if enabled, false otherwise
75
 */
76
function give_is_gateway_active( $gateway ) {
77 2
	$gateways = give_get_enabled_payment_gateways();
78
79 2
	$ret = array_key_exists( $gateway, $gateways );
80
81 2
	return apply_filters( 'give_is_gateway_active', $ret, $gateway, $gateways );
82
}
83
84
/**
85
 * Gets the default payment gateway selected from the Give Settings
86
 *
87
 * @since 1.0
88
 * @global $give_options Array of all the Give Options
89
 *
90
 * @param  $form_id      int ID of the Give Form
91
 *
92
 * @return string Gateway ID
93
 */
94
function give_get_default_gateway( $form_id ) {
95
96 2
	global $give_options;
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...
97
98 2
	$default      = isset( $give_options['default_gateway'] ) && give_is_gateway_active( $give_options['default_gateway'] ) ? $give_options['default_gateway'] : 'paypal';
99 2
	$form_default = get_post_meta( $form_id, '_give_default_gateway', true );
100
101
	//Single Form settings varies compared to the Global default settings
102 2
	if ( ! empty( $form_default ) &&
103 2
	     $form_id !== null &&
104 2
	     $default !== $form_default &&
105 2
	     $form_default !== 'global' &&
106
	     give_is_gateway_active( $form_default )
107 2
	) {
108
		$default = $form_default;
109
	}
110
111 2
	return apply_filters( 'give_default_gateway', $default );
112
}
113
114
/**
115
 * Returns the admin label for the specified gateway
116
 *
117
 * @since 1.0
118
 *
119
 * @param string $gateway Name of the gateway to retrieve a label for
120
 *
121
 * @return string Gateway admin label
122
 */
123
function give_get_gateway_admin_label( $gateway ) {
124 42
	$gateways = give_get_enabled_payment_gateways();
125 42
	$label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
126 42
	$payment  = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false;
127
128 42
	if ( $gateway == 'manual' && $payment ) {
129
		if ( give_get_payment_amount( $payment ) == 0 ) {
130
			$label = esc_html__( 'Test Donation', 'give' );
131
		}
132
	}
133
134 42
	return apply_filters( 'give_gateway_admin_label', $label, $gateway );
135
}
136
137
/**
138
 * Returns the checkout label for the specified gateway
139
 *
140
 * @since 1.0
141
 *
142
 * @param string $gateway Name of the gateway to retrieve a label for
143
 *
144
 * @return string Checkout label for the gateway
145
 */
146
function give_get_gateway_checkout_label( $gateway ) {
147 42
	$gateways = give_get_enabled_payment_gateways();
148 42
	$label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
149
150 42
	if ( $gateway == 'manual' ) {
151 32
		$label = esc_html__( 'Test Donation', 'give' );
152 32
	}
153
154 42
	return apply_filters( 'give_gateway_checkout_label', $label, $gateway );
155
}
156
157
/**
158
 * Returns the options a gateway supports
159
 *
160
 * @since 1.8
161
 *
162
 * @param string $gateway ID of the gateway to retrieve a label for
163
 *
164
 * @return array Options the gateway supports
165
 */
166
function give_get_gateway_supports( $gateway ) {
167
	$gateways = give_get_enabled_payment_gateways();
168
	$supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array();
169
170
	return apply_filters( 'give_gateway_supports', $supports, $gateway );
171
}
172
173
/**
174
 * Checks if a gateway supports buy now
175
 *
176
 * @since 1.8
177
 *
178
 * @param string $gateway ID of the gateway to retrieve a label for
179
 *
180
 * @return bool
181
 */
182
function give_gateway_supports_buy_now( $gateway ) {
183
	$supports = give_get_gateway_supports( $gateway );
184
	$ret      = in_array( 'buy_now', $supports );
185
186
	return apply_filters( 'give_gateway_supports_buy_now', $ret, $gateway );
187
}
188
189
/**
190
 * Checks if an enabled gateway supports buy now
191
 *
192
 * @since 1.0
193
 * @return bool
194
 */
195
function give_give_supports_buy_now() {
196
	$gateways = give_get_enabled_payment_gateways();
197
	$ret      = false;
198
199
	if ( $gateways ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $gateways of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
200
		foreach ( $gateways as $gateway_id => $gateway ) {
201
			if ( give_gateway_supports_buy_now( $gateway_id ) ) {
202
				$ret = true;
203
				break;
204
			}
205
		}
206
	}
207
208
	return apply_filters( 'give_give_supports_buy_now', $ret );
209
}
210
211
/**
212
 * Sends all the payment data to the specified gateway
213
 *
214
 * @since 1.0
215
 *
216
 * @param string $gateway Name of the gateway
217
 * @param array  $payment_data All the payment data to be sent to the gateway
218
 *
219
 * @return void
220
 */
221
function give_send_to_gateway( $gateway, $payment_data ) {
222
223
	$payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' );
224
225
	// $gateway must match the ID used when registering the gateway
226
	do_action( 'give_gateway_' . $gateway, $payment_data );
227
}
228
229
230
/**
231
 * Determines what the currently selected gateway is.
232
 *
233
 * If the amount is zero, no option is shown and the donation form uses the manual
234
 * gateway to emulate a no-gateway-setup for a free donation.
235
 *
236
 * @access public
237
 * @since  1.0
238
 *
239
 * @param  int $form_id The ID of the Form
240
 *
241
 * @return string $enabled_gateway The slug of the gateway
242
 */
243
function give_get_chosen_gateway( $form_id ) {
244 1
	$gateways        = give_get_enabled_payment_gateways();
245 1
	$request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0;
246 1
	if ( empty( $request_form_id ) ) {
247 1
		$request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0;
248 1
	}
249 1
	$chosen          = give_get_default_gateway( $form_id );
250 1
	$enabled_gateway = '';
251
252
	//Take into account request Form ID args
253 1
	if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) {
254
		$chosen = isset( $_REQUEST['payment-mode'] ) ? $_REQUEST['payment-mode'] : '';
255
	}
256
257 1
	if ( $chosen ) {
258 1
		$enabled_gateway = urldecode( $chosen );
259 1
	} else if ( count( $gateways ) >= 1 && ! $chosen ) {
260
		foreach ( $gateways as $gateway_id => $gateway ):
261
			$enabled_gateway = $gateway_id;
262
		endforeach;
263
	} else {
264
		$enabled_gateway = give_get_default_gateway( $form_id );
265
	}
266
267
268 1
	return apply_filters( 'give_chosen_gateway', $enabled_gateway );
269
}
270
271
/**
272
 * Record a gateway error.
273
 *
274
 * A simple wrapper function for give_record_log().
275
 *
276
 * @access public
277
 * @since  1.0
278
 *
279
 * @param string $title Title of the log entry (default: empty)
280
 * @param string $message Message to store in the log entry (default: empty)
281
 * @param int    $parent Parent log entry (default: 0)
282
 *
283
 * @return int ID of the new log entry
284
 */
285
function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) {
286
	return give_record_log( $title, $message, $parent, 'gateway_error' );
287
}
288
289
/**
290
 * Counts the number of purchases made with a gateway
291
 *
292
 * @since 1.0
293
 *
294
 * @param string $gateway_id
295
 * @param string $status
296
 *
297
 * @return int
298
 */
299
function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
300
301
	$ret  = 0;
302
	$args = array(
303
		'meta_key'    => '_give_payment_gateway',
304
		'meta_value'  => $gateway_id,
305
		'nopaging'    => true,
306
		'post_type'   => 'give_payment',
307
		'post_status' => $status,
308
		'fields'      => 'ids'
309
	);
310
311
	$payments = new WP_Query( $args );
312
313
	if ( $payments ) {
314
		$ret = $payments->post_count;
315
	}
316
317
	return $ret;
318
}
319
320
321
/**
322
 * Returns a ordered list of all available gateways.
323
 *
324
 * @since 1.4.5
325
 *
326
 * @param array $gateways List of payment gateways
327
 *
328
 * @return array $gateways All the available gateways
329
 */
330
function give_get_ordered_payment_gateways( $gateways ) {
331
332
	//  Get gateways setting.
333 44
	$gateways_setting = isset( $_POST['gateways'] ) ? $_POST['gateways'] : give_get_option( 'gateways' );
334
335
	// Return from here if we do not have gateways setting.
336 44
	if ( empty( $gateways_setting ) ) {
337
		return $gateways;
338
	}
339
340
	// Reverse array to order payment gateways.
341 44
	$gateways_setting = array_reverse( $gateways_setting );
342
343
	// Reorder gateways array
344 44
	foreach ( $gateways_setting as $gateway_key => $value ) {
345
346 44
		$new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : '';
347 44
		unset( $gateways[ $gateway_key ] );
348
349 44
		if ( ! empty( $new_gateway_value ) ) {
350 44
			$gateways = array_merge( array( $gateway_key => $new_gateway_value ), $gateways );
351 44
		}
352 44
	}
353
354
	/**
355
	 * Filter payment gateways order.
356
	 *
357
	 * @since 1.4.5
358
	 *
359
	 * @param array $gateways All the available gateways
360
	 */
361 44
	return apply_filters( 'give_payment_gateways_order', $gateways );
362
}
363