Passed
Pull Request — master (#375)
by Brian
101:34
created

getpaid_convert_price_string_to_options()   B

Complexity

Conditions 11
Paths 26

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 40
rs 7.3166
cc 11
nc 26
nop 1

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 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 ) ) ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
54
        if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
55
            $amount = str_replace( $thousands_sep, '', $amount );
56
        } elseif( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) {
57
            $amount = str_replace( '.', '', $amount );
58
        }
59
60
        $amount = str_replace( $decimal_sep, '.', $amount );
61
    } 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( $draft = false, $trashed = false, $invoice = false ) {
93
    global $post;
94
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 ( $draft ) {
107
        $invoice_statuses['draft'] = __( 'Draft', 'invoicing' );
108
    }
109
110
    if ( $trashed ) {
111
        $invoice_statuses['trash'] = __( 'Trash', 'invoicing' );
112
    }
113
114
    return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice );
115
}
116
117
function wpinv_status_nicename( $status ) {
118
    $statuses = wpinv_get_invoice_statuses( true, true );
119
    $status   = isset( $statuses[$status] ) ? $statuses[$status] : __( $status, 'invoicing' );
120
121
    return $status;
122
}
123
124
function wpinv_get_currency() {
125
    $currency = wpinv_get_option( 'currency', 'USD' );
126
    
127
    return apply_filters( 'wpinv_currency', $currency );
128
}
129
130
function wpinv_currency_symbol( $currency = '' ) {
131
    if ( empty( $currency ) ) {
132
        $currency = wpinv_get_currency();
133
    }
134
    
135
    $symbols = apply_filters( 'wpinv_currency_symbols', array(
136
        'AED' => '&#x62f;.&#x625;',
137
        'AFN' => '&#x60b;',
138
        'ALL' => 'L',
139
        'AMD' => 'AMD',
140
        'ANG' => '&fnof;',
141
        'AOA' => 'Kz',
142
        'ARS' => '&#36;',
143
        'AUD' => '&#36;',
144
        'AWG' => '&fnof;',
145
        'AZN' => 'AZN',
146
        'BAM' => 'KM',
147
        'BBD' => '&#36;',
148
        'BDT' => '&#2547;',
149
        'BGN' => '&#1083;&#1074;.',
150
        'BHD' => '.&#x62f;.&#x628;',
151
        'BIF' => 'Fr',
152
        'BMD' => '&#36;',
153
        'BND' => '&#36;',
154
        'BOB' => 'Bs.',
155
        'BRL' => '&#82;&#36;',
156
        'BSD' => '&#36;',
157
        'BTC' => '&#3647;',
158
        'BTN' => 'Nu.',
159
        'BWP' => 'P',
160
        'BYN' => 'Br',
161
        'BZD' => '&#36;',
162
        'CAD' => '&#36;',
163
        'CDF' => 'Fr',
164
        'CHF' => '&#67;&#72;&#70;',
165
        'CLP' => '&#36;',
166
        'CNY' => '&yen;',
167
        'COP' => '&#36;',
168
        'CRC' => '&#x20a1;',
169
        'CUC' => '&#36;',
170
        'CUP' => '&#36;',
171
        'CVE' => '&#36;',
172
        'CZK' => '&#75;&#269;',
173
        'DJF' => 'Fr',
174
        'DKK' => 'DKK',
175
        'DOP' => 'RD&#36;',
176
        'DZD' => '&#x62f;.&#x62c;',
177
        'EGP' => 'EGP',
178
        'ERN' => 'Nfk',
179
        'ETB' => 'Br',
180
        'EUR' => '&euro;',
181
        'FJD' => '&#36;',
182
        'FKP' => '&pound;',
183
        'GBP' => '&pound;',
184
        'GEL' => '&#x10da;',
185
        'GGP' => '&pound;',
186
        'GHS' => '&#x20b5;',
187
        'GIP' => '&pound;',
188
        'GMD' => 'D',
189
        'GNF' => 'Fr',
190
        'GTQ' => 'Q',
191
        'GYD' => '&#36;',
192
        'HKD' => '&#36;',
193
        'HNL' => 'L',
194
        'HRK' => 'Kn',
195
        'HTG' => 'G',
196
        'HUF' => '&#70;&#116;',
197
        'IDR' => 'Rp',
198
        'ILS' => '&#8362;',
199
        'IMP' => '&pound;',
200
        'INR' => '&#8377;',
201
        'IQD' => '&#x639;.&#x62f;',
202
        'IRR' => '&#xfdfc;',
203
        'IRT' => '&#x062A;&#x0648;&#x0645;&#x0627;&#x0646;',
204
        'ISK' => 'kr.',
205
        'JEP' => '&pound;',
206
        'JMD' => '&#36;',
207
        'JOD' => '&#x62f;.&#x627;',
208
        'JPY' => '&yen;',
209
        'KES' => 'KSh',
210
        'KGS' => '&#x441;&#x43e;&#x43c;',
211
        'KHR' => '&#x17db;',
212
        'KMF' => 'Fr',
213
        'KPW' => '&#x20a9;',
214
        'KRW' => '&#8361;',
215
        'KWD' => '&#x62f;.&#x643;',
216
        'KYD' => '&#36;',
217
        'KZT' => 'KZT',
218
        'LAK' => '&#8365;',
219
        'LBP' => '&#x644;.&#x644;',
220
        'LKR' => '&#xdbb;&#xdd4;',
221
        'LRD' => '&#36;',
222
        'LSL' => 'L',
223
        'LYD' => '&#x644;.&#x62f;',
224
        'MAD' => '&#x62f;.&#x645;.',
225
        'MDL' => 'MDL',
226
        'MGA' => 'Ar',
227
        'MKD' => '&#x434;&#x435;&#x43d;',
228
        'MMK' => 'Ks',
229
        'MNT' => '&#x20ae;',
230
        'MOP' => 'P',
231
        'MRO' => 'UM',
232
        'MUR' => '&#x20a8;',
233
        'MVR' => '.&#x783;',
234
        'MWK' => 'MK',
235
        'MXN' => '&#36;',
236
        'MYR' => '&#82;&#77;',
237
        'MZN' => 'MT',
238
        'NAD' => '&#36;',
239
        'NGN' => '&#8358;',
240
        'NIO' => 'C&#36;',
241
        'NOK' => '&#107;&#114;',
242
        'NPR' => '&#8360;',
243
        'NZD' => '&#36;',
244
        'OMR' => '&#x631;.&#x639;.',
245
        'PAB' => 'B/.',
246
        'PEN' => 'S/.',
247
        'PGK' => 'K',
248
        'PHP' => '&#8369;',
249
        'PKR' => '&#8360;',
250
        'PLN' => '&#122;&#322;',
251
        'PRB' => '&#x440;.',
252
        'PYG' => '&#8370;',
253
        'QAR' => '&#x631;.&#x642;',
254
        'RMB' => '&yen;',
255
        'RON' => 'lei',
256
        'RSD' => '&#x434;&#x438;&#x43d;.',
257
        'RUB' => '&#8381;',
258
        'RWF' => 'Fr',
259
        'SAR' => '&#x631;.&#x633;',
260
        'SBD' => '&#36;',
261
        'SCR' => '&#x20a8;',
262
        'SDG' => '&#x62c;.&#x633;.',
263
        'SEK' => '&#107;&#114;',
264
        'SGD' => '&#36;',
265
        'SHP' => '&pound;',
266
        'SLL' => 'Le',
267
        'SOS' => 'Sh',
268
        'SRD' => '&#36;',
269
        'SSP' => '&pound;',
270
        'STD' => 'Db',
271
        'SYP' => '&#x644;.&#x633;',
272
        'SZL' => 'L',
273
        'THB' => '&#3647;',
274
        'TJS' => '&#x405;&#x41c;',
275
        'TMT' => 'm',
276
        'TND' => '&#x62f;.&#x62a;',
277
        'TOP' => 'T&#36;',
278
        'TRY' => '&#8378;',
279
        'TTD' => '&#36;',
280
        'TWD' => '&#78;&#84;&#36;',
281
        'TZS' => 'Sh',
282
        'UAH' => '&#8372;',
283
        'UGX' => 'UGX',
284
        'USD' => '&#36;',
285
        'UYU' => '&#36;',
286
        'UZS' => 'UZS',
287
        'VEF' => 'Bs.',
288
        'VND' => '&#8363;',
289
        'VUV' => 'Vt',
290
        'WST' => 'T',
291
        'XAF' => 'Fr',
292
        'XCD' => '&#36;',
293
        'XOF' => 'Fr',
294
        'XPF' => 'Fr',
295
        'YER' => '&#xfdfc;',
296
        'ZAR' => '&#82;',
297
        'ZMW' => 'ZK',
298
    ) );
299
300
    $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency;
301
302
    return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency );
