Completed
Push — master ( b92b5a...92b4ca )
by Devin
19:49
created

functions.php ➔ give_get_ordered_payment_gateways()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 4
eloc 10
c 3
b 0
f 1
nc 4
nop 1
dl 0
loc 30
rs 8.5806
ccs 0
cts 0
cp 0
crap 20
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 36
			'admin_label'    => __( 'PayPal Standard', 'give' ),
28 36
			'checkout_label' => __( 'PayPal', 'give' ),
29 36
			'supports'       => array( 'buy_now' )
30 36
		),
31
		'manual' => array(
32 36
			'admin_label'    => __( 'Test Payment', 'give' ),
33 36
			'checkout_label' => __( 'Test Payment', 'give' )
34 36
		),
35 36
	);
36
37 36
	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 36
	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...
49
50 36
	$gateways = give_get_payment_gateways();
51
52 36
	$enabled = isset( $_POST['gateways'] )
53 36
		? $_POST['gateways']
54 36
		: ( isset( $give_options['gateways'] ) ? $give_options['gateways'] : false );
55
56 36
	$gateway_list = array();
57
58 36
	foreach ( $gateways as $key => $gateway ) {
59 36
		if ( isset( $enabled[ $key ] ) && $enabled[ $key ] == 1 ) {
60 36
			$gateway_list[ $key ] = $gateway;
61 36
		}
62 36
	}
63
64 36
	// Set order of payment gateway in list.
65
	$gateway_list = give_get_ordered_payment_gateways( $gateway_list );
66
67
	return apply_filters( 'give_enabled_payment_gateways', $gateway_list );
68
}
69
70
/**
71
 * Checks whether a specified gateway is activated.
72
 *
73
 * @since 1.0
74
 *
75
 * @param string $gateway Name of the gateway to check for
76
 *
77 2
 * @return boolean true if enabled, false otherwise
78
 */
79 2
function give_is_gateway_active( $gateway ) {
80
	$gateways = give_get_enabled_payment_gateways();
81 2
82
	$ret = array_key_exists( $gateway, $gateways );
83
84
	return apply_filters( 'give_is_gateway_active', $ret, $gateway, $gateways );
85
}
86
87
/**
88
 * Gets the default payment gateway selected from the Give Settings
89
 *
90
 * @since 1.0
91
 * @global $give_options Array of all the Give Options
92
 *
93
 * @param  $form_id      int ID of the Give Form
94
 *
95
 * @return string Gateway ID
96 2
 */
97
function give_get_default_gateway( $form_id ) {
98 2
99 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...
100
101
	$default      = isset( $give_options['default_gateway'] ) && give_is_gateway_active( $give_options['default_gateway'] ) ? $give_options['default_gateway'] : 'paypal';
102 2
	$form_default = get_post_meta( $form_id, '_give_default_gateway', true );
103 2
104 2
	//Single Form settings varies compared to the Global default settings
105 2
	if ( ! empty( $form_default ) &&
106
	     $form_id !== null &&
107 2
	     $default !== $form_default &&
108
	     $form_default !== 'global' &&
109
	     give_is_gateway_active( $form_default )
110
	) {
111 2
		$default = $form_default;
112
	}
113
114
	return apply_filters( 'give_default_gateway', $default );
115
}
116
117
/**
118
 * Returns the admin label for the specified gateway
119
 *
120
 * @since 1.0
121
 *
122
 * @param string $gateway Name of the gateway to retrieve a label for
123
 *
124 34
 * @return string Gateway admin label
125 34
 */
126 34
function give_get_gateway_admin_label( $gateway ) {
127
	$gateways = give_get_enabled_payment_gateways();
128 34
	$label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
129
	$payment  = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : false;
130
131
	if ( $gateway == 'manual' && $payment ) {
132
		if ( give_get_payment_amount( $payment ) == 0 ) {
133
			$label = __( 'Test Donation', 'give' );
134 34
		}
135
	}
136
137
	return apply_filters( 'give_gateway_admin_label', $label, $gateway );
138
}
139
140
/**
141
 * Returns the checkout label for the specified gateway
142
 *
143
 * @since 1.0
144
 *
145
 * @param string $gateway Name of the gateway to retrieve a label for
146
 *
147 34
 * @return string Checkout label for the gateway
148 34
 */
149
function give_get_gateway_checkout_label( $gateway ) {
150 34
	$gateways = give_get_enabled_payment_gateways();
151 34
	$label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
152 34
153
	if ( $gateway == 'manual' ) {
154 34
		$label = __( 'Test Donation', 'give' );
155
	}
156
157
	return apply_filters( 'give_gateway_checkout_label', $label, $gateway );
158
}
159
160
/**
161
 * Returns the options a gateway supports
162
 *
163
 * @since 1.8
164
 *
165
 * @param string $gateway ID of the gateway to retrieve a label for
166
 *
167
 * @return array Options the gateway supports
168
 */
169
function give_get_gateway_supports( $gateway ) {
170
	$gateways = give_get_enabled_payment_gateways();
171
	$supports = isset( $gateways[ $gateway ]['supports'] ) ? $gateways[ $gateway ]['supports'] : array();
172
173
	return apply_filters( 'give_gateway_supports', $supports, $gateway );
174
}
175
176
/**
177
 * Checks if a gateway supports buy now
178
 *
179
 * @since 1.8
180
 *
181
 * @param string $gateway ID of the gateway to retrieve a label for
182
 *
183
 * @return bool
184
 */
185
function give_gateway_supports_buy_now( $gateway ) {
186
	$supports = give_get_gateway_supports( $gateway );
187
	$ret      = in_array( 'buy_now', $supports );
188
189
	return apply_filters( 'give_gateway_supports_buy_now', $ret, $gateway );
190
}
191
192
/**
193
 * Checks if an enabled gateway supports buy now
194
 *
195
 * @since 1.0
196
 * @return bool
197
 */
