PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.4
Pods – Custom Content Types and Fields v3.3.4
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 1 year ago comment.php 4 years ago currency.php 9 months ago date.php 2 years ago datetime.php 1 year ago email.php 1 year ago file.php 11 months ago heading.php 1 year ago html.php 2 years ago link.php 1 year ago number.php 9 months ago oembed.php 1 year ago paragraph.php 2 years ago password.php 1 year ago phone.php 1 year ago pick.php 11 months ago slug.php 3 years ago taxonomy.php 4 years ago text.php 2 years ago time.php 2 years ago website.php 11 months ago wysiwyg.php 1 year ago
currency.php
627 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 for trailing zero decimals', 'pods' ),
124 'description' => __( '', 'pods' ),
125 'default' => 'none',
126 'type' => 'pick',
127 'data' => array(
128 'none' => __( 'Default (Examples: "$1.00", "$0.00")', 'pods' ),
129 'remove' => __( 'Remove decimals (Examples: "$1", "$0")', 'pods' ),
130 'remove_only_zero' => __( 'Remove decimals only when number is zero (Examples: "$1.00", "$0")', 'pods' ),
131 'dash' => __( 'Convert to dash (Examples: "$1.-", "$0.-")', 'pods' ),
132 'dash_only_zero' => __( 'Convert to dash only when number is zero (Examples: "$1.00", "$0.-")', 'pods' ),
133 'dash_whole_zero' => __( 'Convert to the whole value to dash when number is zero (Examples: "$1.00", "$-")', 'pods' ),
134 ),
135 'pick_format_single' => 'dropdown',
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 * Get the currencies and place them in the local property
322 *
323 * @since 2.6.8
324 * @return array
325 */
326 public static function data_currencies() {
327
328 // If it's already done, do not redo the filter.
329 if ( ! empty( static::$currencies ) ) {
330 return static::$currencies;
331 }
332
333 $default_currencies = array(
334 'aud' => array(
335 'label' => 'AUD',
336 'name' => __( 'Australian Dollar', 'pods' ),
337 'sign' => '$',
338 'entity' => '&#36;',
339 ),
340 'brl' => array(
341 'label' => 'BRL',
342 'name' => __( 'Brazilian Real', 'pods' ),
343 'sign' => 'R$',
344 'entity' => 'R&#36;',
345 ),
346 'gbp' => array(
347 'label' => 'GBP',
348 'name' => __( 'British Pound', 'pods' ),
349 'sign' => '£',
350 'entity' => '&pound;',
351 ),
352 'cad' => array(
353 'label' => 'CAD',
354 'name' => __( 'Canadian Dollar', 'pods' ),
355 'sign' => '$',
356 'entity' => '&#36;',
357 ),
358 'cny' => array(
359 'label' => 'CNY',
360 'name' => __( 'Chinese Yen (¥)', 'pods' ),
361 'sign' => '¥',
362 'entity' => '&yen;',
363 ),
364 'cny2' => array(
365 'label' => 'CNY',
366 'name' => __( 'Chinese Yuan (�
367 �)', 'pods' ),
368 'sign' => '�
369 �',
370 'entity' => '&#20803;',
371 ),
372 'czk' => array(
373 'label' => 'CZK',
374 'name' => __( 'Czech Koruna', 'pods' ),
375 'sign' => '',
376 'entity' => 'K&#x10D;',
377 ),
378 'dkk' => array(
379 'label' => 'DKK',
380 'name' => __( 'Danish Krone', 'pods' ),
381 'sign' => 'kr.',
382 'entity' => 'kr.',
383 ),
384 'euro' => array(
385 'label' => 'EUR',
386 'name' => __( 'Euro', 'pods' ),
387 'sign' => '',
388 'entity' => '&euro;',
389 ),
390 'hkd' => array(
391 'label' => 'HKD',
392 'name' => __( 'Hong Kong Dollar', 'pods' ),
393 'sign' => '$',
394 'entity' => '&#36;',
395 ),
396 'huf' => array(
397 'label' => 'HUF',
398 'name' => __( 'Hungarian Forint', 'pods' ),
399 'sign' => 'Ft',
400 'entity' => 'Ft',
401 ),
402 'inr' => array(
403 'label' => 'INR',
404 'name' => __( 'Indian Rupee', 'pods' ),
405 'sign' => '',
406 'entity' => '&#x20B9;',
407 ),
408 'idr' => array(
409 'label' => 'IDR',
410 'name' => __( 'Indonesian Rupiah', 'pods' ),
411 'sign' => 'Rp',
412 'entity' => 'Rp',
413 ),
414 'ils' => array(
415 'label' => 'ILS',
416 'name' => __( 'Israeli New Sheqel', 'pods' ),
417 'sign' => '',
418 'entity' => '&#x20AA;',
419 ),
420 'jpy' => array(
421 'label' => 'JPY',
422 'name' => __( 'Japanese Yen', 'pods' ),
423 'sign' => '¥',
424 'entity' => '&yen;',
425 ),
426 'krw' => array(
427 'label' => 'KRW',
428 'name' => __( 'Korean Won', 'pods' ),
429 'sign' => '',
430 'entity' => '&#8361;',
431 ),
432 'myr' => array(
433 'label' => 'MYR',
434 'name' => __( 'Malaysian Ringgit', 'pods' ),
435 'sign' => 'RM',
436 'entity' => 'RM',
437 ),
438 'mxn' => array(
439 'label' => 'MXN',
440 'name' => __( 'Mexican Peso', 'pods' ),
441 'sign' => '$',
442 'entity' => '&#36;',
443 ),
444 'ngn' => array(
445 'label' => 'NGN',
446 'name' => __( 'Nigerian Naira', 'pods' ),
447 'sign' => '',
448 'entity' => '&#8358;',
449 ),
450 'nzd' => array(
451 'label' => 'NZD',
452 'name' => __( 'New Zealand Dollar', 'pods' ),
453 'sign' => '$',
454 'entity' => '&#36;',
455 ),
456 'nok' => array(
457 'label' => 'NOK',
458 'name' => __( 'Norwegian Krone', 'pods' ),
459 'sign' => 'kr',
460 'entity' => 'kr',
461 ),
462 'php' => array(
463 'label' => 'PHP',
464 'name' => __( 'Philippine Peso', 'pods' ),
465 'sign' => '',
466 'entity' => '&#x20B1;',
467 ),
468 'pln' => array(
469 'label' => 'PLN',
470 'name' => __( 'Polish Złoty', 'pods' ),
471 'sign' => '',
472 'entity' => 'z&#x142;',
473 ),
474 'rub' => array(
475 'label' => 'RUB',
476 'name' => __( 'Russian Ruble', 'pods' ),
477 'sign' => '',
478 'entity' => '&#8381;',
479 ),
480 'sek' => array(
481 'label' => 'SEK',
482 'name' => __( 'Swedish Krona', 'pods' ),
483 'sign' => 'kr',
484 'entity' => 'kr',
485 ),
486 'sgd' => array(
487 'label' => 'SGD',
488 'name' => __( 'Singapore Dollar', 'pods' ),
489 'sign' => '$',
490 'entity' => '&#36;',
491 ),
492 'zar' => array(
493 'label' => 'ZAR',
494 'name' => __( 'South African Rand', 'pods' ),
495 'sign' => 'R',
496 'entity' => 'R',
497 ),
498 'chf' => array(
499 'label' => 'CHF',
500 'name' => __( 'Swiss Franc', 'pods' ),
501 'sign' => 'Fr',
502 'entity' => 'Fr',
503 ),
504 'twd' => array(
505 'label' => 'TWD',
506 'name' => __( 'Taiwan New Dollar', 'pods' ),
507 'sign' => '$',
508 'entity' => '&#36;',
509 ),
510 'thb' => array(
511 'label' => 'THB',
512 'name' => __( 'Thai Baht', 'pods' ),
513 'sign' => '฿',
514 'entity' => '&#x0E3F;',
515 ),
516 'trl' => array(
517 'label' => 'TRL',
518 'name' => __( 'Turkish Lira', 'pods' ),
519 'sign' => '',
520 'entity' => '&#8378;',
521 ),
522 'usd' => array(
523 'label' => 'USD',
524 'name' => __( 'US Dollar', 'pods' ),
525 'sign' => '$',
526 'entity' => '&#36;',
527 ),
528 'usdcent' => array(
529 'label' => 'USDCENT',
530 'name' => __( 'US Dollar Cent', 'pods' ),
531 'sign' => '¢',
532 'entity' => '&cent;',
533 ),
534 'vnd' => array(
535 'label' => 'VND',
536 'name' => __( 'Vietnamese Dong', 'pods' ),
537 'sign' => '',
538 'entity' => '&#8363;',
539 ),
540 );
541
542 /**
543 * Add custom currencies
544 *
545 * @param array $options {
546 * Required array of arrays.
547 *
548 * @type array {
549 * @type string $label The label (example: USD).
550 * @type string $name The full name (example: US Dollar).
551 * @type string $sign The sign (example: $).
552 * @type string $entity The HTML entity (example: &#36;).
553 * }
554 * }
555 * @return array
556 */
557 static::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
558
559 // Sort the currencies
560 ksort( static::$currencies );
561
562 // Backwards compatibility
563 foreach ( static::$currencies as $key => $value ) {
564 if ( is_string( $value ) ) {
565 static::$currencies[ $key ] = array(
566 'label' => strtoupper( $key ),
567 'name' => strtoupper( $key ),
568 'sign' => $value,
569 'entity' => $value,
570 );
571 } elseif ( is_array( $value ) ) {
572 // Make sure all required values are set
573 if ( empty( $value['label'] ) ) {
574 $value['label'] = $key;
575 }
576 if ( empty( $value['name'] ) ) {
577 $value['name'] = $key;
578 }
579 if ( empty( $value['sign'] ) ) {
580 $value['sign'] = $key;
581 }
582 if ( empty( $value['entity'] ) ) {
583 $value['entity'] = $key;
584 }
585 } else {
586 // Invalid
587 unset( static::$currencies[ $key ] );
588 }//end if
589 }//end foreach
590
591 return static::$currencies;
592 }
593
594 /**
595 * Get the max allowed decimals.
596 * Overwrites the default value of Number field. 2 decimals instead of 0.
597 *
598 * @since 2.7.0
599 *
600 * @param array $options Field options.
601 *
602 * @return int
603 */
604 public function get_max_decimals( $options ) {
605
606 $length = (int) pods_v( static::$type . '_max_length', $options, 12, true );
607
608 if ( $length < 1 || 64 < $length ) {
609 $length = 64;
610 }
611
612 $decimals = (int) pods_v( static::$type . '_decimals', $options, 2 );
613
614 if ( $decimals < 1 ) {
615 $decimals = 0;
616 } elseif ( 30 < $decimals ) {
617 $decimals = 30;
618 }
619
620 if ( $length < $decimals ) {
621 $decimals = $length;
622 }
623
624 return $decimals;
625 }
626 }
627