303
}
304
305
function wpinv_currency_position() {
306
    $position = wpinv_get_option( 'currency_position', 'left' );
307
    
308
    return apply_filters( 'wpinv_currency_position', $position );
309
}
310
311
function wpinv_thousands_separator() {
312
    $thousand_sep = wpinv_get_option( 'thousands_separator', ',' );
313
    
314
    return apply_filters( 'wpinv_thousands_separator', $thousand_sep );
315
}
316
317
function wpinv_decimal_separator() {
318
    $decimal_sep = wpinv_get_option( 'decimal_separator', '.' );
319
    
320
    return apply_filters( 'wpinv_decimal_separator', $decimal_sep );
321
}
322
323
function wpinv_decimals() {
324
    $decimals = apply_filters( 'wpinv_decimals', wpinv_get_option( 'decimals', 2 ) );
325
    
326
    return absint( $decimals );
327
}
328
329
function wpinv_get_currencies() {
330
    $currencies = array(
331
        'USD' => __( 'US Dollar', 'invoicing' ),
332
        'EUR' => __( 'Euro', 'invoicing' ),
333
        'GBP' => __( 'Pound Sterling', 'invoicing' ),
334
        'AED' => __( 'United Arab Emirates', 'invoicing' ),
335
        'AFN' => __( 'Afghan Afghani', 'invoicing' ),
336
        'ALL' => __( 'Albanian Lek', 'invoicing' ),
337
        'AMD' => __( 'Armenian Dram', 'invoicing' ),
338
        'ANG' => __( 'Netherlands Antillean Guilder', 'invoicing' ),
339
        'AOA' => __( 'Angolan Kwanza', 'invoicing' ),
340
        'ARS' => __( 'Argentine Peso', 'invoicing' ),
341
        'AUD' => __( 'Australian Dollar', 'invoicing' ),
342
        'AWG' => __( 'Aruban Florin', 'invoicing' ),
343
        'AZN' => __( 'Azerbaijani Manat', 'invoicing' ),
344
        'BAM' => __( 'Bosnia and Herzegovina Convertible Marka', 'invoicing' ),
345
        'BBD' => __( 'Barbadian Dollar', 'invoicing' ),
346
        'BDT' => __( 'Bangladeshi Taka', 'invoicing' ),
347
        'BGN' => __( 'Bulgarian Lev', 'invoicing' ),
348
        'BHD' => __( 'Bahraini Dinar', 'invoicing' ),
349
        'BIF' => __( 'Burundian Franc', 'invoicing' ),
350
        'BMD' => __( 'Bermudian Dollar', 'invoicing' ),
351
        'BND' => __( 'Brunei Dollar', 'invoicing' ),
352
        'BOB' => __( 'Bolivian Boliviano', 'invoicing' ),
353
        'BRL' => __( 'Brazilian Real', 'invoicing' ),
354
        'BSD' => __( 'Bahamian Dollar', 'invoicing' ),
355
        'BTC' => __( 'Bitcoin', 'invoicing' ),
356
        'BTN' => __( 'Bhutanese Ngultrum', 'invoicing' ),
357
        'BWP' => __( 'Botswana Pula', 'invoicing' ),
358
        'BYN' => __( 'Belarusian Ruble', 'invoicing' ),
359
        'BZD' => __( 'Belize Dollar', 'invoicing' ),
360
        'CAD' => __( 'Canadian Dollar', 'invoicing' ),
361
        'CDF' => __( 'Congolese Franc', 'invoicing' ),
362
        'CHF' => __( 'Swiss Franc', 'invoicing' ),
363
        'CLP' => __( 'Chilean Peso', 'invoicing' ),
364
        'CNY' => __( 'Chinese Yuan', 'invoicing' ),
365
        'COP' => __( 'Colombian Peso', 'invoicing' ),
366
        'CRC' => __( 'Costa Rican Colon', 'invoicing' ),
367
        'CUC' => __( 'Cuban Convertible Peso', 'invoicing' ),
368
        'CUP' => __( 'Cuban Peso', 'invoicing' ),
369
        'CVE' => __( 'Cape Verdean escudo', 'invoicing' ),
370
        'CZK' => __( 'Czech Koruna', 'invoicing' ),
371
        'DJF' => __( 'Djiboutian Franc', 'invoicing' ),
372
        'DKK' => __( 'Danish Krone', 'invoicing' ),
373
        'DOP' => __( 'Dominican Peso', 'invoicing' ),
374
        'DZD' => __( 'Algerian Dinar', 'invoicing' ),
375
        'EGP' => __( 'Egyptian Pound', 'invoicing' ),
376
        'ERN' => __( 'Eritrean Nakfa', 'invoicing' ),
377
        'ETB' => __( 'Ethiopian Irr', 'invoicing' ),
378
        'FJD' => __( 'Fijian Dollar', 'invoicing' ),
379
        'FKP' => __( 'Falkland Islands Pound', 'invoicing' ),
380
        'GEL' => __( 'Georgian Lari', 'invoicing' ),
381
        'GGP' => __( 'Guernsey Pound', 'invoicing' ),
382
        'GHS' => __( 'Ghana Cedi', 'invoicing' ),
383
        'GIP' => __( 'Gibraltar Pound', 'invoicing' ),
384
        'GMD' => __( 'Gambian Dalasi', 'invoicing' ),
385
        'GNF' => __( 'Guinean Franc', 'invoicing' ),
386
        'GTQ' => __( 'Guatemalan Quetzal', 'invoicing' ),
387
        'GYD' => __( 'Guyanese Dollar', 'invoicing' ),
388
        'HKD' => __( 'Hong Kong Dollar', 'invoicing' ),
389
        'HNL' => __( 'Honduran Lempira', 'invoicing' ),
390
        'HRK' => __( 'Croatian Kuna', 'invoicing' ),
391
        'HTG' => __( 'Haitian Gourde', 'invoicing' ),
392
        'HUF' => __( 'Hungarian Forint', 'invoicing' ),
393
        'IDR' => __( 'Indonesian Rupiah', 'invoicing' ),
394
        'ILS' => __( 'Israeli New Shekel', 'invoicing' ),
395
        'IMP' => __( 'Manx Pound', 'invoicing' ),
396
        'INR' => __( 'Indian Rupee', 'invoicing' ),
397
        'IQD' => __( 'Iraqi Dinar', 'invoicing' ),
398
        'IRR' => __( 'Iranian Rial', 'invoicing' ),
399
        'IRT' => __( 'Iranian Toman', 'invoicing' ),
400
        'ISK' => __( 'Icelandic Krona', 'invoicing' ),
401
        'JEP' => __( 'Jersey Pound', 'invoicing' ),
402
        'JMD' => __( 'Jamaican Dollar', 'invoicing' ),
403
        'JOD' => __( 'Jordanian Dinar', 'invoicing' ),
404
        'JPY' => __( 'Japanese Yen', 'invoicing' ),
405
        'KES' => __( 'Kenyan Shilling', 'invoicing' ),
406
        'KGS' => __( 'Kyrgyzstani Som', 'invoicing' ),
407
        'KHR' => __( 'Cambodian Riel', 'invoicing' ),
408
        'KMF' => __( 'Comorian Franc', 'invoicing' ),
409
        'KPW' => __( 'North Korean Won', 'invoicing' ),
410
        'KRW' => __( 'South Korean Won', 'invoicing' ),
411
        'KWD' => __( 'Kuwaiti Dinar', 'invoicing' ),
412
        'KYD' => __( 'Cayman Islands Dollar', 'invoicing' ),
413
        'KZT' => __( 'Kazakhstani Tenge', 'invoicing' ),
414
        'LAK' => __( 'Lao Kip', 'invoicing' ),
415
        'LBP' => __( 'Lebanese Pound', 'invoicing' ),
416
        'LKR' => __( 'Sri Lankan Rupee', 'invoicing' ),
417
        'LRD' => __( 'Liberian Dollar', 'invoicing' ),
418
        'LSL' => __( 'Lesotho Loti', 'invoicing' ),
419
        'LYD' => __( 'Libyan Dinar', 'invoicing' ),
420
        'MAD' => __( 'Moroccan Dirham', 'invoicing' ),
421
        'MDL' => __( 'Moldovan Leu', 'invoicing' ),
422
        'MGA' => __( 'Malagasy Ariary', 'invoicing' ),
423
        'MKD' => __( 'Macedonian Denar', 'invoicing' ),
424
        'MMK' => __( 'Burmese Kyat', 'invoicing' ),
425
        'MNT' => __( 'Mongolian Tughrik', 'invoicing' ),
426
        'MOP' => __( 'Macanese Pataca', 'invoicing' ),
427
        'MRO' => __( 'Mauritanian Ouguiya', 'invoicing' ),
428
        'MUR' => __( 'Mauritian Rupee', 'invoicing' ),
429
        'MVR' => __( 'Maldivian Rufiyaa', 'invoicing' ),
430
        'MWK' => __( 'Malawian Kwacha', 'invoicing' ),
431
        'MXN' => __( 'Mexican Peso', 'invoicing' ),
432
        'MYR' => __( 'Malaysian Ringgit', 'invoicing' ),
433
        'MZN' => __( 'Mozambican Metical', 'invoicing' ),
434
        'NAD' => __( 'Namibian Dollar', 'invoicing' ),
435
        'NGN' => __( 'Nigerian Naira', 'invoicing' ),
436
        'NIO' => __( 'Nicaraguan Cordoba', 'invoicing' ),
437
        'NOK' => __( 'Norwegian Krone', 'invoicing' ),
438
        'NPR' => __( 'Nepalese Rupee', 'invoicing' ),
439
        'NZD' => __( 'New Zealand Dollar', 'invoicing' ),
440
        'OMR' => __( 'Omani Rial', 'invoicing' ),
441
        'PAB' => __( 'Panamanian Balboa', 'invoicing' ),
442
        'PEN' => __( 'Peruvian Nuevo Sol', 'invoicing' ),
443
        'PGK' => __( 'Papua New Guinean Kina', 'invoicing' ),
444
        'PHP' => __( 'Philippine Peso', 'invoicing' ),
445
        'PKR' => __( 'Pakistani Rupee', 'invoicing' ),
446
        'PLN' => __( 'Polish Zloty', 'invoicing' ),
447
        'PRB' => __( 'Transnistrian Ruble', 'invoicing' ),
448
        'PYG' => __( 'Paraguayan Guarani', 'invoicing' ),
449
        'QAR' => __( 'Qatari Riyal', 'invoicing' ),
450
        'RON' => __( 'Romanian Leu', 'invoicing' ),
451
        'RSD' => __( 'Serbian Dinar', 'invoicing' ),
452
        'RUB' => __( 'Russian Ruble', 'invoicing' ),
453
        'RWF' => __( 'Rwandan Franc', 'invoicing' ),
454
        'SAR' => __( 'Saudi Riyal', 'invoicing' ),
455
        'SBD' => __( 'Solomon Islands Dollar', 'invoicing' ),
456
        'SCR' => __( 'Seychellois Rupee', 'invoicing' ),
457
        'SDG' => __( 'Sudanese Pound', 'invoicing' ),
458
        'SEK' => __( 'Swedish Krona', 'invoicing' ),
459
        'SGD' => __( 'Singapore Dollar', 'invoicing' ),
460
        'SHP' => __( 'Saint Helena Pound', 'invoicing' ),
461
        'SLL' => __( 'Sierra Leonean Leone', 'invoicing' ),
462
        'SOS' => __( 'Somali Shilling', 'invoicing' ),
463
        'SRD' => __( 'Surinamese Dollar', 'invoicing' ),
464
        'SSP' => __( 'South Sudanese Pound', 'invoicing' ),
465
        'STD' => __( 'Sao Tomean Dobra', 'invoicing' ),
466
        'SYP' => __( 'Syrian Pound', 'invoicing' ),
467
        'SZL' => __( 'Swazi Lilangeni', 'invoicing' ),
468
        'THB' => __( 'Thai Baht', 'invoicing' ),
469
        'TJS' => __( 'Tajikistani Somoni', 'invoicing' ),
470
        'TMT' => __( 'Turkmenistan Manat', 'invoicing' ),
471
        'TND' => __( 'Tunisian Dinar', 'invoicing' ),
472
        'TOP' => __( 'Tongan Pa&#x2bb;anga', 'invoicing' ),
473
        'TRY' => __( 'Turkish Lira', 'invoicing' ),
474
        'TTD' => __( 'Trinidad and Tobago Dollar', 'invoicing' ),
475
        'TWD' => __( 'New Taiwan Dollar', 'invoicing' ),
476
        'TZS' => __( 'Tanzanian Shilling', 'invoicing' ),
477
        'UAH' => __( 'Ukrainian Hryvnia', 'invoicing' ),
478
        'UGX' => __( 'Ugandan Shilling', 'invoicing' ),
479
        'UYU' => __( 'Uruguayan Peso', 'invoicing' ),
480
        'UZS' => __( 'Uzbekistani Som', 'invoicing' ),
481
        'VEF' => __( 'Venezuelan Bol&iacute;var', 'invoicing' ),
482
        'VND' => __( 'Vietnamese Dong', 'invoicing' ),
483
        'VUV' => __( 'Vanuatu Vatu', 'invoicing' ),
484
        'WST' => __( 'Samoan Tala', 'invoicing' ),
485
        'XAF' => __( 'Central African CFA Franc', 'invoicing' ),
486
        'XCD' => __( 'East Caribbean Dollar', 'invoicing' ),
487
        'XOF' => __( 'West African CFA Franc', 'invoicing' ),
488
        'XPF' => __( 'CFP Franc', 'invoicing' ),
489
        'YER' => __( 'Yemeni Rial', 'invoicing' ),
490
        'ZAR' => __( 'South African Rand', 'invoicing' ),
491
        'ZMW' => __( 'Zambian Kwacha', 'invoicing' ),
492
    );
493
    
494
    //asort( $currencies ); // this
495
496
    return apply_filters( 'wpinv_currencies', $currencies );
497
}
498
499
function wpinv_price( $amount = '', $currency = '' ) {
500
    if( empty( $currency ) ) {
501
        $currency = wpinv_get_currency();
502
    }
503
504
    $position = wpinv_currency_position();
505
506
    $negative = $amount < 0;
507
508
    if ( $negative ) {
509
        $amount = substr( $amount, 1 );
510
    }
511
512
    $symbol = wpinv_currency_symbol( $currency );
513
514
    if ( $position == 'left' || $position == 'left_space' ) {
515
        switch ( $currency ) {
516
            case "GBP" :
517
            case "BRL" :
518
            case "EUR" :
519
            case "USD" :
520
            case "AUD" :
521
            case "CAD" :
522
            case "HKD" :
523
            case "MXN" :
524
            case "NZD" :
525
            case "SGD" :
526
            case "JPY" :
527
                $price = $position == 'left_space' ? $symbol . ' ' .  $amount : $symbol . $amount;
528
                break;
529
            default :
530
                //$price = $currency . ' ' . $amount;
531
                $price = $position == 'left_space' ? $symbol . ' ' .  $amount : $symbol . $amount;
532
                break;
533
        }
534
    } else {
535
        switch ( $currency ) {
536
            case "GBP" :
537
            case "BRL" :
538
            case "EUR" :
539
            case "USD" :
540
            case "AUD" :
541
            case "CAD" :
542
            case "HKD" :
543
            case "MXN" :
544
            case "SGD" :
545
            case "JPY" :
546
                $price = $position == 'right_space' ? $amount . ' ' .  $symbol : $amount . $symbol;
547
                break;
548
            default :
549
                //$price = $amount . ' ' . $currency;
550
                $price = $position == 'right_space' ? $amount . ' ' .  $symbol : $amount . $symbol;
551
                break;
552
        }
553
    }
554
    
555
    if ( $negative ) {
556
        $price = '-' . $price;
557
    }
558
    
559
    $price = apply_filters( 'wpinv_' . strtolower( $currency ) . '_currency_filter_' . $position, $price, $currency, $amount );
560
561
    return $price;
562
}
563
564
function wpinv_format_amount( $amount, $decimals = NULL, $calculate = false ) {
565
    $thousands_sep = wpinv_thousands_separator();
566
    $decimal_sep   = wpinv_decimal_separator();
567
568
    if ( $decimals === NULL ) {
569
        $decimals = wpinv_decimals();
570
    }
571
572
    if ( $decimal_sep == ',' && false !== ( $sep_found = strpos( $amount, $decimal_sep ) ) ) {
573
        $whole = substr( $amount, 0, $sep_found );
574
        $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
575
        $amount = $whole . '.' . $part;
576
    }
577
578
    if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
579
        $amount = str_replace( ',', '', $amount );
580
    }
