1: <?php
2: /**
3: * Formatting functions for taking care of proper number formats and such
4: * Adapted from Easy Digital Downloads
5: *
6: * @package EPL
7: * @subpackage Functions/Formatting
8: * @copyright Copyright (c) 2020, Merv Barrett
9: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10: * @since 1.0
11: */
12:
13: // Exit if accessed directly.
14: if ( ! defined( 'ABSPATH' ) ) {
15: exit;
16: }
17:
18: /**
19: * Sanitize Amount
20: *
21: * Returns a sanitized amount by stripping out thousands separators.
22: *
23: * @since 1.0
24: * @param string $amount Price amount to format.
25: * @return string $amount Newly sanitized amount
26: */
27: function epl_sanitize_amount( $amount ) {
28: $thousands_sep = epl_get_thousands_separator();
29: $decimal_sep = epl_get_decimal_separator();
30:
31: // Sanitize the amount.
32: if ( ',' === $decimal_sep && false !== ( strpos( $amount, $decimal_sep ) ) ) {
33: if ( '.' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
34: $amount = str_replace( $thousands_sep, '', $amount );
35: } elseif ( empty( $thousands_sep ) && false !== ( strpos( $amount, '.' ) ) ) {
36: $amount = str_replace( '.', '', $amount );
37: }
38:
39: $amount = str_replace( $decimal_sep, '.', $amount );
40: } elseif ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
41: $amount = str_replace( $thousands_sep, '', $amount );
42: }
43:
44: $decimals = apply_filters( 'epl_sanitize_amount_decimals', 2, $amount );
45: $amount = number_format( $amount, $decimals, '.', '' );
46:
47: return apply_filters( 'epl_sanitize_amount', $amount );
48: }
49:
50: /**
51: * Returns a nicely formatted amount.
52: *
53: * @param string $amount Price amount to format.
54: * @param bool $decimals Whether or not to use decimals. Useful when set to false for non-currency numbers.
55: * @param bool $preserve_format Whether to preserve amount format ie int / floats.
56: *
57: * @return string $amount Newly formatted amount or Price Not Available
58: *
59: * @since 1.0.0
60: * @since 3.4.28 $preserve_format param added.
61: * @since 3.4.44 Fixed warning for strpos when amount is empty.
62: */
63: function epl_format_amount( $amount, $decimals = false, $preserve_format = false ) {
64: $thousands_sep = epl_get_thousands_separator();
65: $decimal_sep = epl_get_decimal_separator();
66: $formatted = '';
67:
68: if ( empty( $amount ) ) {
69: return $amount;
70: }
71:
72: // Format the amount.
73: $sep_found = strpos( $amount, $decimal_sep );
74: if ( ',' === $decimal_sep && false !== $sep_found ) {
75: $whole = substr( $amount, 0, $sep_found );
76: $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
77: $amount = $whole . '.' . $part;
78: }
79:
80: // Strip , from the amount (if set as the thousands separator).
81: if ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
82: $amount = str_replace( ',', '', $amount );
83: }
84:
85: if ( empty( $amount ) ) {
86: $amount = 0;
87: }
88: if ( $preserve_format && false === ( strpos( $amount, $decimal_sep ) ) ) {
89: $decimals = false;
90: }
91:
92: $decimals = apply_filters( 'epl_format_amount_decimals', $decimals ? 2 : 0, $amount );
93: if ( is_numeric( $amount ) ) {
94: $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
95: }
96:
97: return apply_filters( 'epl_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep );
98: }
99:
100: /**
101: * Formats the currency display
102: *
103: * @param string $price Price.
104: *
105: * @return string $currency Currencies displayed correctly
106: * @since 1.0
107: * @since 3.4.44 fixed the appended dash sign for field sliders.
108: */
109: function epl_currency_filter( $price ) {
110: $currency = epl_get_currency();
111: $currency_position = epl_get_currency_position();
112:
113: $position = ( isset( $currency_position ) && ! empty( $currency_position ) ) ? $currency_position : 'before';
114:
115: $negative = floatval( $price ) < 0;
116:
117: if ( $negative ) {
118: $price = substr( $price, 1 ); // Remove proceeding "-".
119: }
120: //phpcs:disable
121: if ( 'before' === $position ) :
122: switch ( $currency ) :
123: case 'GBP':
124: $formatted = '&pound;' . $price;
125: break;
126: case 'BRL':
127: $formatted = 'R&#36;' . $price;
128: break;
129: case 'EUR':
130: $formatted = '&euro;' . $price;
131: break;
132: case 'USD':
133: case 'AUD':
134: case 'NZD':
135: case 'CAD':
136: case 'HKD':
137: case 'MXN':
138: case 'SGD':
139: $formatted = '&#36;' . $price;
140: break;
141: case 'JPY':
142: $formatted = '&yen;' . $price;
143: break;
144: case 'ILS':
145: $formatted = '&#8362;' . $price;
146: break;
147: case 'THB':
148: $formatted = '&#3647;' . $price;
149: break;
150: case 'INR':
151: $formatted = '&#8377;' . $price;
152: break;
153: case 'TRY':
154: $formatted = '&#8378;' . $price;
155: break;
156: case 'RIAL':
157: $formatted = '&#65020;' . $price;
158: break;
159: case 'UAH':
160: $formatted = '&#8372;' . $price;
161: break;
162: case 'VND':
163: $formatted = '&#8363;' . $price;
164: break;
165: case 'ZAR':
166: $formatted = 'R' . $price;
167: break;
168: case 'CZK':
169: $formatted = 'K&#269;' . $price;
170: break;
171: default:
172: $formatted = $currency . ' ' . $price;
173: break;
174: endswitch;
175: $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_before', $formatted, $currency, $price );
176: else :
177: switch ( $currency ) :
178: case 'GBP':
179: $formatted = $price . '&pound;';
180: break;
181: case 'BRL':
182: $formatted = $price . 'R&#36;';
183: break;
184: case 'EUR':
185: $formatted = $price . '&euro;';
186: break;
187: case 'USD':
188: case 'AUD':
189: case 'NZD':
190: case 'CAD':
191: case 'HKD':
192: case 'MXN':
193: case 'SGD':
194: $formatted = $price . '&#36;';
195: break;
196: case 'JPY':
197: $formatted = $price . '&yen;';
198: break;
199: case 'ILS':
200: $formatted = $price . '&#8362;';
201: break;
202: case 'THB':
203: $formatted = $price . '&#3647;';
204: break;
205: case 'INR':
206: $formatted = $price . '&#8377;';
207: break;
208: case 'TRY':
209: $formatted = $price . '&#8378;';
210: break;
211: case 'RIAL':
212: $formatted = $price . '&#65020;';
213: break;
214: case 'UAH':
215: $formatted = $price . '&#8372;';
216: break;
217: case 'VND':
218: $formatted = $price . '&#8363;';
219: break;
220: case 'ZAR':
221: $formatted = $price . 'R';
222: break;
223: case 'CZK':
224: $formatted = $price . ' K&#269;';
225: break;
226: default:
227: $formatted = $price . ' ' . $currency;
228: break;
229: endswitch;
230: $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_after', $formatted, $currency, $price );
231: endif;
232:
233: if ( $negative ) {
234: // Prepend the minus sign before the currency sign.
235: $formatted = '-' . $formatted;
236: }
237:
238: return $formatted;
239: //phpcs:enable
240: }
241:
242: /**
243: * Set the number of decimal places per currency
244: *
245: * @since 1.0
246: * @param int $decimals Number of decimal places.
247: * @return int $decimals
248: */
249: function epl_currency_decimal_filter( $decimals = 2 ) {
250: $currency = epl_get_currency();
251:
252: switch ( $currency ) {
253: case 'RIAL':
254: case 'JPY':
255: case 'TWD':
256: $decimals = 0;
257: break;
258: }
259:
260: return $decimals;
261: }
262: add_filter( 'epl_sanitize_amount_decimals', 'epl_currency_decimal_filter' );
263: add_filter( 'epl_format_amount_decimals', 'epl_currency_decimal_filter' );
264:
265: /**
266: * Apply formatting to epl_the_excerpt
267: *
268: * @param string $content The content.
269: * @since 3.1
270: * @return int $decimals
271: */
272: function epl_format_the_excerpt( $content ) {
273: $content = '<div class="epl-excerpt-content">' . wpautop( $content ) . '</div>';
274: return $content;
275: }
276: add_filter( 'epl_the_excerpt', 'epl_format_the_excerpt' );
277:
278: /**
279: * Sanitizes a string key for EPL
280: *
281: * Keys are used as internal identifiers. Alphanumeric characters, dashes, underscores, stops, colons and slashes are allowed
282: *
283: * @since 3.3.5
284: * @param string $key String key.
285: * @return string Sanitized key
286: */
287: function epl_sanitize_key( $key ) {
288:
289: $raw_key = $key;
290: $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key );
291: return apply_filters( 'epl_sanitize_key', $key, $raw_key );
292: }
293: