llms.functions.currency.php 8.18 KB
Newer Older
cyrille's avatar
cyrille committed
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
<?php
/**
 * Currency and Price related functions for LifterLMS Products
 *
 * @package LifterLMS/Functions
 *
 * @since 1.0.0
 * @version 5.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Format Number as decimal
 *
 * @since Unknown
 * @since 3.24.0 Unknown.
 * @deprecated 5.0.0 Function `llms_format_decimal()` is deprecated with no replacement.
 *
 * @param int     $number     Price value.
 * @param boolean $dp         Decimal points.
 * @param boolean $trim_zeros Trim zeros.
 * @return string
 */
function llms_format_decimal( $number, $dp = false, $trim_zeros = false ) {

	_deprecated_function( 'llms_format_decimal()', '5.0.0' );

	// Remove locale from string.
	if ( ! is_float( $number ) ) {
		$locale   = localeconv();
		$decimals = array( get_option( 'lifterlms_price_decimal_sep' ), $locale['decimal_point'], $locale['mon_decimal_point'] );
		$number   = llms_clean( str_replace( $decimals, '.', $number ) );
	}

	// DP is false - don't use number format, just return a string in our format.
	if ( false !== $dp ) {
		$dp     = 2;     // = intval( $dp == "" ? get_option( 'lifterlms_price_num_decimals' ) : $dp );
		$number = number_format( floatval( $number ), $dp, '.', ',' );
	}

	if ( $trim_zeros && strstr( $number, '.' ) ) {
		$number = rtrim( rtrim( $number, '0' ), '.' );
	}

	return $number;
}

/**
 * Get the currency selected
 *
 * @since 1.0.0
 * @since 3.0.0 Added USD as default when no option is set.
 *
 * @return string Currency code.
 */
function get_lifterlms_currency() {

	/**
	 * Hook Summary
	 *
	 * Hook description.
	 *
	 * @since Unknown
	 *
	 * @param string $currency Currency code.
	 */
	return apply_filters( 'lifterlms_currency', get_option( 'lifterlms_currency', 'USD' ) );

}

/**
 * Get the name of a currency
 *
 * @since  3.0.0
 *
 * @param string $currency A currency code.
 * @return string
 */
function get_lifterlms_currency_name( $currency = '' ) {

	if ( ! $currency ) {
		$currency = get_lifterlms_currency();
	}
	$name = '';

	$currencies = get_lifterlms_currencies();
	if ( isset( $currencies[ $currency ] ) ) {
		$name = $currencies[ $currency ];
	}

	/**
	 * Filters the name of the given currency.
	 *
	 * @since 3.0.0
	 *
	 * @param string $name     Currency name.
	 * @param string $currency Currency code.
	 */
	return apply_filters( 'lifterlms_currency_name', $name, $currency );
}

/**
 * Get array of supported currencies
 *
 * @since Unknown
 * @since 3.0.0 Unknown.
 * @since 5.0.0 Use currency list provided in `languages/currencies.php`.
 *
 * @return array
 */
function get_lifterlms_currencies() {

	$currencies = require LLMS_PLUGIN_DIR . 'languages/currencies.php';

	/**
	 * Filters the list of available currencies
	 *
	 * @since Unknown
	 *
	 * @param array $currencies A list of currency codes to currency names. See "languages/currencies.php" for details.
	 */
	return apply_filters( 'lifterlms_currencies', $currencies );

}

/**
 * Get Currency Symbol text code
 *
 * @since Unknown
 * @since 3.30.3 Removed duplicate key "MAD".
 * @since 5.0.0 Retrieve symbols list from `llms_get_currency_symbols()`.
 *              If a symbol cannot be found for the supplied currency code, return the code instead of an empty string.
 *
 * @param  string $currency Currency Code.
 * @return string
 */
function get_lifterlms_currency_symbol( $currency = '' ) {

	if ( ! $currency ) {
		$currency = get_lifterlms_currency();
	}

	$symbols         = llms_get_currency_symbols();
	$currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : $currency;

	/**
	 * Filters the symbol for the specified currency
	 *
	 * @since Unknown
	 *
	 * @param string $currency_symbol Currency symbol. If the symbol contains non-Latin characters, the HTML entity code for those characters will be used.
	 * @param string $currency        Currency code.
	 */
	return apply_filters( 'lifterlms_currency_symbol', $currency_symbol, $currency );
}

/**
 * Get the number of decimals places used for prices as defined by the setting.
 *
 * @since 3.0.0
 *
 * @return int
 */
function get_lifterlms_decimals() {
	return absint( apply_filters( 'lifterlms_decimals', get_option( 'lifterlms_decimals', 2 ) ) );
}

