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
|
|
|
'usd' => '$', |
48
|
|
|
'euro' => '€', |
49
|
|
|
'gbp' => '£', |
50
|
|
|
'cad' => '$', |
51
|
|
|
'aud' => '$', |
52
|
|
|
'nzd' => '$', |
53
|
|
|
'rub' => '₽', |
54
|
|
|
'chf' => 'CHF', |
55
|
|
|
'dkk' => 'kr', |
56
|
|
|
'nok' => 'kr', |
57
|
|
|
'sek' => 'kr', |
58
|
|
|
'zar' => 'R', |
59
|
|
|
'inr' => '₹', |
60
|
|
|
'jpy' => '¥', |
61
|
|
|
'cny' => '¥', |
62
|
|
|
'sgd' => '$', |
63
|
|
|
'krw' => '₩', |
64
|
|
|
'thb' => '฿', |
65
|
|
|
'trl' => '₺', |
66
|
|
|
'vnd' => '₫' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Do things like register/enqueue scripts and stylesheets |
71
|
|
|
* |
72
|
|
|
* @since 2.0 |
73
|
|
|
*/ |
74
|
|
|
public function __construct() { |
75
|
|
|
|
76
|
|
|
self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', self::$currencies ); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add options and set defaults to |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* @since 2.0 |
86
|
|
|
*/ |
87
|
|
|
public function options() { |
88
|
|
|
|
89
|
|
|
$options = array( |
90
|
|
|
self::$type . '_repeatable' => array( |
91
|
|
|
'label' => __( 'Repeatable Field', 'pods' ), |
92
|
|
|
'default' => 0, |
93
|
|
|
'type' => 'boolean', |
94
|
|
|
'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' ), |
95
|
|
|
'boolean_yes_label' => '', |
96
|
|
|
'dependency' => true, |
97
|
|
|
'developer_mode' => true |
98
|
|
|
), |
99
|
|
|
self::$type . '_format_type' => array( |
100
|
|
|
'label' => __( 'Input Type', 'pods' ), |
101
|
|
|
'default' => 'number', |
102
|
|
|
'type' => 'pick', |
103
|
|
|
'data' => array( |
104
|
|
|
'number' => __( 'Freeform Number', 'pods' ), |
105
|
|
|
'slider' => __( 'Slider', 'pods' ) |
106
|
|
|
), |
107
|
|
|
'dependency' => true |
108
|
|
|
), |
109
|
|
|
self::$type . '_format_sign' => array( |
110
|
|
|
'label' => __( 'Currency Sign', 'pods' ), |
111
|
|
|
'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ), |
112
|
|
|
'type' => 'pick', |
113
|
|
|
'data' => apply_filters( 'pods_form_ui_field_number_currency_options', array( |
114
|
|
|
'usd' => '$ (USD)', |
115
|
|
|
'euro' => '€ (EUR)', |
116
|
|
|
'gbp' => '£ (GBP)', |
117
|
|
|
'aud' => 'Australian Dollar (AUD)', |
118
|
|
|
'cad' => 'Canadian Dollar (CAD)', |
119
|
|
|
'cny' => 'Chinese Yuan (CNY)', |
120
|
|
|
'dkk' => 'Danish Krone (DKK)', |
121
|
|
|
'inr' => 'Indian Rupee (INR)', |
122
|
|
|
'jpy' => 'Japanese Yen (JPY)', |
123
|
|
|
'krw' => 'Korean Won (KRW)', |
124
|
|
|
'nzd' => 'New Zealand Dollar (NZD)', |
125
|
|
|
'nok' => 'Norwegian Krone (NOK)', |
126
|
|
|
'rub' => 'Russian Ruble (RUB)', |
127
|
|
|
'sgd' => 'Singapore Dollar (SGD)', |
128
|
|
|
'zar' => 'South African Rand (ZAR)', |
129
|
|
|
'sek' => 'Swedish Krona (SEK)', |
130
|
|
|
'chf' => 'Swiss Franc (CHF)', |
131
|
|
|
'thb' => 'Thai Baht (THB)', |
132
|
|
|
'trl' => 'Turkish Lira (TRL)', |
133
|
|
|
'vnd' => 'Vietnamese Dong (VND)' |
134
|
|
|
) ) |
135
|
|
|
), |
136
|
|
|
self::$type . '_format_placement' => array( |
137
|
|
|
'label' => __( 'Currency Placement', 'pods' ), |
138
|
|
|
'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ), |
139
|
|
|
'type' => 'pick', |
140
|
|
|
'data' => array( |
141
|
|
|
'before' => __( 'Before (ex. $100)', 'pods' ), |
142
|
|
|
'after' => __( 'After (ex. 100$)', 'pods' ), |
143
|
|
|
'none' => __( 'None (ex. 100)', 'pods' ), |
144
|
|
|
'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' ) |
145
|
|
|
) |
146
|
|
|
), |
147
|
|
|
self::$type . '_format' => array( |
148
|
|
|
'label' => __( 'Format', 'pods' ), |
149
|
|
|
'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ), |
150
|
|
|
'type' => 'pick', |
151
|
|
|
'data' => array( |
152
|
|
|
'i18n' => __( 'Localized Default', 'pods' ), |
153
|
|
|
'9,999.99' => '1,234.00', |
154
|
|
|
'9\'999.99' => '1\'234.00', |
155
|
|
|
'9.999,99' => '1.234,00', |
156
|
|
|
'9 999,99' => '1 234,00', |
157
|
|
|
'9999.99' => '1234.00', |
158
|
|
|
'9999,99' => '1234,00' |
159
|
|
|
) |
160
|
|
|
), |
161
|
|
|
self::$type . '_decimals' => array( |
162
|
|
|
'label' => __( 'Decimals', 'pods' ), |
163
|
|
|
'default' => 2, |
164
|
|
|
'type' => 'number' |
165
|
|
|
), |
166
|
|
|
self::$type . '_step' => array( |
167
|
|
|
'label' => __( 'Slider Increment (Step)', 'pods' ), |
168
|
|
|
'depends-on' => array( self::$type . '_format_type' => 'slider' ), |
169
|
|
|
'default' => 1, |
170
|
|
|
'type' => 'text' |
171
|
|
|
), |
172
|
|
|
self::$type . '_min' => array( |
173
|
|
|
'label' => __( 'Minimum Number', 'pods' ), |
174
|
|
|
'depends-on' => array( self::$type . '_format_type' => 'slider' ), |
175
|
|
|
'default' => 0, |
176
|
|
|
'type' => 'text' |
177
|
|
|
), |
178
|
|
|
self::$type . '_max' => array( |
179
|
|
|
'label' => __( 'Maximum Number', 'pods' ), |
180
|
|
|
'depends-on' => array( self::$type . '_format_type' => 'slider' ), |
181
|
|
|
'default' => 1000, |
182
|
|
|
'type' => 'text' |
183
|
|
|
), |
184
|
|
|
self::$type . '_max_length' => array( |
185
|
|
|
'label' => __( 'Maximum Length', 'pods' ), |
186
|
|
|
'default' => 12, |
187
|
|
|
'type' => 'number', |
188
|
|
|
'help' => __( 'Set to -1 for no limit', 'pods' ) |
189
|
|
|
) |
190
|
|
|
/*, |
|
|
|
|
191
|
|
|
self::$type . '_size' => array( |
192
|
|
|
'label' => __( 'Field Size', 'pods' ), |
193
|
|
|
'default' => 'medium', |
194
|
|
|
'type' => 'pick', |
195
|
|
|
'data' => array( |
196
|
|
|
'small' => __( 'Small', 'pods' ), |
197
|
|
|
'medium' => __( 'Medium', 'pods' ), |
198
|
|
|
'large' => __( 'Large', 'pods' ) |
199
|
|
|
) |
200
|
|
|
)*/ |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
return $options; |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Define the current field's schema for DB table storage |
209
|
|
|
* |
210
|
|
|
* @param array $options |
211
|
|
|
* |
212
|
|
|
* @return array |
213
|
|
|
* @since 2.0 |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function schema( $options = null ) { |
|
|
|
|
216
|
|
|
|
217
|
|
|
$length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); |
218
|
|
|
|
219
|
|
|
if ( $length < 1 || 64 < $length ) { |
220
|
|
|
$length = 64; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); |
224
|
|
|
|
225
|
|
|
if ( $decimals < 1 ) { |
226
|
|
|
$decimals = 0; |
227
|
|
|
} |
228
|
|
|
elseif ( 30 < $decimals ) { |
229
|
|
|
$decimals = 30; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ( $length < $decimals ) { |
233
|
|
|
$decimals = $length; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$schema = 'DECIMAL(' . $length . ',' . $decimals . ')'; |
237
|
|
|
|
238
|
|
|
return $schema; |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Define the current field's preparation for sprintf |
244
|
|
|
* |
245
|
|
|
* @param array $options |
246
|
|
|
* |
247
|
|
|
* @return array |
248
|
|
|
* @since 2.0 |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function prepare( $options = null ) { |
|
|
|
|
251
|
|
|
|
252
|
|
|
$format = self::$prepare; |
253
|
|
|
|
254
|
|
|
$length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); |
255
|
|
|
|
256
|
|
|
if ( $length < 1 || 64 < $length ) { |
257
|
|
|
$length = 64; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); |
261
|
|
|
|
262
|
|
|
if ( $decimals < 1 ) { |
263
|
|
|
$decimals = 0; |
264
|
|
|
} |
265
|
|
|
elseif ( 30 < $decimals ) { |
266
|
|
|
$decimals = 30; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ( $length < $decimals ) { |
270
|
|
|
$decimals = $length; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if ( 0 < $decimals ) { |
274
|
|
|
$format = '%01.' . $decimals . 'F'; |
275
|
|
|
} |
276
|
|
|
else { |
277
|
|
|
$format = '%d'; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $format; |
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Change the way the value of the field is displayed with Pods::get |
286
|
|
|
* |
287
|
|
|
* @param mixed $value |
288
|
|
|
* @param string $name |
289
|
|
|
* @param array $options |
290
|
|
|
* @param array $pod |
291
|
|
|
* @param int $id |
292
|
|
|
* |
293
|
|
|
* @return mixed|null|string |
294
|
|
|
* @since 2.0 |
295
|
|
|
*/ |
296
|
|
|
public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
297
|
|
|
|
298
|
|
|
$value = $this->format( $value, $name, $options, $pod, $id ); |
299
|
|
|
|
300
|
|
|
$currency = 'usd'; |
301
|
|
|
|
302
|
|
View Code Duplication |
if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { |
303
|
|
|
$currency = pods_v( self::$type . '_format_sign', $options ); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$currency_sign = self::$currencies[ $currency ]; |
307
|
|
|
|
308
|
|
|
$placement = pods_v( self::$type . '_format_placement', $options, 'before', true ); |
309
|
|
|
|
310
|
|
|
// Currency placement policy |
311
|
|
|
// Single sign currencies: 100$, £100 |
312
|
|
|
// Multiple sign currencies: 100 Fr, Kr 100 |
313
|
|
|
$currency_gap = ''; |
314
|
|
|
|
315
|
|
|
if ( strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) { |
316
|
|
|
$currency_gap = ' '; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
if ( 'before' == $placement ) { |
320
|
|
|
$value = $currency_sign . $currency_gap . $value; |
321
|
|
|
} |
322
|
|
|
elseif ( 'after' == $placement ) { |
323
|
|
|
$value .= $currency_gap . $currency_sign; |
324
|
|
|
} |
325
|
|
|
elseif ( 'beforeaftercode' == $placement ) { |
326
|
|
|
$value = $currency_sign . $currency_gap . $value . ' ' . strtoupper( $currency ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $value; |
330
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Customize output of the form field |
335
|
|
|
* |
336
|
|
|
* @param string $name |
337
|
|
|
* @param mixed $value |
338
|
|
|
* @param array $options |
339
|
|
|
* @param array $pod |
340
|
|
|
* @param int $id |
341
|
|
|
* |
342
|
|
|
* @since 2.0 |
343
|
|
|
*/ |
344
|
|
View Code Duplication |
public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
|
|
|
345
|
|
|
|
346
|
|
|
$options = (array) $options; |
347
|
|
|
$form_field_type = PodsForm::$field_type; |
|
|
|
|
348
|
|
|
|
349
|
|
|
if ( is_array( $value ) ) { |
350
|
|
|
$value = implode( '', $value ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
if ( 'slider' == pods_v( self::$type . '_format_type', $options, 'number' ) ) { |
354
|
|
|
$field_type = 'slider'; |
355
|
|
|
} |
356
|
|
|
else { |
357
|
|
|
$field_type = 'currency'; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { |
361
|
|
|
if ( pods_v( 'read_only', $options, false ) ) { |
362
|
|
|
$options[ 'readonly' ] = true; |
363
|
|
|
|
364
|
|
|
$field_type = 'text'; |
365
|
|
|
|
366
|
|
|
$value = $this->format( $value, $name, $options, $pod, $id ); |
367
|
|
|
} |
368
|
|
|
else { |
369
|
|
|
return; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
373
|
|
|
$options[ 'readonly' ] = true; |
374
|
|
|
|
375
|
|
|
$field_type = 'text'; |
376
|
|
|
|
377
|
|
|
$value = $this->format( $value, $name, $options, $pod, $id ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
381
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Build regex necessary for JS validation |
386
|
|
|
* |
387
|
|
|
* @param mixed $value |
388
|
|
|
* @param string $name |
389
|
|
|
* @param array $options |
390
|
|
|
* @param string $pod |
391
|
|
|
* @param int $id |
392
|
|
|
* |
393
|
|
|
* @return bool|string |
394
|
|
|
* @since 2.0 |
395
|
|
|
*/ |
396
|
|
|
public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
397
|
|
|
|
398
|
|
|
global $wp_locale; |
|
|
|
|
399
|
|
|
|
400
|
|
View Code Duplication |
if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) { |
401
|
|
|
$thousands = '.'; |
402
|
|
|
$dot = ','; |
403
|
|
|
} |
404
|
|
|
elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) { |
405
|
|
|
$thousands = ','; |
406
|
|
|
$dot = '.'; |
407
|
|
|
} |
408
|
|
|
elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { |
409
|
|
|
$thousands = '\''; |
410
|
|
|
$dot = '.'; |
411
|
|
|
} |
412
|
|
|
elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) { |
413
|
|
|
$thousands = ' '; |
414
|
|
|
$dot = ','; |
415
|
|
|
} |
416
|
|
|
elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { |
417
|
|
|
$thousands = ''; |
418
|
|
|
$dot = '.'; |
419
|
|
|
} |
420
|
|
|
elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) { |
421
|
|
|
$thousands = ''; |
422
|
|
|
$dot = ','; |
423
|
|
|
} |
424
|
|
|
else { |
425
|
|
|
$thousands = $wp_locale->number_format[ 'thousands_sep' ]; |
426
|
|
|
$dot = $wp_locale->number_format[ 'decimal_point' ]; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$currency = 'usd'; |
430
|
|
|
|
431
|
|
View Code Duplication |
if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { |
432
|
|
|
$currency = pods_v( self::$type . '_format_sign', $options ); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
$currency_sign = self::$currencies[ $currency ]; |
436
|
|
|
|
437
|
|
|
return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+'; |
438
|
|
|
|
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Validate a value before it's saved |
443
|
|
|
* |
444
|
|
|
* @param mixed $value |
445
|
|
|
* @param string $name |
446
|
|
|
* @param array $options |
447
|
|
|
* @param array $fields |
448
|
|
|
* @param array $pod |
449
|
|
|
* @param int $id |
450
|
|
|
* @param null $params |
451
|
|
|
* |
452
|
|
|
* @return bool|mixed|void |
453
|
|
|
* @since 2.0 |
454
|
|
|
*/ |
455
|
|
|
public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
456
|
|
|
|
457
|
|
|
global $wp_locale; |
|
|
|
|
458
|
|
|
|
459
|
|
View Code Duplication |
if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) { |
460
|
|
|
$thousands = '.'; |
461
|
|
|
$dot = ','; |
462
|
|
|
} |
463
|
|
|
elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) { |
464
|
|
|
$thousands = ','; |
465
|
|
|
$dot = '.'; |
466
|
|
|
} |
467
|
|
|
elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { |
468
|
|
|
$thousands = '\''; |
469
|
|
|
$dot = '.'; |
470
|
|
|
} |
471
|
|
|
elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) { |
472
|
|
|
$thousands = ' '; |
473
|
|
|
$dot = ','; |
474
|
|
|
} |
475
|
|
|
elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { |
476
|
|
|
$thousands = ','; |
477
|
|
|
$dot = '.'; |
478
|
|
|
} |
479
|
|
|
elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) { |
480
|
|
|
$thousands = '.'; |
481
|
|
|
$dot = ','; |
482
|
|
|
} |
483
|
|
|
else { |
484
|
|
|
$thousands = $wp_locale->number_format[ 'thousands_sep' ]; |
485
|
|
|
$dot = $wp_locale->number_format[ 'decimal_point' ]; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
$currency = 'usd'; |
489
|
|
|
|
490
|
|
View Code Duplication |
if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { |
491
|
|
|
$currency = pods_v( self::$type . '_format_sign', $options ); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
$currency_sign = self::$currencies[ $currency ]; |
495
|
|
|
|
496
|
|
|
$check = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value ); |
497
|
|
|
$check = trim( $check ); |
498
|
|
|
|
499
|
|
|
$check = preg_replace( '/[0-9\.\-]/', '', $check ); |
500
|
|
|
|
501
|
|
|
$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); |
502
|
|
|
|
503
|
|
|
if ( 0 < strlen( $check ) ) { |
504
|
|
|
return sprintf( __( '%s is not numeric', 'pods' ), $label ); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
return true; |
508
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Change the value or perform actions after validation but before saving to the DB |
513
|
|
|
* |
514
|
|
|
* @param mixed $value |
515
|
|
|
* @param int $id |
516
|
|
|
* @param string $name |
517
|
|
|
* @param array $options |
518
|
|
|
* @param array $fields |
519
|
|
|
* @param array $pod |
520
|
|
|
* @param object $params |
521
|
|
|
* |
522
|
|
|
* @return mixed|string |
523
|
|
|
* @since 2.0 |
524
|
|
|
*/ |
525
|
|
|
public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
526
|
|
|
|
527
|
|
|
global $wp_locale; |
|
|
|
|
528
|
|
|
|
529
|
|
View Code Duplication |
if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) { |
530
|
|
|
$thousands = '.'; |
531
|
|
|
$dot = ','; |
532
|
|
|
} |
533
|
|
|
elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) { |
534
|
|
|
$thousands = ','; |
535
|
|
|
$dot = '.'; |
536
|
|
|
} |
537
|
|
|
elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) { |
538
|
|
|
$thousands = '\''; |
539
|
|
|
$dot = '.'; |
540
|
|
|
} |
541
|
|
|
elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) { |
542
|
|
|
$thousands = ' '; |
543
|
|
|
$dot = ','; |
544
|
|
|
} |
545
|
|
|
elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { |
546
|
|
|
$thousands = ','; |
547
|
|
|
$dot = '.'; |
548
|
|
|
} |
549
|
|
|
elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) { |
550
|
|
|
$thousands = '.'; |
551
|
|
|
$dot = ','; |
552
|
|
|
} |
553
|
|
|
else { |
554
|
|
|
$thousands = $wp_locale->number_format[ 'thousands_sep' ]; |
555
|
|
|
$dot = $wp_locale->number_format[ 'decimal_point' ]; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
$currency = 'usd'; |
559
|
|
|
|
560
|
|
View Code Duplication |
if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) { |
561
|
|
|
$currency = pods_v( self::$type . '_format_sign', $options ); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
$currency_sign = self::$currencies[ $currency ]; |
565
|
|
|
|
566
|
|
|
$value = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value ); |
567
|
|
|
$value = trim( $value ); |
568
|
|
|
|
569
|
|
|
$value = preg_replace( '/[^0-9\.\-]/', '', $value ); |
570
|
|
|
|
571
|
|
|
$length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); |
572
|
|
|
|
573
|
|
|
if ( $length < 1 || 64 < $length ) { |
574
|
|
|
$length = 64; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true ); |
578
|
|
|
|
579
|
|
|
if ( $decimals < 1 ) { |
580
|
|
|
$decimals = 0; |
581
|
|
|
} |
582
|
|
|
elseif ( 30 < $decimals ) { |
583
|
|
|
$decimals = 30; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
if ( $length < $decimals ) { |
587
|
|
|
$decimals = $length; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
$value = number_format( (float) $value, $decimals, '.', '' ); |
591
|
|
|
|
592
|
|
|
return $value; |
593
|
|
|
|
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Customize the Pods UI manage table column output |
598
|
|
|
* |
599
|
|
|
* @param int $id |
600
|
|
|
* @param mixed $value |
601
|
|
|
* @param string $name |
602
|
|
|
* @param array $options |
603
|
|
|
* @param array $fields |
604
|
|
|
* @param array $pod |
605
|
|
|
* |
606
|
|
|
* @return mixed|null|string |
607
|
|
|
* @since 2.0 |
608
|
|
|
*/ |
609
|
|
|
public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
610
|
|
|
|
611
|
|
|
return $this->display( $value, $name, $options, $pod, $id ); |
612
|
|
|
|
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Reformat a number to the way the value of the field is displayed |
617
|
|
|
* |
618
|
|
|
* @param mixed $value |
619
|
|
|
* @param string $name |
620
|
|
|
* @param array $options |
621
|
|
|
* @param array $pod |
622
|
|
|
* @param int $id |
623
|
|
|
* |
624
|
|
|
* @return string |
625
|
|
|
* @since 2.0 |
626
|
|
|
*/ |
627
|
|
|
public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
|
|
|
|
628
|
|
|
|
629
|
|
|
global $wp_locale; |
|
|
|
|
630
|
|
|
|
631
|
|
|
if ( null === $value ) { |
632
|
|
|
// Don't enforce a default value here |
633
|
|
|
return null; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
View Code Duplication |
if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) { |
637
|
|
|
$thousands = '.'; |
638
|
|
|
$dot = ','; |
639
|
|
|
} |
640
|
|
|
elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) { |
641
|
|
|
$thousands = ','; |
642
|
|
|
$dot = '.'; |
643
|
|
|
} |
644
|
|
|
elseif ( '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 ( '9999.99' == pods_v( self::$type . '_format', $options ) ) { |
653
|
|
|
$thousands = ''; |
654
|
|
|
$dot = '.'; |
655
|
|
|
} |
656
|
|
|
elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) { |
657
|
|
|
$thousands = ''; |
658
|
|
|
$dot = ','; |
659
|
|
|
} |
660
|
|
|
else { |
661
|
|
|
$thousands = $wp_locale->number_format[ 'thousands_sep' ]; |
662
|
|
|
$dot = $wp_locale->number_format[ 'decimal_point' ]; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
$length = (int) pods_v( self::$type . '_max_length', $options, 12, true ); |
666
|
|
|
|
667
|
|
|
if ( $length < 1 || 64 < $length ) { |
668
|
|
|
$length = 64; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
$decimals = (int) pods_v( self::$type . '_decimals', $options, 2 ); |
672
|
|
|
|
673
|
|
|
if ( $decimals < 1 ) { |
674
|
|
|
$decimals = 0; |
675
|
|
|
} |
676
|
|
|
elseif ( 30 < $decimals ) { |
677
|
|
|
$decimals = 30; |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
if ( $length < $decimals ) { |
681
|
|
|
$decimals = $length; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
View Code Duplication |
if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) { |
685
|
|
|
$value = number_format_i18n( (float) $value, $decimals ); |
686
|
|
|
} |
687
|
|
|
else { |
688
|
|
|
$value = number_format( (float) $value, $decimals, $dot, $thousands ); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
return $value; |
692
|
|
|
|
693
|
|
|
} |
694
|
|
|
} |
695
|
|
|
|
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.