Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Jetpack_Currencies
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 format_price
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Jetpack_Currencies: Utils for displaying and managing currencies.
4 *
5 * @package    Jetpack
6 * @since      9.1.0
7 */
8
9/**
10 * General currencies specific functionality
11 */
12class Jetpack_Currencies {
13    /**
14     * Currencies definition
15     */
16    const CURRENCIES = array(
17        'USD' => array(
18            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
19            'symbol'  => '$',
20            'decimal' => 2,
21        ),
22        'GBP' => array(
23            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
24            'symbol'  => '&#163;',
25            'decimal' => 2,
26        ),
27        'JPY' => array(
28            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
29            'symbol'  => '&#165;',
30            'decimal' => 0,
31        ),
32        'BRL' => array(
33            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
34            'symbol'  => 'R$',
35            'decimal' => 2,
36        ),
37        'EUR' => array(
38            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
39            'symbol'  => '&#8364;',
40            'decimal' => 2,
41        ),
42        'NZD' => array(
43            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
44            'symbol'  => 'NZ$',
45            'decimal' => 2,
46        ),
47        'AUD' => array(
48            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
49            'symbol'  => 'A$',
50            'decimal' => 2,
51        ),
52        'CAD' => array(
53            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
54            'symbol'  => 'C$',
55            'decimal' => 2,
56        ),
57        'ILS' => array(
58            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
59            'symbol'  => '₪',
60            'decimal' => 2,
61        ),
62        'RUB' => array(
63            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
64            'symbol'  => '₽',
65            'decimal' => 2,
66        ),
67        'MXN' => array(
68            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
69            'symbol'  => 'MX$',
70            'decimal' => 2,
71        ),
72        'MYR' => array(
73            'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
74            'symbol'  => 'RM',
75            'decimal' => 2,
76        ),
77        'SEK' => array(
78            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
79            'symbol'  => 'Skr',
80            'decimal' => 2,
81        ),
82        'HUF' => array(
83            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
84            'symbol'  => 'Ft',
85            'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
86        ),
87        'CHF' => array(
88            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
89            'symbol'  => 'CHF',
90            'decimal' => 2,
91        ),
92        'CZK' => array(
93            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
94            'symbol'  => 'Kč',
95            'decimal' => 2,
96        ),
97        'DKK' => array(
98            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
99            'symbol'  => 'Dkr',
100            'decimal' => 2,
101        ),
102        'HKD' => array(
103            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
104            'symbol'  => 'HK$',
105            'decimal' => 2,
106        ),
107        'NOK' => array(
108            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
109            'symbol'  => 'Kr',
110            'decimal' => 2,
111        ),
112        'PHP' => array(
113            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
114            'symbol'  => '₱',
115            'decimal' => 2,
116        ),
117        'PLN' => array(
118            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
119            'symbol'  => 'PLN',
120            'decimal' => 2,
121        ),
122        'SGD' => array(
123            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
124            'symbol'  => 'S$',
125            'decimal' => 2,
126        ),
127        'TWD' => array(
128            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
129            'symbol'  => 'NT$',
130            'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
131        ),
132        'THB' => array(
133            'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
134            'symbol'  => '฿',
135            'decimal' => 2,
136        ),
137        'INR' => array(
138            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
139            'symbol'  => '₹',
140            'decimal' => 0,
141        ),
142    );
143
144    /**
145     * Format a price with currency.
146     *
147     * Uses currency-aware formatting to output a formatted price with a simple fallback.
148     *
149     * Largely inspired by WordPress.com's Store_Price::display_currency
150     *
151     * @param  string $price    Price.
152     * @param  string $currency Currency.
153     * @param  bool   $symbol   Whether to display the currency symbol.
154     * @return string           Formatted price.
155     */
156    public static function format_price( $price, $currency, $symbol = true ) {
157        // Add some basic formatting for the price.
158        $formatted_number = new NumberFormatter( get_locale(), NumberFormatter::DECIMAL );
159        $price            = (float) $formatted_number->parse( $price );
160
161        // Fall back to unspecified currency symbol like `¤1,234.05`.
162        // @link https://en.wikipedia.org/wiki/Currency_sign_(typography).
163        if ( ! array_key_exists( $currency, self::CURRENCIES ) ) {
164            return ( $symbol ? '¤' : '' ) . number_format_i18n( $price, 2 );
165        }
166
167        $currency_details = self::CURRENCIES[ $currency ];
168
169        // Ensure USD displays as 1234.56 even in non-US locales.
170        $amount = 'USD' === $currency
171            ? number_format( $price, $currency_details['decimal'], '.', ',' )
172            : number_format_i18n( $price, $currency_details['decimal'] );
173
174        return sprintf(
175            $currency_details['format'],
176            $symbol ? $currency_details['symbol'] : '',
177            $amount
178        );
179    }
180}