Completed
Push — master ( f597b6...60f62d )
by Brian
03:50 queued 03:47
created

getpaid_format_date()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

694
    } 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...
695
        $uri = wpinv_get_checkout_uri();
696
    }
697
698
    $uri = apply_filters( 'wpinv_get_current_page_url', $uri );
699
700
    if ( $nocache ) {
701
        $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

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

752
        $new_hex_component = dechex( /** @scrutinizer ignore-type */ $new_decimal );
Loading history...
753
        if ( strlen( $new_hex_component ) < 2 ) {
754
            $new_hex_component = "0" . $new_hex_component;
755
        }
756
        $color .= $new_hex_component;
757
    }
758
759
    return $color;
760
}
761
762
function wpinv_hex_lighter( $color, $factor = 30 ) {
763
    $base  = wpinv_rgb_from_hex( $color );
764
    $color = '#';
765
766
    foreach ( $base as $k => $v ) {
767
        $amount      = 255 - $v;
768
        $amount      = $amount / 100;
769
        $amount      = round( $amount * $factor );
770
        $new_decimal = $v + $amount;
771
772
        $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

772
        $new_hex_component = dechex( /** @scrutinizer ignore-type */ $new_decimal );
Loading history...
773
        if ( strlen( $new_hex_component ) < 2 ) {
774
            $new_hex_component = "0" . $new_hex_component;
775
        }
776
        $color .= $new_hex_component;
777
    }
778
779
    return $color;
780
}
781
782
function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
783
    $hex = str_replace( '#', '', $color );
784
785
    $c_r = hexdec( substr( $hex, 0, 2 ) );
786
    $c_g = hexdec( substr( $hex, 2, 2 ) );
787
    $c_b = hexdec( substr( $hex, 4, 2 ) );
788
789
    $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
790
791
    return $brightness > 155 ? $dark : $light;
792
}
793
794
function wpinv_format_hex( $hex ) {
795
    $hex = trim( str_replace( '#', '', $hex ) );
796
797
    if ( strlen( $hex ) == 3 ) {
798
        $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
799
    }
800
801
    return $hex ? '#' . $hex : null;
802
}
803
804
/**
805
 * Get truncated string with specified width.
806
 *
807
 * @since 1.0.0
808
 *
809
 * @param string $str The string being decoded.
810
 * @param int $start The start position offset. Number of characters from the beginning of string.
811
 *                      For negative value, number of characters from the end of the string.
812
 * @param int $width The width of the desired trim. Negative widths count from the end of the string.
813
 * @param string $trimmaker A string that is added to the end of string when string is truncated. Ex: "...".
814
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
815
 * @return string
816
 */
817
function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) {
818
    if ( function_exists( 'mb_strimwidth' ) ) {
819
        return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding );
820
    }
821
    
822
    return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker;
823
}
824
825
/**
826
 * Get the string length.
827
 *
828
 * @since 1.0.0
829
 *
830
 * @param string $str The string being checked for length. 
831
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
832
 * @return int Returns the number of characters in string.
833
 */
834
function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) {
835
    if ( function_exists( 'mb_strlen' ) ) {
836
        return mb_strlen( $str, $encoding );
837
    }
838
        
839
    return strlen( $str );
840
}
841
842
function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) {
843
    if ( function_exists( 'mb_strtolower' ) ) {
844
        return mb_strtolower( $str, $encoding );
845
    }
846
    
847
    return strtolower( $str );
848
}
849
850
function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) {
851
    if ( function_exists( 'mb_strtoupper' ) ) {
852
        return mb_strtoupper( $str, $encoding );
853
    }
854
    
855
    return strtoupper( $str );
856
}
857
858
/**
859
 * Find position of first occurrence of string in a string
860
 *
861
 * @since 1.0.0
862
 *
863
 * @param string $str The string being checked.
864
 * @param string $find The string to find in input string.
865
 * @param int $offset The search offset. Default "0". A negative offset counts from the end of the string.
866
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
867
 * @return int Returns the position of the first occurrence of search in the string.
868
 */
869
function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
870
    if ( function_exists( 'mb_strpos' ) ) {
871
        return mb_strpos( $str, $find, $offset, $encoding );
872
    }
873
        
874
    return strpos( $str, $find, $offset );
875
}
876
877
/**
878
 * Find position of last occurrence of a string in a string.
879
 *
880
 * @since 1.0.0
881
 *
882
 * @param string $str The string being checked, for the last occurrence of search.
883
 * @param string $find The string to find in input string.
884
 * @param int $offset Specifies begin searching an arbitrary number of characters into the string.
885
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
886
 * @return int Returns the position of the last occurrence of search.
887
 */
