PluginProbe
Advanced Custom Fields (ACF®) / 5.11
Advanced Custom Fields (ACF®) v5.11
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 in Advanced Custom Fields (ACF®) 5.11, at includes/validation.php

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