Skip to content

Commit b685d5a

Browse files
authored
1 parent 6f872c8 commit b685d5a

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

Function.HTML-Build-Attributes.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,47 @@
1212
*/
1313
function html_build_attributes( $attr = [], $callback = null )
1414
{
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 ( is_array( $val ) ) {
24-
$val = implode( ' ', $val );
25-
}
26-
27-
if ( is_callable( $callback ) ) {
28-
$val = call_user_func( $callback, $val );
29-
} elseif ( function_exists('esc_attr') ) {
30-
$val = esc_attr( $val );
31-
} else {
32-
$val = htmlspecialchars( $val, ENT_QUOTES );
33-
}
34-
35-
return sprintf( '%1$s="%2$s"', $key, $val );
36-
}
37-
},
38-
$attr,
39-
array_keys( $attr )
40-
);
41-
42-
$html = implode( ' ', $html );
43-
}
44-
45-
return $html;
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+
}
46+
}
47+
},
48+
$attr,
49+
array_keys( $attr )
50+
);
51+
52+
$html = implode( ' ', $html );
53+
}
54+
55+
return $html;
4656
}
4757

4858
endif;

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ Generate a string of HTML attributes from the associative array provided.
1515

1616
- `attr` — Associative array, or object containing properties, representing attribute names and values.
1717
If `attr` is an object, then only public properties will be incorporated into the result.
18-
If a value is an array, its values will be concatenated and delimited with a space.
19-
- `$callback` — Callback function for escaping the values for the HTML attributes.
18+
If a value is an array, its values will be concatenated and delimited with a space.
19+
Values that cannot be converted into strings will be ignored. If the value is Stringable, Arrayble, or a `Closure`, attempts will be made to parse as a string.
20+
- `$callback` — Callback function for escaping the values for the HTML attributes.
21+
If no sanitizer is provided, [`htmlspecialchars()`](http://php.net/htmlspecialchars) is used;
22+
If using WordPress, the [`esc_attr()`](https://developer.wordpress.org/reference/functions/esc_attr/) function is used.
2023

2124
## Return Values
2225

0 commit comments

Comments
 (0)