PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.3.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.3.2
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / currency / class-charitable-currency.php

class-charitable-currency.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.3.2, at includes/currency/class-charitable-currency.php

371 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Currency helper.
4 *
5 * @package Charitable/Classes/Charitable_Currency
6 * @version 1.0.0
7 * @author Eric Daams
8 * @copyright Copyright (c) 2015, Studio 164a
9 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
13
14 if ( ! class_exists( 'Charitable_Currency' ) ) :
15
16 /**
17 * Charitable_Currency
18 *
19 * @final
20 * @since 1.0.0
21 */
22 final class Charitable_Currency {
23
24 /**
25 * The single instance of this class.
26 *
27 * @var Charitable_Currency|null
28 * @access private
29 * @static
30 */
31 private static $instance = null;
32
33 /**
34 * @var string $currency_format The format that the current currency will take.
35 */
36 private $currency_format = array();
37
38 /**
39 * @var string[] $currencies Every currency available.
40 */
41 private $currencies = array();
42
43 /**
44 * @var string $currency The currency in use on the site.
45 */
46 private $currency;
47
48 /**
49 * Create class object. A private constructor, so this is used in a singleton context.
50 *
51 * @access private
52 * @since 1.2.3
53 */
54 private function __construct() {
55 }
56
57 /**
58 * Returns and/or create the single instance of this class.
59 *
60 * @return Charitable_Currency
61 * @access public
62 * @since 1.2.3
63 */
64 public static function get_instance() {
65 if ( is_null( self::$instance ) ) {
66 self::$instance = new Charitable_Currency();
67 }
68
69 return self::$instance;
70 }
71
72 /**
73 * Return an amount as a monetary string.
74 *
75 * 50.00 -> $50.00
76 *
77 * @param string $amount
78 * @param int|false $decimal_count Optional. If not set, default decimal count will be used.
79 * @return string|WP_Error
80 * @access public
81 * @since 1.0.0
82 */
83 public function get_monetary_amount( $amount, $decimal_count = false ) {
84 if ( false === $decimal_count ) {
85 $decimal_count = charitable_get_option( 'decimal_count', 2 );
86 }
87
88 $amount = $this->sanitize_monetary_amount( strval( $amount ) );
89
90 $amount = number_format(
91 $amount,
92 $decimal_count,
93 charitable_get_option( 'decimal_separator', '.' ),
94 charitable_get_option( 'thousands_separator', ',' )
95 );
96
97 return sprintf( $this->get_currency_format(), $this->get_currency_symbol(), $amount );
98 }
99
100 /**
101 * Receives unfiltered monetary amount and sanitizes it, returning it as a float.
102 *
103 * $50.00 -> 50.00
104 *
105 * @param string $amount
106 * @return float
107 * @access public
108 * @since 1.0.0
109 */
110 public function sanitize_monetary_amount( $amount ) {
111 /* Sending anything other than a string can cause unexpected returns, so we require floats. */
112 if ( ! is_string( $amount ) ) {
113 _doing_it_wrong( __METHOD__, 'Amount must be passed as a string.', '1.0.0' );
114 return new WP_Error( 'invalid_parameter_type', 'Amount must be passed as a string.' );
115 }
116
117 /** If we're using commas for decimals, we need to turn any commas into points,
118 and we need to replace existing points with blank spaces. Example:
119 12.500,50 -> 12500.50
120 */
121 if ( $this->is_comma_decimal() ) {
122 $amount = str_replace( ',', '_', $amount );
123 $amount = str_replace( '.', '', $amount );
124 $amount = str_replace( '_', '.', $amount );
125 }
126
127 return floatval( filter_var( $amount, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) );
128 }
129
130 /**
131 * Turns a database amount into an amount formatted for the currency that the site is in.
132 *
133 * @param string $amount
134 * @return string
135 * @access public
136 * @since 1.3.0
137 */
138 public function sanitize_database_amount( $amount ) {
139 if ( $this->is_comma_decimal() ) {
140 $amount = str_replace( '.', ',', $amount );
141 }
142
143 return $amount;
144 }
145
146 /**
147 * Checks whether the comma is being used as the separator.
148 *
149 * @return boolean
150 * @access public
151 * @since 1.0.0
152 */
153 public function is_comma_decimal() {
154 return ( ',' == charitable_get_option( 'decimal_separator', '.' ) );
155 }
156
157 /**
158 * Return the number of decimals to use.
159 *
160 * @uses charitable_currency_decimal_count
161 * @param float $amount
162 * @return int
163 * @access public
164 * @since 1.0.0
165 */
166 public function get_decimals( $amount = "" ) {
167 return charitable_get_option( 'decimal_count', 2 );
168 }
169
170 /**
171 * Return the currency format based on the position of the currency symbol.
172 *
173 * @uses charitable_currency_format
174 * @return string
175 * @access public
176 * @since 1.0.0
177 */
178 public function get_currency_format() {
179 $symbol_position = charitable_get_option( 'currency_format', 'left' );
180
181 switch ( $symbol_position ) {
182 case 'left':
183 $format = '%1$s%2$s';
184 break;
185 case 'right':
186 $format = '%2$s%1$s';
187 break;
188 case 'left-with-space':
189 $format = '%1$s&nbsp;%2$s';
190 break;
191 case 'right-with-space':
192 $format = '%2$s&nbsp;%1$s';
193 break;
194 default:
195 $format = apply_filters( 'charitable_currency_format', '%1$s%2$s', $symbol_position );
196 }
197
198 return $format;
199 }
200
201 /**
202 * Get the currency format for accounting.js
203 *
204 * @return void
205 * @access public
206 * @since 1.3.0
207 */
208 public function get_accounting_js_format() {
209
210 $option = charitable_get_option( 'currency_format', 'left' );
211
212 switch( $option ){
213 case 'right':
214 $format = "%v%s";
215 break;
216 case 'left-with-space':
217 $format = "%s %v";
218 break;
219 case 'right-with-space':
220 $format = "%v %s";
221 break;
222 default:
223 $format = "%s%v";
224 break;
225 }
226 $format = "%s%v";
227
228 return $format;
229
230 }
231
232 /**
233 * Return every currency symbol used with the
234 *
235 * @uses charitable_currencies Hook to add custom currencies.
236 * @return string[]
237 * @access public
238 * @since 1.0.0
239 */
240 public function get_all_currencies() {
241 if ( empty( $this->currencies ) ) {
242 $this->currencies = apply_filters( 'charitable_currencies', array(
243 'AED' => sprintf( __( 'Emirati Dirham (%s)', 'charitable' ), $this->get_currency_symbol( 'AED' ) ),
244 'AUD' => sprintf( __( 'Australian Dollars (%s)', 'charitable' ), $this->get_currency_symbol( 'AUD' ) ),
245 'ARS' => sprintf( __( 'Argentine Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'ARS' ) ),
246 'BDT' => sprintf( __( 'Bangladeshi Taka (%s)', 'charitable' ), $this->get_currency_symbol( 'BDT' ) ),
247 'BRL' => sprintf( __( 'Brazilian Real (%s)', 'charitable' ), $this->get_currency_symbol( 'BRL' ) ),
248 'BGN' => sprintf( __( 'Bulgarian Lev (%s)', 'charitable' ), $this->get_currency_symbol( 'BGN' ) ),
249 'CAD' => sprintf( __( 'Canadian Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'CAD' ) ),
250 'CHF' => sprintf( __( 'Swiss Franc (%s)', 'charitable' ), $this->get_currency_symbol( 'CHF' ) ),
251 'CLP' => sprintf( __( 'Chilean Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'CLP' ) ),
252 'CNY' => sprintf( __( 'Chinese Yuan Renminbi (%s)', 'charitable' ), $this->get_currency_symbol( 'CNY' ) ),
253 'CZK' => sprintf( __( 'Czech Koruna (%s)', 'charitable' ), $this->get_currency_symbol( 'CZK' ) ),
254 'DKK' => sprintf( __( 'Danish Krone (%s)', 'charitable' ), $this->get_currency_symbol( 'DKK' ) ),
255 'EUR' => sprintf( __( 'Euro (%s)', 'charitable' ), $this->get_currency_symbol( 'EUR' ) ),
256 'GBP' => sprintf( __( 'British Pound (%s)', 'charitable' ), $this->get_currency_symbol( 'GBP' ) ),
257 'HKD' => sprintf( __( 'Hong Kong Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'HKD' ) ),
258 'HRK' => sprintf( __( 'Croatian Kuna (%s)', 'charitable' ), $this->get_currency_symbol( 'HRK' ) ),
259 'HUF' => sprintf( __( 'Hungarian Forint (%s)', 'charitable' ), $this->get_currency_symbol( 'HUF' ) ),
260 'IDR' => sprintf( __( 'Indonesian Rupiah (%s)', 'charitable' ), $this->get_currency_symbol( 'IDR' ) ),
261 'ILS' => sprintf( __( 'Israeli Shekel (%s)', 'charitable' ), $this->get_currency_symbol( 'ILS' ) ),
262 'INR' => sprintf( __( 'Indian Rupee (%s)', 'charitable' ), $this->get_currency_symbol( 'INR' ) ),
263 'ISK' => sprintf( __( 'Icelandic Krona (%s)', 'charitable' ), $this->get_currency_symbol( 'ISK' ) ),
264 'JPY' => sprintf( __( 'Japanese Yen (%s)', 'charitable' ), $this->get_currency_symbol( 'JPY' ) ),
265 'KRW' => sprintf( __( 'South Korean Won (%s)', 'charitable' ), $this->get_currency_symbol( 'KRW' ) ),
266 'MXN' => sprintf( __( 'Mexican Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'MXN' ) ),
267 'MYR' => sprintf( __( 'Malaysian Ringgit (%s)', 'charitable' ), $this->get_currency_symbol( 'MYR' ) ),
268 'NGN' => sprintf( __( 'Nigerian Naira (%s)', 'charitable' ), $this->get_currency_symbol( 'NGN' ) ),
269 'NOK' => sprintf( __( 'Norwegian Krone (%s)', 'charitable' ), $this->get_currency_symbol( 'NOK' ) ),
270 'NZD' => sprintf( __( 'New Zealand Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'NZD' ) ),
271 'PHP' => sprintf( __( 'Philippine Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'PHP' ) ),
272 'PLN' => sprintf( __( 'Polish Zloty (%s)', 'charitable' ), $this->get_currency_symbol( 'PLN' ) ),
273 'RON' => sprintf( __( 'Romanian New Leu (%s)', 'charitable' ), $this->get_currency_symbol( 'RON' ) ),
274 'RUB' => sprintf( __( 'Russian Ruble (%s)', 'charitable' ), $this->get_currency_symbol( 'RUB' ) ),
275 'SEK' => sprintf( __( 'Swedish Krona (%s)', 'charitable' ), $this->get_currency_symbol( 'SEK' ) ),
276 'SGD' => sprintf( __( 'Singapore Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'SGD' ) ),
277 'THB' => sprintf( __( 'Thai Baht (%s)', 'charitable' ), $this->get_currency_symbol( 'THB' ) ),
278 'TRY' => sprintf( __( 'Turkish Lira (%s)', 'charitable' ), $this->get_currency_symbol( 'TRY' ) ),
279 'TWD' => sprintf( __( 'Taiwan New Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'TWD' ) ),
280 'USD' => sprintf( __( 'US Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'USD' ) ),
281 'VND' => sprintf( __( 'Vietnamese Dong (%s)', 'charitable' ), $this->get_currency_symbol( 'VND' ) ),
282 'ZAR' => sprintf( __( 'South African Rand (%s)', 'charitable' ), $this->get_currency_symbol( 'ZAR' ) )
283 ) );
284 }
285
286 return $this->currencies;
287 }
288
289 /**
290 * Return the currency symbol for a given currency.
291 *
292 * Credit: This is based on the WooCommerce implemenation.
293 *
294 * @uses charitable_currency_symbol
295 * @param string $currency Optional. If not set, currency is based on currently selected currency.
296 * @return string
297 * @access private
298 * @since 1.0.0
299 */
300 private function get_currency_symbol( $currency = "" ) {
301 if ( ! strlen( $currency ) ) {
302 $currency = charitable_get_option( 'currency', 'AUD' );
303 }
304
305 switch ( $currency ) {
306 case 'AED' :
307 $currency_symbol = 'د.إ';
308 break;
309 case 'BDT':
310 $currency_symbol = '&#2547;';
311 break;
312 case 'BRL' :
313 $currency_symbol = '&#82;&#36;';
314 break;
315 case 'BGN' :
316 $currency_symbol = '&#1083;&#1074;.';
317 break;
318 case 'AUD' :
319 case 'ARS' :
320 case 'CAD' :
321 case 'CLP' :
322 case 'MXN' :
323 case 'NZD' :
324 case 'HKD' :
325 case 'SGD' :
326 case 'USD' :
327 $currency_symbol = '&#36;';
328 break;
329 case 'EUR' :
330 $currency_symbol = '&euro;';
331 break;
332 case 'CNY' :
333 case 'RMB' :
334 case 'JPY' :
335 $currency_symbol = '&yen;';
336 break;
337 case 'RUB' :
338 $currency_symbol = '&#1088;&#1091;&#1073;.';
339 break;
340 case 'KRW' : $currency_symbol = '&#8361;'; break;
341 case 'TRY' : $currency_symbol = '&#8378;'; break;
342 case 'NOK' : $currency_symbol = '&#107;&#114;'; break;
343 case 'ZAR' : $currency_symbol = '&#82;'; break;
344 case 'CZK' : $currency_symbol = '&#75;&#269;'; break;
345 case 'MYR' : $currency_symbol = '&#82;&#77;'; break;
346 case 'DKK' : $currency_symbol = 'kr.'; break;
347 case 'HUF' : $currency_symbol = '&#70;&#116;'; break;
348 case 'IDR' : $currency_symbol = 'Rp'; break;
349 case 'INR' : $currency_symbol = 'Rs.'; break;
350 case 'ISK' : $currency_symbol = 'Kr.'; break;
351 case 'ILS' : $currency_symbol = '&#8362;'; break;
352 case 'PHP' : $currency_symbol = '&#8369;'; break;
353 case 'PLN' : $currency_symbol = '&#122;&#322;'; break;
354 case 'SEK' : $currency_symbol = '&#107;&#114;'; break;
355 case 'CHF' : $currency_symbol = '&#67;&#72;&#70;'; break;
356 case 'TWD' : $currency_symbol = '&#78;&#84;&#36;'; break;
357 case 'THB' : $currency_symbol = '&#3647;'; break;
358 case 'GBP' : $currency_symbol = '&pound;'; break;
359 case 'RON' : $currency_symbol = 'lei'; break;
360 case 'VND' : $currency_symbol = '&#8363;'; break;
361 case 'NGN' : $currency_symbol = '&#8358;'; break;
362 case 'HRK' : $currency_symbol = 'Kn'; break;
363 default : $currency_symbol = ''; break;
364 }
365
366 return apply_filters( 'charitable_currency_symbol', $currency_symbol, $currency );
367 }
368 }
369
370 endif; // End class_exists check
371