581
582
    if ( $thousands_sep == ' ' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
583
        $amount = str_replace( ' ', '', $amount );
584
    }
585
586
    if ( empty( $amount ) ) {
587
        $amount = 0;
588
    }
589
    
590
    $decimals  = apply_filters( 'wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate );
591
    $formatted = number_format( (float)$amount, $decimals, $decimal_sep, $thousands_sep );
592
    
593
    if ( $calculate ) {
594
        if ( $thousands_sep === "," ) {
595
            $formatted = str_replace( ",", "", $formatted );
596
        }
597
        
598
        if ( $decimal_sep === "," ) {
599
            $formatted = str_replace( ",", ".", $formatted );
600
        }
601
    }
602
603
    return apply_filters( 'wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate );
604
}
605
add_filter( 'wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1 );
606
607
function wpinv_sanitize_key( $key ) {
608
    $raw_key = $key;
609
    $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key );
610
611
    return apply_filters( 'wpinv_sanitize_key', $key, $raw_key );
612
}
613
614
function wpinv_get_file_extension( $str ) {
615
    $parts = explode( '.', $str );
616
    return end( $parts );
617
}
618
619
function wpinv_string_is_image_url( $str ) {
620
    $ext = wpinv_get_file_extension( $str );
621
622
    switch ( strtolower( $ext ) ) {
623
        case 'jpeg';
624
        case 'jpg';
625
            $return = true;
626
            break;
627
        case 'png';
628
            $return = true;
629
            break;
630
        case 'gif';
631
            $return = true;
632
            break;
633
        default:
634
            $return = false;
635
            break;
636
    }
637
638
    return (bool)apply_filters( 'wpinv_string_is_image', $return, $str );
639
}
640
641
function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) {
642
    $should_log = apply_filters( 'wpinv_log_errors', WP_DEBUG );
643
    
644
    if ( true === $should_log ) {
645
        $label = '';
646
        if ( $file && $file !== '' ) {
647
            $label .= basename( $file ) . ( $line ? '(' . $line . ')' : '' );
648
        }
649
        
650
        if ( $title && $title !== '' ) {
651
            $label = $label !== '' ? $label . ' ' : '';
652
            $label .= $title . ' ';
653
        }
654
        
655
        $label = $label !== '' ? trim( $label ) . ' : ' : '';
656
        
657
        if ( is_array( $log ) || is_object( $log ) ) {
658
            error_log( $label . print_r( $log, true ) );
659
        } else {
660
            error_log( $label . $log );
661
        }
662
663
        error_log( wp_debug_backtrace_summary() );
664
        if ( $exit ) {
665
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
666
        }
667
    }
668
}
669
670
function wpinv_is_ajax_disabled() {
671
    $retval = false;
672
    return apply_filters( 'wpinv_is_ajax_disabled', $retval );
673
}
674
675
function wpinv_get_current_page_url( $nocache = false ) {
676
    global $wp;
677
678
    if ( get_option( 'permalink_structure' ) ) {
679
        $base = trailingslashit( home_url( $wp->request ) );
680
    } else {
681
        $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) );
