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