Completed
Pull Request — 2.x (#3954)
by Jory
04:16
created

PodsField_Currency::ui()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 6
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Currency extends PodsField {
7
8
	/**
9
	 * Field Type Group
10
	 *
11
	 * @var string
12
	 * @since 2.0
13
	 */
14
	public static $group = 'Number';
15
16
	/**
17
	 * Field Type Identifier
18
	 *
19
	 * @var string
20
	 * @since 2.0
21
	 */
22
	public static $type = 'currency';
23
24
	/**
25
	 * Field Type Label
26
	 *
27
	 * @var string
28
	 * @since 2.0
29
	 */
30
	public static $label = 'Currency';
31
32
	/**
33
	 * Field Type Preparation
34
	 *
35
	 * @var string
36
	 * @since 2.0
37
	 */
38
	public static $prepare = '%d';
39
40
	/**
41
	 * Currency Formats
42
	 *
43
	 * @var array
44
	 * @since 2.0
45
	 */
46
	public static $currencies = array();
47
48
	/**
49
	 * Do things like register/enqueue scripts and stylesheets
50
	 *
51
	 * @since 2.0
52
	 */
53
	public function __construct() {
54
		self::$label = __( 'Currency', 'pods' );
55
		self::data_currencies();
56
	}
57
58
	/**
59
	 * Add options and set defaults to
60
	 *
61
	 * @return array
62
	 *
63
	 * @since 2.0
64
	 */
65
	public function options() {
66
67
		$currency_options = array();
68
		foreach ( self::$currencies as $key => $value ) {
69
			$currency = $value['label'];
70
			if ( $value['label'] != $value['name'] ) {
71
				$currency .= ': ' . $value['name'];
72
			}
73
			$currency .= ' (' . $value['sign'] . ')';
74
			$currency_options[ $key ] = $currency;
75
		}
76
77
		$options = array(
78
			self::$type . '_repeatable' => array(
79
				'label' => __( 'Repeatable Field', 'pods' ),
80
				'default' => 0,
81
				'type' => 'boolean',
82
				'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' ),
83
				'boolean_yes_label' => '',
84
				'dependency' => true,
85
				'developer_mode' => true
86
			),
87
			self::$type . '_format_type' => array(
88
				'label' => __( 'Input Type', 'pods' ),
89
				'default' => 'number',
90
				'type' => 'pick',
91
				'data' => array(
92
					'number' => __( 'Freeform Number', 'pods' ),
93
					'slider' => __( 'Slider', 'pods' )
94
				),
95
				'dependency' => true
96
			),
97
			self::$type . '_format_sign' => array(
98
				'label' => __( 'Currency Sign', 'pods' ),
99
				'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),
100
				'type' => 'pick',
101
				'data' => apply_filters( 'pods_form_ui_field_number_currency_options', $currency_options )
102
			),
103
			self::$type . '_format_placement' => array(
104
				'label' => __( 'Currency Placement', 'pods' ),
105
				'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),
106
				'type' => 'pick',
107
				'data' => array(
108
					'before' => __( 'Before (ex. $100)', 'pods' ),
109
					'after' => __( 'After (ex. 100$)', 'pods' ),
110
					'none' => __( 'None (ex. 100)', 'pods' ),
111
					'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' )
112
				)
113
			),
114
			self::$type . '_format' => array(
115
				'label' => __( 'Format', 'pods' ),
116
				'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),
117
				'type' => 'pick',
118
				'data' => array(
119
					'i18n' => __( 'Localized Default', 'pods' ),
120
					'9,999.99' => '1,234.00',
121
					'9\'999.99' => '1\'234.00',
122
					'9.999,99' => '1.234,00',
123
					'9 999,99' => '1 234,00',
124
					'9999.99' => '1234.00',
125
					'9999,99' => '1234,00'
126
				)
127
			),
128
			self::$type . '_decimals' => array(
129
				'label' => __( 'Decimals', 'pods' ),
130
				'default' => 2,
131
				'type' => 'number'
132
			),
133
			self::$type . '_decimal_handling' => array(
134
				'label' => __( 'Decimal handling when zero', 'pods' ),
135
				'default' => 'none',
136
				'type' => 'pick',
137
				'data' => array(
138
					'none' => __( 'Default', 'pods' ),
139
					'remove' => __( 'Remove decimals', 'pods' ),
140
					'dash' => __( 'Convert to dash', 'pods' ) . ' (-)',
141
				)
142
			),
143
			self::$type . '_step' => array(
144
				'label' => __( 'Slider Increment (Step)', 'pods' ),
145
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
146
				'default' => 1,
147
				'type' => 'text'
148
			),
149
			self::$type . '_min' => array(
150
				'label' => __( 'Minimum Number', 'pods' ),
151
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
152
				'default' => 0,
153
				'type' => 'text'
154
			),
155
			self::$type . '_max' => array(
156
				'label' => __( 'Maximum Number', 'pods' ),
157
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
158
				'default' => 1000,
159
				'type' => 'text'
160
			),
161
			self::$type . '_max_length' => array(
162
				'label' => __( 'Maximum Length', 'pods' ),
163
				'default' => 12,
164
				'type' => 'number',
165
				'help' => __( 'Set to -1 for no limit', 'pods' )
166
			)
167
			/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
			self::$type . '_size' => array(
169
				'label' => __( 'Field Size', 'pods' ),
170
				'default' => 'medium',
171
				'type' => 'pick',
172
				'data' => array(
173
					'small' => __( 'Small', 'pods' ),
174
					'medium' => __( 'Medium', 'pods' ),
175
					'large' => __( 'Large', 'pods' )
176
				)
177
			)*/
178
		);
179
180
		return $options;
181
182
	}
