PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
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 / api / api-input.php
api-input.php
615 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * acf_esc_html
5 *
6 * This function will encode <script> tags for safe output
7 *
8 * @type function
9 * @date 25/6/17
10 * @since 5.6.0
11 *
12 * @param string (string)
13 * @return (string)
14 */
15
16 function acf_esc_html( $string = '' ) {
17
18 // cast
19 $string = (string) $string;
20
21
22 // replace
23 $string = str_replace('<script', htmlspecialchars('<script'), $string);
24 $string = str_replace('</script', htmlspecialchars('</script'), $string);
25
26
27 // return
28 return $string;
29
30 }
31
32
33 /**
34 * acf_clean_atts
35 *
36 * This function will remove empty attributes
37 *
38 * @date 3/10/17
39 * @since 5.6.3
40 *
41 * @param array $atts
42 * @return array
43 */
44
45 function acf_clean_atts( $atts = array() ) {
46
47 // loop
48 foreach( $atts as $k => $v ) {
49 if( $v === '' ) unset( $atts[ $k ] );
50 }
51
52
53 // return
54 return $atts;
55 }
56
57
58 /**
59 * acf_get_atts
60 *
61 * This function will return an array of HTML attributes
62 *
63 * @date 2/10/17
64 * @since 5.6.3
65 *
66 * @param n/a
67 * @return n/a
68 */
69
70 /*
71 function acf_get_atts( $array, $keys ) {
72
73 // vars
74 $atts = array();
75
76
77 // append attributes
78 foreach( $keys as $k ) {
79 if( isset($array[ $k ]) ) $atts[ $k ] = $array[ $k ];
80 }
81
82
83 // modify special attributes
84 foreach( array('readonly', 'disabled', 'required') as $k ) {
85 $atts[ $k ] = $atts[ $k ] ? $k : '';
86 }
87
88
89 // clean up blank attributes
90 foreach( $atts as $k => $v ) {
91 if( $v === '' ) unset( $atts[ $k ] );
92 }
93
94
95 // return
96 return $atts;
97
98 }
99 */
100
101
102 /*
103 * acf_esc_atts
104 *
105 * This function will escape an array of attributes and return as HTML
106 *
107 * @type function
108 * @date 27/6/17
109 * @since 5.6.0
110 *
111 * @param $atts (array)
112 * @return (string)
113 */
114
115 function acf_esc_atts( $atts = array() ) {
116
117 // vars
118 $html = '';
119
120
121 // loop
122 foreach( $atts as $k => $v ) {
123
124 // string
125 if( is_string($v) ) {
126
127 // don't trim value
128 if( $k !== 'value') $v = trim($v);
129
130 // boolean
131 } elseif( is_bool($v) ) {
132
133 $v = $v ? 1 : 0;
134
135 // object
136 } elseif( is_array($v) || is_object($v) ) {
137
138 $v = json_encode($v);
139
140 }
141
142
143 // append
144 $html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
145
146 }
147
148
149 // return
150 return trim( $html );
151
152 }
153
154
155 /*
156 * acf_esc_atts_e
157 *
158 * This function will echo acf_esc_atts
159 *
160 * @type function
161 * @date 27/6/17
162 * @since 5.6.0
163 *
164 * @param $atts (array)
165 * @return n/a
166 */
167
168 function acf_esc_atts_e( $atts = array() ) {
169
170 echo acf_esc_atts( $atts );
171
172 }
173
174
175 /*
176 * acf_get_text_input
177 *
178 * This function will return HTML for a text input
179 *
180 * @type function
181 * @date 3/02/2014
182 * @since 5.0.0
183 *
184 * @param $atts
185 * @return (string)
186 */
187
188 function acf_get_text_input( $atts = array() ) {
189
190 $atts['type'] = isset($atts['type']) ? $atts['type'] : 'text';
191 return '<input ' . acf_esc_atts( $atts ) . ' />';
192
193 }
194
195
196 /*
197 * acf_text_input
198 *
199 * This function will output HTML for a text input
200 *
201 * @type function
202 * @date 3/02/2014
203 * @since 5.0.0
204 *
205 * @param $atts
206 * @return n/a
207 */
208
209 function acf_text_input( $atts = array() ) {
210
211 echo acf_get_text_input( $atts );
212
213 }
214
215
216 /*
217 * acf_get_hidden_input
218 *
219 * This function will return HTML for a hidden input
220 *
221 * @type function
222 * @date 3/02/2014
223 * @since 5.0.0
224 *
225 * @param $atts
226 * @return (string)
227 */
228
229 function acf_get_hidden_input( $atts = array() ) {
230
231 $atts['type'] = 'hidden';
232 return acf_get_text_input( $atts );
233
234 }
235
236
237 /*
238 * acf_hidden_input
239 *
240 * This function will output HTML for a generic input
241 *
242 * @type function
243 * @date 3/02/2014
244 * @since 5.0.0
245 *
246 * @param $atts
247 * @return n/a
248 */
249
250 function acf_hidden_input( $atts = array() ) {
251
252 echo acf_get_hidden_input( $atts ) . "\n";
253
254 }
255
256
257 /*
258 * acf_get_textarea_input
259 *
260 * This function will return HTML for a textarea input
261 *
262 * @type function
263 * @date 3/02/2014
264 * @since 5.0.0
265 *
266 * @param $atts
267 * @return (string)
268 */
269
270 function acf_get_textarea_input( $atts = array() ) {
271
272 $value = acf_extract_var( $atts, 'value', '' );
273 return '<textarea ' . acf_esc_atts( $atts ) . '>' . esc_textarea( $value ) . '</textarea>';
274
275 }
276
277
278 /*
279 * acf_textarea_input
280 *
281 * This function will output HTML for a textarea input
282 *
283 * @type function
284 * @date 3/02/2014
285 * @since 5.0.0
286 *
287 * @param $atts
288 * @return n/a
289 */
290
291 function acf_textarea_input( $atts = array() ) {
292
293 echo acf_get_textarea_input( $atts );
294
295 }
296
297
298 /*
299 * acf_get_checkbox_input
300 *
301 * This function will return HTML for a checkbox input
302 *
303 * @type function
304 * @date 3/02/2014
305 * @since 5.0.0
306 *
307 * @param $atts
308 * @return (string)
309 */
310
311 function acf_get_checkbox_input( $atts = array() ) {
312
313 $label = acf_extract_var( $atts, 'label', '' );
314 $checked = acf_maybe_get( $atts, 'checked', '' );
315 $atts['type'] = acf_maybe_get( $atts, 'type', 'checkbox' );
316 return '<label' . ($checked ? ' class="selected"' : '') . '><input ' . acf_esc_attr( $atts ) . '/>' . acf_esc_html( $label ) . '</label>';
317
318 }
319
320
321 /*
322 * acf_checkbox_input
323 *
324 * This function will output HTML for a checkbox input
325 *
326 * @type function
327 * @date 3/02/2014
328 * @since 5.0.0
329 *
330 * @param $atts
331 * @return n/a
332 */
333
334 function acf_checkbox_input( $atts = array() ) {
335
336 echo acf_get_checkbox_input( $atts );
337
338 }
339
340
341 /*
342 * acf_get_radio_input
343 *
344 * This function will return HTML for a radio input
345 *
346 * @type function
347 * @date 3/02/2014
348 * @since 5.0.0
349 *
350 * @param $atts
351 * @return (string)
352 */
353
354 function acf_get_radio_input( $atts = array() ) {
355
356 $atts['type'] = 'radio';
357 return acf_get_checkbox_input( $atts );
358
359 }
360
361
362 /*
363 * acf_radio_input
364 *
365 * This function will output HTML for a radio input
366 *
367 * @type function
368 * @date 3/02/2014
369 * @since 5.0.0
370 *
371 * @param $atts
372 * @return n/a
373 */
374
375 function acf_radio_input( $atts = array() ) {
376
377 echo acf_get_radio_input( $atts );
378
379 }
380
381
382 /*
383 * acf_get_select_input
384 *
385 * This function will return HTML for a select input
386 *
387 * @type function
388 * @date 3/02/2014
389 * @since 5.0.0
390 *
391 * @param $atts
392 * @return (string)
393 */
394
395 function acf_get_select_input( $atts = array() ) {
396
397 // vars
398 $value = (array) acf_extract_var( $atts, 'value' );
399 $choices = (array) acf_extract_var( $atts, 'choices' );
400
401
402 // html
403 $html = '';
404 $html .= '<select ' . acf_esc_atts( $atts ) . '>' . "\n";
405 $html .= acf_walk_select_input( $choices, $value );
406 $html .= '</select>' . "\n";
407
408
409 // return
410 return $html;
411
412 }
413
414
415 /*
416 * acf_walk_select_input
417 *
418 * This function will return the HTML for a select input's choices
419 *
420 * @type function
421 * @date 27/6/17
422 * @since 5.6.0
423 *
424 * @param $post_id (int)
425 * @return $post_id (int)
426 */
427
428 function acf_walk_select_input( $choices = array(), $values = array(), $depth = 0 ) {
429
430 // bail ealry if no choices
431 if( empty($choices) ) return '';
432
433
434 // vars
435 $html = '';
436
437
438 // sanitize values for 'selected' matching
439 if( $depth == 0 ) {
440 $values = array_map('esc_attr', $values);
441 }
442
443
444 // loop
445 foreach( $choices as $value => $label ) {
446
447 // optgroup
448 if( is_array($label) ){
449
450 $html .= '<optgroup label="' . esc_attr($value) . '">' . "\n";
451 $html .= acf_walk_select_input( $label, $values, $depth+1 );
452 $html .= '</optgroup>';
453
454 // option
455 } else {
456
457 // vars
458 $atts = array( 'value' => $value );
459 $pos = array_search( esc_attr($value), $values );
460
461
462 // selected
463 if( $pos !== false ) {
464 $atts['selected'] = 'selected';
465 $atts['data-i'] = $pos;
466 }
467
468
469 // append
470 $html .= '<option ' . acf_esc_attr($atts) . '>' . esc_html($label) . '</option>' . "\n";
471
472 }
473
474 }
475
476
477 // return
478 return $html;
479
480 }
481
482
483 /*
484 * acf_select_input
485 *
486 * This function will output HTML for a select input
487 *
488 * @type function
489 * @date 3/02/2014
490 * @since 5.0.0
491 *
492 * @param $atts
493 * @return n/a
494 */
495
496 function acf_select_input( $atts = array() ) {
497
498 echo acf_get_select_input( $atts );
499
500 }
501
502
503
504 /*
505 function acf_test_esc_html( $string = '' ) {
506
507 $s = '';
508
509
510 $time_start = microtime(true);
511 $s .= wp_kses_post( $string );
512 $s .= ' = ('. (microtime(true) - $time_start) .')';
513
514 $s .= '-----';
515
516
517 $time_start = microtime(true);
518 $s .= str_replace(array('<script', '</script'), array(htmlspecialchars('<script'), htmlspecialchars('</script')), $string);
519 $s .= ' = ('. (microtime(true) - $time_start) .')';
520
521
522 $time_start = microtime(true);
523 if( strpos($string, '<script') ) {
524 $s .= str_replace(array('<script', '</script'), array(htmlspecialchars('<script'), htmlspecialchars('</script')), $string);
525 }
526 $s .= ' = ('. (microtime(true) - $time_start) .')';
527
528 return $s;
529
530 }
531 */
532
533
534 /*
535 * acf_get_file_input
536 *
537 * This function will return HTML for a file input
538 *
539 * @type function
540 * @date 3/02/2014
541 * @since 5.0.0
542 *
543 * @param $atts
544 * @return (string)
545 */
546
547 function acf_get_file_input( $atts = array() ) {
548
549 $atts['type'] = 'file';
550 return acf_get_text_input( $atts );
551
552 }
553
554
555 /*
556 * acf_file_input
557 *
558 * This function will output HTML for a file input
559 *
560 * @type function
561 * @date 3/02/2014
562 * @since 5.0.0
563 *
564 * @param $atts
565 * @return n/a
566 */
567
568 function acf_file_input( $atts = array() ) {
569
570 echo acf_get_file_input( $atts );
571
572 }
573
574
575 /*
576 * acf_esc_attr
577 *
578 * Deprecated since 5.6.0
579 *
580 * @type function
581 * @date 1/10/13
582 * @since 5.0.0
583 *
584 * @param $atts (array)
585 * @return n/a
586 */
587
588 function acf_esc_attr( $atts ) {
589
590 return acf_esc_atts( $atts );
591
592 }
593
594
595 /*
596 * acf_esc_attr_e
597 *
598 * Deprecated since 5.6.0
599 *
600 * @type function
601 * @date 1/10/13
602 * @since 5.0.0
603 *
604 * @param $atts (array)
605 * @return n/a
606 */
607
608 function acf_esc_attr_e( $atts ) {
609
610 acf_esc_atts_e( $atts );
611
612 }
613
614
615 ?>