PluginProbe
Advanced Custom Fields (ACF®) / 6.1.0
Advanced Custom Fields (ACF®) v6.1.0
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
412 lines 7.3 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 #[AllowDynamicProperties]
10 class acf_validation {
11
12
13 /*
14 * __construct
15 *
16 * This function will setup the class functionality
17 *
18 * @type function
19 * @date 5/03/2014
20 * @since 5.0.0
21 *
22 * @param n/a
23 * @return n/a
24 */
25
26 function __construct() {
27
28 // vars
29 $this->errors = array();
30
31 // ajax
32 add_action( 'wp_ajax_acf/validate_save_post', array( $this, 'ajax_validate_save_post' ) );
33 add_action( 'wp_ajax_nopriv_acf/validate_save_post', array( $this, 'ajax_validate_save_post' ) );
34 add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 5 );
35
36 }
37
38
39 /*
40 * add_error
41 *
42 * This function will add an error message for a field
43 *
44 * @type function
45 * @date 25/11/2013
46 * @since 5.0.0
47 *
48 * @param $input (string) name attribute of DOM elmenet
49 * @param $message (string) error message
50 * @return $post_id (int)
51 */
52
53 function add_error( $input, $message ) {
54
55 // add to array
56 $this->errors[] = array(
57 'input' => $input,
58 'message' => $message,
59 );
60
61 }
62
63
64 /*
65 * get_error
66 *
67 * This function will return an error for a given input
68 *
69 * @type function
70 * @date 5/03/2016
71 * @since 5.3.2
72 *
73 * @param $input (string) name attribute of DOM elmenet
74 * @return (mixed)
75 */
76
77 function get_error( $input ) {
78
79 // bail early if no errors
80 if ( empty( $this->errors ) ) {
81 return false;
82 }
83
84 // loop
85 foreach ( $this->errors as $error ) {
86
87 if ( $error['input'] === $input ) {
88 return $error;
89 }
90 }
91
92 // return
93 return false;
94
95 }
96
97
98 /*
99 * get_errors
100 *
101 * This function will return validation errors
102 *
103 * @type function
104 * @date 25/11/2013
105 * @since 5.0.0
106 *
107 * @param n/a
108 * @return (array|boolean)
109 */
110
111 function get_errors() {
112
113 // bail early if no errors
114 if ( empty( $this->errors ) ) {
115 return false;
116 }
117
118 // return
119 return $this->errors;
120
121 }
122
123
124 /*
125 * reset_errors
126 *
127 * This function will remove all errors
128 *
129 * @type function
130 * @date 4/03/2016
131 * @since 5.3.2
132 *
133 * @param n/a
134 * @return n/a
135 */
136
137 function reset_errors() {
138
139 $this->errors = array();
140
141 }
142
143
144 /*
145 * ajax_validate_save_post
146 *
147 * This function will validate the $_POST data via AJAX
148 *
149 * @type function
150 * @date 27/10/2014
151 * @since 5.0.9
152 *
153 * @param n/a
154 * @return n/a
155 */
156
157 function ajax_validate_save_post() {
158
159 // validate
160 if ( ! acf_verify_ajax() ) {
161 die();
162 }
163
164 // vars
165 $json = array(
166 'valid' => 1,
167 'errors' => 0,
168 );
169
170 // success
171 if ( acf_validate_save_post() ) {
172
173 wp_send_json_success( $json );
174
175 }
176
177 // update vars
178 $json['valid'] = 0;
179 $json['errors'] = acf_get_validation_errors();
180
181 // return
182 wp_send_json_success( $json );
183
184 }
185
186 /**
187 * Loops over $_POST data and validates ACF values.
188 *
189 * @since 5.4.0
190 *
191 * @return void
192 */
193 public function acf_validate_save_post() {
194 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
195 $post_type = acf_request_arg( 'post_type', false );
196 $screen = acf_request_arg( '_acf_screen', false );
197
198 if ( in_array( $screen, array( 'post_type', 'taxonomy' ), true ) && in_array( $post_type, array( 'acf-post-type', 'acf-taxonomy' ), true ) ) {
199 acf_validate_internal_post_type_values( $post_type );
200 } else {
201 // Bail early if no matching $_POST.
202 if ( empty( $_POST['acf'] ) ) {
203 return;
204 }
205
206 acf_validate_values( $_POST['acf'], 'acf' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
207 }
208 // phpcs:enable WordPress.Security.NonceVerification.Missing
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 early 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