682
        $base = remove_query_arg( array( 'post_type', 'name' ), $base );
683
    }
684
685
    $scheme = is_ssl() ? 'https' : 'http';
686
    $uri    = set_url_scheme( $base, $scheme );
687
688
    if ( is_front_page() ) {
689
        $uri = home_url( '/' );
690
    } 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(). ( Ignorable by Annotation )

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

690
    } elseif ( /** @scrutinizer ignore-call */ wpinv_is_checkout( array(), false ) ) {

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. Please note the @ignore annotation hint above.

Loading history...
691
        $uri = wpinv_get_checkout_uri();
692
    }
693
694
    $uri = apply_filters( 'wpinv_get_current_page_url', $uri );
695
696
    if ( $nocache ) {
697
        $uri = wpinv_add_cache_busting( $uri );
0 ignored issues
show
Bug introduced by
The function wpinv_add_cache_busting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

697
        $uri = /** @scrutinizer ignore-call */ wpinv_add_cache_busting( $uri );
Loading history...
698
    }
699
700
    return $uri;
701
}
702
703
/**
704
 * Define a constant if it is not already defined.
705
 *
706
 * @since 1.0.19
707
 * @param string $name  Constant name.
708
 * @param mixed  $value Value.
709
 */
710
function getpaid_maybe_define_constant( $name, $value ) {
711
	if ( ! defined( $name ) ) {
712
		define( $name, $value );
713
	}
714
}
715
716
function wpinv_get_php_arg_separator_output() {
717
	return ini_get( 'arg_separator.output' );
718
}
719
720
function wpinv_rgb_from_hex( $color ) {
721
    $color = str_replace( '#', '', $color );
722
723
    // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
724
    $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
725
    if ( empty( $color ) ) {
726
        return NULL;
727
    }
728
729
    $color = str_split( $color );
730
731
    $rgb      = array();
732
    $rgb['R'] = hexdec( $color[0] . $color[1] );
733
    $rgb['G'] = hexdec( $color[2] . $color[3] );
734
    $rgb['B'] = hexdec( $color[4] . $color[5] );
735
736
    return $rgb;
737
}
738
739
function wpinv_hex_darker( $color, $factor = 30 ) {
740
    $base  = wpinv_rgb_from_hex( $color );
741
    $color = '#';
742
743
    foreach ( $base as $k => $v ) {
744
        $amount      = $v / 100;
745
        $amount      = round( $amount * $factor );
746
        $new_decimal = $v - $amount;
747
748
        $new_hex_component = dechex( $new_decimal );
0 ignored issues
show
Bug introduced by
$new_decimal of type double is incompatible with the type integer expected by parameter $number of dechex(). ( Ignorable by Annotation )

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

748
        $new_hex_component = dechex( /** @scrutinizer ignore-type */ $new_decimal );
Loading history...
749
        if ( strlen( $new_hex_component ) < 2 ) {
750
            $new_hex_component = "0" . $new_hex_component;
751
        }
752
        $color .= $new_hex_component;
753
    }
754
755
    return $color;
756
}
757
758
function wpinv_hex_lighter( $color, $factor = 30 ) {
759
    $base  = wpinv_rgb_from_hex( $color );
760
    $color = '#';
761
762
    foreach ( $base as $k => $v ) {
763
        $amount      = 255 - $v;
764
        $amount      = $amount / 100;
765
        $amount      = round( $amount * $factor );
766
        $new_decimal = $v + $amount;
767
768
        $new_hex_component = dechex( $new_decimal );
0 ignored issues
show
Bug introduced by
$new_decimal of type double is incompatible with the type integer expected by parameter $number of dechex(). ( Ignorable by Annotation )

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

768
        $new_hex_component = dechex( /** @scrutinizer ignore-type */ $new_decimal );
Loading history...
769
        if ( strlen( $new_hex_component ) < 2 ) {
770
            $new_hex_component = "0" . $new_hex_component;
771
        }
772
        $color .= $new_hex_component;
773
    }
774
775
    return $color;
776
}
777
778
function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
779
    $hex = str_replace( '#', '', $color );
