PluginProbe
Advanced Custom Fields (ACF®) / 5.8.8
Advanced Custom Fields (ACF®) v5.8.8
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / validation.php
validation.php
418 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('acf_validation') ) :
6
7 class acf_validation {
8
9
10 /*
11 * __construct
12 *
13 * This function will setup the class functionality
14 *
15 * @type function
16 * @date 5/03/2014
17 * @since 5.0.0
18 *
19 * @param n/a
20 * @return n/a
21 */
22
23 function __construct() {
24
25 // vars
26 $this->errors = array();
27
28
29 // ajax
30 add_action('wp_ajax_acf/validate_save_post', array($this, 'ajax_validate_save_post'));
31 add_action('wp_ajax_nopriv_acf/validate_save_post', array($this, 'ajax_validate_save_post'));
32 add_action('acf/validate_save_post', array($this, 'acf_validate_save_post'), 5);
33
34 }
35
36
37 /*
38 * add_error
39 *
40 * This function will add an error message for a field
41 *
42 * @type function
43 * @date 25/11/2013
44 * @since 5.0.0
45 *
46 * @param $input (string) name attribute of DOM elmenet
47 * @param $message (string) error message
48 * @return $post_id (int)
49 */
50
51 function add_error( $input, $message ) {
52
53 // add to array
54 $this->errors[] = array(
55 'input' => $input,
56 'message' => $message
57 );
58
59 }
60
61
62 /*
63 * get_error
64 *
65 * This function will return an error for a given input
66 *
67 * @type function
68 * @date 5/03/2016
69 * @since 5.3.2
70 *
71 * @param $input (string) name attribute of DOM elmenet
72 * @return (mixed)
73 */
74
75 function get_error( $input ) {
76
77 // bail early if no errors
78 if( empty($this->errors) ) return false;
79
80
81 // loop
82 foreach( $this->errors as $error ) {
83
84 if( $error['input'] === $input ) return $error;
85
86 }
87
88
89 // return
90 return false;
91
92 }
93
94
95 /*
96 * get_errors
97 *
98 * This function will return validation errors
99 *
100 * @type function
101 * @date 25/11/2013
102 * @since 5.0.0
103 *
104 * @param n/a
105 * @return (array|boolean)
106 */
107
108 function get_errors() {
109
110 // bail early if no errors
111 if( empty($this->errors) ) return false;
112
113
114 // return
115 return $this->errors;
116
117 }
118
119
120 /*
121 * reset_errors
122 *
123 * This function will remove all errors
124 *
125 * @type function
126 * @date 4/03/2016
127 * @since 5.3.2
128 *
129 * @param n/a
130 * @return n/a
131 */
132
133 function reset_errors() {
134
135 $this->errors = array();
136
137 }
138
139
140 /*
141 * ajax_validate_save_post
142 *
143 * This function will validate the $_POST data via AJAX
144 *
145 * @type function
146 * @date 27/10/2014
147 * @since 5.0.9
148 *
149 * @param n/a
150 * @return n/a
151 */
152
153 function ajax_validate_save_post() {
154
155 // validate
156 if( !acf_verify_ajax() ) die();
157
158
159 // vars
160 $json = array(
161 'valid' => 1,
162 'errors' => 0
163 );
164
165
166 // success
167 if( acf_validate_save_post() ) {
168
169 wp_send_json_success($json);
170
171 }
172
173
174 // update vars
175 $json['valid'] = 0;
176 $json['errors'] = acf_get_validation_errors();
177
178
179 // return
180 wp_send_json_success($json);
181
182 }
183
184
185 /*
186 * acf_validate_save_post
187 *
188 * This function will loop over $_POST data and validate
189 *
190 * @type function
191 * @date 7/09/2016
192 * @since 5.4.0
193 *
194 * @param n/a
195 * @return n/a
196 */
197
198 function acf_validate_save_post() {
199
200 // bail early if no $_POST
201 if( empty($_POST['acf']) ) return;
202
203
204 // validate
205 acf_validate_values( $_POST['acf'], 'acf' );
206
207 }
208
209 }
210
211 // initialize
212 acf()->validation = new acf_validation();
213
214 endif; // class_exists check
215
216
217 /*
218 * Public functions
219 *
220 * alias of acf()->validation->function()
221 *
222 * @type function
223 * @date 6/10/13
224 * @since 5.0.0
225 *
226 * @param n/a
227 * @return n/a
228 */
229
230 function acf_add_validation_error( $input, $message = '' ) {
231
232 return acf()->validation->add_error( $input, $message );
233
234 }
235
236 function acf_get_validation_errors() {
237
238 return acf()->validation->get_errors();
239
240 }
241
242 function acf_get_validation_error() {
243
244 return acf()->validation->get_error( $input );
245
246 }
247
248 function acf_reset_validation_errors() {
249
250 return acf()->validation->reset_errors();
251
252 }
253
254
255 /*
256 * acf_validate_save_post
257 *
258 * This function will validate $_POST data and add errors
259 *
260 * @type function
261 * @date 25/11/2013
262 * @since 5.0.0
263 *
264 * @param $show_errors (boolean) if true, errors will be shown via a wp_die screen
265 * @return (boolean)
266 */
267
268 function acf_validate_save_post( $show_errors = false ) {
269
270 // action
271 do_action('acf/validate_save_post');
272
273
274 // vars
275 $errors = acf_get_validation_errors();
276
277
278 // bail ealry if no errors
279 if( !$errors ) return true;
280
281
282 // show errors
283 if( $show_errors ) {
284
285 $message = '<h2>' . __('Validation failed', 'acf') . '</h2>';
286 $message .= '<ul>';
287 foreach( $errors as $error ) {
288
289 $message .= '<li>' . $error['message'] . '</li>';
290
291 }
292 $message .= '</ul>';
293
294
295 // die
296 wp_die( $message, __('Validation failed', 'acf') );
297
298 }
299
300
301 // return
302 return false;
303
304 }
305
306
307 /*
308 * acf_validate_values
309 *
310 * This function will validate an array of field values
311 *
312 * @type function
313 * @date 6/10/13
314 * @since 5.0.0
315 *
316 * @param values (array)
317 * @param $input_prefix (string)
318 * @return n/a
319 */
320
321 function acf_validate_values( $values, $input_prefix = '' ) {
322
323 // bail early if empty
324 if( empty($values) ) return;
325
326
327 // loop
328 foreach( $values as $key => $value ) {
329
330 // vars
331 $field = acf_get_field( $key );
332 $input = $input_prefix . '[' . $key . ']';
333
334
335 // bail early if not found
336 if( !$field ) continue;
337
338
339 // validate
340 acf_validate_value( $value, $field, $input );
341
342 }
343
344 }
345
346
347 /*
348 * acf_validate_value
349 *
350 * This function will validate a field's value
351 *
352 * @type function
353 * @date 6/10/13
354 * @since 5.0.0
355 *
356 * @param n/a
357 * @return n/a
358 */
359
360 function acf_validate_value( $value, $field, $input ) {
361
362 // vars
363 $valid = true;
364 $message = sprintf( __( '%s value is required', 'acf' ), $field['label'] );
365
366
367 // valid
368 if( $field['required'] ) {
369
370 // valid is set to false if the value is empty, but allow 0 as a valid value
371 if( empty($value) && !is_numeric($value) ) {
372
373 $valid = false;
374
375 }
376
377 }
378
379
380 /**
381 * Filters whether the value is valid.
382 *
383 * @date 28/09/13
384 * @since 5.0.0
385 *
386 * @param bool $valid The valid status. Return a string to display a custom error message.
387 * @param mixed $value The value.
388 * @param array $field The field array.
389 * @param string $input The input element's name attribute.
390 */
391 $valid = apply_filters( "acf/validate_value/type={$field['type']}", $valid, $value, $field, $input );
392 $valid = apply_filters( "acf/validate_value/name={$field['_name']}", $valid, $value, $field, $input );
393 $valid = apply_filters( "acf/validate_value/key={$field['key']}", $valid, $value, $field, $input );
394 $valid = apply_filters( "acf/validate_value", $valid, $value, $field, $input );
395
396
397 // allow $valid to be a custom error message
398 if( !empty($valid) && is_string($valid) ) {
399
400 $message = $valid;
401 $valid = false;
402
403 }
404
405
406 if( !$valid ) {
407
408 acf_add_validation_error( $input, $message );
409 return false;
410
411 }
412
413
414 // return
415 return true;
416
417 }
418