183
184
	/**
185
	 * Define the current field's schema for DB table storage
186
	 *
187
	 * @param array $options
188
	 *
189
	 * @return array
190
	 * @since 2.0
191
	 */
192 View Code Duplication
	public function schema( $options = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
194
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
195
196
		if ( $length < 1 || 64 < $length ) {
197
			$length = 64;
198
		}
199
200
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
201
202
		if ( $decimals < 1 ) {
203
			$decimals = 0;
204
		}
205
		elseif ( 30 < $decimals ) {
206
			$decimals = 30;
207
		}
208
209
		if ( $length < $decimals ) {
210
			$decimals = $length;
211
		}
212
213
		$schema = 'DECIMAL(' . $length . ',' . $decimals . ')';
214
215
		return $schema;
216
217
	}
218
219
	/**
220
	 * Define the current field's preparation for sprintf
221
	 *
222
	 * @param array $options
223
	 *
224
	 * @return array
225
	 * @since 2.0
226
	 */
227 View Code Duplication
	public function prepare( $options = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
229
		$format = self::$prepare;
230
231
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
232
233
		if ( $length < 1 || 64 < $length ) {
234
			$length = 64;
235
		}
236
237
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
238
239
		if ( $decimals < 1 ) {
240
			$decimals = 0;
241
		}
242
		elseif ( 30 < $decimals ) {
243
			$decimals = 30;
244
		}
245
246
		if ( $length < $decimals ) {
247
			$decimals = $length;
248
		}
249
250
		if ( 0 < $decimals ) {
251
			$format = '%01.' . $decimals . 'F';
252
		}
253
		else {
254
			$format = '%d';
255
		}
256
257
		return $format;
258
259
	}
260
261
	/**
262
	 * Change the way the value of the field is displayed with Pods::get
263
	 *
264
	 * @param mixed $value
265
	 * @param string $name
266
	 * @param array $options
267
	 * @param array $pod
268
	 * @param int $id
269
	 *
270
	 * @return mixed|null|string
271
	 * @since 2.0
272
	 */
273
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
274
275
		$value = $this->format( $value, $name, $options, $pod, $id );
276
277
		$currency = 'usd';
278
279 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
280
			$currency = pods_v( self::$type . '_format_sign', $options );
281
		}
282
283
		$currency_sign = self::$currencies[ $currency ]['sign'];
284
		$currency_label = self::$currencies[ $currency ]['label'];
285
286
		$placement = pods_v( self::$type . '_format_placement', $options, 'before', true );
287
288
		// Currency placement policy
289
		// Single sign currencies: 100$, £100
290
		// Multiple sign currencies: 100 Fr, Kr 100
291
		$currency_gap = '';
292
293
		if ( strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) {
294
			$currency_gap = ' ';
295
		}
296
297
		if ( 'before' == $placement ) {
298
			$value = $currency_sign . $currency_gap . $value;
299
		}
300
		elseif ( 'after' == $placement ) {
301
			$value .= $currency_gap . $currency_sign;
302
		}
303
		elseif ( 'beforeaftercode' == $placement ) {
304
			$value = $currency_sign . $currency_gap . $value . ' ' . $currency_label;
305
		}
