Nav apraksta

class-jetpack-currencies.php 4.5KB

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