Passed
Pull Request — master (#97)
by Kiran
05:19
created

wpinv-helper-functions.php ➔ wpinv_currency_symbol()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 174
Code Lines 168

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 168
nc 4
nop 1
dl 0
loc 174
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

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 functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
function wpinv_item_quantities_enabled() {
15
    $ret = wpinv_get_option( 'item_quantities', true );
16
    
17
    return (bool) apply_filters( 'wpinv_item_quantities_enabled', $ret );
18
}
19
20
function wpinv_get_ip() {
21
    $ip = '127.0.0.1';
22
23
    if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
24
        $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] );
25
    } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
26
        $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] );
27
    } elseif( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
28
        $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] );
29
    }
30
    
31
    return apply_filters( 'wpinv_get_ip', $ip );
32
}
33
34
function wpinv_get_user_agent() {
35
    if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
36
        $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
37
    } else {
38
        $user_agent = '';
39
    }
40
    
41
    return apply_filters( 'wpinv_get_user_agent', $user_agent );
42
}
43
44
function wpinv_sanitize_amount( $amount, $decimals = NULL ) {
45
    $is_negative   = false;
46
    $thousands_sep = wpinv_thousands_separator();
47
    $decimal_sep   = wpinv_decimal_separator();
48
    if ( $decimals === NULL ) {
49
        $decimals = wpinv_decimals();
50
    }
51
52
    // Sanitize the amount
53
    if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) {
54
        if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
55
            $amount = str_replace( $thousands_sep, '', $amount );
56 View Code Duplication
        } elseif( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) {
57
            $amount = str_replace( '.', '', $amount );
58
        }
59
60
        $amount = str_replace( $decimal_sep, '.', $amount );
61 View Code Duplication
    } elseif( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
62
        $amount = str_replace( $thousands_sep, '', $amount );
63
    }
64
65
    if( $amount < 0 ) {
66
        $is_negative = true;
67
    }
68
69
    $amount   = preg_replace( '/[^0-9\.]/', '', $amount );
70
71
    $decimals = apply_filters( 'wpinv_sanitize_amount_decimals', absint( $decimals ), $amount );
72
    $amount   = number_format( (double) $amount, absint( $decimals ), '.', '' );
73
74
    if( $is_negative ) {
75
        $amount *= -1;
76
    }
77
78
    return apply_filters( 'wpinv_sanitize_amount', $amount, $decimals );