/**
 * Retrieve the character used as a decimal separator
 *
 * @since 3.0.0
 *
 * @return string
 */
function get_lifterlms_decimal_separator() {
	return apply_filters( 'lifterlms_decimal_separator', get_option( 'lifterlms_decimal_separator', '.' ) );
}

/**
 * Retrieve the setting for trimming zero value decimals from the end of prices
 *
 * @since  3.0.0
 *
 * @return string Either 'yes' or 'no'.
 */
function get_lifterlms_trim_zero_decimals() {
	return apply_filters( 'lifterlms_trim_zero_decimals', get_option( 'lifterlms_trim_zero_decimals', 'no' ) );
}

/**
 * Get a format string that can be passed to printf or sprintf to format prices
 *
 * The format string is created using user-defined price formatting settings.
 *
 * @since  3.0.0
 *
 * @return string
 */
function get_lifterlms_price_format() {
	$pos    = get_option( 'lifterlms_currency_position', 'left' );
	$format = '%1$s%2$s';
	switch ( $pos ) {
		case 'left':
			$format = '%1$s%2$s';
			break;
		case 'right':
			$format = '%2$s%1$s';
			break;
		case 'left_space':
			$format = '%1$s&nbsp;%2$s';
			break;
		case 'right_space':
			$format = '%2$s&nbsp;%1$s';
			break;
	}
	return apply_filters( 'lifterlms_price_format', $format, $pos );
}

/**
 * Retrieve the character used as the thousands separator
 *
 * @since 3.0.0
 *
 * @return string
 */
function get_lifterlms_thousand_separator() {
	return apply_filters( 'lifterlms_thousand_separator', get_option( 'lifterlms_thousand_separator', '.' ) );
}

/**
 * Retrieve a list of available currency symbols
 *
 * Retrieves the symbols list from `languages/currency-symbols.php`.
 *
 * @since 5.0.0
 *
 * @return array Array of currency codes to their symbols. Any non-Latin characters found in a symbol are returned as an HTML character entity code.
 */
function llms_get_currency_symbols() {

	$symbols = require LLMS_PLUGIN_DIR . 'languages/currency-symbols.php';

	/**
	 * Filters the list of currency symbols
	 *
	 * @since Unknown
	 *
	 * @param array $symbols List of currency codes to their symbol. See "languages/currency-symbols.php" for details.
	 */
	return apply_filters( 'lifterlms_currency_symbols', $symbols );

}

/**
 * Get a formatted price price
 *
 * @since Unknown
 * @since 3.0.0 Unknown.
 *
 * @param int   $price Price to display.
 * @param array $args  Array of arguments.
 * @return string
 */
function llms_price( $price, $args = array() ) {

	extract(
		apply_filters(
			'llms_price_args',
			array_merge(
				array(
					'currency'           => '',
					'decimal_separator'  => get_lifterlms_decimal_separator(),
					'decimals'           => get_lifterlms_decimals(),
					'format'             => get_lifterlms_price_format(),
					'thousand_separator' => get_lifterlms_thousand_separator(),
					'trim_zeros'         => get_lifterlms_trim_zero_decimals(),
				),
				$args
			)
		)
	);

	$negative = $price < 0;
	$price    = apply_filters( 'raw_lifterlms_price', floatval( $negative ? $price * -1 : $price ) );
	$price    = apply_filters( 'formatted_lifterlms_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

	if ( 'yes' === $trim_zeros && $decimals > 0 ) {
		$price = llms_trim_zeros( $price );
	}

	$formatted_price = ( $negative ? '-' : '' ) . sprintf( $format, '<span class="llms-price-currency-symbol">' . get_lifterlms_currency_symbol( $currency ) . '</span>', $price );
	$r               = '<span class="lifterlms-price">' . $formatted_price . '</span>';

	return apply_filters( 'llms_price', $r, $price, $args );
}

/**
 * Get a simple string (no html) based on the output of llms_price
 *
 * @since Unknown
 * @since 3.0.0 Unknown.
 *
 * @param int   $price Price to display.
 * @param array $args  Array of arguments.
 * @return string
 */
function llms_price_raw( $price, $args = array() ) {
	return html_entity_decode( strip_tags( llms_price( $price, $args ) ) );
}

/**
 * Trim trailing zeros off a price
 *
 * @since 3.0.0
 *
 * @param mixed $price Price string.
 * @return string
 */
function llms_trim_zeros( $price ) {
	return preg_replace( '/' . preg_quote( get_lifterlms_decimal_separator(), '/' ) . '0++$/', '', $price );
}