888
function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) {
889
    if ( function_exists( 'mb_strrpos' ) ) {
890
        return mb_strrpos( $str, $find, $offset, $encoding );
891
    }
892
        
893
    return strrpos( $str, $find, $offset );
894
}
895
896
/**
897
 * Get the part of string.
898
 *
899
 * @since 1.0.0
900
 *
901
 * @param string $str The string to extract the substring from.
902
 * @param int $start If start is non-negative, the returned string will start at the entered position in string, counting from zero.
903
 *                      If start is negative, the returned string will start at the entered position from the end of string. 
904
 * @param int|null $length Maximum number of characters to use from string.
905
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
906
 * @return string
907
 */
908
function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) {
909
    if ( function_exists( 'mb_substr' ) ) {
910
        if ( $length === null ) {
911
            return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding );
912
        } else {
913
            return mb_substr( $str, $start, $length, $encoding );
914
        }
915
    }
916
        
917
    return substr( $str, $start, $length );
918
}
919
920
/**
921
 * Get the width of string.
922
 *
923
 * @since 1.0.0
924
 *
925
 * @param string $str The string being decoded.
926
 * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8".
927
 * @return string The width of string.
928
 */
929
function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) {
930
    if ( function_exists( 'mb_strwidth' ) ) {
931
        return mb_strwidth( $str, $encoding );
932
    }
933
    
934
    return wpinv_utf8_strlen( $str, $encoding );
935
}
936
937
function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) {
938
    if ( function_exists( 'mb_strlen' ) ) {
939
        $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding );
940
        $str_end = "";
941
        
942
        if ( $lower_str_end ) {
943
            $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding );
944
        } else {
945
            $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding );
946
        }
947
948
        return $first_letter . $str_end;
949
    }
950
    
951
    return ucfirst( $str );
952
}
953
954
function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) {
955
    if ( function_exists( 'mb_convert_case' ) ) {
956
        return mb_convert_case( $str, MB_CASE_TITLE, $encoding );
957
    }
958
    
959
    return ucwords( $str );
960
}
961
962
function wpinv_period_in_days( $period, $unit ) {
963
    $period = absint( $period );
964
    
965
    if ( $period > 0 ) {
966
        if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) {
967
            $period = $period * 7;
968
        } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) {
969
            $period = $period * 30;
970
        } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) {
971
            $period = $period * 365;
972
        }
973
    }
974
    
975
    return $period;
976
}
977
978
function wpinv_cal_days_in_month( $calendar, $month, $year ) {
979
    if ( function_exists( 'cal_days_in_month' ) ) {
980
        return cal_days_in_month( $calendar, $month, $year );
981
    }
982
983
    // Fallback in case the calendar extension is not loaded in PHP
984
    // Only supports Gregorian calendar
985
    return date( 't', mktime( 0, 0, 0, $month, 1, $year ) );
986
}
987
988
/**
989
 * Display a help tip for settings.
990
 *
991
 * @param  string $tip Help tip text
992
 * @param  bool $allow_html Allow sanitized HTML if true or escape
993
 *
994
 * @return string
995
 */
996
function wpi_help_tip( $tip, $allow_html = false ) {
997
    if ( $allow_html ) {
998
        $tip = wpi_sanitize_tooltip( $tip );
999
    } else {
1000
        $tip = esc_attr( $tip );
1001
    }
1002
1003
    return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>';
1004
}
1005
1006
/**
1007
 * Sanitize a string destined to be a tooltip.
1008
 *
1009
 * Tooltips are encoded with htmlspecialchars to prevent XSS. Should not be used in conjunction with esc_attr()
1010
 *
1011
 * @param string $var
1012
 * @return string
1013
 */
1014
function wpi_sanitize_tooltip( $var ) {
1015
    return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
1016
        'br'     => array(),
1017
        'em'     => array(),
1018
        'strong' => array(),
1019
        'small'  => array(),
1020
        'span'   => array(),
1021
        'ul'     => array(),
1022
        'li'     => array(),
1023
        'ol'     => array(),
1024
        'p'      => array(),
1025
    ) ) );
1026
}
1027
1028
/**
1029
 * Get all WPI screen ids.
1030
 *
1031
 * @return array
1032
 */
1033
function wpinv_get_screen_ids() {
1034
1035
    $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) );
1036
1037
    $screen_ids = array(
1038
        'toplevel_page_' . $screen_id,
1039
        'wpi_invoice',
1040
        'wpi_item',
1041
        'wpi_quote',
1042
        'wpi_discount',
1043
        'edit-wpi_invoice',
1044
        'edit-wpi_item',
1045
        'edit-wpi_discount',
1046
        'edit-wpi_quote',
1047
        'invoicing_page_wpinv-settings',
1048
        'invoicing_page_wpinv-subscriptions',
1049
        'invoicing_page_wpinv-reports',
1050
        'invoicing_page_wpi-addons',
1051
    );
