Passed
Push — master ( f1fdbd...9032a8 )
by Brian
04:32
created

wpinv_settings_update_gateways()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 14
nop 1
dl 0
loc 41
rs 6.9666
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
2
/**
3
 * Contains gateway functions.
4
 *
5
 */
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * Returns an array of payment gateways.
10
 *
11
 * @return array
12
 */
13
function wpinv_get_payment_gateways() {
14
    return apply_filters( 'wpinv_payment_gateways', array() );
15
}
16
17
function wpinv_payment_gateway_titles( $all_gateways ) {
18
    global $wpinv_options;
19
20
    $gateways = array();
21
    foreach ( $all_gateways as $key => $gateway ) {
22
        if ( !empty( $wpinv_options[$key . '_title'] ) ) {
23
            $all_gateways[$key]['checkout_label'] = __( $wpinv_options[$key . '_title'], 'invoicing' );
24
        }
25
26
        $gateways[$key] = isset( $wpinv_options[$key . '_ordering'] ) ? $wpinv_options[$key . '_ordering'] : ( isset( $gateway['ordering'] ) ? $gateway['ordering'] : '' );
27
    }
28
29
    asort( $gateways );
30
31
    foreach ( $gateways as $gateway => $key ) {
32
        $gateways[$gateway] = $all_gateways[$gateway];
33
    }
34
35
    return $gateways;
36
}
37
add_filter( 'wpinv_payment_gateways', 'wpinv_payment_gateway_titles', 1000, 1 );
38
39
/**
40
 * Returns an array of enabled gateways.
41
 *
42
 * @param bool $sort
43
 * @return array
44
 */
45
function wpinv_get_enabled_payment_gateways( $sort = false ) {
46
47
    $enabled = array();
48
49
    foreach ( wpinv_get_payment_gateways() as $gateway => $data ) {
50
51
        if ( (int) wpinv_get_option( "{$gateway}_active", $gateway === 'manual' ) === 1 ) {
52
            $enabled[ $gateway ] = $data;
53
        }
54
55
    }
56
57
    if ( true === $sort ) {
58
        uasort( $enabled, 'wpinv_sort_gateway_order' );
59
60
        // Reorder our gateways so the default is first
61
        $default_gateway_id = wpinv_get_default_gateway();
62
        if ( isset( $enabled[ $default_gateway_id ] ) ) {
63
            $default_gateway = array(
64
                $default_gateway_id => $enabled[ $default_gateway_id ]
65
            );
66
67
            unset( $enabled[ $default_gateway_id ] );
68
            $enabled = array_merge( $default_gateway, $enabled );
69
        }
70
71
    }
72
73
    return apply_filters( 'wpinv_enabled_payment_gateways', $enabled );
74
}
75
76
function wpinv_sort_gateway_order( $a, $b ) {
77
    return $a['ordering'] - $b['ordering'];
78
}
79
80
/**
81
 * Checks if a given gateway is active.
82
 *
83
 * @param string $gateway
84
 * @return bool
85
 */
86
function wpinv_is_gateway_active( $gateway ) {
87
    $is_active = (int) wpinv_get_option( "{$gateway}_active", $gateway === 'manual' ) === 1 ;
88
    return apply_filters( 'wpinv_is_gateway_active', $is_active, $gateway );
89
}
90
91
/**
92
 * Retrieves the default gateway.
93
 *
94
 * @return string|false
95
 */
96
function wpinv_get_default_gateway() {
97
    $default  = wpinv_get_option( 'default_gateway' );
98
    $gateways = wpinv_get_enabled_payment_gateways();
99
    $default  = ! empty( $default ) && isset( $gateways[ $default ] ) ? $default : false;
100
101
    return apply_filters( 'wpinv_default_gateway', $default );
102
}
103
104
/**
105
 * Returns a gateway's name.
106
 *
107
 * @param string $gateway The gateway to key.
108
 * @return string
109
 */
110
function wpinv_get_gateway_admin_label( $gateway ) {
111
112
    if ( empty( $gateway ) || 'none' == $gateway ) {
113
        return esc_html__( 'No Gateway', 'invoicing' );
114
    }
115
116
    $gateways = wpinv_get_payment_gateways();
117
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['admin_label'] : $gateway;
118
    $gateway  = apply_filters( 'wpinv_gateway_admin_label', $label, $gateway );
119
120
    return wpinv_clean( $gateway );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_clean($gateway) also could return the type array which is incompatible with the documented return type string.
Loading history...
121
}
122
123
/**
124
 * Retrieves the gateway description.
125
 *
126
 * @param string $gateway
127
 */
