44 /**
55 * Generate a string of HTML attributes.
66 *
7- * @param array|object $attr Associative array or object containing properties,
7+ * @param array|object $attributes
8+ * Associative array or object containing properties,
89 * representing attribute names and values.
9- * @param callable|null $callback Callback function to escape the values for HTML attributes.
10+ * @param callable|null $escape
11+ * Callback function to escape the values for HTML attributes.
1012 * Defaults to `esc_attr()`, if available, otherwise `htmlspecialchars()`.
1113 * @return string Returns a string of HTML attributes
12- * or a empty string if $attr is invalid or empty.
14+ * or a empty string if $attributes is invalid or empty.
1315 */
14- function html_build_attributes ($ attr , callable $ callback = null )
16+ function html_build_attributes ($ attributes , callable $ escape = null )
1517 {
16- if (is_object ($ attr ) && !($ attr instanceof \Traversable)) {
17- $ attr = get_object_vars ($ attr );
18+ if (is_object ($ attributes ) && !($ attributes instanceof \Traversable)) {
19+ $ attributes = get_object_vars ($ attributes );
1820 }
1921
20- if (!is_array ($ attr ) || !count ($ attr )) {
22+ if (!is_array ($ attributes ) || !count ($ attributes )) {
2123 return '' ;
2224 }
2325
2426 $ html = [];
25- foreach ($ attr as $ key => $ val ) {
26- if (is_string ($ key )) {
27- $ key = trim ($ key );
27+ foreach ($ attributes as $ attribute_name => $ attribute_value ) {
28+ if (is_string ($ attribute_name )) {
29+ $ attribute_name = trim ($ attribute_name );
2830
29- if (strlen ($ key ) === 0 ) {
31+ if (strlen ($ attribute_name ) === 0 ) {
3032 continue ;
3133 }
3234 }
3335
34- if (is_object ($ val ) && is_callable ($ val )) {
35- $ val = $ val ();
36+ if (is_object ($ attribute_value ) && is_callable ($ attribute_value )) {
37+ $ attribute_value = $ attribute_value ();
3638 }
3739
38- if (is_null ($ val )) {
40+ if (is_null ($ attribute_value )) {
3941 continue ;
4042 }
4143
42- if (is_object ($ val )) {
43- if (is_callable ([ $ val , 'toArray ' ])) {
44- $ val = $ val ->toArray ();
45- } elseif (is_callable ([ $ val , '__toString ' ])) {
46- $ val = strval ($ val );
44+ if (is_object ($ attribute_value )) {
45+ if (is_callable ([ $ attribute_value , 'toArray ' ])) {
46+ $ attribute_value = $ attribute_value ->toArray ();
47+ } elseif (is_callable ([ $ attribute_value , '__toString ' ])) {
48+ $ attribute_value = strval ($ attribute_value );
4749 }
4850 }
4951
50- if (is_bool ($ val )) {
51- if ($ val ) {
52- $ html [] = $ key ;
52+ if (is_bool ($ attribute_value )) {
53+ if ($ attribute_value ) {
54+ $ html [] = $ attribute_name ;
5355 }
5456 continue ;
55- } elseif (is_array ($ val )) {
56- $ val = implode (' ' , array_reduce ($ val , function ($ tokens , $ token ) {
57+ } elseif (is_array ($ attribute_value )) {
58+ $ attribute_value = implode (' ' , array_reduce ($ attribute_value , function ($ tokens , $ token ) {
5759 if (is_string ($ token )) {
5860 $ token = trim ($ token );
5961
@@ -67,22 +69,26 @@ function html_build_attributes($attr, callable $callback = null)
6769 return $ tokens ;
6870 }, []));
6971
70- if (strlen ($ val ) === 0 ) {
72+ if (strlen ($ attribute_value ) === 0 ) {
7173 continue ;
7274 }
73- } elseif (!is_string ($ val ) && !is_numeric ($ val )) {
74- $ val = json_encode ($ val , (JSON_UNESCAPED_SLASHES |JSON_UNESCAPED_UNICODE ));
75+ } elseif (!is_string ($ attribute_value ) && !is_numeric ($ attribute_value )) {
76+ $ attribute_value = json_encode ($ attribute_value , (JSON_UNESCAPED_SLASHES |JSON_UNESCAPED_UNICODE ));
7577 }
7678
77- if (is_callable ($ callback )) {
78- $ val = $ callback ( $ val );
79+ if (is_callable ($ escape )) {
80+ $ attribute_value = $ escape ( $ attribute_value );
7981 } elseif (function_exists ('esc_attr ' )) {
80- $ val = esc_attr ($ val );
82+ $ attribute_value = esc_attr ($ attribute_value );
8183 } else {
82- $ val = htmlspecialchars ($ val , ENT_QUOTES );
84+ $ attribute_value = htmlspecialchars ($ attribute_value , ENT_QUOTES );
8385 }
8486
85- $ html [] = sprintf ('%1$s="%2$s" ' , $ key , $ val );
87+ $ html [] = sprintf (
88+ '%1$s="%2$s" ' ,
89+ $ attribute_name ,
90+ $ attribute_value
91+ );
8692 }
8793
8894 return implode (' ' , $ html );
0 commit comments