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

PodsField_Currency   D

Complexity

Total Complexity 89

Size/Duplication

Total Lines 962
Duplicated Lines 23.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 228
loc 962
rs 4.4444
c 0
b 0
f 0
wmc 89
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B options() 0 118 3
B schema() 26 26 6
C prepare() 33 33 7
C display() 3 37 7
C input() 39 39 8
C regex() 31 44 8
C validate() 31 70 9
D pre_save() 31 85 13
A ui() 0 5 1
F format() 34 80 17
D data_currencies() 0 246 9

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PodsField_Currency often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PodsField_Currency, and based on these observations, apply Extract Interface, too.

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 string
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 string
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 ]['sign'];
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
		$currency_entity = self::$currencies[ $currency ]['entity'];
474
475
		// Remove currency and thousands symbols
476
		$check = str_replace(
477
			array(
478
				$thousands,
479
				$currency_sign,
480
				$currency_entity,
481
				html_entity_decode( $thousands ),
482
				html_entity_decode( $currency_sign ),
483
				html_entity_decode( $currency_entity ),
484
			),
485
			'',
486
			$value
487
		);
488
		// Convert decimal type for numeric type
489
		$check = str_replace( $dot, '.', $check );
490
		$check = trim( $check );
491
492
		$check = preg_replace( '/[0-9\.\-\s]/', '', $check );
493
494
		$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
495
496
		if ( 0 < strlen( $check ) ) {
497
			return sprintf( __( '%s is not numeric', 'pods' ), $label );
498
		}
499
500
		return true;
501
502
	}
503
504
	/**
505
	 * Change the value or perform actions after validation but before saving to the DB
506
	 *
507
	 * @param mixed $value
508
	 * @param int $id
509
	 * @param string $name
510
	 * @param array $options
511
	 * @param array $fields
512
	 * @param array $pod
513
	 * @param object $params
514
	 *
515
	 * @return mixed|string
516
	 * @since 2.0
517
	 */
518
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
519
520
		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...
521
522 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
523
			$thousands = '.';
524
			$dot = ',';
525
		}
526
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
527
			$thousands = ',';
528
			$dot = '.';
529
		}
530
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
531
			$thousands = '\'';
532
			$dot = '.';
533
		}
534
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
535
			$thousands = ' ';
536
			$dot = ',';
537
		}
538
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
539
			$thousands = ',';
540
			$dot = '.';
541
		}
542
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
543
			$thousands = '.';
544
			$dot = ',';
545
		}
546
		else {
547
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
548
			$dot = $wp_locale->number_format[ 'decimal_point' ];
549
		}
550
551
		$currency = 'usd';
552
553 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
554
			$currency = pods_v( self::$type . '_format_sign', $options );
555
		}
556
557
		$currency_sign = self::$currencies[ $currency ]['sign'];
558
		$currency_entity = self::$currencies[ $currency ]['entity'];
559
560
		// Convert decimal type for numeric type
561
		$value = str_replace(
562
			array(
563
				$thousands,
564
				$currency_sign,
565
				$currency_entity,
566
				html_entity_decode( $thousands ),
567
				html_entity_decode( $currency_sign ),
568
				html_entity_decode( $currency_entity ),
569
			),
570
			'',
571
			$value
572
		);
573
		// Convert decimal type for numeric type
574
		$value = str_replace( $dot, '.', $value );
575
		$value = trim( $value );
576
577
		$value = preg_replace( '/[^0-9\.\-]/', '', $value );
578
579
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
580
581
		if ( $length < 1 || 64 < $length ) {
582
			$length = 64;
583
		}
584
585
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
586
587
		if ( $decimals < 1 ) {
588
			$decimals = 0;
589
		}
590
		elseif ( 30 < $decimals ) {
591
			$decimals = 30;
592
		}
593
594
		if ( $length < $decimals ) {
595
			$decimals = $length;
596
		}
597
598
		$value = number_format( (float) $value, $decimals, '.', '' );