128
function wpinv_get_gateway_description( $gateway ) {
129
    global $wpinv_options;
130
131
    $description = ! empty( $wpinv_options[$gateway . '_desc'] ) ? $wpinv_options[$gateway . '_desc'] : '';
132
133
    return apply_filters( 'wpinv_gateway_description', $description, $gateway );
134
}
135
136
function wpinv_get_gateway_button_label( $gateway ) {
137
    return apply_filters( 'wpinv_gateway_' . $gateway . '_button_label', '' );
138
}
139
140
function wpinv_get_gateway_checkout_label( $gateway ) {
141
    $gateways = wpinv_get_payment_gateways();
142
    $label    = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['checkout_label'] : $gateway;
143
144
    if ( $gateway == 'none' ) {
145
        $label = __( 'None', 'invoicing' );
146
    }
147
148
    return apply_filters( 'wpinv_gateway_checkout_label', ucfirst( $label ), $gateway );
149
}
150
151
function wpinv_settings_sections_gateways( $settings ) {
152
    $gateways = wpinv_get_payment_gateways();
153
154
    if (!empty($gateways)) {
155
        foreach  ($gateways as $key => $gateway) {
156
            $settings[$key] = $gateway['admin_label'];
157
        }
158
    }
159
160
    return $settings;
161
}
162
add_filter( 'wpinv_settings_sections_gateways', 'wpinv_settings_sections_gateways', 10, 1 );
163
164
/**
165
 * Adds GateWay settings.
166
 */
167
function wpinv_settings_gateways( $settings ) {
168
169
    // Loop through each gateway.
170
    foreach  ( wpinv_get_payment_gateways() as $key => $gateway ) {
171
172
        $gateway_settings = array(
173
174
            // Header.
175
            "{$key}_header" => array(
176
177
                'id'     => "{$key}_gateway_header",
178
                'name'   => '<h3>' . wp_sprintf( __( '%s Settings', 'invoicing' ), $gateway['admin_label'] ) . '</h3>',
179
                'custom' => $key,
180
                'type'   => 'gateway_header',
181
182
            ),
183
184
            // Activate/Deactivate a gateway.
185
            "{$key}_active" => array(
186
                'id'   => $key . '_active',
187
                'name' => __( 'Activate', 'invoicing' ),
188
                'desc' => wp_sprintf( __( 'Enable %s', 'invoicing' ), $gateway['admin_label'] ),
189
                'type' => 'checkbox',
190
                'std'  => $key === 'manual' ? '1' : '0',
191
            ),
192
193
            // Activate/Deactivate sandbox.
194
            "{$key}_sandbox" => array(
195
                'id'   => $key . '_sandbox',
196
                'name' => __( 'Sandbox', 'invoicing' ),
197
                'desc' => __( 'Enable sandbox to test payments', 'invoicing' ),
198
                'type' => 'checkbox',
199
                'std'  => '1',
200
            ),
201
202
            // Checkout title.
203
            "{$key}_title" => array(
204
                'id'   => $key . '_title',
205
                'name' => __( 'Checkout Title', 'invoicing' ),
206
                'std'  => isset( $gateway['checkout_label'] ) ? $gateway['checkout_label'] : '',
207
                'type' => 'text',
208
            ),
209
210
            // Checkout description.
211
            "{$key}_desc" => array(
212
                'id'   => $key . '_desc',
213
                'name' => __( 'Checkout Description', 'invoicing' ),
214
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
215
                'type' => 'text',
216
            ),
217
218
            // Checkout order.
219
            "{$key}_ordering" => array(
220
                'id'   => $key . '_ordering',
221
                'name' => __( 'Priority', 'invoicing' ),
222
                'std'  => apply_filters( "getpaid_default_{$key}_checkout_description", '' ),
223
                'type' => 'number',
224
                'step' => '1',
225
                'min'  => '0',
226
                'max'  => '100000',
227
                'std'  => isset( $gateway['ordering'] ) ? $gateway['ordering'] : '10',
228
            ),
229
230
        );
231
232
        // Maybe remove the sandbox.
233
        if ( ! getpaid_payment_gateway_supports( $key, 'sandbox' ) ) {
234
            unset( $gateway_settings["{$key}_sandbox"] );
235
        }
236
237
        $gateway_settings = apply_filters( 'wpinv_gateway_settings', $gateway_settings, $key, $gateway );
238
        $gateway_settings = apply_filters( 'wpinv_gateway_settings_' . $key, $gateway_settings, $gateway );
239
240
        $settings[$key] = $gateway_settings;
241
    }
242
243
    return $settings;
244
245
}
246
add_filter( 'wpinv_settings_gateways', 'wpinv_settings_gateways', 10, 1 );
247
248
function wpinv_gateway_header_callback( $args ) {
249
    echo '<input type="hidden" id="wpinv_settings[save_gateway]" name="wpinv_settings[save_gateway]" value="' . esc_attr( $args['custom'] ) . '" />';
250
}
251
252
/**
253
 * Checks if a given gateway supports a given feature.
254
 *
255
 * @param string $gateway
256
 * @param string $feature
257
 * @return bool
258
 * @since 2.3.0
259
 */