780
781
    $c_r = hexdec( substr( $hex, 0, 2 ) );
782
    $c_g = hexdec( substr( $hex, 2, 2 ) );
783
    $c_b = hexdec( substr( $hex, 4, 2 ) );
784
785
    $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
786
787
    return $brightness > 155 ? $dark : $light;
788
}
789
790
function wpinv_format_hex( $hex ) {
791
    $hex = trim( str_replace( '#', '', $hex ) );
792
793
    if ( strlen( $hex ) == 3 ) {
794
        $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
795
    }
796
797
    return $hex ? '#' . $hex : null;
798
}
799
800
/**
801
 * Get truncated string with specified width.
802
 *
803
 * @since 1.0.0
804
 *
805
 * @param string $str The string being decoded.
806
 * @param int $start The start position offset. Number of characters from the beginning of string.
807
 *                      For negative value, number of characters from the end of the string.
808
 * @param int $width The width of the desired trim. Negative widths count from the end of the string.
809
 * @param string $trimmaker A string that is added to the end of string when string is truncated. Ex: "...".
810
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
811
 * @return string
812
 */
813
function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) {
814
    if ( function_exists( 'mb_strimwidth' ) ) {
815
        return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding );
816
    }
817
    
818
    return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker;
819
}
820
821
/**
822
 * Get the string length.
823
 *
824
 * @since 1.0.0
825
 *
826
 * @param string $str The string being checked for length. 
827
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
828
 * @return int Returns the number of characters in string.
829
 */
