Test Failed
Push — issues/2220 ( 2cc82f )
by Ravinder
04:16
created

currency-functions.php ➔ give_is_zero_based_currency()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 4
nop 1
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Currency Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
7
 * @copyright   Copyright (c) 2017, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.8.17
10
 */
11
12
/**
13
 * Get the set currency
14
 *
15
 * @since 1.0
16
 * @since 1.8.15 Upgrade function to handle dynamic currency
17
 *
18
 * @param int          $donation_or_form_id Donation or Form ID
19
 * @param array|object $args                Additional data
20
 *
21
 * @return string The currency code
22
 */
23
function give_get_currency( $donation_or_form_id = null, $args = array() ) {
24
25
	// Get currency from donation
26
	if ( is_numeric( $donation_or_form_id ) && 'give_payment' === get_post_type( $donation_or_form_id ) ) {
27
		$donation_meta = give_get_meta( $donation_or_form_id, '_give_payment_meta', true );
28
29
		if ( ! empty( $donation_meta['currency'] ) ) {
30
			$currency = $donation_meta['currency'];
31
		} else {
32
			$currency = give_get_option( 'currency', 'USD' );
33
		}
34
	} else {
35
		$currency = give_get_option( 'currency', 'USD' );
36
	}
37
38
	/**
39
	 * Filter the currency on basis of donation or form id or addtional data.
40
	 *
41
	 * @since 1.0
42
	 */
43
	return apply_filters( 'give_currency', $currency, $donation_or_form_id, $args );
44
}
45
46
/**
47
 * Get the set currency position
48
 *
49
 * @since 1.3.6
50
 *
51
 * @return string The currency code
52
 */
53
function give_get_currency_position() {
54
55
	$currency_pos = give_get_option( 'currency_position', 'before' );
56
57
	return apply_filters( 'give_currency_position', $currency_pos );
58
}
59
60
61
/**
62
 * Get Currencies
63
 *
64
 * @since 1.0
65
 *
66
 * @param string $info Specify currency info
67
 *
68
 * @return array $currencies A list of the available currencies
69
 */