260
function getpaid_payment_gateway_supports( $gateway, $feature ) {
261
262
    $supports = false;
263
264
    if ( wpinv_is_gateway_active( $gateway ) ) {
265
266
        $supports = apply_filters( "getpaid_{$gateway}_supports_{$feature}", false );
267
268
        // Backwards compatibility.
269
        $supports = apply_filters( "wpinv_{$gateway}_supports_{$feature}", $supports );
270
        $supports = apply_filters( "wpinv_{$gateway}_support_{$feature}", $supports );
271
272
        $supports = apply_filters( "getpaid_gateway_supports_{$feature}", $supports, $gateway );
273
        $supports = apply_filters( 'getpaid_payment_gateway_supports', $supports, $feature, $gateway );
274
    }
275
276
    return $supports;
277
}
278
279
function wpinv_get_chosen_gateway( $invoice_id = 0 ) {
280
	$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
281
282
    $chosen = false;
283
    if ( $invoice_id > 0 && $invoice = wpinv_get_invoice( $invoice_id ) ) {
284
        $chosen = $invoice->get_gateway();
285
    }
286
287
	$chosen   = isset( $_REQUEST['payment-mode'] ) ? sanitize_text_field( $_REQUEST['payment-mode'] ) : $chosen;
288
289
	if ( false !== $chosen ) {
290
		$chosen = preg_replace('/[^a-zA-Z0-9-_]+/', '', $chosen );
291
	}
292
293
	if ( ! empty ( $chosen ) ) {
294
		$enabled_gateway = urldecode( $chosen );
295
	} else if (  !empty( $invoice ) && (float)$invoice->get_subtotal() <= 0 ) {
296
		$enabled_gateway = 'manual';
297
	} else {
298
		$enabled_gateway = wpinv_get_default_gateway();
299
	}
300
301
    if ( !wpinv_is_gateway_active( $enabled_gateway ) && !empty( $gateways ) ) {
0 ignored issues
show
Bug introduced by
It seems like $enabled_gateway can also be of type false; however, parameter $gateway of wpinv_is_gateway_active() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

301
    if ( !wpinv_is_gateway_active( /** @scrutinizer ignore-type */ $enabled_gateway ) && !empty( $gateways ) ) {
Loading history...
302
        if(wpinv_is_gateway_active( wpinv_get_default_gateway()) ){
303
            $enabled_gateway = wpinv_get_default_gateway();
304
        }else{
305
            $enabled_gateway = $gateways[0];
306
        }
307
308
    }
309
310
	return apply_filters( 'wpinv_chosen_gateway', $enabled_gateway );
311
}
312
313
function wpinv_record_gateway_error( $title = '', $message = '' ) {
314
    return wpinv_error_log( $message, $title );
0 ignored issues
show
Bug introduced by
Are you sure the usage of wpinv_error_log($message, $title) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
315
}
316
317
function wpinv_count_sales_by_gateway( $gateway_id = 'paypal', $status = 'publish' ) {
318
	$ret  = 0;
319
	$args = array(
320
		'meta_key'    => '_wpinv_gateway',
321
		'meta_value'  => $gateway_id,
322
		'nopaging'    => true,
323
		'post_type'   => 'wpi_invoice',
324
		'post_status' => $status,
325
		'fields'      => 'ids'
326
	);
327
328
	$payments = new WP_Query( $args );
329
330
	if( $payments )
0 ignored issues
show
introduced by
$payments is of type WP_Query, thus it always evaluated to true.
Loading history...
331
		$ret = $payments->post_count;
332
	return $ret;
333
}
334
335
// PayPal Standard settings
336
function wpinv_gateway_settings_paypal( $setting ) {
337
    $setting['paypal_active']['desc'] = $setting['paypal_active']['desc'] . ' ' . __( '( Supported Currencies: AUD, BRL, CAD, CZK, DKK, EUR, HKD, HUF, ILS, JPY, MYR, MXN, NOK, NZD, PHP, PLN, GBP, SGD, SEK, CHF, TWD, THB, USD )', 'invoicing' );
338
    $setting['paypal_desc']['std'] = __( 'Pay via PayPal: you can pay with your credit card if you don\'t have a PayPal account.', 'invoicing' );
339
340
    $setting['paypal_sandbox'] = array(
341
            'type' => 'checkbox',
342
            'id'   => 'paypal_sandbox',
343
            'name' => __( 'PayPal Sandbox', 'invoicing' ),
344
            'desc' => __( 'PayPal sandbox can be used to test payments.', 'invoicing' ),
345
            'std'  => 1
346
        );
347
348
    $setting['paypal_email'] = array(
349
            'type' => 'text',
350
            'id'   => 'paypal_email',
351
            'name' => __( 'PayPal Email', 'invoicing' ),
352
            'desc' => __( 'Please enter your PayPal account\'s email address. Ex: [email protected]', 'invoicing' ),
353
            'std' => __( '[email protected]', 'invoicing' ),
354
        );
355
    /*
356
    $setting['paypal_ipn_url'] = array(
357
            'type' => 'text',
358
            'id'   => 'paypal_ipn_url',
359
            'name' => __( 'PayPal IPN Url', 'invoicing' ),
360
            'desc' => __( 'Configure Instant Payment Notifications(IPN) url at PayPal. Ex: http://yoursite.com/?wpi-ipn=paypal', 'invoicing' ),
361
            'size' => 'large'
362
        );
363
    */
364
365
    return $setting;
366
}
367
add_filter( 'wpinv_gateway_settings_paypal', 'wpinv_gateway_settings_paypal', 10, 1 );
368
369
/**
370
 * Displays the ipn url field.
371
 */
372
function wpinv_ipn_url_callback( $args ) {
373
    $sanitize_id = wpinv_sanitize_key( $args['id'] );
374
375
    $attrs = $args['readonly'] ? ' readonly' : '';
376
377
    $html = '<input class="regular-text" type="text" ' . $attrs . ' value="' . esc_attr( $args['std'] ) . '" name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . ']" onClick="this.select()">';
378
    $html .= '<label for="wpinv_settings[' . $sanitize_id . ']">'  . $args['desc'] . '</label>';
379
380
    echo $html;
381
}
382
383
/**
384
 * Checks if a gateway is in test mode.
385
 *
386
 * @param string $gateway The gateway to check for.
387
 *
388
 * @return bool
389
 */