830
function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) {
831
    if ( function_exists( 'mb_strlen' ) ) {
832
        return mb_strlen( $str, $encoding );
833
    }
834
        
835
    return strlen( $str );
836
}
837
838
function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) {
839
    if ( function_exists( 'mb_strtolower' ) ) {
840
        return mb_strtolower( $str, $encoding );
841
    }
842
    
843
    return strtolower( $str );
844
}
845
846
function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) {
847
    if ( function_exists( 'mb_strtoupper' ) ) {
848
        return mb_strtoupper( $str, $encoding );
849
    }
850
    
851
    return strtoupper( $str );
852
}
853
854
/**
855
 * Find position of first occurrence of string in a string
856
 *
857
 * @since 1.0.0
858
 *
859
 * @param string $str The string being checked.
860
 * @param string $find The string to find in input string.
861
 * @param int $offset The search offset. Default "0". A negative offset counts from the end of the string.
862
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
863
 * @return int Returns the position of the first occurrence of search in the string.
864
 */
865
function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
866
    if ( function_exists( 'mb_strpos' ) ) {
867
        return mb_strpos( $str, $find, $offset, $encoding );
868
    }
869
        
870
    return strpos( $str, $find, $offset );
871
}
872
873
/**
874
 * Find position of last occurrence of a string in a string.
875
 *
876
 * @since 1.0.0
877
 *
878
 * @param string $str The string being checked, for the last occurrence of search.
879
 * @param string $find The string to find in input string.
880
 * @param int $offset Specifies begin searching an arbitrary number of characters into the string.
881
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
882
 * @return int Returns the position of the last occurrence of search.
883
 */