70
function give_get_currencies( $info = 'admin_label' ) {
71
	$currencies = array(
72
		'USD'  => array(
73
			'admin_label' => __( 'US Dollars ($)', 'give' ),
74
			'symbol'      => '&#36;',
75
			'setting'     => array(
76
				'currency_position'   => 'before',
77
				'thousands_separator' => ',',
78
				'decimal_separator'   => '.',
79
				'number_decimals'     => 2,
80
			),
81
		),
82
		'EUR'  => array(
83
			'admin_label' => __( 'Euros (€)', 'give' ),
84
			'symbol'      => '&euro;',
85
			'setting'     => array(
86
				'currency_position'   => 'before',
87
				'thousands_separator' => '.',
88
				'decimal_separator'   => ',',
89
				'number_decimals'     => 2,
90
			),
91
		),
92
		'GBP'  => array(
93
			'admin_label' => __( 'Pounds Sterling (£)', 'give' ),
94
			'symbol'      => '&pound;',
95
			'setting'     => array(
96
				'currency_position'   => 'before',
97
				'thousands_separator' => ',',
98
				'decimal_separator'   => '.',
99
				'number_decimals'     => 2,
100
			),
101
		),
102
		'AUD'  => array(
103
			'admin_label' => __( 'Australian Dollars ($)', 'give' ),
104
			'symbol'      => '&#36;',
105
			'setting'     => array(
106
				'currency_position'   => 'before',
107
				'thousands_separator' => ',',
108
				'decimal_separator'   => '.',
109
				'number_decimals'     => 2,
110
			),
111
		),
112
		'BRL'  => array(
113
			'admin_label' => __( 'Brazilian Real (R$)', 'give' ),
114
			'symbol'      => '&#82;&#36;',
115
			'setting'     => array(
116
				'currency_position'   => 'before',
117
				'thousands_separator' => ',',
118
				'decimal_separator'   => '.',
119
				'number_decimals'     => 2,
120
			),
121
		),
122
		'CAD'  => array(
123
			'admin_label' => __( 'Canadian Dollars ($)', 'give' ),
124
			'symbol'      => '&#36;',
125
			'setting'     => array(
126
				'currency_position'   => 'before',
127
				'thousands_separator' => ',',
128
				'decimal_separator'   => '.',
129
				'number_decimals'     => 2,
130
			),
131
		),
132
		'CZK'  => array(
133
			'admin_label' => __( 'Czech Koruna (Kč)', 'give' ),
134
			'symbol'      => '&#75;&#269;',
135
			'setting'     => array(
136
				'currency_position'   => 'before',
137
				'thousands_separator' => ',',
138
				'decimal_separator'   => '.',
139
				'number_decimals'     => 2,
140
			),
141
		),
142
		'DKK'  => array(
143
			'admin_label' => __( 'Danish Krone (kr.)', 'give' ),
144
			'symbol'      => '&nbsp;kr.&nbsp;',
145
			'setting'     => array(
146
				'currency_position'   => 'before',
147
				'thousands_separator' => ',',
148
				'decimal_separator'   => '.',
149
				'number_decimals'     => 2,
150
			),
151
		),
152
		'HKD'  => array(
153
			'admin_label' => __( 'Hong Kong Dollar ($)', 'give' ),
154
			'symbol'      => '&#36;',
155
			'setting'     => array(
156
				'currency_position'   => 'before',
157
				'thousands_separator' => ',',
158
				'decimal_separator'   => '.',
159
				'number_decimals'     => 2,
160
			),
161
		),
162
		'HUF'  => array(
163
			'admin_label' => __( 'Hungarian Forint (Ft)', 'give' ),
164
			'symbol'      => '&#70;&#116;',
165
			'setting'     => array(
166
				'currency_position'   => 'before',
167
				'thousands_separator' => ',',
168
				'decimal_separator'   => '.',
169
				'number_decimals'     => 2,
170
			),
171
		),
172
		'ILS'  => array(
173
			'admin_label' => __( 'Israeli Shekel (₪)', 'give' ),
174
			'symbol'      => '&#8362;',
175
			'setting'     => array(
176
				'currency_position'   => 'before',
177
				'thousands_separator' => ',',
178
				'decimal_separator'   => '.',
179
				'number_decimals'     => 2,
180
			),
181
		),
182
		'JPY'  => array(
183
			'admin_label' => __( 'Japanese Yen (¥)', 'give' ),
184
			'symbol'      => '&yen;',
185
			'setting'     => array(
186
				'currency_position'   => 'before',
187
				'thousands_separator' => ',',
188
				'decimal_separator'   => '.',
189
				'number_decimals'     => 0,
190
			),
191
		),
192
		'MYR'  => array(
193
			'admin_label' => __( 'Malaysian Ringgits (RM)', 'give' ),
194
			'symbol'      => '&#82;&#77;',
195
			'setting'     => array(
196
				'currency_position'   => 'before',
197
				'thousands_separator' => ',',
198
				'decimal_separator'   => '.',
199
				'number_decimals'     => 2,
200
			),
201
		),
202
		'MXN'  => array(
203
			'admin_label' => __( 'Mexican Peso ($)', 'give' ),
204
			'symbol'      => '&#36;',
205
			'setting'     => array(
206
				'currency_position'   => 'before',
207
				'thousands_separator' => ',',
208
				'decimal_separator'   => '.',
209
				'number_decimals'     => 2,
210
			),
211
		),
212
		'MAD'  => array(
213
			'admin_label' => __( 'Moroccan Dirham (&#x2e;&#x62f;&#x2e;&#x645;)', 'give' ),
214
			'symbol'      => '&#x2e;&#x62f;&#x2e;&#x645;',
215
			'setting'     => array(
216
				'currency_position'   => 'before',
217
				'thousands_separator' => ',',
218
				'decimal_separator'   => '.',
219
				'number_decimals'     => 2,
220
			),
221
		),
222
		'NZD'  => array(
223
			'admin_label' => __( 'New Zealand Dollar ($)', 'give' ),
224
			'symbol'      => '&#36;',
225
			'setting'     => array(
226
				'currency_position'   => 'before',
227
				'thousands_separator' => ',',
228
				'decimal_separator'   => '.',
229
				'number_decimals'     => 2,
230
			),
231
		),
232
		'NOK'  => array(
233
			'admin_label' => __( 'Norwegian Krone (Kr.)', 'give' ),
234
			'symbol'      => '&#107;&#114;.',
235
			'setting'     => array(
236
				'currency_position'   => 'before',
237
				'thousands_separator' => '.',
238
				'decimal_separator'   => ',',
239
				'number_decimals'     => 2,
240
			),
241
		),
242
		'PHP'  => array(
243
			'admin_label' => __( 'Philippine Pesos (₱)', 'give' ),
244
			'symbol'      => '&#8369;',
245
			'setting'     => array(
246
				'currency_position'   => 'before',
247
				'thousands_separator' => ',',
248
				'decimal_separator'   => '.',
249
				'number_decimals'     => 2,
250
			),
251
		),
252
		'PLN'  => array(
253
			'admin_label' => __( 'Polish Zloty (zł)', 'give' ),
254
			'symbol'      => '&#122;&#322;',
255
			'setting'     => array(
256
				'currency_position'   => 'before',
257
				'thousands_separator' => ' ',
258
				'decimal_separator'   => ',',
259
				'number_decimals'     => 2,
260
			),
261
		),
262
		'SGD'  => array(
263
			'admin_label' => __( 'Singapore Dollar ($)', 'give' ),
264
			'symbol'      => '&#36;',
265
			'setting'     => array(
266
				'currency_position'   => 'before',
267
				'thousands_separator' => ',',
268
				'decimal_separator'   => '.',
269
				'number_decimals'     => 2,
270
			),
271
		),
272
		'KRW'  => array(
273
			'admin_label' => __( 'South Korean Won (₩)', 'give' ),
274
			'symbol'      => '&#8361;',
275
			'setting'     => array(
276
				'currency_position'   => 'before',
277
				'thousands_separator' => ',',
278
				'decimal_separator'   => '.',
279
				'number_decimals'     => 0,
280
			),
281
		),
282
		'ZAR'  => array(
283
			'admin_label' => __( 'South African Rand (R)', 'give' ),
284
			'symbol'      => '&#82;',
285
			'setting'     => array(
286
				'currency_position'   => 'before',
287
				'thousands_separator' => ' ',
288
				'decimal_separator'   => '.',
289
				'number_decimals'     => 2,
290
			),
291
		),
292
		'SEK'  => array(
293
			'admin_label' => __( 'Swedish Krona (kr)', 'give' ),
294
			'symbol'      => '&nbsp;kr.&nbsp;',
295
			'setting'     => array(
296
				'currency_position'   => 'before',
297
				'thousands_separator' => ' ',
298
				'decimal_separator'   => ',',
299
				'number_decimals'     => 2,
300
			),
301
		),
302
		'CHF'  => array(
303
			'admin_label' => __( 'Swiss Franc (CHF)', 'give' ),
304
			'symbol'      => 'CHF',
305
			'setting'     => array(
306
				'currency_position'   => 'before',
307
				'thousands_separator' => ',',
308
				'decimal_separator'   => '.',
309
				'number_decimals'     => 2,
310
			),
311
		),
312
		'TWD'  => array(
313
			'admin_label' => __( 'Taiwan New Dollars (NT$)', 'give' ),
314
			'symbol'      => '&#78;&#84;&#36;',
315
			'setting'     => array(
316
				'currency_position'   => 'before',
317
				'thousands_separator' => '\'',
318
				'decimal_separator'   => '.',
319
				'number_decimals'     => 2,
320
			),
321
		),
322
		'THB'  => array(
323
			'admin_label' => __( 'Thai Baht (฿)', 'give' ),
324
			'symbol'      => '&#3647;',
325
			'setting'     => array(
326
				'currency_position'   => 'before',
327
				'thousands_separator' => ',',
328
				'decimal_separator'   => '.',
329
				'number_decimals'     => 2,
330
			),
331
		),
332
		'INR'  => array(
333
			'admin_label' => __( 'Indian Rupee (₹)', 'give' ),
334
			'symbol'      => '&#8377;',
335
			'setting'     => array(
336
				'currency_position'   => 'before',
337
				'thousands_separator' => ',',
338
				'decimal_separator'   => '.',
339
				'number_decimals'     => 2,
340
			),
341
		),
342
		'TRY'  => array(
343
			'admin_label' => __( 'Turkish Lira (₺)', 'give' ),
344
			'symbol'      => '&#8378;',
345
			'setting'     => array(
346
				'currency_position'   => 'before',
347
				'thousands_separator' => ',',
348
				'decimal_separator'   => '.',
349
				'number_decimals'     => 2,
350
			),
351
		),
352
		'IRR' => array(
353
			'admin_label' => __( 'Iranian Rial (﷼)', 'give' ),
354
			'symbol'      => '&#xfdfc;',
355
			'setting'     => array(
356
				'currency_position'   => 'after',
357
				'thousands_separator' => ',',
358
				'decimal_separator'   => '.',
359
				'number_decimals'     => 2,
360
			),
361
		),
362
		'RUB'  => array(
363
			'admin_label' => __( 'Russian Rubles (руб)', 'give' ),
364
			'symbol'      => '&#8381;',
365
			'setting'     => array(
366
				'currency_position'   => 'before',
367
				'thousands_separator' => '.',
368
				'decimal_separator'   => ',',
369
				'number_decimals'     => 2,
370
			),
371
		),
372
	);
373
374
	/**
375
	 * Filter the currencies
376
	 * Note: you can register new currency by using this filter
377
	 * array(
378
	 *     'admin_label' => '',  // required
379
	 *     'symbol'      => '',  // required
380
	 *     'setting'     => ''   // required
381
	 *     ....
382
	 * )
383
	 *
384
	 * @since 1.8.15
385
	 *
386
	 * @param array $currencies
387
	 */
388
	$currencies = apply_filters( 'give_currencies', $currencies );
389
390
	// Backward compatibility: handle old way of currency registration.
391
	// Backward compatibility: Return desired result.
392
	if ( ! empty( $currencies ) ) {
393
		foreach ( $currencies as $currency_code => $currency_setting ) {
394
			if ( is_string( $currency_setting ) ) {
395
				$currencies[ $currency_code ] = array(
396
					'admin_label' => $currency_setting,
397
				);
398
			}
399
400
			$currencies[ $currency_code ] = wp_parse_args(
401
				$currencies[ $currency_code ],
402
				array(
403
					'admin_label' => '',
404
					'symbol'      => $currency_code,
405
					'setting'     => array(),
406
				)
407
			);
408
		}
409
410
		if ( ! empty( $info ) && is_string( $info ) && 'all' !== $info ) {
411
			$currencies = wp_list_pluck( $currencies, $info );
412
		}
413
	}
414
415
	return $currencies;
416
}
417
418
419
/**
420
 * Get all currency symbols
421
 *
422
 * @since 1.8.14
423
 *
424
 * @param bool $decode_currencies
425
 *
426
 * @return array
427
 */
