Form
5 months ago
Frontend
2 years ago
Gateways
4 years ago
ArrayDataSet.php
1 year ago
Call.php
3 years ago
Date.php
4 years ago
EnqueueScript.php
4 years ago
Hooks.php
4 years ago
Html.php
4 years ago
IntlTelInput.php
2 years ago
Language.php
7 months ago
Table.php
4 years ago
Utils.php
1 year ago
Html.php
57 lines
| 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 | /** |
| 13 | * A helper creating class attribute strings |
| 14 | * |
| 15 | * Note that no deduplication of class names takes place. |
| 16 | * |
| 17 | * Usage: |
| 18 | * |
| 19 | * ```php |
| 20 | * Html::classNames( |
| 21 | * // Provide default class names |
| 22 | * 'field-label', |
| 23 | * // Conditionally set class names |
| 24 | * [ |
| 25 | * 'fancy': $this->isFancy(), |
| 26 | * 'hidden': $this->isHidden(), |
| 27 | * ], |
| 28 | * // This works the same providing them as individual arguments |
| 29 | * ['w-1/3', 'flex-grow', 'flex-shrink-0'] |
| 30 | * ); |
| 31 | * ``` |
| 32 | * |
| 33 | * @param string|array ...$arguments |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | public static function classNames(...$arguments) |
| 38 | { |
| 39 | $classList = []; |
| 40 | |
| 41 | array_walk_recursive( |
| 42 | $arguments, |
| 43 | static function ($value, $key) use (&$classList) { |
| 44 | if (is_string($key) && $value === true) { |
| 45 | // In this case, a class name (the array key) is being set conditionally. |
| 46 | // We add the class name to the list if it passed the condition (the array value). |
| 47 | $classList[] = $key; |
| 48 | } elseif (is_string($value)) { |
| 49 | $classList[] = $value; |
| 50 | } |
| 51 | } |
| 52 | ); |
| 53 | |
| 54 | return implode(' ', $classList); |
| 55 | } |
| 56 | } |
| 57 |