PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.8
Pods – Custom Content Types and Fields v3.3.8
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / currency.php
pods / classes / fields Last commit date
avatar.php 4 months ago boolean.php 4 months ago code.php 4 months ago color.php 4 months ago comment.php 4 months ago currency.php 4 months ago date.php 4 months ago datetime.php 4 months ago email.php 4 months ago file.php 4 months ago heading.php 4 months ago html.php 4 months ago link.php 4 months ago number.php 4 months ago oembed.php 4 months ago paragraph.php 4 months ago password.php 4 months ago phone.php 4 months ago pick.php 4 months ago slug.php 4 months ago taxonomy.php 4 months ago text.php 4 months ago time.php 4 months ago website.php 4 months ago wysiwyg.php 4 months ago
currency.php
631 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Fields
10 */
11 class PodsField_Currency extends PodsField_Number {
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $group = 'Number';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $type = 'currency';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $label = 'Currency';
27
28 /**
29 * {@inheritdoc}
30 */
31 public static $prepare = '%d';
32
33 /**
34 * Currency Formats
35 *
36 * @var array
37 * @since 2.0.0
38 */
39 public static $currencies = [];
40
41 /**
42 * {@inheritdoc}
43 */
44 public function setup() {
45
46 static::$group = __( 'Number', 'pods' );
47 static::$label = __( 'Currency', 'pods' );
48
49 static::data_currencies();
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function options() {
56
57 $currency_options = [];
58 foreach ( static::$currencies as $key => $value ) {
59 $currency = $value['label'];
60 if ( $value['label'] !== $value['name'] ) {
61 $currency .= ': ' . $value['name'];
62 }
63 $currency .= ' (' . $value['sign'] . ')';
64 $currency_options[ $key ] = $currency;
65 }
66
67 $options = [
68 static::$type . '_format_type' => [
69 'label' => __( 'Input Type', 'pods' ),
70 'default' => 'number',
71 'type' => 'pick',
72 'data' => [
73 'number' => __( 'Freeform Number', 'pods' ),
74 'slider' => __( 'Slider', 'pods' ),
75 ],
76 'pick_format_single' => 'dropdown',
77 'pick_show_select_text' => 0,
78 'dependency' => true,
79 ],
80 static::$type . '_format_sign' => [
81 'label' => __( 'Currency Sign', 'pods' ),
82 'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),
83 'type' => 'pick',
84 'data' => apply_filters( 'pods_form_ui_field_number_currency_options', $currency_options ),
85 'pick_format_single' => 'dropdown',
86 'pick_show_select_text' => 0,
87 ],
88 static::$type . '_format_placement' => [
89 'label' => __( 'Currency Placement on Display', 'pods' ),
90 'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),
91 'help' => __( 'This is the placement of the currency sign when displaying the value. The input will always have the sign on the left to identify the currency', 'pods' ),
92 'type' => 'pick',
93 'data' => [
94 'before' => __( 'Before (ex. $100)', 'pods' ),
95 'after' => __( 'After (ex. 100$)', 'pods' ),
96 'before_space' => __( 'Before with space (ex. $ 100)', 'pods' ),
97 'after_space' => __( 'After with space (ex. 100 $)', 'pods' ),
98 'none' => __( 'None (ex. 100)', 'pods' ),
99 'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' ),
100 'beforeaftercode_space' => __( 'Before with space and with Currency Code after (ex. $ 100 USD)', 'pods' ),
101 ],
102 'pick_format_single' => 'dropdown',
103 'pick_show_select_text' => 0,
104 ],
105 static::$type . '_format' => [
106 'label' => __( 'Number Format', 'pods' ),
107 'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),
108 'type' => 'pick',
109 'data' => [
110 'i18n' => __( 'Localized Default', 'pods' ),
111 '9,999.99' => '1,234.00',
112 '9999.99' => '1234.00',
113 '9.999,99' => '1.234,00',
114 '9999,99' => '1234,00',
115 '9 999,99' => '1 234,00',
116 '9\'999.99' => '1\'234.00',
117 ],
118 'pick_format_single' => 'dropdown',
119 'pick_show_select_text' => 0,
120 ],
121 static::$type . '_decimals' => [
122 'label' => __( 'Decimals', 'pods' ),
123 'default' => 2,
124 'type' => 'number',
125 'help' => __( 'Set to a positive number to enable decimals. The upper limit in the database for this field is 30 decimals.', 'pods' ),
126 ],
127 static::$type . '_decimal_handling' => [
128 'label' => __( 'Decimal handling for trailing zero decimals', 'pods' ),
129 'default' => 'none',
130 'type' => 'pick',
131 'data' => [
132 'none' => __( 'Default (Examples: "$1.00", "$0.00")', 'pods' ),
133 'remove' => __( 'Remove decimals (Examples: "$1", "$0")', 'pods' ),
134 'remove_only_zero' => __( 'Remove decimals only when number is zero (Examples: "$1.00", "$0")', 'pods' ),
135 'dash' => __( 'Convert to dash (Examples: "$1.-", "$0.-")', 'pods' ),
136 'dash_only_zero' => __( 'Convert to dash only when number is zero (Examples: "$1.00", "$0.-")', 'pods' ),
137 'dash_whole_zero' => __( 'Convert to the whole value to dash when number is zero (Examples: "$1.00", "$-")', 'pods' ),
138 ],
139 'pick_format_single' => 'dropdown',
140 'pick_show_select_text' => 0,
141 ],
142 static::$type . '_step' => [
143 'label' => __( 'Slider Increment (Step)', 'pods' ),
144 'depends-on' => [ static::$type . '_format_type' => 'slider' ],
145 'default' => 1,
146 'type' => 'text',
147 ],
148 static::$type . '_min' => [
149 'label' => __( 'Minimum Number', 'pods' ),
150 'depends-on-any' => [
151 static::$type . '_format_type' => 'slider',
152 static::$type . '_html5' => true,
153 ],
154 'default' => 0,
155 'type' => 'text',
156 ],
157 static::$type . '_max' => [
158 'label' => __( 'Maximum Number', 'pods' ),
159 'depends-on-any' => [
160 static::$type . '_format_type' => 'slider',
161 static::$type . '_html5' => true,
162 ],
163 'default' => 1000,
164 'type' => 'text',
165 ],
166 static::$type . '_max_length' => [
167 'label' => __( 'Maximum Digits', 'pods' ),
168 'default' => 12,
169 'type' => 'number',
170 'help' => __( 'Set to -1 for no limit. The upper limit in the database for this field is 64 digits.', 'pods' ),
171 ],
172 static::$type . '_html5' => [
173 'label' => __( 'Enable HTML5 Input Field', 'pods' ),
174 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
175 'type' => 'boolean',
176 ],
177 static::$type . '_placeholder' => [
178 'label' => __( 'HTML Placeholder', 'pods' ),
179 'default' => '',
180 'type' => 'text',
181 'help' => [
182 __( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
183 'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
184 ],
185 ],
186 ];
187
188 return $options;
189
190 }
191
192 /**
193 * {@inheritdoc}
194 */
195 public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
196
197 $value = $this->format( $value, $name, $options, $pod, $id );
198
199 $currency = 'usd';
200
201 if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, - 1 ) ] ) ) {
202 $currency = (string) pods_v( static::$type . '_format_sign', $options );
203 }
204
205 $currency_sign = (string) static::$currencies[ $currency ]['sign'];
206 $currency_label = (string) static::$currencies[ $currency ]['label'];
207
208 $placement = pods_v( static::$type . '_format_placement', $options, 'before', true );
209
210 // Currency placement policy.
211 // Single sign currencies: 100$, £100
212 // Multiple sign currencies: 100 Fr, Kr 100
213 $currency_gap = '';
214
215 if ( mb_strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) {
216 $currency_gap = ' ';
217 } elseif ( in_array( $placement, [ 'before_space', 'after_space', 'beforeaftercode_space' ], true ) ) {
218 $currency_gap = ' ';
219 }
220
221 switch ( $placement ) {
222 case 'before':
223 case 'before_space':
224 $value = $currency_sign . $currency_gap . $value;
225 break;
226 case 'after':
227 case 'after_space':
228 $value .= $currency_gap . $currency_sign;
229 break;
230 case 'beforeaftercode':
231 case 'beforeaftercode_space':
232 $value = $currency_sign . $currency_gap . $value . ' ' . $currency_label;
233 break;
234 }
235
236 return $value;
237
238 }
239
240 /**
241 * {@inheritdoc}
242 */
243 public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
244
245 $format_args = $this->get_number_format_args( $options );
246 $thousands = $format_args['thousands'];
247 $dot = $format_args['dot'];
248
249 $currency = 'usd';
250
251 if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, - 1 ) ] ) ) {
252 $currency = pods_v( static::$type . '_format_sign', $options );
253 }
254
255 $currency_sign = static::$currencies[ $currency ]['sign'];
256
257 return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( [ $dot, $thousands ] ) ) . ']+';
258
259 }
260
261 /**
262 * {@inheritdoc}
263 */
264 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
265
266 $currency = 'usd';
267
268 if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, - 1 ) ] ) ) {
269 $currency = pods_v( static::$type . '_format_sign', $options );
270 }
271
272 $currency_sign = static::$currencies[ $currency ]['sign'];
273 $currency_entity = static::$currencies[ $currency ]['entity'];
274
275 // Remove currency and thousands symbols.
276 $value = str_replace(
277 [
278 $currency_sign,
279 $currency_entity,
280 html_entity_decode( $currency_sign, ENT_COMPAT ),
281 html_entity_decode( $currency_entity, ENT_COMPAT ),
282 ],
283 '',
284 $value
285 );
286
287 return parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
288
289 }
290
291 /**
292 * {@inheritdoc}
293 */
294 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
295
296 $currency = 'usd';
297
298 if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, - 1 ) ] ) ) {
299 $currency = pods_v( static::$type . '_format_sign', $options );
300 }
301
302 $currency_sign = static::$currencies[ $currency ]['sign'];
303 $currency_entity = static::$currencies[ $currency ]['entity'];
304
305 // Convert decimal type for numeric type.
306 $value = str_replace(
307 [
308 $currency_sign,
309 $currency_entity,
310 html_entity_decode( $currency_sign, ENT_COMPAT ),
311 html_entity_decode( $currency_entity, ENT_COMPAT ),
312 ],
313 '',
314 $value
315 );
316
317 // Remove trailing dash only from the end of the string.
318 $value = rtrim( $value, '-' );
319
320 return parent::pre_save( $value, $id, $name, $options, $fields, $pod, $params );
321
322 }
323
324 /**
325 * Get the currencies and place them in the local property
326 *
327 * @since 2.6.8
328 * @return array
329 */
330 public static function data_currencies() {
331
332 // If it's already done, do not redo the filter.
333 if ( ! empty( static::$currencies ) ) {
334 return static::$currencies;
335 }
336
337 $default_currencies = [
338 'aud' => [
339 'label' => 'AUD',
340 'name' => __( 'Australian Dollar', 'pods' ),
341 'sign' => '$',
342 'entity' => '&#36;',
343 ],
344 'brl' => [
345 'label' => 'BRL',
346 'name' => __( 'Brazilian Real', 'pods' ),
347 'sign' => 'R$',
348 'entity' => 'R&#36;',
349 ],
350 'gbp' => [
351 'label' => 'GBP',
352 'name' => __( 'British Pound', 'pods' ),
353 'sign' => '£',
354 'entity' => '&pound;',
355 ],
356 'cad' => [
357 'label' => 'CAD',
358 'name' => __( 'Canadian Dollar', 'pods' ),
359 'sign' => '$',
360 'entity' => '&#36;',
361 ],
362 'cny' => [
363 'label' => 'CNY',
364 'name' => __( 'Chinese Yen (¥)', 'pods' ),
365 'sign' => '¥',
366 'entity' => '&yen;',
367 ],
368 'cny2' => [
369 'label' => 'CNY',
370 'name' => __( 'Chinese Yuan (�
371 �)', 'pods' ),
372 'sign' => '�
373 �',
374 'entity' => '&#20803;',
375 ],
376 'czk' => [
377 'label' => 'CZK',
378 'name' => __( 'Czech Koruna', 'pods' ),
379 'sign' => '',
380 'entity' => 'K&#x10D;',
381 ],
382 'dkk' => [
383 'label' => 'DKK',
384 'name' => __( 'Danish Krone', 'pods' ),
385 'sign' => 'kr.',
386 'entity' => 'kr.',
387 ],
388 'euro' => [
389 'label' => 'EUR',
390 'name' => __( 'Euro', 'pods' ),
391 'sign' => '',
392 'entity' => '&euro;',
393 ],
394 'hkd' => [
395 'label' => 'HKD',
396 'name' => __( 'Hong Kong Dollar', 'pods' ),
397 'sign' => '$',
398 'entity' => '&#36;',
399 ],
400 'huf' => [
401 'label' => 'HUF',
402 'name' => __( 'Hungarian Forint', 'pods' ),
403 'sign' => 'Ft',
404 'entity' => 'Ft',
405 ],
406 'inr' => [
407 'label' => 'INR',
408 'name' => __( 'Indian Rupee', 'pods' ),
409 'sign' => '',
410 'entity' => '&#x20B9;',
411 ],
412 'idr' => [
413 'label' => 'IDR',
414 'name' => __( 'Indonesian Rupiah', 'pods' ),
415 'sign' => 'Rp',
416 'entity' => 'Rp',
417 ],
418 'ils' => [
419 'label' => 'ILS',
420 'name' => __( 'Israeli New Sheqel', 'pods' ),
421 'sign' => '',
422 'entity' => '&#x20AA;',
423 ],
424 'jpy' => [
425 'label' => 'JPY',
426 'name' => __( 'Japanese Yen', 'pods' ),
427 'sign' => '¥',
428 'entity' => '&yen;',
429 ],
430 'krw' => [
431 'label' => 'KRW',
432 'name' => __( 'Korean Won', 'pods' ),
433 'sign' => '',
434 'entity' => '&#8361;',
435 ],
436 'myr' => [
437 'label' => 'MYR',
438 'name' => __( 'Malaysian Ringgit', 'pods' ),
439 'sign' => 'RM',
440 'entity' => 'RM',
441 ],
442 'mxn' => [
443 'label' => 'MXN',
444 'name' => __( 'Mexican Peso', 'pods' ),
445 'sign' => '$',
446 'entity' => '&#36;',
447 ],
448 'ngn' => [
449 'label' => 'NGN',
450 'name' => __( 'Nigerian Naira', 'pods' ),
451 'sign' => '',
452 'entity' => '&#8358;',
453 ],
454 'nzd' => [
455 'label' => 'NZD',
456 'name' => __( 'New Zealand Dollar', 'pods' ),
457 'sign' => '$',
458 'entity' => '&#36;',
459 ],
460 'nok' => [
461 'label' => 'NOK',
462 'name' => __( 'Norwegian Krone', 'pods' ),
463 'sign' => 'kr',
464 'entity' => 'kr',
465 ],
466 'php' => [
467 'label' => 'PHP',
468 'name' => __( 'Philippine Peso', 'pods' ),
469 'sign' => '',
470 'entity' => '&#x20B1;',
471 ],
472 'pln' => [
473 'label' => 'PLN',
474 'name' => __( 'Polish Złoty', 'pods' ),
475 'sign' => '',
476 'entity' => 'z&#x142;',
477 ],
478 'rub' => [
479 'label' => 'RUB',
480 'name' => __( 'Russian Ruble', 'pods' ),
481 'sign' => '',
482 'entity' => '&#8381;',
483 ],
484 'sek' => [
485 'label' => 'SEK',
486 'name' => __( 'Swedish Krona', 'pods' ),
487 'sign' => 'kr',
488 'entity' => 'kr',
489 ],
490 'sgd' => [
491 'label' => 'SGD',
492 'name' => __( 'Singapore Dollar', 'pods' ),
493 'sign' => '$',
494 'entity' => '&#36;',
495 ],
496 'zar' => [
497 'label' => 'ZAR',
498 'name' => __( 'South African Rand', 'pods' ),
499 'sign' => 'R',
500 'entity' => 'R',
501 ],
502 'chf' => [
503 'label' => 'CHF',
504 'name' => __( 'Swiss Franc', 'pods' ),
505 'sign' => 'Fr',
506 'entity' => 'Fr',
507 ],
508 'twd' => [
509 'label' => 'TWD',
510 'name' => __( 'Taiwan New Dollar', 'pods' ),
511 'sign' => '$',
512 'entity' => '&#36;',
513 ],
514 'thb' => [
515 'label' => 'THB',
516 'name' => __( 'Thai Baht', 'pods' ),
517 'sign' => '฿',
518 'entity' => '&#x0E3F;',
519 ],
520 'trl' => [
521 'label' => 'TRL',
522 'name' => __( 'Turkish Lira', 'pods' ),
523 'sign' => '',
524 'entity' => '&#8378;',
525 ],
526 'usd' => [
527 'label' => 'USD',
528 'name' => __( 'US Dollar', 'pods' ),
529 'sign' => '$',
530 'entity' => '&#36;',
531 ],
532 'usdcent' => [
533 'label' => 'USDCENT',
534 'name' => __( 'US Dollar Cent', 'pods' ),
535 'sign' => '¢',
536 'entity' => '&cent;',
537 ],
538 'vnd' => [
539 'label' => 'VND',
540 'name' => __( 'Vietnamese Dong', 'pods' ),
541 'sign' => '',
542 'entity' => '&#8363;',
543 ],
544 ];
545
546 /**
547 * Add custom currencies
548 *
549 * @param array $options {
550 * Required array of arrays.
551 *
552 * @type array {
553 * @type string $label The label (example: USD).
554 * @type string $name The full name (example: US Dollar).
555 * @type string $sign The sign (example: $).
556 * @type string $entity The HTML entity (example: &#36;).
557 * }
558 * }
559 * @return array
560 */
561 static::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
562
563 // Sort the currencies
564 ksort( static::$currencies );
565
566 // Backwards compatibility
567 foreach ( static::$currencies as $key => $value ) {
568 if ( is_string( $value ) ) {
569 static::$currencies[ $key ] = [
570 'label' => strtoupper( $key ),
571 'name' => strtoupper( $key ),
572 'sign' => $value,
573 'entity' => $value,
574 ];
575 } elseif ( is_array( $value ) ) {
576 // Make sure all required values are set
577 if ( empty( $value['label'] ) ) {
578 $value['label'] = $key;
579 }
580 if ( empty( $value['name'] ) ) {
581 $value['name'] = $key;
582 }
583 if ( empty( $value['sign'] ) ) {
584 $value['sign'] = $key;
585 }
586 if ( empty( $value['entity'] ) ) {
587 $value['entity'] = $key;
588 }
589 } else {
590 // Invalid
591 unset( static::$currencies[ $key ] );
592 }//end if
593 }//end foreach
594
595 return static::$currencies;
596 }
597
598 /**
599 * Get the max allowed decimals.
600 * Overwrites the default value of Number field. 2 decimals instead of 0.
601 *
602 * @since 2.7.0
603 *
604 * @param array $options Field options.
605 *
606 * @return int
607 */
608 public function get_max_decimals( $options ) {
609
610 $length = (int) pods_v( static::$type . '_max_length', $options, 12, true );
611
612 if ( $length < 1 || 64 < $length ) {
613 $length = 64;
614 }
615
616 $decimals = (int) pods_v( static::$type . '_decimals', $options, 2 );
617
618 if ( $decimals < 1 ) {
619 $decimals = 0;
620 } elseif ( 30 < $decimals ) {
621 $decimals = 30;
622 }
623
624 if ( $length < $decimals ) {
625 $decimals = $length;
626 }
627
628 return $decimals;
629 }
630 }
631