79
}
80
add_filter( 'wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1 );
81
82
function wpinv_round_amount( $amount, $decimals = NULL ) {
83
    if ( $decimals === NULL ) {
84
        $decimals = wpinv_decimals();
85
    }
86
    
87
    $amount = round( (double)$amount, wpinv_currency_decimal_filter( absint( $decimals ) ) );
88
89
    return apply_filters( 'wpinv_round_amount', $amount, $decimals );
90
}
91
92
function wpinv_get_invoice_statuses( $trashed = false ) {
93
    global $post;
94
    $invoice_statuses = array();
0 ignored issues
show
Unused Code introduced by
$invoice_statuses is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
    $invoice_statuses = array(
96
        'wpi-pending' => __('Pending Payment', 'invoicing'),
97
        'publish' => __('Paid', 'invoicing'),
98
        'wpi-processing' => __('Processing', 'invoicing'),
99
        'wpi-onhold' => __('On Hold', 'invoicing'),
100
        'wpi-refunded' => __('Refunded', 'invoicing'),
101
        'wpi-cancelled' => __('Cancelled', 'invoicing'),
102
        'wpi-failed' => __('Failed', 'invoicing'),
103
        'wpi-renewal' => __('Renewal Payment', 'invoicing')
104
    );
105
    
106
    if ( $trashed ) {
107
        $invoice_statuses['trash'] = __( 'Trash', 'invoicing' );
108
    }
109
110
    return apply_filters( 'wpinv_statuses', $invoice_statuses );
111
}
112
113
function wpinv_status_nicename( $status ) {
114
    $statuses = wpinv_get_invoice_statuses();
115
    $status   = isset( $statuses[$status] ) ? $statuses[$status] : __( $status, 'invoicing' );
116
117
    return $status;
118
}
119
120
function wpinv_get_currency() {
121
    $currency = wpinv_get_option( 'currency', 'USD' );
0 ignored issues
show
Documentation introduced by
'USD' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
    
123
    return apply_filters( 'wpinv_currency', $currency );
124
}
125
126
function wpinv_currency_symbol( $currency = '' ) {
127
    if ( empty( $currency ) ) {
128
        $currency = wpinv_get_currency();
129
    }
130
    
131
    $symbols = apply_filters( 'wpinv_currency_symbols', array(
132
        'AED' => '&#x62f;.&#x625;',
133
        'AFN' => '&#x60b;',
134
        'ALL' => 'L',
135
        'AMD' => 'AMD',
136
        'ANG' => '&fnof;',
137
        'AOA' => 'Kz',
138
        'ARS' => '&#36;',
139
        'AUD' => '&#36;',
140
        'AWG' => '&fnof;',
141
        'AZN' => 'AZN',
142
        'BAM' => 'KM',
143
        'BBD' => '&#36;',
144
        'BDT' => '&#2547;',
145
        'BGN' => '&#1083;&#1074;.',
146
        'BHD' => '.&#x62f;.&#x628;',
147
        'BIF' => 'Fr',
148
        'BMD' => '&#36;',
149
        'BND' => '&#36;',
150
        'BOB' => 'Bs.',
151
        'BRL' => '&#82;&#36;',
152
        'BSD' => '&#36;',
153
        'BTC' => '&#3647;',
154
        'BTN' => 'Nu.',
155
        'BWP' => 'P',
156
        'BYR' => 'Br',
157
        'BZD' => '&#36;',
158
        'CAD' => '&#36;',
159
        'CDF' => 'Fr',
160
        'CHF' => '&#67;&#72;&#70;',
161
        'CLP' => '&#36;',
162
        'CNY' => '&yen;',
163
        'COP' => '&#36;',
164
        'CRC' => '&#x20a1;',
165
        'CUC' => '&#36;',
166
        'CUP' => '&#36;',
167
        'CVE' => '&#36;',
168
        'CZK' => '&#75;&#269;',
169
        'DJF' => 'Fr',
170
        'DKK' => 'DKK',
171
        'DOP' => 'RD&#36;',
172
        'DZD' => '&#x62f;.&#x62c;',
173
        'EGP' => 'EGP',
174
        'ERN' => 'Nfk',
175
        'ETB' => 'Br',
176
        'EUR' => '&euro;',
177
        'FJD' => '&#36;',
178
        'FKP' => '&pound;',
179
        'GBP' => '&pound;',
180
        'GEL' => '&#x10da;',
181
        'GGP' => '&pound;',
182
        'GHS' => '&#x20b5;',
183
        'GIP' => '&pound;',
184
        'GMD' => 'D',
185
        'GNF' => 'Fr',
186
        'GTQ' => 'Q',
187
        'GYD' => '&#36;',
188
        'HKD' => '&#36;',
189
        'HNL' => 'L',
190
        'HRK' => 'Kn',
191
        'HTG' => 'G',
192
        'HUF' => '&#70;&#116;',
193
        'IDR' => 'Rp',
194
        'ILS' => '&#8362;',
195
        'IMP' => '&pound;',
196
        'INR' => '&#8377;',
197
        'IQD' => '&#x639;.&#x62f;',
198
        'IRR' => '&#xfdfc;',
199
        'IRT' => '&#x062A;&#x0648;&#x0645;&#x0627;&#x0646;',
200
        'ISK' => 'kr.',
201
        'JEP' => '&pound;',
202
        'JMD' => '&#36;',
203
        'JOD' => '&#x62f;.&#x627;',
204
        'JPY' => '&yen;',
205
        'KES' => 'KSh',
206
        'KGS' => '&#x441;&#x43e;&#x43c;',
207
        'KHR' => '&#x17db;',
208
        'KMF' => 'Fr',
209
        'KPW' => '&#x20a9;',
210
        'KRW' => '&#8361;',
211
        'KWD' => '&#x62f;.&#x643;',
212
        'KYD' => '&#36;',
213
        'KZT' => 'KZT',
214
        'LAK' => '&#8365;',
215
        'LBP' => '&#x644;.&#x644;',
216
        'LKR' => '&#xdbb;&#xdd4;',
217
        'LRD' => '&#36;',
218
        'LSL' => 'L',
219
        'LYD' => '&#x644;.&#x62f;',
220
        'MAD' => '&#x62f;.&#x645;.',
221
        'MDL' => 'MDL',
222
        'MGA' => 'Ar',
223
        'MKD' => '&#x434;&#x435;&#x43d;',
224
        'MMK' => 'Ks',
225
        'MNT' => '&#x20ae;',
226
        'MOP' => 'P',
227
        'MRO' => 'UM',
228
        'MUR' => '&#x20a8;',
229
        'MVR' => '.&#x783;',
230
        'MWK' => 'MK',
231
        'MXN' => '&#36;',
232
        'MYR' => '&#82;&#77;',
233
        'MZN' => 'MT',
234
        'NAD' => '&#36;',
235
        'NGN' => '&#8358;',
236
        'NIO' => 'C&#36;',
237
        'NOK' => '&#107;&#114;',
238
        'NPR' => '&#8360;',
239
        'NZD' => '&#36;',
240
        'OMR' => '&#x631;.&#x639;.',
241
        'PAB' => 'B/.',
242
        'PEN' => 'S/.',
243
        'PGK' => 'K',
244
        'PHP' => '&#8369;',
245
        'PKR' => '&#8360;',
246
        'PLN' => '&#122;&#322;',
247
        'PRB' => '&#x440;.',
248
        'PYG' => '&#8370;',
249
        'QAR' => '&#x631;.&#x642;',
250
        'RMB' => '&yen;',
251
        'RON' => 'lei',
252
        'RSD' => '&#x434;&#x438;&#x43d;.',
253
        'RUB' => '&#8381;',
254
        'RWF' => 'Fr',
255
        'SAR' => '&#x631;.&#x633;',
256
        'SBD' => '&#36;',
257
        'SCR' => '&#x20a8;',
258
        'SDG' => '&#x62c;.&#x633;.',
259
        'SEK' => '&#107;&#114;',
260
        'SGD' => '&#36;',
261
        'SHP' => '&pound;',
262
        'SLL' => 'Le',
263
        'SOS' => 'Sh',
264
        'SRD' => '&#36;',
265
        'SSP' => '&pound;',
266
        'STD' => 'Db',
267
        'SYP' => '&#x644;.&#x633;',
268
        'SZL' => 'L',
269
        'THB' => '&#3647;',
270
        'TJS' => '&#x405;&#x41c;',
271
        'TMT' => 'm',
272
        'TND' => '&#x62f;.&#x62a;',
273
        'TOP' => 'T&#36;',
274
        'TRY' => '&#8378;',
275
        'TTD' => '&#36;',
276
        'TWD' => '&#78;&#84;&#36;',
277
        'TZS' => 'Sh',
278
        'UAH' => '&#8372;',
279
        'UGX' => 'UGX',
280
        'USD' => '&#36;',
281
        'UYU' => '&#36;',
282
        'UZS' => 'UZS',
283
        'VEF' => 'Bs.',
284
        'VND' => '&#8363;',
285
        'VUV' => 'Vt',
286
        'WST' => 'T',
287
        'XAF' => 'Fr',
288
        'XCD' => '&#36;',
289
        'XOF' => 'Fr',
290
        'XPF' => 'Fr',
291
        'YER' => '&#xfdfc;',
292
        'ZAR' => '&#82;',
293
        'ZMW' => 'ZK',
294
    ) );
295
296
    $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency;
297
298
    return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency );
299
}
300
301
function wpinv_currency_position() {
302
    $position = wpinv_get_option( 'currency_position', 'left' );
0 ignored issues
show
Documentation introduced by
'left' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
303
    
304
    return apply_filters( 'wpinv_currency_position', $position );
305
}
306
307
function wpinv_thousands_separator() {
308
    $thousand_sep = wpinv_get_option( 'thousands_separator', ',' );
0 ignored issues
show
Documentation introduced by
',' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
309
    
310
    return apply_filters( 'wpinv_thousands_separator', $thousand_sep );
311
}
312
313
function wpinv_decimal_separator() {
314
    $decimal_sep = wpinv_get_option( 'decimal_separator', '.' );
0 ignored issues
show
Documentation introduced by
'.' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
315
    
316
    return apply_filters( 'wpinv_decimal_separator', $decimal_sep );
317
}
318
319
function wpinv_decimals() {
320
    $decimals = apply_filters( 'wpinv_decimals', wpinv_get_option( 'decimals', 2 ) );
0 ignored issues
show
Documentation introduced by
2 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
321
    
322
    return absint( $decimals );
323
}
324
325
function wpinv_get_currencies() {
326
    $currencies = array(
327
        'USD' => __( 'US Dollar', 'invoicing' ),
328
        'EUR' => __( 'Euro', 'invoicing' ),
329
        'GBP' => __( 'Pound Sterling', 'invoicing' ),
330
        'AFN' => __( 'Afghan Afghani', 'invoicing' ),
331
        'ALL' => __( 'Albanian Lek', 'invoicing' ),
332
        'AMD' => __( 'Armenian Dram', 'invoicing' ),
333
        'ANG' => __( 'Netherlands Antillean Guilder', 'invoicing' ),
334
        'AOA' => __( 'Angolan Kwanza', 'invoicing' ),
335
        'ARS' => __( 'Argentine Peso', 'invoicing' ),
336
        'AUD' => __( 'Australian Dollar', 'invoicing' ),
337
        'AWG' => __( 'Aruban Florin', 'invoicing' ),
338
        'AZN' => __( 'Azerbaijani Manat', 'invoicing' ),
339
        'BAM' => __( 'Bosnia and Herzegovina Convertible Marka', 'invoicing' ),
340
        'BBD' => __( 'Barbadian Dollar', 'invoicing' ),
341
        'BDT' => __( 'Bangladeshi Taka', 'invoicing' ),
342
        'BGN' => __( 'Bulgarian Lev', 'invoicing' ),
343
        'BHD' => __( 'Bahraini Dinar', 'invoicing' ),
344
        'BIF' => __( 'Burundian Franc', 'invoicing' ),
345
        'BMD' => __( 'Bermudian Dollar', 'invoicing' ),
346
        'BND' => __( 'Brunei Dollar', 'invoicing' ),
347
        'BOB' => __( 'Bolivian Boliviano', 'invoicing' ),
348
        'BRL' => __( 'Brazilian Real', 'invoicing' ),
349
        'BSD' => __( 'Bahamian Dollar', 'invoicing' ),
350
        'BTC' => __( 'Bitcoin', 'invoicing' ),
351
        'BTN' => __( 'Bhutanese Ngultrum', 'invoicing' ),
352
        'BWP' => __( 'Botswana Pula', 'invoicing' ),
353
        'BYR' => __( 'Belarusian Ruble', 'invoicing' ),
354
        'BZD' => __( 'Belize Dollar', 'invoicing' ),
355
        'CAD' => __( 'Canadian Dollar', 'invoicing' ),
356
        'CDF' => __( 'Congolese Franc', 'invoicing' ),
357
        'CHF' => __( 'Swiss Franc', 'invoicing' ),
358
        'CLP' => __( 'Chilean Peso', 'invoicing' ),
359
        'CNY' => __( 'Chinese Yuan', 'invoicing' ),
360
        'COP' => __( 'Colombian Peso', 'invoicing' ),
361
        'CRC' => __( 'Costa Rican Colon', 'invoicing' ),
362
        'CUC' => __( 'Cuban Convertible Peso', 'invoicing' ),
363
        'CUP' => __( 'Cuban Peso', 'invoicing' ),
364
        'CVE' => __( 'Cape Verdean escudo', 'invoicing' ),
365
        'CZK' => __( 'Czech Koruna', 'invoicing' ),
366
        'DJF' => __( 'Djiboutian Franc', 'invoicing' ),
367
        'DKK' => __( 'Danish Krone', 'invoicing' ),
368
        'DOP' => __( 'Dominican Peso', 'invoicing' ),
369
        'DZD' => __( 'Algerian Dinar', 'invoicing' ),
370
        'EGP' => __( 'Egyptian Pound', 'invoicing' ),
371
        'ERN' => __( 'Eritrean Nakfa', 'invoicing' ),
372
        'ETB' => __( 'Ethiopian irr', 'invoicing' ),
373
        'FJD' => __( 'Fijian Dollar', 'invoicing' ),
374
        'FKP' => __( 'Falkland Islands Pound', 'invoicing' ),
375
        'GEL' => __( 'Georgian lari', 'invoicing' ),
376
        'GGP' => __( 'Guernsey Pound', 'invoicing' ),
377
        'GHS' => __( 'Ghana cedi', 'invoicing' ),
378
        'GIP' => __( 'Gibraltar Pound', 'invoicing' ),
379
        'GMD' => __( 'Gambian Dalasi', 'invoicing' ),
380
        'GNF' => __( 'Guinean Franc', 'invoicing' ),
381
        'GTQ' => __( 'Guatemalan Quetzal', 'invoicing' ),
382
        'GYD' => __( 'Guyanese Dollar', 'invoicing' ),
383
        'HKD' => __( 'Hong Kong Dollar', 'invoicing' ),
384
        'HNL' => __( 'Honduran Lempira', 'invoicing' ),
385
        'HRK' => __( 'Croatian Kuna', 'invoicing' ),
386
        'HTG' => __( 'Haitian Gourde', 'invoicing' ),
387
        'HUF' => __( 'Hungarian Forint', 'invoicing' ),
388
        'IDR' => __( 'Indonesian Rupiah', 'invoicing' ),
389
        'ILS' => __( 'Israeli New Shekel', 'invoicing' ),
390
        'IMP' => __( 'Manx Pound', 'invoicing' ),
391
        'INR' => __( 'Indian Rupee', 'invoicing' ),
392
        'IQD' => __( 'Iraqi Dinar', 'invoicing' ),
393
        'IRR' => __( 'Iranian Rial', 'invoicing' ),
394
        'IRT' => __( 'Iranian Toman', 'invoicing' ),
395
        'ISK' => __( 'Icelandic Krona', 'invoicing' ),
396
        'JEP' => __( 'Jersey Pound', 'invoicing' ),
397
        'JMD' => __( 'Jamaican Dollar', 'invoicing' ),
398
        'JOD' => __( 'Jordanian Dinar', 'invoicing' ),
399
        'JPY' => __( 'Japanese Yen', 'invoicing' ),
400
        'KES' => __( 'Kenyan Shilling', 'invoicing' ),
401
        'KGS' => __( 'Kyrgyzstani Som', 'invoicing' ),
402
        'KHR' => __( 'Cambodian Riel', 'invoicing' ),
403
        'KMF' => __( 'Comorian Franc', 'invoicing' ),
404
        'KPW' => __( 'North Korean Won', 'invoicing' ),
405
        'KRW' => __( 'South Korean Won', 'invoicing' ),
406
        'KWD' => __( 'Kuwaiti Dinar', 'invoicing' ),
407
        'KYD' => __( 'Cayman Islands Dollar', 'invoicing' ),
408
        'KZT' => __( 'Kazakhstani Tenge', 'invoicing' ),
409
        'LAK' => __( 'Lao Kip', 'invoicing' ),
410
        'LBP' => __( 'Lebanese Pound', 'invoicing' ),
411
        'LKR' => __( 'Sri Lankan Rupee', 'invoicing' ),
412
        'LRD' => __( 'Liberian Dollar', 'invoicing' ),
413
        'LSL' => __( 'Lesotho Loti', 'invoicing' ),
414
        'LYD' => __( 'Libyan Dinar', 'invoicing' ),
415
        'MAD' => __( 'Moroccan Dirham', 'invoicing' ),
416
        'MDL' => __( 'Moldovan Leu', 'invoicing' ),
417
        'MGA' => __( 'Malagasy Ariary', 'invoicing' ),
418
        'MKD' => __( 'Macedonian Denar', 'invoicing' ),
419
        'MMK' => __( 'Burmese Kyat', 'invoicing' ),
420
        'MNT' => __( 'Mongolian Tughrik', 'invoicing' ),
421
        'MOP' => __( 'Macanese Pataca', 'invoicing' ),
422
        'MRO' => __( 'Mauritanian Ouguiya', 'invoicing' ),
423
        'MUR' => __( 'Mauritian Rupee', 'invoicing' ),
424
        'MVR' => __( 'Maldivian Rufiyaa', 'invoicing' ),
425
        'MWK' => __( 'Malawian Kwacha', 'invoicing' ),
426
        'MXN' => __( 'Mexican Peso', 'invoicing' ),
427
        'MYR' => __( 'Malaysian Ringgit', 'invoicing' ),
428
        'MZN' => __( 'Mozambican Metical', 'invoicing' ),
429
        'NAD' => __( 'Namibian Dollar', 'invoicing' ),
430
        'NGN' => __( 'Nigerian Naira', 'invoicing' ),
431
        'NIO' => __( 'Nicaraguan Cordoba', 'invoicing' ),
432
        'NOK' => __( 'Norwegian Krone', 'invoicing' ),
433
        'NPR' => __( 'Nepalese Rupee', 'invoicing' ),
434
        'NZD' => __( 'New Zealand Dollar', 'invoicing' ),
435
        'OMR' => __( 'Omani Rial', 'invoicing' ),
436
        'PAB' => __( 'Panamanian Balboa', 'invoicing' ),
437
        'PEN' => __( 'Peruvian Nuevo Sol', 'invoicing' ),
438
        'PGK' => __( 'Papua New Guinean Kina', 'invoicing' ),
439
        'PHP' => __( 'Philippine Peso', 'invoicing' ),
440
        'PKR' => __( 'Pakistani Rupee', 'invoicing' ),
441
        'PLN' => __( 'Polish Zloty', 'invoicing' ),
442
        'PRB' => __( 'Transnistrian Ruble', 'invoicing' ),
443
        'PYG' => __( 'Paraguayan Guarani', 'invoicing' ),
444
        'QAR' => __( 'Qatari Riyal', 'invoicing' ),
445
        'RON' => __( 'Romanian Leu', 'invoicing' ),
446
        'RSD' => __( 'Serbian Dinar', 'invoicing' ),
447
        'RUB' => __( 'Russian Ruble', 'invoicing' ),
448
        'RWF' => __( 'Rwandan Franc', 'invoicing' ),
449
        'SAR' => __( 'Saudi Riyal', 'invoicing' ),
450
        'SBD' => __( 'Solomon Islands Dollar', 'invoicing' ),
451
        'SCR' => __( 'Seychellois Rupee', 'invoicing' ),
452
        'SDG' => __( 'Sudanese Pound', 'invoicing' ),
453
        'SEK' => __( 'Swedish Krona', 'invoicing' ),
454
        'SGD' => __( 'Singapore Dollar', 'invoicing' ),
455
        'SHP' => __( 'Saint Helena Pound', 'invoicing' ),
456
        'SLL' => __( 'Sierra Leonean Leone', 'invoicing' ),
457
        'SOS' => __( 'Somali Shilling', 'invoicing' ),
458
        'SRD' => __( 'Surinamese Dollar', 'invoicing' ),
459
        'SSP' => __( 'South Sudanese Pound', 'invoicing' ),
460
        'STD' => __( 'Sao Tomean Dobra', 'invoicing' ),
461
        'SYP' => __( 'Syrian Pound', 'invoicing' ),
462
        'SZL' => __( 'Swazi Lilangeni', 'invoicing' ),
463
        'THB' => __( 'Thai Baht', 'invoicing' ),
464
        'TJS' => __( 'Tajikistani Somoni', 'invoicing' ),
465
        'TMT' => __( 'Turkmenistan Manat', 'invoicing' ),
466
        'TND' => __( 'Tunisian Dinar', 'invoicing' ),
467
        'TOP' => __( 'Tongan Pa&#x2bb;anga', 'invoicing' ),
468
        'TRY' => __( 'Turkish Lira', 'invoicing' ),
469
        'TTD' => __( 'Trinidad and Tobago Dollar', 'invoicing' ),
470
        'TWD' => __( 'New Taiwan Dollar', 'invoicing' ),
471
        'TZS' => __( 'Tanzanian Shilling', 'invoicing' ),
472
        'UAH' => __( 'Ukrainian Hryvnia', 'invoicing' ),
473
        'UGX' => __( 'Ugandan Shilling', 'invoicing' ),
474
        'UYU' => __( 'Uruguayan Peso', 'invoicing' ),
475
        'UZS' => __( 'Uzbekistani Som', 'invoicing' ),
476
        'VEF' => __( 'Venezuelan Bol&iacute;var', 'invoicing' ),
477
        'VND' => __( 'Vietnamese Dong', 'invoicing' ),
478
        'VUV' => __( 'Vanuatu Vatu', 'invoicing' ),
479
        'WST' => __( 'Samoan Tala', 'invoicing' ),
480
        'XAF' => __( 'Central African CFA Franc', 'invoicing' ),
481
        'XCD' => __( 'East Caribbean Dollar', 'invoicing' ),
482
        'XOF' => __( 'West African CFA Franc', 'invoicing' ),
483
        'XPF' => __( 'CFP Franc', 'invoicing' ),
484
        'YER' => __( 'Yemeni Rial', 'invoicing' ),
485
        'ZAR' => __( 'South African Rand', 'invoicing' ),
486
        'ZMW' => __( 'Zambian Kwacha', 'invoicing' ),
487
    );
488
    
489
    //asort( $currencies ); // this
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
490
491
    return apply_filters( 'wpinv_currencies', $currencies );
492
}
493
494
function wpinv_price( $amount = '', $currency = '' ) {
495
    if( empty( $currency ) ) {
496
        $currency = wpinv_get_currency();
497
    }
498
499
    $position = wpinv_currency_position();
500
501
    $negative = $amount < 0;
502
503
    if ( $negative ) {
504
        $amount = substr( $amount, 1 );
505
    }
506
507
    $symbol = wpinv_currency_symbol( $currency );
508
509
    if ( $position == 'left' || $position == 'left_space' ) {
510 View Code Duplication
        switch ( $currency ) {
511
            case "GBP" :
512
            case "BRL" :
513
            case "EUR" :
514
            case "USD" :
515
            case "AUD" :
516
            case "CAD" :
517
            case "HKD" :
518
            case "MXN" :
519
            case "NZD" :
520
            case "SGD" :
521
            case "JPY" :
522
                $price = $position == 'left_space' ? $symbol . ' ' .  $amount : $symbol . $amount;
523
                break;
524
            default :
525
                //$price = $currency . ' ' . $amount;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
526
                $price = $position == 'left_space' ? $symbol . ' ' .  $amount : $symbol . $amount;
527
                break;
528
        }
529
    } else {
530 View Code Duplication
        switch ( $currency ) {
531
            case "GBP" :
532
            case "BRL" :
533
            case "EUR" :
534
            case "USD" :
535
            case "AUD" :
536
            case "CAD" :
537
            case "HKD" :
538
            case "MXN" :
539
            case "SGD" :
540
            case "JPY" :
541
                $price = $position == 'right_space' ? $amount . ' ' .  $symbol : $amount . $symbol;
542
                break;
543
            default :
544
                //$price = $amount . ' ' . $currency;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
545
                $price = $position == 'right_space' ? $amount . ' ' .  $symbol : $amount . $symbol;
546
                break;
547
        }
548
    }
549
    
550
    if ( $negative ) {
551
        $price = '-' . $price;
552
    }
553
    
554
    $price = apply_filters( 'wpinv_' . strtolower( $currency ) . '_currency_filter_' . $position, $price, $currency, $amount );
555
556
    return $price;
557
}
558
559
function wpinv_format_amount( $amount, $decimals = NULL, $calculate = false ) {
560
    $thousands_sep = wpinv_thousands_separator();
561
    $decimal_sep   = wpinv_decimal_separator();
562
563
    if ( $decimals === NULL ) {
564
        $decimals = wpinv_decimals();
565
    }
566
567
    if ( $decimal_sep == ',' && false !== ( $sep_found = strpos( $amount, $decimal_sep ) ) ) {
568
        $whole = substr( $amount, 0, $sep_found );
569
        $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
570
        $amount = $whole . '.' . $part;
571
    }
572
573 View Code Duplication
    if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
574
        $amount = str_replace( ',', '', $amount );
575
    }
