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