mirror of
https://code.castopod.org/adaures/castopod
synced 2025-04-23 01:01:20 +00:00
34 lines
814 B
PHP
34 lines
814 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
if (! function_exists('flatten_attributes')) {
|
|
/**
|
|
* Stringify attributes for use in HTML tags.
|
|
*
|
|
* Helper function used to convert a string, array, or object of attributes to a string.
|
|
*
|
|
* @param mixed $attributes string, array, object
|
|
*/
|
|
function flatten_attributes(mixed $attributes, bool $js = false): string
|
|
{
|
|
$atts = '';
|
|
|
|
if ($attributes === null) {
|
|
return $atts;
|
|
}
|
|
|
|
if (is_string($attributes)) {
|
|
return ' ' . $attributes;
|
|
}
|
|
|
|
$attributes = (array) $attributes;
|
|
|
|
foreach ($attributes as $key => $val) {
|
|
$atts .= ($js) ? $key . '=' . esc($val, 'js') . ',' : ' ' . $key . '="' . $val . '"';
|
|
}
|
|
|
|
return rtrim($atts, ',');
|
|
}
|
|
}
|