Passed
Push — master ( f897ad...1c210d )
by Brian
05:00 queued 12s
created

wpinv_get_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
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
        if ( $exit ) {
664
            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...
665
        }
666
    }
667
}
668
669
function wpinv_is_ajax_disabled() {
670
    $retval = false;
671
    return apply_filters( 'wpinv_is_ajax_disabled', $retval );
672
}
673
674
function wpinv_get_current_page_url( $nocache = false ) {
675
    global $wp;
676
677
    if ( get_option( 'permalink_structure' ) ) {
678
        $base = trailingslashit( home_url( $wp->request ) );
679
    } else {
680
        $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) );
681
        $base = remove_query_arg( array( 'post_type', 'name' ), $base );
682
    }
683
684
    $scheme = is_ssl() ? 'https' : 'http';
685
    $uri    = set_url_scheme( $base, $scheme );
686
687
    if ( is_front_page() ) {
688
        $uri = home_url( '/' );
689
    } 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

689
    } 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...
690
        $uri = wpinv_get_checkout_uri();
691
    }
692
693
    $uri = apply_filters( 'wpinv_get_current_page_url', $uri );
694
695
    if ( $nocache ) {
696
        $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

696
        $uri = /** @scrutinizer ignore-call */ wpinv_add_cache_busting( $uri );
Loading history...
697
    }
698
699
    return $uri;
700
}
701
702
function wpinv_get_php_arg_separator_output() {
703
	return ini_get( 'arg_separator.output' );
704
}
705
706
function wpinv_rgb_from_hex( $color ) {
707
    $color = str_replace( '#', '', $color );
708
    // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
709
    $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
710
711
    $rgb      = array();
712
    $rgb['R'] = hexdec( $color{0}.$color{1} );
713
    $rgb['G'] = hexdec( $color{2}.$color{3} );
714
    $rgb['B'] = hexdec( $color{4}.$color{5} );
715
716
    return $rgb;
717
}
718
719
function wpinv_hex_darker( $color, $factor = 30 ) {
720
    $base  = wpinv_rgb_from_hex( $color );
721
    $color = '#';
722
723
    foreach ( $base as $k => $v ) {
724
        $amount      = $v / 100;
725
        $amount      = round( $amount * $factor );
726
        $new_decimal = $v - $amount;
727
728
        $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

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