This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @package Pods\Fields |
||
5 | */ |
||
6 | class PodsField_Number extends PodsField { |
||
7 | |||
8 | /** |
||
9 | * {@inheritdoc} |
||
10 | */ |
||
11 | public static $group = 'Number'; |
||
12 | |||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | */ |
||
16 | public static $type = 'number'; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | public static $label = 'Plain Number'; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | public static $prepare = '%d'; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function setup() { |
||
32 | |||
33 | self::$label = __( 'Plain Number', 'pods' ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function options() { |
||
40 | |||
41 | $options = array( |
||
42 | static::$type . '_repeatable' => array( |
||
43 | 'label' => __( 'Repeatable Field', 'pods' ), |
||
44 | 'default' => 0, |
||
45 | 'type' => 'boolean', |
||
46 | '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' ), |
||
47 | 'boolean_yes_label' => '', |
||
48 | 'dependency' => true, |
||
49 | 'developer_mode' => true, |
||
50 | ), |
||
51 | static::$type . '_format_type' => array( |
||
52 | 'label' => __( 'Input Type', 'pods' ), |
||
53 | 'default' => 'number', |
||
54 | 'type' => 'pick', |
||
55 | 'data' => array( |
||
56 | 'number' => __( 'Freeform Number', 'pods' ), |
||
57 | 'slider' => __( 'Slider', 'pods' ), |
||
58 | ), |
||
59 | 'dependency' => true, |
||
60 | ), |
||
61 | static::$type . '_format' => array( |
||
62 | 'label' => __( 'Format', 'pods' ), |
||
63 | 'default' => apply_filters( 'pods_form_ui_field_number_format_default', 'i18n' ), |
||
64 | 'type' => 'pick', |
||
65 | 'data' => array( |
||
66 | 'i18n' => __( 'Localized Default', 'pods' ), |
||
67 | '9,999.99' => '1,234.00', |
||
68 | '9.999,99' => '1.234,00', |
||
69 | '9 999,99' => '1 234,00', |
||
70 | '9999.99' => '1234.00', |
||
71 | '9999,99' => '1234,00', |
||
72 | ), |
||
73 | ), |
||
74 | static::$type . '_decimals' => array( |
||
75 | 'label' => __( 'Decimals', 'pods' ), |
||
76 | 'default' => 0, |
||
77 | 'type' => 'number', |
||
78 | 'dependency' => true, |
||
79 | ), |
||
80 | static::$type . '_format_soft' => array( |
||
81 | 'label' => __( 'Soft format?', 'pods' ), |
||
82 | 'help' => __( 'Remove trailing decimals (0)', 'pods' ), |
||
83 | 'default' => 0, |
||
84 | 'type' => 'boolean', |
||
85 | 'excludes-on' => array( static::$type . '_decimals' => 0 ), |
||
86 | ), |
||
87 | static::$type . '_step' => array( |
||
88 | 'label' => __( 'Slider Increment (Step)', 'pods' ), |
||
89 | 'depends-on' => array( static::$type . '_format_type' => 'slider' ), |
||
90 | 'default' => 1, |
||
91 | 'type' => 'text', |
||
92 | ), |
||
93 | static::$type . '_min' => array( |
||
94 | 'label' => __( 'Minimum Number', 'pods' ), |
||
95 | 'depends-on' => array( static::$type . '_format_type' => 'slider' ), |
||
96 | 'default' => 0, |
||
97 | 'type' => 'text', |
||
98 | ), |
||
99 | static::$type . '_max' => array( |
||
100 | 'label' => __( 'Maximum Number', 'pods' ), |
||
101 | 'depends-on' => array( static::$type . '_format_type' => 'slider' ), |
||
102 | 'default' => 100, |
||
103 | 'type' => 'text', |
||
104 | ), |
||
105 | static::$type . '_max_length' => array( |
||
106 | 'label' => __( 'Maximum Length', 'pods' ), |
||
107 | 'default' => 12, |
||
108 | 'type' => 'number', |
||
109 | 'help' => __( 'Set to -1 for no limit', 'pods' ), |
||
110 | ), |
||
111 | static::$type . '_placeholder' => array( |
||
112 | 'label' => __( 'HTML Placeholder', 'pods' ), |
||
113 | 'default' => '', |
||
114 | 'type' => 'text', |
||
115 | 'help' => array( |
||
116 | __( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ), |
||
117 | 'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text', |
||
118 | ), |
||
119 | ), |
||
120 | ); |
||
121 | |||
122 | return $options; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function schema( $options = null ) { |
||
129 | |||
130 | $length = (int) pods_v( static::$type . '_max_length', $options, 12, true ); |
||
131 | |||
132 | if ( $length < 1 || 64 < $length ) { |
||
133 | $length = 64; |
||
134 | } |
||
135 | |||
136 | $decimals = $this->get_max_decimals( $options ); |
||
137 | |||
138 | $schema = 'DECIMAL(' . $length . ',' . $decimals . ')'; |
||
139 | |||
140 | return $schema; |
||
141 | |||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function prepare( $options = null ) { |
||
148 | |||
149 | $format = static::$prepare; |
||
150 | |||
151 | $decimals = $this->get_max_decimals( $options ); |
||
152 | |||
153 | if( 6 < $decimals ) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
154 | // %F only allows 6 decimals by default |
||
155 | $format = '%.' . $decimals . 'F'; |
||
156 | } else if ( 0 < $decimals ) { |
||
157 | $format = '%F'; |
||
158 | } |
||
159 | |||
160 | return $format; |
||
161 | |||
162 | } |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function is_empty( $value = null ) { |
||
168 | |||
169 | $is_empty = false; |
||
170 | |||
171 | $value += 0; |
||
172 | |||
173 | if ( empty( $value ) ) { |
||
174 | $is_empty = true; |
||
175 | } |
||
176 | |||
177 | return $is_empty; |
||
178 | |||
179 | } |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a ![]() |
|||
185 | |||
186 | $value = $this->format( $value, $name, $options, $pod, $id ); |
||
187 | |||
188 | return $value; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
195 | |||
196 | $options = (array) $options; |
||
197 | $form_field_type = PodsForm::$field_type; |
||
198 | |||
199 | if ( is_array( $value ) ) { |
||
200 | $value = implode( '', $value ); |
||
201 | } |
||
202 | |||
203 | if ( 'slider' === pods_v( static::$type . '_format_type', $options, 'number' ) ) { |
||
204 | $field_type = 'slider'; |
||
205 | } else { |
||
206 | $field_type = static::$type; |
||
207 | } |
||
208 | |||
209 | if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
||
210 | if ( pods_v( 'read_only', $options, false ) ) { |
||
211 | $options['readonly'] = true; |
||
212 | |||
213 | $field_type = 'text'; |
||
214 | |||
215 | $value = $this->format( $value, $name, $options, $pod, $id ); |
||
216 | } else { |
||
217 | return; |
||
218 | } |
||
219 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
||
220 | $options['readonly'] = true; |
||
221 | |||
222 | $field_type = 'text'; |
||
223 | |||
224 | $value = $this->format( $value, $name, $options, $pod, $id ); |
||
225 | } |
||
226 | |||
227 | pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
||
228 | |||
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
235 | |||
236 | $format_args = $this->get_number_format_args( $options ); |
||
237 | $thousands = $format_args['thousands']; |
||
238 | $dot = $format_args['dot']; |
||
239 | |||
240 | return '\-*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+'; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
247 | |||
248 | $format_args = $this->get_number_format_args( $options ); |
||
249 | $thousands = $format_args['thousands']; |
||
250 | $dot = $format_args['dot']; |
||
251 | |||
252 | $check = str_replace( |
||
253 | array( $thousands, $dot, html_entity_decode( $thousands ) ), array( |
||
254 | '', |
||
255 | '.', |
||
256 | '', |
||
257 | ), $value |
||
258 | ); |
||
259 | $check = trim( $check ); |
||
260 | |||
261 | $check = preg_replace( '/[0-9\.\-\s]/', '', $check ); |
||
262 | |||
263 | $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); |
||
264 | |||
265 | if ( 0 < strlen( $check ) ) { |
||
266 | return sprintf( __( '%s is not numeric', 'pods' ), $label ); |
||
0 ignored issues
–
show
The return type of
return sprintf(__('%s is...ric', 'pods'), $label); (string ) is incompatible with the return type of the parent method PodsField::validate of type boolean .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function ![]() |
|||
267 | } |
||
268 | |||
269 | return true; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
276 | |||
277 | $format_args = $this->get_number_format_args( $options ); |
||
278 | $thousands = $format_args['thousands']; |
||
279 | $dot = $format_args['dot']; |
||
280 | $decimals = $format_args['decimals']; |
||
281 | |||
282 | $value = str_replace( array( $thousands, $dot ), array( '', '.' ), $value ); |
||
283 | $value = trim( $value ); |
||
284 | |||
285 | $value = preg_replace( '/[^0-9\.\-]/', '', $value ); |
||
286 | |||
287 | $value = number_format( (float) $value, $decimals, '.', '' ); |
||
288 | |||
289 | return $value; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a ![]() |
|||
296 | |||
297 | if ( null === $value ) { |
||
298 | // Don't enforce a default value here. |
||
299 | return null; |
||
300 | } |
||
301 | |||
302 | $format_args = $this->get_number_format_args( $options ); |
||
303 | $thousands = $format_args['thousands']; |
||
304 | $dot = $format_args['dot']; |
||
305 | $decimals = $format_args['decimals']; |
||
306 | |||
307 | if ( 'i18n' === pods_v( static::$type . '_format', $options ) ) { |
||
308 | $value = number_format_i18n( (float) $value, $decimals ); |
||
309 | } else { |
||
310 | $value = number_format( (float) $value, $decimals, $dot, $thousands ); |
||
311 | } |
||
312 | |||
313 | // Optionally remove trailing decimal zero's. |
||
314 | if ( pods_v( static::$type . '_format_soft', $options, 0 ) ) { |
||
315 | $parts = explode( $dot, $value ); |
||
316 | if ( isset( $parts[1] ) ) { |
||
317 | $parts[1] = rtrim( $parts[1], '0' ); |
||
318 | $parts = array_filter( $parts ); |
||
319 | } |
||
320 | $value = implode( $dot, $parts ); |
||
321 | } |
||
322 | |||
323 | return $value; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Get the formatting arguments for numbers. |
||
328 | * |
||
329 | * @since 2.7 |
||
330 | * |
||
331 | * @param array $options Field options. |
||
332 | * |
||
333 | * @return array { |
||
334 | * @type string $thousands |
||
335 | * @type string $dot |
||
336 | * @type int $decimals |
||
337 | * } |
||
338 | */ |
||
339 | public function get_number_format_args( $options ) { |
||
340 | |||
341 | 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 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
![]() |
|||
342 | |||
343 | if ( '9.999,99' === pods_v( static::$type . '_format', $options ) ) { |
||
344 | $thousands = '.'; |
||
345 | $dot = ','; |
||
346 | } elseif ( '9,999.99' === pods_v( static::$type . '_format', $options ) ) { |
||
347 | $thousands = ','; |
||
348 | $dot = '.'; |
||
349 | } elseif ( '9\'999.99' === pods_v( static::$type . '_format', $options ) ) { |
||
350 | $thousands = '\''; |
||
351 | $dot = '.'; |
||
352 | } elseif ( '9 999,99' === pods_v( static::$type . '_format', $options ) ) { |
||
353 | $thousands = ' '; |
||
354 | $dot = ','; |
||
355 | } elseif ( '9999.99' === pods_v( static::$type . '_format', $options ) ) { |
||
356 | $thousands = ''; |
||
357 | $dot = '.'; |
||
358 | } elseif ( '9999,99' === pods_v( static::$type . '_format', $options ) ) { |
||
359 | $thousands = ''; |
||
360 | $dot = ','; |
||
361 | } else { |
||
362 | $thousands = $wp_locale->number_format['thousands_sep']; |
||
363 | $dot = $wp_locale->number_format['decimal_point']; |
||
364 | }//end if |
||
365 | |||
366 | $decimals = $this->get_max_decimals( $options ); |
||
367 | |||
368 | return array( |
||
369 | 'thousands' => $thousands, |
||
370 | 'dot' => $dot, |
||
371 | 'decimals' => $decimals, |
||
372 | ); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get the max allowed decimals. |
||
377 | * |
||
378 | * @since 2.7 |
||
379 | * |
||
380 | * @param array $options Field options. |
||
381 | * |
||
382 | * @return int |
||
383 | */ |
||
384 | public function get_max_decimals( $options ) { |
||
385 | |||
386 | $length = (int) pods_v( static::$type . '_max_length', $options, 12, true ); |
||
387 | |||
388 | if ( $length < 1 || 64 < $length ) { |
||
389 | $length = 64; |
||
390 | } |
||
391 | |||
392 | $decimals = (int) pods_v( static::$type . '_decimals', $options, 0 ); |
||
393 | |||
394 | if ( $decimals < 1 ) { |
||
395 | $decimals = 0; |
||
396 | } elseif ( 30 < $decimals ) { |
||
397 | $decimals = 30; |
||
398 | } |
||
399 | |||
400 | if ( $length < $decimals ) { |
||
401 | $decimals = $length; |
||
402 | } |
||
403 | |||
404 | return $decimals; |
||
405 | } |
||
406 | } |
||
407 |