$attrs Attribute name => value. `null`/`false` values are skipped,
* `true` renders a bare attribute, arrays are space-joined.
*/
public static function render( array $attrs ): string {
$parts = [];
foreach ( $attrs as $name => $value ) {
$name = strtolower( (string) preg_replace( '/[^a-zA-Z0-9_\-:]/', '', (string) $name ) );
if ( '' === $name || null === $value || false === $value ) {
continue;
}
if ( true === $value ) {
$parts[] = esc_attr( $name );
continue;
}
if ( is_array( $value ) ) {
$value = implode( ' ', array_map( 'strval', array_filter( $value, 'is_scalar' ) ) );
}
$parts[] = esc_attr( $name ) . '="' . esc_attr( (string) $value ) . '"';
}
return implode( ' ', $parts );
}
}