installed plugin Easy Digital Downloads version 3.1.0.3

This commit is contained in:
2022-11-27 15:03:07 +00:00
committed by Gitium
parent 555673545b
commit c5dce2cec6
1200 changed files with 238970 additions and 0 deletions

View File

@ -0,0 +1,236 @@
<?php
/**
* Currency
*
* @package easy-digital-downloads
* @copyright Copyright (c) 2021, Sandhills Development, LLC
* @license GPL2+
* @since 3.0
*/
namespace EDD\Currency;
class Currency {
/**
* @var string Currency code.
*/
public $code;
/**
* @var string Symbol/text to display before amounts.
*/
public $prefix;
/**
* @var string Symbol/text to display after amounts.
*/
public $suffix;
/**
* @var string Symbol to use in the prefix/suffix.
*/
public $symbol;
/**
* @var string Decimal separator.
*/
public $decimal_separator = '.';
/**
* @var string Thousands separator.
*/
public $thousands_separator = ',';
/**
* @var int Number of decimals.
*/
public $number_decimals = 2;
/**
* Currency constructor.
*
* @param string $currency_code
*/
public function __construct( $currency_code ) {
$this->code = strtoupper( $currency_code );
$this->setup();
}
/**
* Returns a new currency object.
*
* @param string $currency_code
*
* @since 3.0
*
* @return Currency
*/
public static function from_code( $currency_code ) {
return new self( $currency_code );
}
/**
* Sets up properties.
*
* @since 3.0
*/
private function setup() {
$this->symbol = $this->get_symbol();
$this->decimal_separator = edd_get_option( 'decimal_separator', '.' );
$this->thousands_separator = edd_get_option( 'thousands_separator', ',' );
/**
* Filters the decimal separator.
*
* @param string $decimal_separator
* @param string $code
*
* @since 3.0
*/
$this->decimal_separator = apply_filters( 'edd_currency_decimal_separator', $this->decimal_separator, $this->code );
/**
* Filters the thousands separator.
*
* @param string $thousands_separator
* @param string $code
*
* @since 3.0
*/
$this->thousands_separator = apply_filters( 'edd_currency_thousands_separator', $this->thousands_separator, $this->code );
$separator = $this->_has_space_around_symbol() ? ' ' : '';
if ( 'before' === edd_get_option( 'currency_position', 'before' ) ) {
$this->prefix = $this->symbol . $separator;
} else {
$this->suffix = $separator . $this->symbol;
}
/**
* Filters the currency prefix.
*
* @param string $prefix
* @param string $code
*
* @since 3.0
*/
$this->prefix = apply_filters( 'edd_currency_prefix', $this->prefix, $this->code );
/**
* Filters the currency suffix.
*
* @param string $prefix
* @param string $code
*
* @since 3.0
*/
$this->suffix = apply_filters( 'edd_currency_suffix', $this->suffix, $this->code );
$this->number_decimals = $this->_is_zero_decimal() ? 0 : 2;
}
/**
* Whether or not this currency has a space between the symbol and the amount.
*
* @since 3.0
*
* @return bool
*/
private function _has_space_around_symbol() {
return ! in_array( $this->code, array(
'GBP',
'BRL',
'EUR',
'USD',
'AUD',
'CAD',
'HKD',
'MXN',
'SGD',
'JPY'
) );
}
/**
* Returns the symbol for this currency.
* Depending on settings, this will be used as either the prefix or the suffix.
*
* @since 3.0
* @return string
*/
public function get_symbol() {
switch ( $this->code ) :
case "GBP" :
$symbol = '&pound;';
break;
case "BRL" :
$symbol = 'R&#36;';
break;
case "EUR" :
$symbol = '&euro;';
break;
case "USD" :
case "AUD" :
case "NZD" :
case "CAD" :
case "HKD" :
case "MXN" :
case "SGD" :
$symbol = '&#36;';
break;
case "JPY" :
$symbol = '&yen;';
break;
case "AOA" :
$symbol = 'Kz';
break;
default :
$symbol = $this->code;
break;
endswitch;
/**
* Filters the currency symbol.
*
* @since unknown
*
* @param string $symbol Currency symbol.
* @param string $code Currency code.
*/
return apply_filters( 'edd_currency_symbol', $symbol, $this->code );
}
/**
* Determines whether or not the currency is zero-decimal.
*
* @since 3.0
*
* @return bool
*/
private function _is_zero_decimal() {
$currencies = array(
'bif',
'clp',
'djf',
'gnf',
'huf',
'jpy',
'kmf',
'krw',
'mga',
'pyg',
'rwf',
'ugx',
'vnd',
'vuv',
'xaf',
'xof',
'xpf',
);
return in_array( strtolower( $this->code ), $currencies, true );
}
}

View File