576
577 View Code Duplication
    if ( $thousands_sep == ' ' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
578
        $amount = str_replace( ' ', '', $amount );
579
    }
580
581
    if ( empty( $amount ) ) {
582
        $amount = 0;
583
    }
584
    
585
    $decimals  = apply_filters( 'wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate );
586
    $formatted = number_format( (float)$amount, $decimals, $decimal_sep, $thousands_sep );
587
    
588
    if ( $calculate ) {
589
        if ( $thousands_sep === "," ) {
590
            $formatted = str_replace( ",", "", $formatted );
591
        }
592
        
593
        if ( $decimal_sep === "," ) {
594
            $formatted = str_replace( ",", ".", $formatted );
595
        }
596
    }
597
598
    return apply_filters( 'wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate );
599
}
600
add_filter( 'wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1 );
601
602
function wpinv_sanitize_key( $key ) {
603
    $raw_key = $key;
604
    $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key );
605
606
    return apply_filters( 'wpinv_sanitize_key', $key, $raw_key );
607
}
608
609
function wpinv_get_file_extension( $str ) {
610
    $parts = explode( '.', $str );
611
    return end( $parts );
612
}
613
614
function wpinv_string_is_image_url( $str ) {
615
    $ext = wpinv_get_file_extension( $str );
616
617
    switch ( strtolower( $ext ) ) {
618
        case 'jpeg';
619
        case 'jpg';
620
            $return = true;
621
            break;
622
        case 'png';
623
            $return = true;
624
            break;
625
        case 'gif';
626
            $return = true;
627
            break;
628
        default:
629
            $return = false;
630
            break;
631
    }
632
633
    return (bool)apply_filters( 'wpinv_string_is_image', $return, $str );
634
}
635
636
function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) {
637
    $should_log = apply_filters( 'wpinv_log_errors', WP_DEBUG );
638
    
639
    if ( true === $should_log ) {
640
        $label = '';
641
        if ( $file && $file !== '' ) {
642
            $label .= basename( $file ) . ( $line ? '(' . $line . ')' : '' );
643
        }
644
        
645
        if ( $title && $title !== '' ) {
646
            $label = $label !== '' ? $label . ' ' : '';
647
            $label .= $title . ' ';
648
        }
649
        
650
        $label = $label !== '' ? trim( $label ) . ' : ' : '';
651
        
652
        if ( is_array( $log ) || is_object( $log ) ) {
653
            error_log( $label . print_r( $log, true ) );
654
        } else {
655
            error_log( $label . $log );
656
        }
657
        
658
        if ( $exit ) {
659
            exit;
660
        }
661
    }
662
}
663
664
function wpinv_is_ajax_disabled() {
665
    $retval = false;
666
    return apply_filters( 'wpinv_is_ajax_disabled', $retval );
667
}
668
669
function wpinv_get_current_page_url( $nocache = false ) {
670
    global $wp;
671
672
    if ( get_option( 'permalink_structure' ) ) {
673
        $base = trailingslashit( home_url( $wp->request ) );
674
    } else {
675
        $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) );
