summaryrefslogtreecommitdiff
blob: adebcf2b94d73678ff47d8d5112a36466c062161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
 * Jetpack_Currencies: Utils for displaying and managing currencies.
 *
 * @package    Jetpack
 * @since      9.1.0
 */

/**
 * General currencies specific functionality
 */
class Jetpack_Currencies {
	/**
	 * Currencies definition
	 */
	const CURRENCIES = array(
		'USD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => '$',
			'decimal' => 2,
		),
		'GBP' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => '&#163;',
			'decimal' => 2,
		),
		'JPY' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => '&#165;',
			'decimal' => 0,
		),
		'BRL' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'R$',
			'decimal' => 2,
		),
		'EUR' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => '&#8364;',
			'decimal' => 2,
		),
		'NZD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'NZ$',
			'decimal' => 2,
		),
		'AUD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'A$',
			'decimal' => 2,
		),
		'CAD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'C$',
			'decimal' => 2,
		),
		'ILS' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => '₪',
			'decimal' => 2,
		),
		'RUB' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => '₽',
			'decimal' => 2,
		),
		'MXN' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'MX$',
			'decimal' => 2,
		),
		'MYR' => array(
			'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
			'symbol'  => 'RM',
			'decimal' => 2,
		),
		'SEK' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'Skr',
			'decimal' => 2,
		),
		'HUF' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'Ft',
			'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
		),
		'CHF' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'CHF',
			'decimal' => 2,
		),
		'CZK' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'Kč',
			'decimal' => 2,
		),
		'DKK' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'Dkr',
			'decimal' => 2,
		),
		'HKD' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'HK$',
			'decimal' => 2,
		),
		'NOK' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'Kr',
			'decimal' => 2,
		),
		'PHP' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => '₱',
			'decimal' => 2,
		),
		'PLN' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => 'PLN',
			'decimal' => 2,
		),
		'SGD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'S$',
			'decimal' => 2,
		),
		'TWD' => array(
			'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
			'symbol'  => 'NT$',
			'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
		),
		'THB' => array(
			'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
			'symbol'  => '฿',
			'decimal' => 2,
		),
		'INR' => array(
			'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
			'symbol'  => '₹',
			'decimal' => 0,
		),
	);

	/**
	 * Format a price with currency.
	 *
	 * Uses currency-aware formatting to output a formatted price with a simple fallback.
	 *
	 * Largely inspired by WordPress.com's Store_Price::display_currency
	 *
	 * @param  string $price    Price.
	 * @param  string $currency Currency.
	 * @param  bool   $symbol   Whether to display the currency symbol.
	 * @return string           Formatted price.
	 */
	public static function format_price( $price, $currency, $symbol = true ) {
		// Fall back to unspecified currency symbol like `¤1,234.05`.
		// @link https://en.wikipedia.org/wiki/Currency_sign_(typography).
		if ( ! array_key_exists( $currency, self::CURRENCIES ) ) {
			return '¤' . number_format_i18n( $price, 2 );
		}

		$currency_details = self::CURRENCIES[ $currency ];

		// Ensure USD displays as 1234.56 even in non-US locales.
		$amount = 'USD' === $currency
			? number_format( $price, $currency_details['decimal'], '.', ',' )
			: number_format_i18n( $price, $currency_details['decimal'] );

		return sprintf(
			$currency_details['format'],
			$symbol ? $currency_details['symbol'] : '',
			$amount
		);
	}
}