599
600
		return $value;
601
602
	}
603
604
	/**
605
	 * Customize the Pods UI manage table column output
606
	 *
607
	 * @param int $id
608
	 * @param mixed $value
609
	 * @param string $name
610
	 * @param array $options
611
	 * @param array $fields
612
	 * @param array $pod
613
	 *
614
	 * @return mixed|null|string
615
	 * @since 2.0
616
	 */
617
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
618
619
		return $this->display( $value, $name, $options, $pod, $id );
620
621
	}
622
623
	/**
624
	 * Reformat a number to the way the value of the field is displayed
625
	 *
626
	 * @param mixed $value
627
	 * @param string $name
628
	 * @param array $options
629
	 * @param array $pod
630
	 * @param int $id
631
	 *
632
	 * @return string
633
	 * @since 2.0
634
	 */
635
	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...
636
637
		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...
638
639
		if ( null === $value ) {
640
			// Don't enforce a default value here
641
			return null;
642
		}
643
644 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
645
			$thousands = '.';
646
			$dot = ',';
647
		}
648
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
649
			$thousands = ',';
650
			$dot = '.';
651
		}
652
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
653
			$thousands = '\'';
654
			$dot = '.';
655
		}
656
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
657
			$thousands = ' ';
658
			$dot = ',';
659
		}
660
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
661
			$thousands = '';
662
			$dot = '.';
663
		}
664
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
665
			$thousands = '';
666
			$dot = ',';
667
		}
668
		else {
669
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
670
			$dot = $wp_locale->number_format[ 'decimal_point' ];
671
		}
672
673
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
674
675
		if ( $length < 1 || 64 < $length ) {
676
			$length = 64;
677
		}
678
679
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2 );
680
681
		if ( $decimals < 1 ) {
682
			$decimals = 0;
683
		}
684
		elseif ( 30 < $decimals ) {
685
			$decimals = 30;
686
		}
687
688
		if ( $length < $decimals ) {
689
			$decimals = $length;
690
		}
691
692 View Code Duplication
		if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) {
693
			$value = number_format_i18n( (float) $value, $decimals );
694
		}
695
		else {
696
			$value = number_format( (float) $value, $decimals, $dot, $thousands );
697
		}
698
699
		// Additional output handling for decimals
700
		$decimal_handling = pods_v( self::$type . '_decimal_handling', $options, 'none' ) ;
701
		if ( 'none' !== $decimal_handling ) {
702
			$value_parts = explode( $dot, $value );
703
			if ( 'remove' === $decimal_handling ) {
704
				array_pop( $value_parts );
705
			} elseif ( 'dash' === $decimal_handling ) {
706
				array_pop( $value_parts );
707
				$value_parts[] = '-';
708
			}
709
			$value = implode( $dot, $value_parts );
710
		}
711
712
		return $value;
713
714
	}
715
716
	/**
717
	 * Get the currencies and place them in the local property
718
	 * @since  2.6.8
719
	 * @return array
720
	 */