676
        $base = remove_query_arg( array( 'post_type', 'name' ), $base );
677
    }
678
679
    $scheme = is_ssl() ? 'https' : 'http';
680
    $uri    = set_url_scheme( $base, $scheme );
681
682
    if ( is_front_page() ) {
683
        $uri = home_url( '/' );
684
    } elseif ( wpinv_is_checkout( array(), false ) ) {
0 ignored issues
show
Unused Code introduced by
The call to wpinv_is_checkout() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
685
        $uri = wpinv_get_checkout_uri();
686
    }
687
688
    $uri = apply_filters( 'wpinv_get_current_page_url', $uri );
689
690
    if ( $nocache ) {
691
        $uri = wpinv_add_cache_busting( $uri );
692
    }
693
694
    return $uri;
695
}
696
697
function wpinv_get_php_arg_separator_output() {
698
	return ini_get( 'arg_separator.output' );
699
}
700
701
function wpinv_rgb_from_hex( $color ) {
702
    $color = str_replace( '#', '', $color );
703
    // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
704
    $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
705
706
    $rgb      = array();
707
    $rgb['R'] = hexdec( $color{0}.$color{1} );
708
    $rgb['G'] = hexdec( $color{2}.$color{3} );
709
    $rgb['B'] = hexdec( $color{4}.$color{5} );
710
711
    return $rgb;
712
}
713
714 View Code Duplication
function wpinv_hex_darker( $color, $factor = 30 ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
715
    $base  = wpinv_rgb_from_hex( $color );
716
    $color = '#';
717
718
    foreach ( $base as $k => $v ) {
719
        $amount      = $v / 100;
720
        $amount      = round( $amount * $factor );
721
        $new_decimal = $v - $amount;
722
723
        $new_hex_component = dechex( $new_decimal );
724
        if ( strlen( $new_hex_component ) < 2 ) {
725
            $new_hex_component = "0" . $new_hex_component;
726
        }
727
        $color .= $new_hex_component;
728
    }
729
730
    return $color;
731
}
732
733 View Code Duplication
function wpinv_hex_lighter( $color, $factor = 30 ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
734
    $base  = wpinv_rgb_from_hex( $color );
735
    $color = '#';
736
737
    foreach ( $base as $k => $v ) {
738
        $amount      = 255 - $v;
739
        $amount      = $amount / 100;
740
        $amount      = round( $amount * $factor );
741
        $new_decimal = $v + $amount;
742
743
        $new_hex_component = dechex( $new_decimal );
744
        if ( strlen( $new_hex_component ) < 2 ) {
745
            $new_hex_component = "0" . $new_hex_component;
746
        }
747
        $color .= $new_hex_component;
748
    }
749
750
    return $color;
751
}
752
753
function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
754
    $hex = str_replace( '#', '', $color );