306
307
		return $value;
308
309
	}
310
311
	/**
312
	 * Customize output of the form field
313
	 *
314
	 * @param string $name
315
	 * @param mixed $value
316
	 * @param array $options
317
	 * @param array $pod
318
	 * @param int $id
319
	 *
320
	 * @since 2.0
321
	 */
322 View Code Duplication
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
324
		$options = (array) $options;
325
		$form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
326
327
		if ( is_array( $value ) ) {
328
			$value = implode( '', $value );
329
		}
330
331
		if ( 'slider' == pods_v( self::$type . '_format_type', $options, 'number' ) ) {
332
			$field_type = 'slider';
333
		}
334
		else {
335
			$field_type = 'currency';
336
		}
337
338
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
339
			if ( pods_v( 'read_only', $options, false ) ) {
340
				$options[ 'readonly' ] = true;
341
342
				$field_type = 'text';
343
344
				$value = $this->format( $value, $name, $options, $pod, $id );
345
			}
346
			else {
347
				return;
348
			}
349
		}
350
		elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
351
			$options[ 'readonly' ] = true;
352
353
			$field_type = 'text';
354
355
			$value = $this->format( $value, $name, $options, $pod, $id );
356
		}
357
358
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
359
360
	}
361
362
	/**
363
	 * Build regex necessary for JS validation
364
	 *
365
	 * @param mixed $value
366
	 * @param string $name
367
	 * @param array $options
368
	 * @param string $pod
369
	 * @param int $id
370
	 *
371
	 * @return bool|string
372
	 * @since 2.0
373
	 */
374
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
375
376
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
377
378 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
379
			$thousands = '.';
380
			$dot = ',';
381
		}
382
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
383
			$thousands = ',';
384
			$dot = '.';
385
		}
386
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
387
			$thousands = '\'';
388
			$dot = '.';
389
		}
390
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
391
			$thousands = ' ';
392
			$dot = ',';
393
		}
394
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
395
			$thousands = '';
396
			$dot = '.';
397
		}
398
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
399
			$thousands = '';
400
			$dot = ',';
401
		}
402
		else {
403
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
404
			$dot = $wp_locale->number_format[ 'decimal_point' ];
405
		}
406
407
		$currency = 'usd';
408
409 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
410
			$currency = pods_v( self::$type . '_format_sign', $options );
411
		}
412
413
		$currency_sign = self::$currencies[ $currency ];
414
415
		return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+';
416
417
	}
418
419
	/**
420
	 * Validate a value before it's saved
421
	 *
422
	 * @param mixed $value
423
	 * @param string $name
424
	 * @param array $options
425
	 * @param array $fields
426
	 * @param array $pod
427
	 * @param int $id
428
	 * @param null $params
429
	 *
430
	 * @return bool|mixed
431
	 * @since 2.0
432
	 */
433
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
434
435
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
436
437 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
438
			$thousands = '.';
439
			$dot = ',';
440
		}
441
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
442
			$thousands = ',';
443
			$dot = '.';
444
		}
445
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
446
			$thousands = '\'';
447
			$dot = '.';
448
		}
449
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
450
			$thousands = ' ';
451
			$dot = ',';
452
		}
453
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
454
			$thousands = ',';
455
			$dot = '.';
456
		}
457
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
458
			$thousands = '.';
459
			$dot = ',';
460
		}
461
		else {
462
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
463
			$dot = $wp_locale->number_format[ 'decimal_point' ];
464
		}
465
466
		$currency = 'usd';
467
468 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
469
			$currency = pods_v( self::$type . '_format_sign', $options );
470
		}
471
472
		$currency_sign = self::$currencies[ $currency ]['sign'];
473
474
		$check = str_replace(
475
			array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ), html_entity_decode( $thousands ) ),
476
			array( '', '.', '', '', '' ),
477
			$value
478
		);
479
		$check = trim( $check );
480
481
		$check = preg_replace( '/[0-9\.\-\s]/', '', $check );
482
483
		$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
484
485
		if ( 0 < strlen( $check ) ) {
486
			return sprintf( __( '%s is not numeric', 'pods' ), $label );
487
		}
488
489
		return true;
490
491
	}
492
493
	/**
494
	 * Change the value or perform actions after validation but before saving to the DB
495
	 *
496
	 * @param mixed $value
497
	 * @param int $id
498
	 * @param string $name
499
	 * @param array $options
500
	 * @param array $fields
501
	 * @param array $pod
502
	 * @param object $params
503
	 *
504
	 * @return mixed|string
505
	 * @since 2.0
506
	 */