721
	public static function data_currencies() {
722
723
		// If it's already done, do not redo the filter
724
		if ( ! empty( self::$currencies ) ) {
725
			return self::$currencies;
726
		}
727
728
		$default_currencies = array(
729
			'aud' => array(
730
				'label'  => 'AUD',
731
				'name'   => __( 'Australian Dollar', 'pods' ),
732
				'sign'   => '$',
733
				'entity' => '&#36;',
734
			),
735
			'brl' => array(
736
				'label'  => 'BRL',
737
				'name'   => __( 'Brazilian Real', 'pods' ),
738
				'sign'   => 'R$',
739
				'entity' => 'R&#36;',
740
			),
741
			'cad' => array(
742
				'label'  => 'CAD',
743
				'name'   => __( 'Canadian Dollar', 'pods' ),
744
				'sign'   => '$',
745
				'entity' => '&#36;',
746
			),
747
			'chf' => array(
748
				'label'  => 'CHF',
749
				'name'   => __( 'Swiss Franc', 'pods' ),
750
				'sign'   => 'Fr',
751
				'entity' => 'Fr',
752
			),
753
			'cny' => array(
754
				'label'  => 'CNY',
755
				'name'   => __( 'Chinese Yuan', 'pods' ),
756
				'sign'   => '¥',
757
				'entity' => '&yen;',
758
			),
759
			'cny2' => array(
760
				'label'  => 'CNY',
761
				'name'   => __( 'Chinese Yuan', 'pods' ),
762
				'sign'   => '元',
763
				'entity' => '&#20803;',
764
			),
765
			'czk' => array(
766
				'label'  => 'CZK',
767
				'name'   => __( 'Czech Koruna', 'pods' ),
768
				'sign'   => 'Kč',
769
				'entity' => 'K&#x10D;',
770
			),
771
			'dkk' => array(
772
				'label'  => 'DKK',
773
				'name'   => __( 'Danish Krone', 'pods' ),
774
				'sign'   => 'kr.',
775
				'entity' => 'kr.',
776
			),
777
			'euro' => array(
778
				'label'  => 'EUR',
779
				'name'   => __( 'Euro', 'pods' ),
780
				'sign'   => '€',
781
				'entity' => '&euro;',
782
			),
783
			'gbp' => array(
784
				'label'  => 'GBP',
785
				'name'   => __( 'British Pound', 'pods' ),
786
				'sign'   => '£',
787
				'entity' => '&pound;',
788
			),
789
			'hkd' => array(
790
				'label'  => 'HKD',
791
				'name'   => __( 'Hong Kong Dollar', 'pods' ),
792
				'sign'   => '$',
793
				'entity' => '&#36;',
794
			),
795
			'huf' => array(
796
				'label'  => 'HUF',
797
				'name'   => __( 'Hungarian Forint', 'pods' ),
798
				'sign'   => 'Ft',
799
				'entity' => 'Ft',
800
			),
801
			'ils' => array(
802
				'label'  => 'ILS',
803
				'name'   => __( 'Israeli New Sheqel', 'pods' ),
804
				'sign'   => '₪',
805
				'entity' => '&#x20AA;',
806
			),
807
			'jpy' => array(
808
				'label'  => 'JPY',
809
				'name'   => __( 'Japanese Yen', 'pods' ),
810
				'sign'   => '¥',
811
				'entity' => '&yen;',
812
			),
813
			'krw' => array(
814
				'label'  => 'KRW',
815
				'name'   => __( 'Korean Won', 'pods' ),
816
				'sign'   => '₩',
817
				'entity' => '&#8361;',
818
			),
819
			'myr' => array(
820
				'label'  => 'MYR',
821
				'name'   => __( 'Malaysian Ringgit', 'pods' ),
822
				'sign'   => 'MR',
823
				'entity' => 'MR',
824
			),
825
			'mxn' => array(
826
				'label'  => 'MXN',
827
				'name'   => __( 'Mexican Peso', 'pods' ),
828
				'sign'   => '$',
829
				'entity' => '&#36;',
830
			),
831
			'nok' => array(
832
				'label'  => 'NOK',
833
				'name'   => __( 'Norwegian Krone', 'pods' ),
834
				'sign'   => 'kr',
835
				'entity' => 'kr',
836
			),
837
			'nzd' => array(
838
				'label'  => 'NZD',
839
				'name'   => __( 'New Zealand Dollar', 'pods' ),
840
				'sign'   => '$',
841
				'entity' => '&#36;',
842
			),
843
			'php' => array(
844
				'label'  => 'PHP',
845
				'name'   => __( 'Philippine Peso', 'pods' ),
846
				'sign'   => '₱',
847
				'entity' => '&#x20B1;',
848
			),
849
			'pln' => array(
850
				'label'  => 'PLN',
851
				'name'   => __( 'Polish Złoty', 'pods' ),
852
				'sign'   => 'zł',
853
				'entity' => 'z&#x142;',
854
			),
855
			'rub' => array(
856
				'label'  => 'RUB',
857
				'name'   => __( 'Russian Ruble', 'pods' ),
858
				'sign'   => '₽',
859
				'entity' => '&#8381;',
860
			),
861
			'sek' => array(
862
				'label'  => 'SEK',
863
				'name'   => __( 'Swedish Krona', 'pods' ),
864
				'sign'   => 'kr',
865
				'entity' => 'kr',
866
			),
867
			'sgd' => array(
868
				'label'  => 'SGD',
869
				'name'   => __( 'Singapore Dollar', 'pods' ),
870
				'sign'   => '$',
871
				'entity' => '&#36;',
872
			),
873
			'thb' => array(
874
				'label'  => 'THB',
875
				'name'   => __( 'Thai Baht', 'pods' ),
876
				'sign'   => '฿',
877
				'entity' => '&#x0E3F;',
878
			),
879
			'trl' => array(
880
				'label'  => 'TRL',
881
				'name'   => __( 'Turkish Lira', 'pods' ),
882
				'sign'   => '₺',
883
				'entity' => '&#8378;',
884
			),
885
			'twd' => array(
886
				'label'  => 'TWD',
887
				'name'   => __( 'Taiwan New Dollar', 'pods' ),
888
				'sign'   => '$',
889
				'entity' => '&#36;',
890
			),
891
			'usd' => array(
892
				'label'  => 'USD',
893
				'name'   => __( 'US Dollar', 'pods' ),
894
				'sign'   => '$',
895
				'entity' => '&#36;',
896
			),
897
			'vnd' => array(
898
				'label'  => 'VND',
899
				'name'   => __( 'Vietnamese Dong', 'pods' ),
900
				'sign'   => '₫',
901
				'entity' => '&#8363;',
902
			),
903
			'zar' => array(
904
				'label'  => 'ZAR',
905
				'name'   => __( 'South African Rand', 'pods' ),
906
				'sign'   => 'R',
907
				'entity' => 'R',
908
			),
909
			'inr' => array(
910
				'label'  => 'INR',
911
				'name'   => __( 'Indian Rupee', 'pods' ),
912
				'sign'   => '₹',
913
				'entity' => '&#x20B9;',
914
			),
915
		);
916
917
		/**
918
		 * Add custom currencies
919
		 *
920
		 * @param  array  $options {
921
		 *     Required array of arrays.
922
		 *     @type  array {
923
		 *         @type  string  $label   The label (example: USD).
924
		 *         @type  string  $name    The full name (example: US Dollar).
925
		 *         @type  string  $sign    The sign (example: $).
926
		 *         @type  string  $entity  The HTML entity (example: &#36;).
927
		 *     }
928
		 * }
929
		 * @return array
930
		 */
931
		self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
932
933
		// Sort the currencies
934
		ksort( self::$currencies );
935
936
		// Backwards compatibility
937
		foreach ( self::$currencies as $key => $value ) {
938
			if ( is_string( $value ) ) {
939
				self::$currencies[ $key ] = array(
940
					'label'  => strtoupper( $key ),
941
					'name'   => strtoupper( $key ),
942
					'sign'   => $value,
943
					'entity' => $value,
944
				);
945
			} elseif ( is_array( $value ) ) {
946
				// Make sure all required values are set
947
				if ( empty( $value['label'] ) ) {
948
					$value['label'] = $key;
949
				}
950
				if ( empty( $value['name'] ) ) {
951
					$value['name'] = $key;
952
				}
953
				if ( empty( $value['sign'] ) ) {
954
					$value['sign'] = $key;
955
				}
956
				if ( empty( $value['entity'] ) ) {
957
					$value['entity'] = $key;
958
				}
959
			} else {
960
				// Invalid
961
				unset( self::$currencies[ $key ] );
962
			}
963
		}
964
965
		return self::$currencies;
966
	}
967
}
968