755
756
    $c_r = hexdec( substr( $hex, 0, 2 ) );
757
    $c_g = hexdec( substr( $hex, 2, 2 ) );
758
    $c_b = hexdec( substr( $hex, 4, 2 ) );
759
760
    $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
761
762
    return $brightness > 155 ? $dark : $light;
763
}
764
765
function wpinv_format_hex( $hex ) {
766
    $hex = trim( str_replace( '#', '', $hex ) );
767
768
    if ( strlen( $hex ) == 3 ) {
769
        $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
770
    }
771
772
    return $hex ? '#' . $hex : null;
773
}
774
775
/**
776
 * Get truncated string with specified width.
777
 *
778
 * @since 1.0.0
779
 *
780
 * @param string $str The string being decoded.
781
 * @param int $start The start position offset. Number of characters from the beginning of string.
782
 *                      For negative value, number of characters from the end of the string.
783
 * @param int $width The width of the desired trim. Negative widths count from the end of the string.
784
 * @param string $trimmaker A string that is added to the end of string when string is truncated. Ex: "...".
785
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
786
 * @return string
787
 */
788
function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) {
789
    if ( function_exists( 'mb_strimwidth' ) ) {
790
        return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding );
791
    }
792
    
793
    return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker;
794
}
795
796
/**
797
 * Get the string length.
798
 *
799
 * @since 1.0.0
800
 *
801
 * @param string $str The string being checked for length. 
802
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
803
 * @return int Returns the number of characters in string.
804
 */