507
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
508
509
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
510
511 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
512
			$thousands = '.';
513
			$dot = ',';
514
		}
515
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
516
			$thousands = ',';
517
			$dot = '.';
518
		}
519
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
520
			$thousands = '\'';
521
			$dot = '.';
522
		}
523
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
524
			$thousands = ' ';
525
			$dot = ',';
526
		}
527
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
528
			$thousands = ',';
529
			$dot = '.';
530
		}
531
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
532
			$thousands = '.';
533
			$dot = ',';
534
		}
535
		else {
536
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
537
			$dot = $wp_locale->number_format[ 'decimal_point' ];
538
		}
539
540
		$currency = 'usd';
541
542 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
543
			$currency = pods_v( self::$type . '_format_sign', $options );
544
		}
545
546
		$currency_sign = self::$currencies[ $currency ]['sign'];
547
548
		$value = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value );
549
		$value = trim( $value );
550
551
		$value = preg_replace( '/[^0-9\.\-]/', '', $value );
552
553
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
554
555
		if ( $length < 1 || 64 < $length ) {
556
			$length = 64;
557
		}
558
559
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
560
561
		if ( $decimals < 1 ) {
562
			$decimals = 0;
563
		}
564
		elseif ( 30 < $decimals ) {
565
			$decimals = 30;
566
		}
567
568
		if ( $length < $decimals ) {
569
			$decimals = $length;
570
		}
571
572
		$value = number_format( (float) $value, $decimals, '.', '' );
573
574
		return $value;
575
576
	}
577
578
	/**
579
	 * Customize the Pods UI manage table column output
580
	 *
581
	 * @param int $id
582
	 * @param mixed $value
583
	 * @param string $name
584
	 * @param array $options
585
	 * @param array $fields
586
	 * @param array $pod
587
	 *
588
	 * @return mixed|null|string
589
	 * @since 2.0
590
	 */
591
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
592
593
		return $this->display( $value, $name, $options, $pod, $id );
594
595
	}
596
597
	/**
598
	 * Reformat a number to the way the value of the field is displayed
599
	 *
600
	 * @param mixed $value
601
	 * @param string $name
602
	 * @param array $options
603
	 * @param array $pod
604
	 * @param int $id
605
	 *
606
	 * @return string
607
	 * @since 2.0
608
	 */
609
	public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
610
611
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
612
613
		if ( null === $value ) {
614
			// Don't enforce a default value here
615
			return null;
616
		}
617
618 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
619
			$thousands = '.';
620
			$dot = ',';
621
		}
622
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
623
			$thousands = ',';
624
			$dot = '.';
625
		}
626
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
627
			$thousands = '\'';
628
			$dot = '.';
629
		}
630
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
631
			$thousands = ' ';
632
			$dot = ',';
633
		}
634
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
635
			$thousands = '';
636
			$dot = '.';
637
		}
638
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
639
			$thousands = '';
640
			$dot = ',';
641
		}
642
		else {
643
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
644
			$dot = $wp_locale->number_format[ 'decimal_point' ];
645
		}
646
647
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
648
649
		if ( $length < 1 || 64 < $length ) {
650
			$length = 64;
651
		}
652
653
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2 );
654
655
		if ( $decimals < 1 ) {
656
			$decimals = 0;
657
		}
658
		elseif ( 30 < $decimals ) {
659
			$decimals = 30;
660
		}
661
662
		if ( $length < $decimals ) {
663
			$decimals = $length;
664
		}
665
666 View Code Duplication
		if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) {
667
			$value = number_format_i18n( (float) $value, $decimals );
668
		}
669
		else {
670
			$value = number_format( (float) $value, $decimals, $dot, $thousands );
671
		}
672
673
		// Additional output handling for decimals
674
		$decimal_handling = pods_v( self::$type . '_decimal_handling', $options, 'none' ) ;
675
		if ( 'none' !== $decimal_handling ) {
676
			$value_parts = explode( $dot, $value );
677
			if ( 'remove' === $decimal_handling ) {
678
				array_pop( $value_parts );
679
			} elseif ( 'dash' === $decimal_handling ) {
680
				array_pop( $value_parts );
681
				$value_parts[] = '-';
682
			}
683
			$value = implode( $dot, $value_parts );
684
		}
685
686
		return $value;
687
688
	}