884
function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
885
    if ( function_exists( 'mb_strrpos' ) ) {
886
        return mb_strrpos( $str, $find, $offset, $encoding );
887
    }
888
        
889
    return strrpos( $str, $find, $offset );
890
}
891
892
/**
893
 * Get the part of string.
894
 *
895
 * @since 1.0.0
896
 *
897
 * @param string $str The string to extract the substring from.
898
 * @param int $start If start is non-negative, the returned string will start at the entered position in string, counting from zero.
899
 *                      If start is negative, the returned string will start at the entered position from the end of string. 
900
 * @param int|null $length Maximum number of characters to use from string.
901
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
902
 * @return string
903
 */
904
function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) {
905
    if ( function_exists( 'mb_substr' ) ) {
906
        if ( $length === null ) {
907
            return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding );
908
        } else {
909
            return mb_substr( $str, $start, $length, $encoding );
910
        }
911
    }
912
        
913
    return substr( $str, $start, $length );
914
}
915
916
/**
917
 * Get the width of string.
918
 *
919
 * @since 1.0.0
920
 *
921
 * @param string $str The string being decoded.
922
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
923
 * @return string The width of string.
924
 */
925
function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) {
926
    if ( function_exists( 'mb_strwidth' ) ) {
927
        return mb_strwidth( $str, $encoding );
928
    }
929
    
930
    return wpinv_utf8_strlen( $str, $encoding );
931
}
932
933
function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) {
934
    if ( function_exists( 'mb_strlen' ) ) {
935
        $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding );
936
        $str_end = "";
937
        
938
        if ( $lower_str_end ) {
939
            $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding );
940
        } else {
941
            $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding );
942
        }
943
944
        return $first_letter . $str_end;
945
    }
946
    
947
    return ucfirst( $str );
948
}
949
950
function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) {
951
    if ( function_exists( 'mb_convert_case' ) ) {
952
        return mb_convert_case( $str, MB_CASE_TITLE, $encoding );
953
    }
954
    
955
    return ucwords( $str );
956
}
957
958
function wpinv_period_in_days( $period, $unit ) {
959
    $period = absint( $period );
960
    
961
    if ( $period > 0 ) {
962
        if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) {
963
            $period = $period * 7;
964
        } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) {
965
            $period = $period * 30;
966
        } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) {
967
            $period = $period * 365;
968
        }
969
    }
970
    
971
    return $period;
972
}
973
974
function wpinv_cal_days_in_month( $calendar, $month, $year ) {
975
    if ( function_exists( 'cal_days_in_month' ) ) {
976
        return cal_days_in_month( $calendar, $month, $year );
977
    }
978
979
    // Fallback in case the calendar extension is not loaded in PHP
980
    // Only supports Gregorian calendar
981
    return date( 't', mktime( 0, 0, 0, $month, 1, $year ) );
982
}
983
984
/**
985
 * Display a help tip for settings.
986
 *
987
 * @param  string $tip Help tip text
988
 * @param  bool $allow_html Allow sanitized HTML if true or escape
989
 *
990
 * @return string
991
 */
