| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* HTML related helper functions |
| 7 |
* |
| 8 |
* @since 2.12.0 |
| 9 |
*/ |
| 10 |
class Html { |
| 11 |
/** |
| 12 |
* A helper creating class attribute strings |
| 13 |
* |
| 14 |
* Note that no deduplication of class names takes place. |
| 15 |
* |
| 16 |
* Usage: |
| 17 |
* |
| 18 |
* ```php |
| 19 |
* Html::classNames( |
| 20 |
* // Provide default class names |
| 21 |
* 'field-label', |
| 22 |
* // Conditionally set class names |
| 23 |
* [ |
| 24 |
* 'fancy': $this->isFancy(), |
| 25 |
* 'hidden': $this->isHidden(), |
| 26 |
* ], |
| 27 |
* // This works the same providing them as individual arguments |
| 28 |
* ['w-1/3', 'flex-grow', 'flex-shrink-0'] |
| 29 |
* ); |
| 30 |
* ``` |
| 31 |
* |
| 32 |
* @param string|array ...$arguments |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public static function classNames( ...$arguments ) { |
| 37 |
$classList = []; |
| 38 |
|
| 39 |
array_walk_recursive( |
| 40 |
$arguments, |
| 41 |
static function ( $value, $key ) use ( &$classList ) { |
| 42 |
if ( is_string( $key ) && $value === true ) { |
| 43 |
// In this case, a class name (the array key) is being set conditionally. |
| 44 |
// We add the class name to the list if it passed the condition (the array value). |
| 45 |
$classList[] = $key; |
| 46 |
} elseif ( is_string( $value ) ) { |
| 47 |
$classList[] = $value; |
| 48 |
} |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
return implode( ' ', $classList ); |
| 53 |
} |
| 54 |
} |
| 55 |
|