PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / tribe-common / src / Tribe / Validate.php
pods / tribe-common / src / Tribe Last commit date
Admin 2 weeks ago Ajax 2 weeks ago Asset 2 weeks ago Context 2 weeks ago Customizer 2 weeks ago Debug_Bar 2 weeks ago Dialog 2 weeks ago Documentation 2 weeks ago Duplicate 2 weeks ago Editor 2 weeks ago Image 2 weeks ago JSON_LD 2 weeks ago Languages 2 weeks ago Log 2 weeks ago Meta 2 weeks ago Models 2 weeks ago PUE 2 weeks ago Process 2 weeks ago Promoter 2 weeks ago REST 2 weeks ago Repository 2 weeks ago Service_Providers 2 weeks ago Shortcode 2 weeks ago Support 2 weeks ago Tabbed_View 2 weeks ago Tooltip 2 weeks ago Traits 2 weeks ago Utils 2 weeks ago Validator 2 weeks ago Widget 2 weeks ago Abstract_Deactivation.php 2 weeks ago Abstract_Plugin_Register.php 2 weeks ago App_Shop.php 2 weeks ago Assets.php 2 weeks ago Assets_Pipeline.php 2 weeks ago Autoloader.php 2 weeks ago Cache.php 2 weeks ago Cache_Listener.php 2 weeks ago Changelog_Reader.php 2 weeks ago Container.php 2 weeks ago Context.php 2 weeks ago Cost_Utils.php 2 weeks ago Credits.php 2 weeks ago Customizer.php 2 weeks ago DB_Lock.php 2 weeks ago Data.php 2 weeks ago Date_Utils.php 2 weeks ago Db.php 2 weeks ago Debug.php 2 weeks ago Dependency.php 2 weeks ago Deprecation.php 2 weeks ago Editor.php 2 weeks ago Error.php 2 weeks ago Exception.php 2 weeks ago Extension.php 2 weeks ago Extension_Loader.php 2 weeks ago Feature_Detection.php 2 weeks ago Field.php 2 weeks ago Field_Conditional.php 2 weeks ago Freemius.php 2 weeks ago Log.php 2 weeks ago Main.php 2 weeks ago Notices.php 2 weeks ago Plugin_Meta_Links.php 2 weeks ago Plugins.php 2 weeks ago Plugins_API.php 2 weeks ago Post_History.php 2 weeks ago Post_Transient.php 2 weeks ago Promise.php 2 weeks ago Repository.php 2 weeks ago Rewrite.php 2 weeks ago Settings.php 2 weeks ago Settings_Manager.php 2 weeks ago Settings_Tab.php 2 weeks ago Simple_Table.php 2 weeks ago Support.php 2 weeks ago Tabbed_View.php 2 weeks ago Template.php 2 weeks ago Template_Factory.php 2 weeks ago Template_Part_Cache.php 2 weeks ago Templates.php 2 weeks ago Terms.php 2 weeks ago Timezones.php 2 weeks ago Tracker.php 2 weeks ago Updater.php 2 weeks ago Validate.php 2 weeks ago View_Helpers.php 2 weeks ago
Validate.php
504 lines
1 <?php
2
3 // Don't load directly
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 if ( ! class_exists( 'Tribe__Validate' ) ) {
9 /**
10 * helper class that validates fields for use in Settings, MetaBoxes, Users, anywhere.
11 * Instantiate whenever you want to validate a field
12 *
13 */
14 class Tribe__Validate {
15
16 /**
17 * the field object to validate
18 * @var array
19 */
20 public $field;
21
22 /**
23 * the field's value
24 * @var mixed
25 */
26 public $value;
27
28 /**
29 * additional arguments for validation
30 * used by some methods only
31 * @var array
32 */
33 public $additional_args;
34
35 /**
36 * the field's label, used in error messages
37 * @var string
38 */
39 public $label;
40
41 /**
42 * the type of validation to perform
43 * @var string
44 */
45 public $type;
46
47 /**
48 * the result object of the validation
49 * @var stdClass
50 */
51 public $result;
52
53 /**
54 * Class constructor
55 *
56 * @param string $field_id The field ID to validate
57 * @param array $field The field object to validate
58 * @param mixed $value The value to validate
59 */
60 public function __construct( $field_id, $field, $value, $additional_args = [] ) {
61
62 // prepare object properties
63 $this->result = new stdClass;
64 $this->field = $field;
65 $this->field['id'] = $field_id;
66 $this->value = $value;
67 $this->additional_args = $additional_args;
68
69 // if the field is invalid or incomplete, fail validation
70 if ( ! is_array( $this->field ) || ! ( isset( $this->field['validation_type'] ) || isset( $this->field['validation_callback'] ) ) ) {
71 $this->result->valid = false;
72 $this->result->error = esc_html__( 'Invalid or incomplete field passed', 'tribe-common' );
73 $this->result->error .= ( isset( $this->field['id'] ) ) ? ' (' . esc_html__( 'Field ID:', 'tribe-common' ) . ' ' . $this->field['id'] . ' )' : '';
74 }
75
76 // call validation callback if a validation callback function is set
77 if ( isset( $this->field['validation_callback'] ) ) {
78 if ( is_callable( $this->field['validation_callback'] ) || function_exists( $this->field['validation_callback'] ) ) {
79 if ( ( ! isset( $_POST[ $field_id ] ) || ! $_POST[ $field_id ] || $_POST[ $field_id ] == '' ) && isset( $this->field['can_be_empty'] ) && $this->field['can_be_empty'] ) {
80 $this->result->valid = true;
81 } else {
82 $this->result->valid = call_user_func( $this->field['validation_callback'], $value );
83 if ( ! $this->result->valid ) {
84 $this->result->error = esc_html__( 'Invalid or incomplete field passed', 'tribe-common' );
85 $this->result->error .= ( isset( $this->field['id'] ) ) ? ' (' . esc_html__( 'Field ID:', 'tribe-common' ) . ' ' . $this->field['id'] . ' )' : '';
86 }
87 }
88 }
89 }
90
91 if ( isset( $this->field['validation_type'] ) ) {
92 if ( method_exists( $this, $this->field['validation_type'] ) ) {
93 // make sure there's a field validation type set for this validation and that such method exists
94 $this->type = $this->field['validation_type'];
95 $this->label = isset( $this->field['label'] ) ? $this->field['label'] : $this->field['id'];
96 if ( ( ! isset( $_POST[ $field_id ] ) || ! $_POST[ $field_id ] || $_POST[ $field_id ] == '' ) && isset( $this->field['can_be_empty'] ) && $this->field['can_be_empty'] ) {
97 $this->result->valid = true;
98 } else {
99 call_user_func( [ $this, $this->type ] ); // run the validation
100 }
101 } else {
102 // invalid validation type set, validation fails
103 $this->result->valid = false;
104 $this->result->error = esc_html__( 'Non-existant field validation function passed', 'tribe-common' );
105 $this->result->error .= ( isset( $this->field['id'] ) ) ? ' (' . esc_html__( 'Field ID:', 'tribe-common' ) . ' ' . $this->field['id'] . ' ' . _x( 'with function name:', 'non-existant function name passed for field validation', 'tribe-common' ) . ' ' . $this->field['validation_type'] . ' )' : '';
106 }
107 }
108 }
109
110 /**
111 * validates a field as a string containing only letters and numbers
112 */
113 public function alpha_numeric() {
114 if ( preg_match( '/^[a-zA-Z0-9]+$/', $this->value ) ) {
115 $this->result->valid = true;
116 } else {
117 $this->result->valid = false;
118 $this->result->error = sprintf( esc_html__( '%s must contain numbers and letters only', 'tribe-common' ), $this->label );
119 }
120 }
121
122 /**
123 * validates a field as a string containing only letters,
124 * numbers and carriage returns
125 */
126 public function alpha_numeric_multi_line() {
127 if ( preg_match( '/^[a-zA-Z0-9\s]+$/', $this->value ) ) {
128 $this->result->valid = true;
129 $this->value = tribe_multi_line_remove_empty_lines( $this->value );
130 } else {
131 $this->result->valid = false;
132 $this->result->error = sprintf( esc_html__( '%s must contain numbers and letters only', 'tribe-common' ), $this->label );
133 }
134 }
135
136 /**
137 * Validates a field as a string containing only letters,
138 * numbers, dots and carriage returns
139 */
140 public function alpha_numeric_multi_line_with_dots_and_dashes() {
141 if ( preg_match( '/^[a-zA-Z0-9\s.-]+$/', $this->value ) ) {
142 $this->result->valid = true;
143 $this->value = tribe_multi_line_remove_empty_lines( $this->value );
144 } else {
145 $this->result->valid = false;
146 $this->result->error = sprintf( esc_html__( '%s must contain numbers, letters and dots only', 'tribe-common' ), $this->label );
147 }
148 }
149
150 /**
151 * Validates a field as a string containing only letters,
152 * numbers, dashes and underscores
153 */
154 public function alpha_numeric_with_dashes_and_underscores() {
155 $this->value = trim( $this->value );
156 if ( preg_match( '/^[a-zA-Z0-9_-]+$/', $this->value ) ) {
157 $this->result->valid = true;
158 } else {
159 $this->result->valid = false;
160 $this->result->error = sprintf( esc_html__( '%s must contain numbers, letters, dashes and undescores only', 'tribe-common' ), $this->label );
161 }
162 }
163
164 /**
165 * Validates a field as just "not empty".
166 *
167 * @since 4.7.6
168 */
169 public function not_empty() {
170 $this->value = trim( $this->value );
171
172 if ( empty( $this->value ) ) {
173 $this->result->valid = false;
174 $this->result->error = sprintf( esc_html__( '%s must not be empty', 'tribe-common' ), $this->label );
175 } else {
176 $this->result->valid = true;
177 }
178 }
179
180 /**
181 * validates a field as being positive decimal
182 */
183 public function positive_decimal() {
184 if ( preg_match( '/^[0-9]+(\.[0-9]+)?$/', $this->value ) && $this->value > 0 ) {
185 $this->result->valid = true;
186 } else {
187 $this->result->valid = false;
188 $this->result->error = sprintf( esc_html__( '%s must be a positive number.', 'tribe-common' ), $this->label );
189 }
190 }
191
192 /**
193 * validates a field as being positive decimal or percent
194 */
195 public function positive_decimal_or_percent() {
196 if ( preg_match( '/^[0-9]+(\.[0-9]+)?%?$/', $this->value ) && $this->value > 0 ) {
197 $this->result->valid = true;
198 } else {
199 $this->result->valid = false;
200 $this->result->error = sprintf( esc_html__( '%s must be a positive number or percent.', 'tribe-common' ), $this->label );
201 }
202 }
203
204 /**
205 * validates a field as being positive integers
206 */
207 public function positive_int() {
208 if ( preg_match( '/^[0-9]+$/', $this->value ) && $this->value > 0 ) {
209 $this->result->valid = true;
210 } else {
211 $this->result->valid = false;
212 $this->result->error = sprintf( esc_html__( '%s must be a positive number.', 'tribe-common' ), $this->label );
213 }
214 }
215
216 /**
217 * validates a field as being an integer
218 *
219 * The expected value is a whole number (positive or negative). This method is named "int" to
220 * match the mathematical definition of the word AND to closely match the pre-exiting method
221 * with a similar name: positive_int(). This method WILL validate whole numbers that go beyond
222 * values that PHP's int type supports, however, if someone enters something like that, that's
223 * on them. Smart people do smart things.
224 */
225 public function int() {
226 if ( preg_match( '/^-?[0-9]+$/', $this->value ) ) {
227 $this->result->valid = true;
228 } else {
229 $this->result->valid = false;
230 $this->result->error = sprintf( esc_html__( '%s must be a whole number.', 'tribe-common' ), $this->label );
231 }
232 }
233
234 /**
235 * validates & sanitizes fields as URL slugs
236 */
237 public function slug() {
238 $maybe_valid_value = esc_url_raw( $this->value );
239
240 // esc_url_raw does the work of validating chars, but returns the checked string with a
241 // prepended URL protocol; so let's use strpos to match the values.
242 if (
243 ! empty( $maybe_valid_value )
244 && false !== strpos( $maybe_valid_value, $this->value )
245 ) {
246 $this->result->valid = true;
247 $this->value = sanitize_title( $this->value );
248 } else {
249 $this->result->valid = false;
250 $this->result->error = sprintf( esc_html__( '%s must be a valid slug (numbers, letters, dashes, and underscores).', 'tribe-common' ), $this->label );
251 }
252 }
253
254 /**
255 * validates & sanitizes fields as URLs
256 */
257 public function url() {
258
259 if ( esc_url_raw( $this->value ) == $this->value ) {
260 $this->result->valid = true;
261 } else {
262 $this->result->valid = false;
263 $this->result->error = sprintf( esc_html__( '%s must be a valid URL.', 'tribe-common' ), $this->label );
264 }
265 }
266
267 /**
268 * validates fields that have options (radios, dropdowns, etc.)
269 * by making sure the value is part of the options array
270 */
271 public function options() {
272 if ( array_key_exists( $this->value, $this->field['options'] ) ) {
273 $this->value = ( $this->value === 0 ) ? false : $this->value;
274 $this->result->valid = true;
275 } else {
276 $this->result->valid = false;
277 $this->result->error = sprintf( esc_html__( "%s must have a value that's part of its options.", 'tribe-common' ), $this->label );
278 }
279 }
280
281 /**
282 * Validates fields that have multiple options (checkbox list, etc.)
283 * by making sure the value is part of the options array.
284 */
285 public function options_multi() {
286 // if we are here it cannot be empty
287 if ( empty( $this->value ) ) {
288 $this->result->valid = false;
289 $this->result->error = sprintf( esc_html__( "%s must have a value that's part of its options.", 'tribe-common' ), $this->label );
290
291 return;
292 }
293
294 $this->value = is_array( $this->value ) ? $this->value : [ $this->value ];
295
296 foreach ( $this->value as $val ) {
297 if ( array_key_exists( $val, $this->field['options'] ) ) {
298 $this->value = ( $this->value === 0 ) ? false : $this->value;
299 $this->result->valid = true;
300 } else {
301 $this->result->valid = false;
302 $this->result->error = sprintf( esc_html__( "%s must have a value that's part of its options.", 'tribe-common' ), $this->label );
303 }
304 }
305 }
306
307 /**
308 * validates fields that have options (radios, dropdowns, etc.)
309 * by making sure the value is part of the options array
310 * then combines the value into an array containing the value
311 * and name from the option
312 */
313 public function options_with_label() {
314 if ( array_key_exists( $this->value, $this->field['options'] ) ) {
315 $this->value = ( $this->value === 0 ) ? false : [
316 $this->value,
317 $this->field['options'][ $this->value ],
318 ];
319 $this->result->valid = true;
320 } else {
321 $this->result->valid = false;
322 $this->result->error = sprintf( esc_html__( "%s must have a value that's part of its options.", 'tribe-common' ), $this->label );
323 }
324 }
325
326 /**
327 * validates a field as not being able to be the same
328 * as the specified value as specified in
329 * $this->additional_args['compare_name']
330 */
331 public function cannot_be_the_same_as() {
332 if ( ! isset( $this->additional_args['compare'] ) ) {
333 $this->result->valid = false;
334 $this->result->error = sprintf( esc_html__( 'Comparison validation failed because no comparison value was provided, for field %s', 'tribe-common' ), $this->field['id'] );
335 } else {
336 if ( $this->value != $this->additional_args['compare'] ) {
337 $this->result = true;
338 } else {
339 $this->result->valid = false;
340 if ( isset( $this->additional_args['compare_name'] ) ) {
341 $this->result->error = sprintf( esc_html__( '%s cannot be the same as %s.', 'tribe-common' ), $this->label, $this->additional_args['compare_name'] );
342 } else {
343 $this->result->error = sprintf( esc_html__( '%s cannot be a duplicate', 'tribe-common' ), $this->label );
344 }
345 }
346 }
347 }
348
349 /**
350 * validates a field as being a number or a percentage
351 */
352 public function number_or_percent() {
353 if ( preg_match( '/^[0-9]+%{0,1}$/', $this->value ) ) {
354 $this->result->valid = true;
355 } else {
356 $this->result->valid = false;
357 $this->result->error = sprintf( esc_html__( '%s must be a number or percentage.', 'tribe-common' ), $this->label );
358 }
359 }
360
361 /**
362 * sanitizes an html field
363 */
364 public function html() {
365 $this->value = balanceTags( $this->value );
366 $this->result->valid = true;
367 }
368
369 /**
370 * sanitizes a license key
371 */
372 public function license_key() {
373 $this->value = trim( $this->value );
374 $this->result->valid = true;
375 }
376
377 /**
378 * sanitizes a textarea field
379 */
380 public function textarea() {
381 $this->value = wp_kses( $this->value, [] );
382 $this->result->valid = true;
383 }
384
385 /**
386 * sanitizes a field as being a boolean
387 */
388 public function boolean() {
389 $this->value = (bool) $this->value;
390 $this->result->valid = true;
391 }
392
393 /**
394 * validates a Google Maps Zoom field
395 */
396 public function google_maps_zoom() {
397 if ( preg_match( '/^([0-9]|[0-1][0-9]|2[0-1])$/', $this->value ) ) {
398 $this->result->valid = true;
399 } else {
400 $this->result->valid = false;
401 $this->result->error = sprintf( esc_html__( '%s must be a number between 0 and 21.', 'tribe-common' ), $this->label );
402 }
403 }
404
405 /**
406 * validates a field as being part of an address
407 * allows for letters, numbers, dashes and spaces only
408 */
409 public function address() {
410 $this->value = stripslashes( $this->value );
411 if ( preg_match( "/^[0-9\S '-]+$/", $this->value ) ) {
412 $this->result->valid = true;
413 } else {
414 $this->result->valid = false;
415 $this->result->error = sprintf( esc_html__( '%s must consist of letters, numbers, dashes, apostrophes, and spaces only.', 'tribe-common' ), $this->label );
416 }
417 }
418
419 /**
420 * validates a field as being a city or province
421 * allows for letters, dashes and spaces only
422 */
423 public function city_or_province() {
424 $this->value = stripslashes( $this->value );
425 if ( preg_match( "/^[\D '\-]+$/", $this->value ) ) {
426 $this->result->valid = true;
427 } else {
428 $this->result->valid = false;
429 $this->result->error = sprintf( esc_html__( '%s must consist of letters, spaces, apostrophes, and dashes.', 'tribe-common' ), $this->label );
430 }
431 }
432
433 /**
434 * validates a field as being a zip code
435 */
436 public function zip() {
437 if ( preg_match( '/^[0-9]{5}$/', $this->value ) ) {
438 $this->result->valid = true;
439 } else {
440 $this->result->valid = false;
441 $this->result->error = sprintf( esc_html__( '%s must consist of 5 numbers.', 'tribe-common' ), $this->label );
442 }
443 }
444
445 /**
446 * validates a field as being a phone number
447 */
448 public function phone() {
449 if ( preg_match( '/^[0-9\(\)\+ -]+$/', $this->value ) ) {
450 $this->result->valid = true;
451 } else {
452 $this->result->valid = false;
453 $this->result->error = sprintf( esc_html__( '%s must be a phone number.', 'tribe-common' ), $this->label );
454 }
455 }
456
457 /**
458 * validates & sanitizes a field as being a country list
459 */
460 public function country_list() {
461 $country_rows = explode( "\n", $this->value );
462 if ( is_array( $country_rows ) ) {
463 foreach ( $country_rows as $crow ) {
464 $country = explode( ',', $crow );
465 if ( ! isset( $country[0] ) || ! isset( $country[1] ) ) {
466 $this->result->valid = false;
467 $this->result->error = sprintf( esc_html__( 'Country List must be formatted as one country per line in the following format: <br>US, United States <br> UK, United Kingdom.', 'tribe-common' ), $this->label );
468 $this->value = wp_kses( $this->value, [] );
469
470 return;
471 }
472 }
473 }
474 $this->result->valid = true;
475 }
476
477 /**
478 * automatically validate a field regardless of the value
479 * Don't use this unless you know what you are doing
480 */
481 public function none() {
482 $this->result->valid = true;
483 }
484
485 /**
486 * Validates and sanitizes an email address.
487 *
488 * @since 4.7.4
489 */
490 public function email( ) {
491 $candidate = trim( $this->value );
492
493 $this->result->valid = filter_var( $candidate, FILTER_VALIDATE_EMAIL );
494
495 if ( ! $this->result->valid ) {
496 $this->result->error = sprintf( esc_html__( '%s must be an email address.', 'tribe-common' ), $this->label );
497 } else {
498 $this->value = filter_var( trim( $candidate ), FILTER_SANITIZE_EMAIL );
499 }
500 }
501
502 } // end class
503 } // endif class_exists
504