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