428
function give_currency_symbols( $decode_currencies = false ) {
429
	$currencies = give_get_currencies( 'symbol' );
430
431
	if ( $decode_currencies ) {
432
		array_walk( $currencies, function( &$currency_symbol ){
433
			$currency_symbol = html_entity_decode( $currency_symbol, ENT_COMPAT, 'UTF-8' );
434
		});
435
	}
436
437
	/**
438
	 * Filter the currency symbols
439
	 *
440
	 * @since 1.8.14
441
	 *
442
	 * @param array $currencies
443
	 */
444
	return apply_filters( 'give_currency_symbols', $currencies );
445
}
446
447
448
/**
449
 * Give Currency Symbol
450
 *
451
 * Given a currency determine the symbol to use. If no currency given, site default is used. If no symbol is determine,
452
 * the currency string is returned.
453
 *
454
 * @since      1.0
455
 *
456
 * @param  string $currency        The currency string.
457
 * @param  bool   $decode_currency Option to HTML decode the currency symbol.
458
 *
459
 * @return string           The symbol to use for the currency
460
 */
461 View Code Duplication
function give_currency_symbol( $currency = '', $decode_currency = false ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

Loading history...
462
463
	if ( empty( $currency ) ) {
464
		$currency = give_get_currency();
465
	}
466
467
	$currencies = give_currency_symbols( $decode_currency );
468
	$symbol     = array_key_exists( $currency, $currencies ) ? $currencies[ $currency ] : $currency;
469
470
	/**
471
	 * Filter the currency symbol
472
	 *
473
	 * @since 1.0
474
	 *
475
	 * @param string $symbol
476
	 * @param string $currency
477
	 */
478
	return apply_filters( 'give_currency_symbol', $symbol, $currency );
479
}
480
481
482
/**
483
 * Get currency name.
484
 *
485
 * @since 1.8.8
486
 *
487
 * @param string $currency_code
488
 *
489
 * @return string
490
 */