805
function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) {
806
    if ( function_exists( 'mb_strlen' ) ) {
807
        return mb_strlen( $str, $encoding );
808
    }
809
        
810
    return strlen( $str );
811
}
812
813
function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) {
814
    if ( function_exists( 'mb_strtolower' ) ) {
815
        return mb_strtolower( $str, $encoding );
816
    }
817
    
818
    return strtolower( $str );
819
}
820
821
function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) {
822
    if ( function_exists( 'mb_strtoupper' ) ) {
823
        return mb_strtoupper( $str, $encoding );
824
    }
825
    
826
    return strtoupper( $str );
827
}
828
829
/**
830
 * Find position of first occurrence of string in a string
831
 *
832
 * @since 1.0.0
833
 *
834
 * @param string $str The string being checked.
835
 * @param string $find The string to find in input string.
836
 * @param int $offset The search offset. Default "0". A negative offset counts from the end of the string.
837
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
838
 * @return int Returns the position of the first occurrence of search in the string.
839
 */
840
function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
841
    if ( function_exists( 'mb_strpos' ) ) {
842
        return mb_strpos( $str, $find, $offset, $encoding );
843
    }
844
        
845
    return strpos( $str, $find, $offset );
846
}
847
848
/**
849
 * Find position of last occurrence of a string in a string.
850
 *
851
 * @since 1.0.0
852
 *
853
 * @param string $str The string being checked, for the last occurrence of search.
854
 * @param string $find The string to find in input string.
855
 * @param int $offset Specifies begin searching an arbitrary number of characters into the string.
856
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
857
 * @return int Returns the position of the last occurrence of search.
858
 */