@ -0,0 +1,232 @@
<?php
/**
* Money Formatter
*
* Formats an amount of money in various ways, according to the provided currency.
*
* @package easy-digital-downloads
* @copyright Copyright (c) 2021, Sandhills Development, LLC
* @license GPL2+
* @since 3.0
*/
namespace EDD\Currency;
class Money_Formatter {
/**
* @var int|float Current working amount.
*/
public $amount;
/**
* @var float Typed amount.
*/
public $typed_amount;
/**
* @var int|float Original, unmodified amount passed in via constructor.
*/
private $original_amount;
/**
* @var Currency
*/
private $currency;
/**
* Money_Formatter constructor.
*
* @param $amount
* @param Currency $currency
*/
public function __construct( $amount, Currency $currency ) {
$this->original_amount = $amount;
$this->amount = $amount;
$this->typed_amount = $amount;
$this->currency = $currency;
}
/**
* Un-formats an amount.
* This ensures the amount is put into a state where we can perform mathematical
* operations on it --- that means using `.` as the decimal separator and no
* thousands separator.
*
* @return float|int
*/
private function unformat() {
$amount = $this->original_amount;
$sep_found = strpos( $amount, $this->currency->decimal_separator );
if ( ',' === $this->currency->decimal_separator && false !== $sep_found ) {
$whole = substr( $amount, 0, $sep_found );
$part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
$amount = $whole . '.' . $part;
}
// Strip "," and " " from the amount (if set as the thousands separator).
foreach ( array( ',', ' ' ) as $thousands_separator ) {
if ( $thousands_separator === $this->currency->thousands_separator && false !== strpos( $amount, $this->currency->thousands_separator ) ) {
$amount = str_replace( $thousands_separator, '', $amount );
}
}
return $amount;
}
/**
* Returns the number of decimals ot use for the formatted amount.
*
* Based on the currency code used when instantiating the class, determines how many
* decimal points the value should have once formatted.
*
* @param bool $decimals If we should include decimals or not in the formatted amount.
* @param float $amount The amount to format.
*
* @return int The number of decimals places to use when formatting the amount.
*/
private function get_decimals( $decimals, $amount ) {
/**
* Filter number of decimals to use for formatted amount
*
* @since unknown
*
* @param int $number Default 2. Number of decimals.
* @param int|string $amount Amount being formatted.
* @param string $currency_code Currency code being formatted.
*/
return apply_filters( 'edd_format_amount_decimals', $decimals ? $this->currency->number_decimals : 0, $amount, $this->currency->code );
}
/**
* Formats the amount for display.
* Does not apply the currency code.
*
* @since 3.0
*
* @param bool $decimals If we should include decimal places or not when formatting.
*
* @return Money_Formatter
*/
public function format_for_display( $decimals = true ) {
$amount = $this->unformat();
if ( empty( $amount ) ) {
$amount = 0;
}
$decimals = $this->get_decimals( $decimals, $amount );
// Format amount using decimals and separators (also rounds up or down).
$formatted = number_format( (float) $amount, $decimals, $this->currency->decimal_separator, $this->currency->thousands_separator );
/**
* Filter the formatted amount before returning
*
* @since unknown
*
* @param mixed $formatted Formatted amount.
* @param mixed $amount Original amount.
* @param int $decimals Default 2. Number of decimals.
* @param string $decimal_separator Default '.'. Decimal separator.
* @param string $thousands_separator Default ','. Thousands separator.
* @param string $currency_code Currency used for formatting.
*/
$this->amount = apply_filters( 'edd_format_amount', $formatted, $amount, $decimals, $this->currency->decimal_separator, $this->currency->thousands_separator, $this->currency->code );
return $this;
}
/**
* Formats the amount for typed data returns.
* Does not apply the currency code and returns a foat instead of a string.
*
* @since 3.0
*
* @param bool $decimals If we should include decimal places or not when formatting.
*
* @return Money_Formatter
*/
public function format_for_typed( $decimals = true ) {
$amount = $this->unformat();
if ( empty( $amount ) ) {
$amount = 0;
}
$decimals = $this->get_decimals( $decimals, $amount );
/**
* Since we want to return a float value here, intentionally only supply a decimal separator.
*
* The separators here are hard coded intentionally as we're looking to get truncated, raw format of float
* which requires '.' for decimal separators and no thousands separator.
*
* This is also intentionally not filtered for the time being.
*/
$formatted = floatval( number_format( (float) $amount, $decimals, '.', '' ) );
// Set the amount to $this->amount.
$this->typed_amount = $formatted;
return $this;
}
/**
* Applies the currency prefix/suffix to the amount.
*
* @since 3.0
* @return string
*/
public function apply_symbol() {
$amount = $this->amount;
$is_negative = is_numeric( $this->amount ) && $this->amount < 0;
// Remove "-" from start.
if ( $is_negative ) {
$amount = substr( $amount, 1 );
}
$formatted = '';
if ( ! empty( $this->currency->prefix ) ) {
$formatted .= $this->currency->prefix;
}
$formatted .= $amount;
if ( ! empty( $this->currency->suffix ) ) {
$formatted .= $this->currency->suffix;
}
if ( ! empty( $this->currency->prefix ) ) {
/**
* Filters the output with a prefix.
*
* @param string $formatted
* @param string $currency_code
* @param string $amount
*/
$formatted = apply_filters( 'edd_' . strtolower( $this->currency->code ) . '_currency_filter_before', $formatted, $this->currency->code, $amount );
}
if ( ! empty( $this->currency->suffix ) ) {
/**
* Filters the output with a suffix.
*
* @param string $formatted
* @param string $currency_code
* @param string $amount
*/
$formatted = apply_filters( 'edd_' . strtolower( $this->currency->code ) . '_currency_filter_after', $formatted, $this->currency->code, $amount );
}
// Add the "-" sign back to the start of the string.
if ( $is_negative ) {
$formatted = '-' . $formatted;
}
return $formatted;
}
}

