-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register block attributes in the server and format amounts
- Loading branch information
Showing
7 changed files
with
285 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
/** | ||
* Jetpack_Currencies: Utils for displaying and managing currencies. | ||
* | ||
* @package Jetpack | ||
* @since 9.1.0 | ||
*/ | ||
|
||
/** | ||
* General Gutenberg editor 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' => '£', | ||
'decimal' => 2, | ||
), | ||
'JPY' => array( | ||
'format' => '%1$s%2$s', // 1: Symbol 2: currency value | ||
'symbol' => '¥', | ||
'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' => '€', | ||
'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, | ||
), | ||
); | ||
|
||
/** | ||
* 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 string $symbol (Optional) If provided, overrides the default currency symbol. | ||
* @return string Formatted price. | ||
*/ | ||
public static function format_price( $price, $currency, $symbol = null ) { | ||
$currency_details = self::CURRENCIES[ $currency ]; | ||
|
||
if ( $currency_details ) { | ||
// 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'], | ||
null !== $symbol ? $symbol : $currency_details['symbol'], | ||
$amount | ||
); | ||
} | ||
|
||
// Fall back to unspecified currency symbol like `¤1,234.05`. | ||
// @link https://en.wikipedia.org/wiki/Currency_sign_(typography). | ||
if ( ! $currency ) { | ||
return '¤' . number_format_i18n( $price, 2 ); | ||
} | ||
|
||
return number_format_i18n( $price, 2 ) . ' ' . $currency; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.