Constants
3 days ago
Accordion.php
3 days ago
Alert.php
3 days ago
AttachmentCard.php
3 days ago
Avatar.php
3 days ago
Badge.php
3 days ago
BaseComponent.php
3 days ago
Button.php
3 days ago
ConfirmationModal.php
3 days ago
CourseFilter.php
3 days ago
DateFilter.php
3 days ago
DropdownFilter.php
3 days ago
EmptyState.php
3 days ago
FileUploader.php
3 days ago
InputField.php
3 days ago
Modal.php
3 days ago
Nav.php
3 days ago
Pagination.php
3 days ago
Popover.php
3 days ago
PreviewTrigger.php
3 days ago
Progress.php
3 days ago
SearchFilter.php
3 days ago
Sorting.php
3 days ago
StarRating.php
3 days ago
StarRatingInput.php
3 days ago
StatusSelect.php
3 days ago
SvgIcon.php
3 days ago
Table.php
3 days ago
Tabs.php
3 days ago
Tooltip.php
3 days ago
WPEditor.php
3 days ago
Alert.php
208 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Alert Component Class. |
| 4 | * |
| 5 | * Provides a fluent builder for rendering alerts with |
| 6 | * different variants and actions. |
| 7 | * |
| 8 | * @package Tutor\Components |
| 9 | * @author Themeum |
| 10 | * @link https://themeum.com |
| 11 | * @since 4.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Tutor\Components; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Alert Component Class. |
| 20 | * |
| 21 | * Example usage: |
| 22 | * ``` |
| 23 | * Alert::make() |
| 24 | * ->text( 'This is an alert' ) |
| 25 | * ->variant( Alert::SUCCESS ) |
| 26 | * ->icon( Icon::PRIME_CHECK_CIRCLE ) |
| 27 | * ->action( '<button class="...">Resume</button>' ) |
| 28 | * ->render(); |
| 29 | * ``` |
| 30 | * |
| 31 | * @since 4.0.0 |
| 32 | */ |
| 33 | class Alert extends BaseComponent { |
| 34 | |
| 35 | /** |
| 36 | * Variant constants. |
| 37 | * |
| 38 | * @since 4.0.0 |
| 39 | */ |
| 40 | public const DEFAULT = 'default'; |
| 41 | public const INFO = 'info'; |
| 42 | public const SUCCESS = 'success'; |
| 43 | public const WARNING = 'warning'; |
| 44 | public const ERROR = 'error'; |
| 45 | |
| 46 | /** |
| 47 | * Alert text content. |
| 48 | * |
| 49 | * @since 4.0.0 |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $text = ''; |
| 54 | |
| 55 | /** |
| 56 | * Alert variant style. |
| 57 | * |
| 58 | * @since 4.0.0 |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | protected $variant = self::DEFAULT; |
| 63 | |
| 64 | /** |
| 65 | * The SVG icon markup or name. |
| 66 | * |
| 67 | * @since 4.0.0 |
| 68 | * |
| 69 | * @var string |
| 70 | */ |
| 71 | protected $icon = ''; |
| 72 | |
| 73 | /** |
| 74 | * Icon width. |
| 75 | * |
| 76 | * @var int |
| 77 | */ |
| 78 | protected $icon_width = 20; |
| 79 | |
| 80 | /** |
| 81 | * Icon height. |
| 82 | * |
| 83 | * @var int |
| 84 | */ |
| 85 | protected $icon_height = 20; |
| 86 | |
| 87 | /** |
| 88 | * Alert action HTML. |
| 89 | * |
| 90 | * @since 4.0.0 |
| 91 | * |
| 92 | * @var string |
| 93 | */ |
| 94 | protected $action = ''; |
| 95 | |
| 96 | /** |
| 97 | * Set alert text content. |
| 98 | * |
| 99 | * @since 4.0.0 |
| 100 | * |
| 101 | * @param string $text Alert text content. |
| 102 | * |
| 103 | * @return $this |
| 104 | */ |
| 105 | public function text( string $text ): self { |
| 106 | $this->text = $text; |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Set alert variant style. |
| 112 | * |
| 113 | * @since 4.0.0 |
| 114 | * |
| 115 | * @param string $variant Alert variant (default|info|success|warning|error). |
| 116 | * |
| 117 | * @return $this |
| 118 | */ |
| 119 | public function variant( string $variant ): self { |
| 120 | $this->variant = $variant; |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set the SVG icon for the alert. |
| 126 | * |
| 127 | * @since 4.0.0 |
| 128 | * |
| 129 | * @param string $icon SVG icon name or markup. |
| 130 | * @param int $width Optional. Icon width. |
| 131 | * @param int $height Optional. Icon height. |
| 132 | * |
| 133 | * @return $this |
| 134 | */ |
| 135 | public function icon( string $icon, int $width = 20, int $height = 20 ): self { |
| 136 | $this->icon = $icon; |
| 137 | $this->icon_width = $width; |
| 138 | $this->icon_height = $height; |
| 139 | return $this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set alert action HTML. |
| 144 | * |
| 145 | * @since 4.0.0 |
| 146 | * |
| 147 | * @param string $action Alert action HTML. |
| 148 | * |
| 149 | * @return $this |
| 150 | */ |
| 151 | public function action( string $action ): self { |
| 152 | $this->action = $action; |
| 153 | return $this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the final alert HTML. |
| 158 | * |
| 159 | * @since 4.0.0 |
| 160 | * |
| 161 | * @return string HTML output. |
| 162 | */ |
| 163 | public function get(): string { |
| 164 | $classes = sprintf( 'tutor-alert tutor-alert-%s', esc_attr( $this->variant ) ); |
| 165 | |
| 166 | // Merge with any custom class attribute. |
| 167 | $this->attributes['class'] = trim( "{$classes} " . ( $this->attributes['class'] ?? '' ) ); |
| 168 | |
| 169 | $attributes = $this->get_attributes_string(); |
| 170 | |
| 171 | // Build icon HTML if exists. |
| 172 | $icon_html = ''; |
| 173 | if ( ! empty( $this->icon ) ) { |
| 174 | if ( false !== strpos( $this->icon, '<svg' ) ) { |
| 175 | $icon_html = $this->icon; |
| 176 | } else { |
| 177 | ob_start(); |
| 178 | SvgIcon::make()->name( $this->icon )->width( $this->icon_width )->height( $this->icon_height )->render(); |
| 179 | $icon_html = ob_get_clean(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Build content wrapper. |
| 184 | $content_html = sprintf( |
| 185 | '<div class="tutor-alert-content"> |
| 186 | %s |
| 187 | <div class="tutor-alert-text">%s</div> |
| 188 | </div>', |
| 189 | ! empty( $icon_html ) ? '<div class="tutor-alert-icon">' . $icon_html . '</div>' : '', |
| 190 | $this->text // Allow HTML in text if needed, or we could esc_html here. Usually components like this might need small HTML in text. |
| 191 | ); |
| 192 | |
| 193 | // Build action wrapper. |
| 194 | $action_html = ! empty( $this->action ) |
| 195 | ? sprintf( '<div class="tutor-alert-action">%s</div>', $this->action ) |
| 196 | : ''; |
| 197 | |
| 198 | $this->component_string = sprintf( |
| 199 | '<div %s>%s%s</div>', |
| 200 | $attributes, |
| 201 | $content_html, |
| 202 | $action_html |
| 203 | ); |
| 204 | |
| 205 | return $this->component_string; |
| 206 | } |
| 207 | } |
| 208 |