390
function wpinv_is_test_mode( $gateway = '' ) {
391
    $sandbox  = empty( $gateway ) ? false : wpinv_get_option( "{$gateway}_sandbox", true );
392
    $supports = getpaid_payment_gateway_supports( $gateway, 'sandbox' );
393
    return apply_filters( 'wpinv_is_test_mode', $sandbox && $supports, $gateway );
394
}
395
396
/**
397
 * Retrieves the ipn url.
398
 *
399
 * @param string $gateway The gateway whose IPN url we should retrieve.
400
 * @param array $args extra args to add to the url.
401
 *
402
 * @return string
403
 */
404
function wpinv_get_ipn_url( $gateway = false, $args = array() ) {
405
    $args = wp_parse_args(
406
        array(
407
            'wpi-listener' => 'IPN',
408
            'wpi-gateway'  => $gateway
409
        ),
410
        $args
411
    );
412
413
    return apply_filters( 'wpinv_ipn_url', add_query_arg( $args,  home_url( 'index.php' ) ), $gateway, $args );
414
415
}
416
417
/**
418
 * Retrieves the non-query string ipn url.
419
 *
420
 * @param string $gateway The gateway whose IPN url we should retrieve.
421
 *
422
 * @return string
423
 */
424
function getpaid_get_non_query_string_ipn_url( $gateway ) {
425
    $gateway = wpinv_sanitize_key( $gateway );
426
    return home_url( "getpaid-ipn/$gateway" );
427
}
428
429
430
/**
431
 * Retrieves request data with slashes removed slashes.
432
 */
433
function wpinv_get_post_data( $method = 'request' ) {
434
435
    if ( $method == 'post' ) {
436
        return wp_unslash( $_POST );
437
    }
438
439
    if ( $method == 'get' ) {
440
        return wp_unslash( $_GET );
441
    }
442
443
    return wp_unslash( $_REQUEST );
444
445
}
446
447
/**
448
 * Checks if a given gateway supports subscription payments.
449
 */
450
function wpinv_gateway_support_subscription( $gateway ) {
451
    return getpaid_payment_gateway_supports( $gateway, 'subscription' );
452
}
453
454
/**
455
 * Filters payment form gateways.
456
 *
457
 * @param array $gateways an array of gateways.
458
 * @param GetPaid_Payment_Form $form payment form.
459
 */
460
function wpinv_payment_gateways_on_cart( $gateways, $form ) {
461
462
    if ( $form->is_recurring() ) {
463
464
        foreach ( array_keys( $gateways ) as $gateway ) {
465
466
            if ( ! wpinv_gateway_support_subscription( $gateway ) ) {
467
                unset( $gateways[$gateway] );
468
            }
469
470
        }
471
472
    }
473
474
    return $gateways;
475
}
476
add_filter( 'getpaid_payment_form_gateways', 'wpinv_payment_gateways_on_cart', 10, 2 );
477