859
function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
860
    if ( function_exists( 'mb_strrpos' ) ) {
861
        return mb_strrpos( $str, $find, $offset, $encoding );
862
    }
863
        
864
    return strrpos( $str, $find, $offset );
865
}
866
867
/**
868
 * Get the part of string.
869
 *
870
 * @since 1.0.0
871
 *
872
 * @param string $str The string to extract the substring from.
873
 * @param int $start If start is non-negative, the returned string will start at the entered position in string, counting from zero.
874
 *                      If start is negative, the returned string will start at the entered position from the end of string. 
875
 * @param int|null $length Maximum number of characters to use from string.
876
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
877
 * @return string
878
 */
879
function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) {
880
    if ( function_exists( 'mb_substr' ) ) {
881
        if ( $length === null ) {
882
            return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding );
883
        } else {
884
            return mb_substr( $str, $start, $length, $encoding );
885
        }
886
    }
887
        
888
    return substr( $str, $start, $length );
889
}
890
891
/**
892
 * Get the width of string.
893
 *
894
 * @since 1.0.0
895
 *
896
 * @param string $str The string being decoded.
897
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
898
 * @return string The width of string.
899
 */
900
function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) {
901
    if ( function_exists( 'mb_strwidth' ) ) {
902
        return mb_strwidth( $str, $encoding );
903
    }
904
    
905
    return wpinv_utf8_strlen( $str, $encoding );
906
}
907
908
function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) {
909
    if ( function_exists( 'mb_strlen' ) ) {
910
        $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding );
911
        $str_end = "";
0 ignored issues
show
Unused Code introduced by
$str_end is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
912
        
913
        if ( $lower_str_end ) {
914
            $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding );
915
        } else {
916
            $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding );
917
        }
918
919
        return $first_letter . $str_end;
920
    }
921
    
922
    return ucfirst( $str );
923
}
924
925
function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) {
926
    if ( function_exists( 'mb_convert_case' ) ) {
927
        return mb_convert_case( $str, MB_CASE_TITLE, $encoding );
928
    }
929
    
930
    return ucwords( $str );
931
}
932
933
function wpinv_period_in_days( $period, $unit ) {
934
    $period = absint( $period );
935
    
936
    if ( $period > 0 ) {
937
        if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) {
938
            $period = $period * 7;
939
        } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) {
940
            $period = $period * 30;
941
        } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) {
942
            $period = $period * 365;
943
        }
944
    }
945
    
946
    return $period;
947
}
948