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