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