| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Returns a formatted string of HTML attributes. |
| 5 |
* |
| 6 |
* @param array $atts Associative array of attribute name and value pairs. |
| 7 |
* @return string Formatted HTML attributes. |
| 8 |
*/ |
| 9 |
function bogo_format_atts( $atts ) { |
| 10 |
$atts_filtered = array(); |
| 11 |
|
| 12 |
foreach ( $atts as $name => $value ) { |
| 13 |
$name = strtolower( trim( $name ) ); |
| 14 |
|
| 15 |
if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $name ) ) { |
| 16 |
continue; |
| 17 |
} |
| 18 |
|
| 19 |
static $boolean_attributes = array( |
| 20 |
'checked', 'disabled', 'multiple', 'readonly', 'required', 'selected', |
| 21 |
); |
| 22 |
|
| 23 |
if ( in_array( $name, $boolean_attributes ) and '' === $value ) { |
| 24 |
$value = false; |
| 25 |
} |
| 26 |
|
| 27 |
if ( is_numeric( $value ) ) { |
| 28 |
$value = (string) $value; |
| 29 |
} |
| 30 |
|
| 31 |
if ( null === $value or false === $value ) { |
| 32 |
unset( $atts_filtered[$name] ); |
| 33 |
} elseif ( true === $value ) { |
| 34 |
$atts_filtered[$name] = $name; // boolean attribute |
| 35 |
} elseif ( is_string( $value ) ) { |
| 36 |
$atts_filtered[$name] = trim( $value ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$output = ''; |
| 41 |
|
| 42 |
foreach ( $atts_filtered as $name => $value ) { |
| 43 |
$output .= sprintf( ' %1$s="%2$s"', $name, esc_attr( $value ) ); |
| 44 |
} |
| 45 |
|
| 46 |
return trim( $output ); |
| 47 |
} |
| 48 |
|