|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -if ( ! function_exists('html_build_attributes') ) : |
| 3 | +if (!function_exists('html_build_attributes')) { |
4 | 4 |
|
5 | 5 | /**
|
6 | 6 | * Generate a string of HTML attributes
|
|
10 | 10 | * Defaults to `htmlspecialchars()`.
|
11 | 11 | * @return string Returns a string of HTML attributes.
|
12 | 12 | */
|
13 |
| - function html_build_attributes( array $attr, callable $callback = null ) |
| 13 | + function html_build_attributes(array $attr, callable $callback = null) |
14 | 14 | {
|
15 |
| - if ( ! count($attr) ) { |
| 15 | + if (!count($attr)) { |
16 | 16 | return '';
|
17 | 17 | }
|
18 | 18 |
|
19 | 19 | $html = array_map(
|
20 |
| - function ( $val, $key ) use ( $callback ) { |
21 |
| - if ( is_bool( $val ) ) { |
22 |
| - return ( $val ? $key : '' ); |
23 |
| - } elseif ( isset( $val ) ) { |
24 |
| - if ( $val instanceof \Closure ) { |
| 20 | + function ($val, $key) use ($callback) { |
| 21 | + if (is_bool($val)) { |
| 22 | + return ($val ? $key : ''); |
| 23 | + } elseif (isset($val)) { |
| 24 | + if ($val instanceof Closure) { |
25 | 25 | $val = $val();
|
26 |
| - } elseif ( $val instanceof \JsonSerializable ) { |
| 26 | + } elseif ($val instanceof JsonSerializable) { |
27 | 27 | $val = json_encode(
|
28 | 28 | $val->jsonSerialize(),
|
29 | 29 | (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
|
30 | 30 | );
|
31 |
| - } elseif ( is_callable( [ $val, 'toArray' ] ) ) { |
| 31 | + } elseif (is_callable([ $val, 'toArray' ])) { |
32 | 32 | $val = $val->toArray();
|
33 |
| - } elseif ( is_callable( [ $val, '__toString' ] ) ) { |
34 |
| - $val = strval( $val ); |
| 33 | + } elseif (is_callable([ $val, '__toString' ])) { |
| 34 | + $val = strval($val); |
35 | 35 | }
|
36 | 36 |
|
37 |
| - if ( is_array( $val ) ) { |
38 |
| - $val = implode( ' ', $val ); |
| 37 | + if (is_array($val)) { |
| 38 | + if (function_exists('is_blank')) { |
| 39 | + $filter = function ($var) { |
| 40 | + return !is_blank($var); |
| 41 | + }; |
| 42 | + } else { |
| 43 | + $filter = function ($var) { |
| 44 | + return !empty($var) || is_numeric($var); |
| 45 | + }; |
| 46 | + } |
| 47 | + $val = implode(' ', array_filter($val, $filter)); |
39 | 48 | }
|
40 | 49 |
|
41 |
| - if ( is_callable( $callback ) ) { |
42 |
| - $val = call_user_func( $callback, $val ); |
43 |
| - } elseif ( function_exists('esc_attr') ) { |
44 |
| - $val = esc_attr( $val ); |
| 50 | + if (is_callable($callback)) { |
| 51 | + $val = call_user_func($callback, $val); |
| 52 | + } elseif (function_exists('esc_attr')) { |
| 53 | + $val = esc_attr($val); |
45 | 54 | } else {
|
46 |
| - $val = htmlspecialchars( $val, ENT_QUOTES ); |
| 55 | + $val = htmlspecialchars($val, ENT_QUOTES); |
47 | 56 | }
|
48 | 57 |
|
49 |
| - if ( is_string( $val ) ) { |
50 |
| - return sprintf( '%1$s="%2$s"', $key, $val ); |
| 58 | + if (is_string($val)) { |
| 59 | + return sprintf('%1$s="%2$s"', $key, $val); |
51 | 60 | }
|
52 | 61 | }
|
53 | 62 | },
|
54 | 63 | $attr,
|
55 |
| - array_keys( $attr ) |
| 64 | + array_keys($attr) |
56 | 65 | );
|
57 | 66 |
|
58 |
| - return implode( ' ', $html ); |
| 67 | + return implode(' ', $html); |
59 | 68 | }
|
60 |
| - |
61 |
| -endif; |
| 69 | +} |
0 commit comments