1052
1053
    return apply_filters( 'wpinv_screen_ids', $screen_ids );
1054
}
1055
1056
/**
1057
 * Cleans up an array, comma- or space-separated list of scalar values.
1058
 *
1059
 * @since 1.0.13
1060
 *
1061
 * @param array|string $list List of values.
1062
 * @return array Sanitized array of values.
1063
 */
1064
function wpinv_parse_list( $list ) {
1065
1066
    if ( empty( $list ) ) {
1067
        $list = array();
1068
    }
1069
1070
	if ( ! is_array( $list ) ) {
1071
		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...
1072
	}
1073
1074
	return $list;
1075
}
1076
1077
/**
1078
 * Fetches data stored on disk.
1079
 *
1080
 * @since 1.0.14
1081
 *
1082
 * @param string $key Type of data to fetch.
1083
 * @return mixed Fetched data.
1084
 */
1085
function wpinv_get_data( $key ) {
1086
    
1087
    // Try fetching it from the cache.
1088
    $data = wp_cache_get( "wpinv-$key", 'wpinv' );
1089
    if( $data ) {
1090
        return $data;
1091
    }
1092
1093
    $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" );
1094
	wp_cache_set( "wpinv-$key", $data, 'wpinv' );
1095
1096
	return $data;
1097
}
1098
1099
/**
1100
 * (Maybe) Adds an empty option to an array of options.
1101
 *
1102
 * @since 1.0.14
1103
 *
1104
 * @param array $options
1105
 * @param bool $first_empty Whether or not the first item in the list should be empty
1106
 * @return mixed Fetched data.
1107
 */
1108
function wpinv_maybe_add_empty_option( $options, $first_empty ) {
1109
1110
    if ( ! empty( $options ) && $first_empty ) {
1111
        return array_merge( array( '' => '' ), $options );
1112
    }
1113
    return $options;
1114
1115
}
1116
1117
/**
1118
 * Clean variables using sanitize_text_field.
1119
 *
1120
 * @param mixed $var Data to sanitize.
1121
 * @return string|array
1122
 */
1123
function wpinv_clean( $var ) {
1124
1125
	if ( is_array( $var ) ) {
1126
		return array_map( 'wpinv_clean', $var );
1127
    }
1128
1129
    if ( is_object( $var ) ) {
1130
		$object_vars = get_object_vars( $var );
1131
		foreach ( $object_vars as $property_name => $property_value ) {
1132
			$var->$property_name = wpinv_clean( $property_value );
1133
        }
1134
        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...
1135
	}
1136
    
1137
    return is_string( $var ) ? sanitize_text_field( $var ) : $var;
1138
}
1139
1140
/**
1141
 * Converts a price string into an options array.
1142
 *
1143
 * @param string $str Data to convert.
1144
 * @return string|array
1145
 */
1146
function getpaid_convert_price_string_to_options( $str ) {
1147
1148
	$raw_options = array_map( 'trim', explode( ',', $str ) );
1149
    $options     = array();
1150
1151
    foreach ( $raw_options as $option ) {
1152
1153
        if ( '' == $option ) {
1154
            continue;
1155
        }
1156
1157
        $option = array_map( 'trim', explode( '|', $option ) );
1158
1159
        $price = null;
1160
        $label = null;
1161
1162
        if ( isset( $option[0] ) && '' !=  $option[0] ) {
1163
            $label  = $option[0];
1164
        }
1165
1166
        if ( isset( $option[1] ) && '' !=  $option[1] ) {
1167
            $price = $option[1];
1168
        }
1169
1170
        if ( ! isset( $price ) ) {
1171
            $price = $label;
1172
        }
1173
1174
        if ( ! isset( $price ) || ! is_numeric( $price ) ) {
1175
            continue;
1176
        }
1177
1178
        if ( ! isset( $label ) ) {
1179
            $label = $price;
1180
        }
1181
1182
        $options[ $price ] = $label;
1183
    }
1184
1185
    return $options;
1186
}
1187
1188
/**
1189
 * Returns the help tip.
1190
 */
1191
function getpaid_get_help_tip( $tip, $additional_classes = '' ) {
1192
    $additional_classes = sanitize_html_class( $additional_classes );
1193
    $tip                = esc_attr__( $tip );
1194
    return "<span class='wpi-help-tip dashicons dashicons-editor-help $additional_classes' title='$tip'></span>";
1195
}
1196
1197
/**
1198
 * Formats a date
1199
 */
1200
function getpaid_format_date( $date ) {
1201
1202
    if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) {
1203
        return '';
1204
    }
1205
1206
1207
    return date_i18n( get_option( 'date_format' ), strtotime( $date ) );
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

1207
    return date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $date ) );
Loading history...
1208
1209
}
1210