689
690
	/**
691
	 * Get the currencies and place them in the local property
692
	 * @since  2.6.8
693
	 * @return array
694
	 */
695
	public static function data_currencies() {
696
697
		// If it's already done, do not redo the filter
698
		if ( ! empty( self::$currencies ) ) {
699
			return self::$currencies;
700
		}
701
702
		$default_currencies = array(
703
			'aud' => array(
704
				'label'  => 'AUD',
705
				'name'   => __( 'Australian Dollar', 'pods' ),
706
				'sign'   => '$',
707
				'entity' => '&#36;',
708
			),
709
			'brl' => array(
710
				'label'  => 'BRL',
711
				'name'   => __( 'Brazilian Real', 'pods' ),
712
				'sign'   => 'R$',
713
				'entity' => 'R&#36;',
714
			),
715
			'cad' => array(
716
				'label'  => 'CAD',
717
				'name'   => __( 'Canadian Dollar', 'pods' ),
718
				'sign'   => '$',
719
				'entity' => '&#36;',
720
			),
721
			'chf' => array(
722
				'label'  => 'CHF',
723
				'name'   => __( 'Swiss Franc', 'pods' ),
724
				'sign'   => 'Fr',
725
				'entity' => 'Fr',
726
			),
727
			'cny' => array(
728
				'label'  => 'CNY',
729
				'name'   => __( 'Chinese Yuan', 'pods' ),
730
				'sign'   => '¥',
731
				'entity' => '&yen;',
732
			),
733
			'cny2' => array(
734
				'label'  => 'CNY',
735
				'name'   => __( 'Chinese Yuan', 'pods' ),
736
				'sign'   => '元',
737
				'entity' => '&#20803;',
738
			),
739
			'czk' => array(
740
				'label'  => 'CZK',
741
				'name'   => __( 'Czech Koruna', 'pods' ),
742
				'sign'   => 'Kč',
743
				'entity' => 'K&#x10D;',
744
			),
745
			'dkk' => array(
746
				'label'  => 'DKK',
747
				'name'   => __( 'Danish Krone', 'pods' ),
748
				'sign'   => 'kr.',
749
				'entity' => 'kr.',
750
			),
751
			'euro' => array(
752
				'label'  => 'EUR',
753
				'name'   => __( 'Euro', 'pods' ),
754
				'sign'   => '€',
755
				'entity' => '&euro;',
756
			),
757
			'gbp' => array(
758
				'label'  => 'GBP',
759
				'name'   => __( 'British Pound', 'pods' ),
760
				'sign'   => '£',
761
				'entity' => '&pound;',
762
			),
763
			'hkd' => array(
764
				'label'  => 'HKD',
765
				'name'   => __( 'Hong Kong Dollar', 'pods' ),
766
				'sign'   => '$',
767
				'entity' => '&#36;',
768
			),
769
			'huf' => array(
770
				'label'  => 'HUF',
771
				'name'   => __( 'Hungarian Forint', 'pods' ),
772
				'sign'   => 'Ft',
773
				'entity' => 'Ft',
774
			),
775
			'ils' => array(
776
				'label'  => 'ILS',
777
				'name'   => __( 'Israeli New Sheqel', 'pods' ),
778
				'sign'   => '₪',
779
				'entity' => '&#x20AA;',
780
			),
781
			'jpy' => array(
782
				'label'  => 'JPY',
783
				'name'   => __( 'Japanese Yen', 'pods' ),
784
				'sign'   => '¥',
785
				'entity' => '&yen;',
786
			),
787
			'krw' => array(
788
				'label'  => 'KRW',
789
				'name'   => __( 'Korean Won', 'pods' ),
790
				'sign'   => '₩',
791
				'entity' => '&#8361;',
792
			),
793
			'myr' => array(
794
				'label'  => 'MYR',
795
				'name'   => __( 'Malaysian Ringgit', 'pods' ),
796
				'sign'   => 'MR',
797
				'entity' => 'MR',
798
			),
799
			'mxn' => array(
800
				'label'  => 'MXN',
801
				'name'   => __( 'Mexican Peso', 'pods' ),
802
				'sign'   => '$',
803
				'entity' => '&#36;',
804
			),
805
			'nok' => array(
806
				'label'  => 'NOK',
807
				'name'   => __( 'Norwegian Krone', 'pods' ),
808
				'sign'   => 'kr',
809
				'entity' => 'kr',
810
			),
811
			'nzd' => array(
812
				'label'  => 'NZD',
813
				'name'   => __( 'New Zealand Dollar', 'pods' ),
814
				'sign'   => '$',
815
				'entity' => '&#36;',
816
			),
817
			'php' => array(
818
				'label'  => 'PHP',
819
				'name'   => __( 'Philippine Peso', 'pods' ),
820
				'sign'   => '₱',
821
				'entity' => '&#x20B1;',
822
			),
823
			'pln' => array(
824
				'label'  => 'PLN',
825
				'name'   => __( 'Polish Złoty', 'pods' ),
826
				'sign'   => 'zł',
827
				'entity' => 'z&#x142;',
828
			),
829
			'rub' => array(
830
				'label'  => 'RUB',
831
				'name'   => __( 'Russian Ruble', 'pods' ),
832
				'sign'   => '₽',
833
				'entity' => '&#8381;',
834
			),
835
			'sek' => array(
836
				'label'  => 'SEK',
837
				'name'   => __( 'Swedish Krona', 'pods' ),
838
				'sign'   => 'kr',
839
				'entity' => 'kr',
840
			),
841
			'sgd' => array(
842
				'label'  => 'SGD',
843
				'name'   => __( 'Singapore Dollar', 'pods' ),
844
				'sign'   => '$',
845
				'entity' => '&#36;',
846
			),
847
			'thb' => array(
848
				'label'  => 'THB',
849
				'name'   => __( 'Thai Baht', 'pods' ),
850
				'sign'   => '฿',
851
				'entity' => '&#x0E3F;',
852
			),
853
			'trl' => array(
854
				'label'  => 'TRL',
855
				'name'   => __( 'Turkish Lira', 'pods' ),
856
				'sign'   => '₺',
857
				'entity' => '&#8378;',
858
			),
859
			'twd' => array(
860
				'label'  => 'TWD',
861
				'name'   => __( 'Taiwan New Dollar', 'pods' ),
862
				'sign'   => '$',
863
				'entity' => '&#36;',
864
			),
865
			'usd' => array(
866
				'label'  => 'USD',
867
				'name'   => __( 'US Dollar', 'pods' ),
868
				'sign'   => '$',
869
				'entity' => '&#36;',
870
			),
871
			'vnd' => array(
872
				'label'  => 'VND',
873
				'name'   => __( 'Vietnamese Dong', 'pods' ),
874
				'sign'   => '₫',
875
				'entity' => '&#8363;',
876
			),
877
			'zar' => array(
878
				'label'  => 'ZAR',
879
				'name'   => __( 'South African Rand', 'pods' ),
880
				'sign'   => 'R',
881
				'entity' => 'R',
882
			),
883
			'inr' => array(
884
				'label'  => 'INR',
885
				'name'   => __( 'Indian Rupee', 'pods' ),
886
				'sign'   => '₹',
887
				'entity' => '&#x20B9;',
888
			),
889
		);
890
891
		/**
892
		 * Add custom currencies
893
		 *
894
		 * @param  array  $options {
895
		 *     Required array of arrays.
896
		 *     @type  array {
897
		 *         @type  string  $label   The label (example: USD).
898
		 *         @type  string  $name    The full name (example: US Dollar).
899
		 *         @type  string  $sign    The sign (example: $).
900
		 *         @type  string  $entity  The HTML entity (example: &#36;).
901
		 *     }
902
		 * }
903
		 * @return array
904
		 */
905
		self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
906
907
		// Sort the currencies
908
		ksort( self::$currencies );
909
910
		// Backwards compatibility
911
		foreach ( self::$currencies as $key => $value ) {
912
			if ( is_string( $value ) ) {
913
				self::$currencies[ $key ] = array(
914
					'label'  => strtoupper( $key ),
915
					'name'   => strtoupper( $key ),
916
					'sign'   => $value,
917
					'entity' => $value,
918
				);
919
			} elseif ( is_array( $value ) ) {
920
				// Make sure all required values are set
921
				if ( empty( $value['label'] ) ) {
922
					$value['label'] = $key;
923
				}
924
				if ( empty( $value['name'] ) ) {
925
					$value['name'] = $key;
926
				}
927
				if ( empty( $value['sign'] ) ) {
928
					$value['sign'] = $key;
929
				}
930
				if ( empty( $value['entity'] ) ) {
931
					$value['entity'] = $key;
932
				}
933
			} else {
934
				// Invalid
935
				unset( self::$currencies[ $key ] );
936
			}
937
		}
938
939
		return self::$currencies;
940
	}
941
}
942