View File

@ -0,0 +1,119 @@
<?php
/**
* Currency Functions
*
* @package easy-digital-downloads
* @copyright Copyright (c) 2021, Sandhills Development, LLC
* @license GPL2+
* @since 3.0
*/
use EDD\Currency\Currency;
use EDD\Currency\Money_Formatter;
/**
* Get Currencies
*
* @since 1.0
* @return array $currencies A list of the available currencies
*/
function edd_get_currencies() {
$currencies = array(
'USD' => __( 'US Dollars (&#36;)', 'easy-digital-downloads' ),
'EUR' => __( 'Euros (&euro;)', 'easy-digital-downloads' ),
'GBP' => __( 'Pound Sterling (&pound;)', 'easy-digital-downloads' ),
'AUD' => __( 'Australian Dollars (&#36;)', 'easy-digital-downloads' ),
'BRL' => __( 'Brazilian Real (R&#36;)', 'easy-digital-downloads' ),
'CAD' => __( 'Canadian Dollars (&#36;)', 'easy-digital-downloads' ),
'CZK' => __( 'Czech Koruna', 'easy-digital-downloads' ),
'DKK' => __( 'Danish Krone', 'easy-digital-downloads' ),
'HKD' => __( 'Hong Kong Dollar (&#36;)', 'easy-digital-downloads' ),
'HUF' => __( 'Hungarian Forint', 'easy-digital-downloads' ),
'ILS' => __( 'Israeli Shekel (&#8362;)', 'easy-digital-downloads' ),
'JPY' => __( 'Japanese Yen (&yen;)', 'easy-digital-downloads' ),
'MYR' => __( 'Malaysian Ringgits', 'easy-digital-downloads' ),
'MXN' => __( 'Mexican Peso (&#36;)', 'easy-digital-downloads' ),
'NZD' => __( 'New Zealand Dollar (&#36;)', 'easy-digital-downloads' ),
'NOK' => __( 'Norwegian Krone', 'easy-digital-downloads' ),
'PHP' => __( 'Philippine Pesos', 'easy-digital-downloads' ),
'PLN' => __( 'Polish Zloty', 'easy-digital-downloads' ),
'SGD' => __( 'Singapore Dollar (&#36;)', 'easy-digital-downloads' ),
'SEK' => __( 'Swedish Krona', 'easy-digital-downloads' ),
'CHF' => __( 'Swiss Franc', 'easy-digital-downloads' ),
'TWD' => __( 'Taiwan New Dollars', 'easy-digital-downloads' ),
'THB' => __( 'Thai Baht (&#3647;)', 'easy-digital-downloads' ),
'INR' => __( 'Indian Rupee (&#8377;)', 'easy-digital-downloads' ),
'TRY' => __( 'Turkish Lira (&#8378;)', 'easy-digital-downloads' ),
'RIAL' => __( 'Iranian Rial (&#65020;)', 'easy-digital-downloads' ),
'RUB' => __( 'Russian Rubles', 'easy-digital-downloads' ),
'AOA' => __( 'Angolan Kwanza', 'easy-digital-downloads' ),
);
return apply_filters( 'edd_currencies', $currencies );
}
/**
* Accepts an amount (ideally from the database, unmodified) and formats it
* for display. The amount itself is formatted and the currency prefix/suffix
* is applied and positioned.
*
* @since 3.0
*
* @param int|float|string $amount
* @param string $currency
*
* @return string
*/
function edd_display_amount( $amount, $currency ) {
$formatter = new Money_Formatter( $amount, new Currency( $currency ) );
return $formatter->format_for_display()
->apply_symbol();
}
/**
* Get the store's set currency
*
* @since 1.5.2
* @return string The currency code
*/
function edd_get_currency() {
$currency = edd_get_option( 'currency', 'USD' );
return apply_filters( 'edd_currency', $currency );
}
/**
* Given a currency determine the symbol to use. If no currency given, site default is used.
* If no symbol is determined, the currency string is returned.
*
* @since 2.2
*
* @param string $currency The currency string
*
* @return string The symbol to use for the currency
*/
function edd_currency_symbol( $currency = '' ) {
if ( empty( $currency ) ) {
$currency = edd_get_currency();
}
$currency = new Currency( $currency );
return $currency->symbol;
}
/**
* Get the name of a currency
*
* @since 2.2
*
* @param string $code The currency code
*
* @return string The currency's name
*/
function edd_get_currency_name( $code = 'USD' ) {
$currencies = edd_get_currencies();
$name = isset( $currencies[ $code ] ) ? $currencies[ $code ] : $code;
return apply_filters( 'edd_currency_name', $name );
}