198
function give_give_supports_buy_now() {
199
	$gateways = give_get_enabled_payment_gateways();
200
	$ret      = false;
201
202
	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...
203
		foreach ( $gateways as $gateway_id => $gateway ) {
204
			if ( give_gateway_supports_buy_now( $gateway_id ) ) {
205
				$ret = true;
206
				break;
207
			}
208
		}
209
	}
210
211
	return apply_filters( 'give_give_supports_buy_now', $ret );
212
}
213
214
/**
215
 * Sends all the payment data to the specified gateway
216
 *
217
 * @since 1.0
218
 *
219
 * @param string $gateway Name of the gateway
220
 * @param array $payment_data All the payment data to be sent to the gateway
221
 *
222
 * @return void
223
 */
224
function give_send_to_gateway( $gateway, $payment_data ) {
225
226
	$payment_data['gateway_nonce'] = wp_create_nonce( 'give-gateway' );
227
228
	// $gateway must match the ID used when registering the gateway
229
	do_action( 'give_gateway_' . $gateway, $payment_data );
230
}
231
232
233
/**
234
 * Determines what the currently selected gateway is
235
 *
236
 * If the amount is zero, no option is shown and the checkout uses the manual
237
 * gateway to emulate a no-gateway-setup for a free donation
238
 *
239
 * @access public
240
 * @since  1.0
241
 *
242
 * @param  int $form_id The ID of the Form
243
 *
244 1
 * @return string $enabled_gateway The slug of the gateway
245 1
 */
246 1
function give_get_chosen_gateway( $form_id ) {
247 1
	$gateways        = give_get_enabled_payment_gateways();
248 1
	$request_form_id = isset( $_REQUEST['give_form_id'] ) ? $_REQUEST['give_form_id'] : 0;
249 1
	if ( empty( $request_form_id ) ) {
250 1
		$request_form_id = isset( $_REQUEST['form-id'] ) ? $_REQUEST['form-id'] : 0;
251
	}
252
	$chosen          = give_get_default_gateway( $form_id );
253 1
	$enabled_gateway = '';
254
255
	//Take into account request Form ID args
256
	if ( ! empty( $request_form_id ) && $form_id == $request_form_id ) {
257 1
		$chosen = $_REQUEST['payment-mode'];
258 1
	}
259 1
260
	if ( $chosen ) {
261
		$enabled_gateway = urldecode( $chosen );
262
	} else if ( count( $gateways ) >= 1 && ! $chosen ) {
263
		foreach ( $gateways as $gateway_id => $gateway ):
264
			$enabled_gateway = $gateway_id;
265
		endforeach;
266
	} else {
267
		$enabled_gateway = give_get_default_gateway( $form_id );
268 1
	}
269
270
271
	return apply_filters( 'give_chosen_gateway', $enabled_gateway );
272
}
273
274
/**
275
 * Record a gateway error
276
 *
277
 * A simple wrapper function for give_record_log()
278
 *
279
 * @access public
280
 * @since  1.0
281
 *
282
 * @param string $title Title of the log entry (default: empty)
283
 * @param string $message Message to store in the log entry (default: empty)
284
 * @param int $parent Parent log entry (default: 0)
285
 *
286
 * @return int ID of the new log entry
287
 */
288
function give_record_gateway_error( $title = '', $message = '', $parent = 0 ) {
289
	return give_record_log( $title, $message, $parent, 'gateway_error' );
290
}
291
292
/**
293
 * Counts the number of purchases made with a gateway
294
 *
295
 * @since 1.0
296
 *
297
 * @param string $gateway_id
298
 * @param string $status
299
 *
300
 * @return int
301
 */
302
function give_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
303
304
	$ret  = 0;
305
	$args = array(
306
		'meta_key'    => '_give_payment_gateway',
307
		'meta_value'  => $gateway_id,
308
		'nopaging'    => true,
309
		'post_type'   => 'give_payment',
310
		'post_status' => $status,
311
		'fields'      => 'ids'
312
	);
313
314
	$payments = new WP_Query( $args );
315
316
	if ( $payments ) {
317
		$ret = $payments->post_count;
318
	}
319
320
	return $ret;
321
}
322
323
324
/**
325
 * Returns a ordered list of all available gateways.
326
 *
327
 * @since 1.4.5
328
 *
329
 * @param array $gateways List of payment gateways
330
 *
331
 * @return array $gateways All the available gateways
332
 */
333
function give_get_ordered_payment_gateways( $gateways ) {
334
335
	//  Get gateways setting.
336
	$gateways_setting = give_get_option( 'gateways' );
337
	
338
	// Return from here if we do not have gateways setting.
339
	if ( empty( $gateways_setting ) ) {
340
		return $gateways;
341
	}
342
343
	// Reverse array to order payment gateways.
344
	$gateways_setting = array_reverse( $gateways_setting );
345
346
	// Reorder gateways array
347
	foreach ( $gateways_setting as $gateway_key => $value ) {
348
349
		$new_gateway_value = isset( $gateways[ $gateway_key ] ) ? $gateways[ $gateway_key ] : '';
350
		unset( $gateways[ $gateway_key ] );
351
		$gateways = array_merge( array( $gateway_key => $new_gateway_value ), $gateways );
352
	}
353
354
	/**
355
	 * Filter payment gateways order.
356
	 *
357
	 * @since 1.4.5
358
	 *
359
	 * @param array $gateways All the available gateways
360
	 */
361
	return apply_filters( 'give_payment_gateways_order', $gateways );
362
}
363