Skip to content

Commit 76b82c7

Browse files
authored
1 parent 611cebb commit 76b82c7

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

Function.HTML-Build-Attributes.php

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
if ( ! function_exists('html_build_attributes') ) :
3+
if (!function_exists('html_build_attributes')) {
44

55
/**
66
* Generate a string of HTML attributes
@@ -10,52 +10,60 @@
1010
* Defaults to `htmlspecialchars()`.
1111
* @return string Returns a string of HTML attributes.
1212
*/
13-
function html_build_attributes( array $attr, callable $callback = null )
13+
function html_build_attributes(array $attr, callable $callback = null)
1414
{
15-
if ( ! count($attr) ) {
15+
if (!count($attr)) {
1616
return '';
1717
}
1818

1919
$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) {
2525
$val = $val();
26-
} elseif ( $val instanceof \JsonSerializable ) {
26+
} elseif ($val instanceof JsonSerializable) {
2727
$val = json_encode(
2828
$val->jsonSerialize(),
2929
(JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
3030
);
31-
} elseif ( is_callable( [ $val, 'toArray' ] ) ) {
31+
} elseif (is_callable([ $val, 'toArray' ])) {
3232
$val = $val->toArray();
33-
} elseif ( is_callable( [ $val, '__toString' ] ) ) {
34-
$val = strval( $val );
33+
} elseif (is_callable([ $val, '__toString' ])) {
34+
$val = strval($val);
3535
}
3636

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));
3948
}
4049

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);
4554
} else {
46-
$val = htmlspecialchars( $val, ENT_QUOTES );
55+
$val = htmlspecialchars($val, ENT_QUOTES);
4756
}
4857

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);
5160
}
5261
}
5362
},
5463
$attr,
55-
array_keys( $attr )
64+
array_keys($attr)
5665
);
5766

58-
return implode( ' ', $html );
67+
return implode(' ', $html);
5968
}
60-
61-
endif;
69+
}

0 commit comments

Comments
 (0)