491
function give_get_currency_name( $currency_code ) {
492
	$currency_name  = '';
493
	$currency_names = give_get_currencies();
494
495
	if ( $currency_code && array_key_exists( $currency_code, $currency_names ) ) {
496
		$currency_name = explode( '(', $currency_names[ $currency_code ] );
497
		$currency_name = trim( current( $currency_name ) );
498
	}
499
500
	/**
501
	 * Filter the currency name
502
	 *
503
	 * @since 1.8.8
504
	 *
505
	 * @param string $currency_name
506
	 * @param string $currency_code
507
	 */
508
	return apply_filters( 'give_currency_name', $currency_name, $currency_code );
509
}
510
511
/**
512
 * Formats the currency displayed.
513
 *
514
 * @since 1.0
515
 *
516
 * @param string $price           The donation amount.
517
 * @param string $currency_code   The currency code.
518
 * @param bool   $decode_currency Whether to decode the currency HTML format or not.
519
 *
520
 * @return mixed|string
521
 */
522
function give_currency_filter( $price = '', $currency_code = '', $decode_currency = false ) {
523
524
	if ( empty( $currency_code ) || ! array_key_exists( (string) $currency_code, give_get_currencies() ) ) {
525
		$currency_code = give_get_currency();
526
	}
527
528
	$position = give_get_option( 'currency_position', 'before' );
529
530
	$negative = $price < 0;
531
532
	if ( $negative ) {
533
		// Remove proceeding "-".
534
		$price = substr( $price, 1 );
535
	}
536
537
	$symbol = give_currency_symbol( $currency_code, $decode_currency );
538
539
	switch ( $currency_code ) :
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 'NZD' :
549
		case 'SGD' :
550
		case 'JPY' :
551
		case 'THB' :
552
		case 'INR' :
553
		case 'IRR' :
554
		case 'TRY' :
555
		case 'RUB' :
556
		case 'SEK' :
557
		case 'PLN' :
558
		case 'PHP' :
559
		case 'TWD' :
560
		case 'MYR' :
561
		case 'CZK' :
562
		case 'DKK' :
563
		case 'HUF' :
564
		case 'ILS' :
565
		case 'MAD' :
566
		case 'KRW' :
567 View Code Duplication
		case 'ZAR' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
568
			$formatted = ( 'before' === $position ? $symbol . '&#x200e;' . $price : $price . '&#x200f;' . $symbol );
569
			break;
570 View Code Duplication
		case 'NOK':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
571
			$formatted = ( 'before' === $position ? $symbol . ' ' . $price : $price . ' ' . $symbol );
572
			break;
573 View Code Duplication
		default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
574
			$formatted = ( 'before' === $position ? $symbol . ' ' . $price : $price . ' ' . $symbol );
575
			break;
576
	endswitch;
577
578
	/**
579
	 * Filter formatted amount with currency
580
	 *
581
	 * Filter name depends upon current value of currency and currency position.
582
	 * For example :
583
	 *           if currency is USD and currency position is before then
584
	 *           filter name will be give_usd_currency_filter_before
585
	 *
586
	 *           and if currency is USD and currency position is after then
587
	 *           filter name will be give_usd_currency_filter_after
588
	 */
589
	$formatted = apply_filters( 'give_' . strtolower( $currency_code ) . "_currency_filter_{$position}", $formatted, $currency_code, $price );
590
591
	if ( $negative ) {
592
		// Prepend the minus sign before the currency sign.
593
		$formatted = '-' . $formatted;
594
	}
595
596
	return $formatted;
597
}
598
599
600
/**
601
 * Zero Decimal based Currency.
602
 *
603
 * @since 1.8.14
604
 * @see   https://github.com/WordImpress/Give/issues/2191
605
 *
606
 * @param string $currency Currency code
607
 *
608
 * @return bool
609
 */
610
function give_is_zero_based_currency( $currency = '' ) {
611
	$zero_based_currency = array(
612
		'PYG', // Paraguayan Guarani.
613
		'GNF', // Guinean Franc.
614
		'RWF', // Rwandan Franc.
615
		'JPY', // Japanese Yen.
616
		'BIF', // Burundian Franc.
617
		'KRW', // South Korean Won.
618
		'MGA', // Malagasy Ariary.
619
		'XAF', // Central African Cfa Franc.
620
		'XPF', // Cfp Franc.
621
		'CLP', // Chilean Peso.
622
		'KMF', // Comorian Franc.
623
		'DJF', // Djiboutian Franc.
624
		'VUV', // Vanuatu Vatu.
625
		'VND', // Vietnamese Dong.
626
		'XOF', // West African Cfa Franc.
627
	);
628
629
	// Set default currency.
630
	if ( empty( $currency ) ) {
631
		$currency = give_get_currency();
632
	}
633
634
	// Check for Zero Based Currency.
635
	if ( in_array( $currency, $zero_based_currency ) ) {
636
		return true;
637
	}
638
639
	return false;
640
}