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