992
function wpi_help_tip( $tip, $allow_html = false ) {
993
    if ( $allow_html ) {
994
        $tip = wpi_sanitize_tooltip( $tip );
995
    } else {
996
        $tip = esc_attr( $tip );
997
    }
998
999
    return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>';
1000
}
1001
1002
/**
1003
 * Sanitize a string destined to be a tooltip.
1004
 *
1005
 * Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
1006
 *
1007
 * @param string $var
1008
 * @return string
1009
 */
1010
function wpi_sanitize_tooltip( $var ) {
1011
    return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
1012
        'br'     => array(),
1013
        'em'     => array(),
1014
        'strong' => array(),
1015
        'small'  => array(),
1016
        'span'   => array(),
1017
        'ul'     => array(),
1018
        'li'     => array(),
1019
        'ol'     => array(),
1020
        'p'      => array(),
1021
    ) ) );
1022
}
1023
1024
/**
1025
 * Get all WPI screen ids.
1026
 *
1027
 * @return array
1028
 */
1029
function wpinv_get_screen_ids() {
1030
1031
    $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) );
1032
1033
    $screen_ids = array(
1034
        'toplevel_page_' . $screen_id,
1035
        'wpi_invoice',
1036
        'wpi_item',
1037
        'wpi_quote',
1038
        'wpi_discount',
1039
        'edit-wpi_invoice',
1040
        'edit-wpi_item',
1041
        'edit-wpi_discount',
1042
        'edit-wpi_quote',
1043
        'invoicing_page_wpinv-settings',
1044
        'invoicing_page_wpinv-subscriptions',
1045
        'invoicing_page_wpinv-reports',
1046
        'invoicing_page_wpi-addons',
1047
    );
1048
1049
    return apply_filters( 'wpinv_screen_ids', $screen_ids );
1050
}
1051
1052
/**
1053
 * Cleans up an array, comma- or space-separated list of scalar values.
1054
 *
1055
 * @since 1.0.13
1056
 *
1057
 * @param array|string $list List of values.
1058
 * @return array Sanitized array of values.
1059
 */
1060
function wpinv_parse_list( $list ) {
1061
	if ( ! is_array( $list ) ) {
1062
		return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY );
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('/[\s,...1, PREG_SPLIT_NO_EMPTY) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
1063
	}
1064
1065
	return $list;
1066
}
1067
1068
/**
1069
 * Fetches data stored on disk.
1070
 *
1071
 * @since 1.0.14
1072
 *
1073
 * @param string $key Type of data to fetch.
1074
 * @return mixed Fetched data.
1075
 */
1076
function wpinv_get_data( $key ) {
1077
    
1078
    // Try fetching it from the cache.
1079
    $data = wp_cache_get( "wpinv-$key", 'wpinv' );
1080
    if( $data ) {
1081
        return $data;
1082
    }
1083
1084
    $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" );
1085
	wp_cache_set( "wpinv-$key", $data, 'wpinv' );
1086
1087
	return $data;
1088
}
1089
1090
/**
1091
 * (Maybe) Adds an empty option to an array of options.
1092
 *
1093
 * @since 1.0.14
1094
 *
1095
 * @param array $options
1096
 * @param bool $first_empty Whether or not the first item in the list should be empty
1097
 * @return mixed Fetched data.
1098
 */
1099
function wpinv_maybe_add_empty_option( $options, $first_empty ) {
1100
1101
    if ( ! empty( $options ) && $first_empty ) {
1102
        return array_merge( array( '' => '' ), $options );
1103
    }
1104
    return $options;
1105
1106
}
1107
1108
/**
1109
 * Clean variables using sanitize_text_field.
1110
 *
1111
 * @param mixed $var Data to sanitize.
1112
 * @return string|array
1113
 */
1114
function wpinv_clean( $var ) {
1115
1116
	if ( is_array( $var ) ) {
1117
		return array_map( 'wpinv_clean', $var );
1118
    }
1119
1120
    if ( is_object( $var ) ) {
1121
		$object_vars = get_object_vars( $var );
1122
		foreach ( $object_vars as $property_name => $property_value ) {
1123
			$var->$property_name = wpinv_clean( $property_value );
1124
        }
1125
        return $var;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $var returns the type object which is incompatible with the documented return type array|string.
Loading history...
1126
	}
1127
    
1128
    return is_string( $var ) ? sanitize_text_field( $var ) : $var;
1129
}
1130
1131
/**
1132
 * Converts a price string into an options array.
1133
 *
1134
 * @param string $str Data to convert.
1135
 * @return string|array
1136
 */
1137
function getpaid_convert_price_string_to_options( $str ) {
1138
1139
	$raw_options = array_map( 'trim', explode( ',', $str ) );
1140
    $options     = array();
1141
1142
    foreach ( $raw_options as $option ) {
1143
1144
        if ( '' == $option ) {
1145
            continue;
1146
        }
1147
1148
        $option = array_map( 'trim', explode( '|', $option ) );
1149
1150
        $price = null;
1151
        $label = null;
1152
1153
        if ( isset( $option[0] ) && '' !=  $option[0] ) {
1154
            $label  = $option[0];
1155
        }
1156
1157
        if ( isset( $option[1] ) && '' !=  $option[1] ) {
1158
            $price = $option[1];
1159
        }
1160
1161
        if ( ! isset( $price ) ) {
1162
            $price = $label;
1163
        }
1164
1165
        if ( ! isset( $price ) || ! is_numeric( $price ) ) {
1166
            continue;
1167
        }
1168
1169
        if ( ! isset( $label ) ) {
1170
            $label = $price;
1171
        }
1172
1173
        $options[ $price ] = $label;
1174
    }
1175
1176
    return $options;
1177
}
1178