PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Membership / CurrencyFormatter.php
wp-user-avatar / src / Membership Last commit date
Controllers 1 week ago DigitalProducts 5 months ago Emails 3 months ago Models 1 month ago PaymentMethods 1 month ago Repositories 2 months ago Services 4 months ago CheckoutFields.php 1 month ago CurrencyFormatter.php 9 months ago Init.php 2 years ago StatSync.php 1 year ago index.php 3 years ago
CurrencyFormatter.php
231 lines
1 <?php
2
3 namespace ProfilePress\Core\Membership;
4
5 use ProfilePress\Core\Membership\Services\Calculator;
6
7 class CurrencyFormatter
8 {
9 private $amount;
10
11 /**
12 * @var string Original, unmodified amount passed in via constructor.
13 */
14 private $original_amount;
15
16 private $currency_code;
17
18 private $symbol;
19
20 private $decimal_separator;
21
22 private $thousands_separator;
23
24 private $number_decimals;
25
26 private $currency_position;
27
28 /**
29 * @var string Symbol/text to display before amounts.
30 */
31 private $prefix = '';
32
33 /**
34 * @var string Symbol/text to display after amounts.
35 */
36 private $suffix = '';
37
38 /**
39 * CurrencyFormatter constructor.
40 *
41 * @param string $amount
42 * @param string $currency_code
43 */
44 public function __construct($amount, $currency_code = '')
45 {
46 $this->amount = $this->original_amount = $amount;
47
48 $this->currency_code = strtoupper(! empty($currency_code) ? $currency_code : ppress_get_currency());
49
50 $this->symbol = html_entity_decode(ppress_get_currency_symbol($this->currency_code));
51
52 $this->currency_position = apply_filters(
53 'ppress_currency_position',
54 ppress_get_setting('currency_position', 'left', true),
55 $this->currency_code
56 );
57
58 $this->number_decimals = apply_filters(
59 'ppress_currency_decimal_number',
60 ppress_get_setting('currency_decimal_number', '2', true),
61 $this->currency_code
62 );
63
64 $this->decimal_separator = apply_filters(
65 'ppress_currency_decimal_separator',
66 ppress_get_setting('currency_decimal_separator', '.', true),
67 $this->currency_code
68 );
69
70 $this->thousands_separator = apply_filters(
71 'ppress_currency_thousands_separator',
72 ppress_get_setting('currency_thousand_separator', ',', true),
73 $this->currency_code
74 );
75
76 switch ($this->currency_position) {
77 case 'left_space':
78 $this->prefix = $this->symbol . ' ';
79 break;
80 case 'left':
81 $this->prefix = $this->symbol;
82 break;
83 case 'right_space':
84 $this->suffix = ' ' . $this->symbol;
85 break;
86 case 'right':
87 $this->suffix = $this->symbol;
88 break;
89 }
90
91 $this->prefix = apply_filters('ppress_currency_prefix', $this->prefix, $this->currency_code);
92
93 $this->suffix = apply_filters('ppress_currency_suffix', $this->suffix, $this->currency_code);
94 }
95
96 /**
97 * Un-formats an amount.
98 * This ensures the amount is put into a state where we can perform mathematical
99 * operations on it --- that means using `.` as the decimal separator and no
100 * thousands separator.
101 *
102 * @return string
103 */
104 private function unformat()
105 {
106 $amount = $this->amount;
107
108 if (Calculator::init($amount)->isNegativeOrZero()) $amount = '0';
109
110 $sep_found = strpos($amount, $this->decimal_separator);
111 if (',' === $this->decimal_separator && false !== $sep_found) {
112 $whole = substr($amount, 0, $sep_found);
113 $part = substr($amount, $sep_found + 1, (strlen($amount) - 1));
114 $amount = $whole . '.' . $part;
115 }
116
117 // Strip "," and " " from the amount (if set as the thousands separator).
118 foreach ([',', ' '] as $thousands_separator) {
119 if ($thousands_separator === $this->thousands_separator && false !== strpos($amount, $this->thousands_separator)) {
120 $amount = str_replace($thousands_separator, '', $amount);
121 }
122 }
123
124 // one last formatting check especially when amount contains currency
125 $amount = preg_replace('/[^0-9\.]/', '', $amount);
126
127 return $amount;
128 }
129
130 /**
131 * Formats the amount for display.
132 * Does not apply the currency code.
133 *
134 * @param bool $decimals
135 *
136 * @return self
137 */
138 public function format($decimals = true)
139 {
140 $amount = $this->unformat();
141
142 if (empty($amount)) $amount = 0;
143
144 $decimals = apply_filters('ppress_format_amount_decimals', $decimals ? $this->number_decimals : 0, $amount, $this->currency_code);
145
146 // Format amount using decimals and separators (also rounds up or down)
147 $formatted = number_format((float)$amount, $decimals, $this->decimal_separator, $this->thousands_separator);
148
149 $this->amount = apply_filters('ppress_format_amount', $formatted, $amount, $decimals, $this->decimal_separator, $this->thousands_separator, $this->currency_code);
150
151 return $this;
152 }
153
154 /**
155 * Formats the amount for display.
156 * Does not apply the currency code.
157 *
158 * @param bool $decimals
159 *
160 * @return self
161 */
162 public function sanitize($decimals = true)
163 {
164 $amount = $this->unformat();
165
166 if (empty($amount)) $amount = 0;
167
168 $decimals = (int)apply_filters('ppress_format_amount_decimals', $decimals ? $this->number_decimals : 0, $amount, $this->currency_code);
169
170 $formatted = Calculator::init($amount)->toScale($decimals)->val();
171
172 $this->amount = apply_filters('ppress_sanitize_amount', $formatted, $amount, $decimals, $this->decimal_separator, $this->thousands_separator, $this->currency_code);
173
174 return $this;
175 }
176
177 /**
178 * Applies the currency prefix/suffix to the amount.
179 *
180 * @return self
181 */
182 public function apply_symbol()
183 {
184 $amount = $this->amount;
185 $is_negative = is_numeric($this->amount) && $this->amount < 0;
186
187 // Remove "-" from start.
188 if ($is_negative) {
189 $amount = substr($amount, 1);
190 }
191
192 $formatted = '';
193 if ( ! empty($this->prefix)) {
194 $formatted .= $this->prefix;
195 }
196
197 $formatted .= $amount;
198
199 if ( ! empty($this->suffix)) {
200 $formatted .= $this->suffix;
201 }
202
203 if ( ! empty($this->prefix)) {
204 $formatted = apply_filters('ppress_' . strtolower($this->currency_code) . '_currency_filter_before', $formatted, $this->currency_code, $amount);
205 }
206
207 if ( ! empty($this->suffix)) {
208 $formatted = apply_filters('ppress_' . strtolower($this->currency_code) . '_currency_filter_after', $formatted, $this->currency_code, $amount);
209 }
210
211 // Add the "-" sign back to the start of the string.
212 if ($is_negative) {
213 $formatted = '-' . $formatted;
214 }
215
216 $this->amount = $formatted;
217
218 return $this;
219 }
220
221 /**
222 * Current working amount.
223 *
224 * @return mixed
225 */
226 public function val()
227 {
228 return (string)$this->amount;
229 }
230 }
231