Constants
1 month ago
Accordion.php
2 weeks ago
Alert.php
2 weeks ago
AttachmentCard.php
2 weeks ago
Avatar.php
2 weeks ago
Badge.php
2 weeks ago
BaseComponent.php
1 month ago
Button.php
2 weeks ago
ConfirmationModal.php
2 weeks ago
CourseFilter.php
2 weeks ago
DateFilter.php
2 weeks ago
DropdownFilter.php
2 weeks ago
EmptyState.php
2 weeks ago
FileUploader.php
2 weeks ago
InputField.php
2 weeks ago
Modal.php
2 weeks ago
Nav.php
2 weeks ago
Pagination.php
2 weeks ago
Popover.php
2 weeks ago
PreviewTrigger.php
2 weeks ago
Progress.php
2 weeks ago
SearchFilter.php
2 weeks ago
Sorting.php
2 weeks ago
StarRating.php
2 weeks ago
StarRatingInput.php
2 weeks ago
StatusSelect.php
2 weeks ago
SvgIcon.php
2 weeks ago
Table.php
2 weeks ago
Tabs.php
2 weeks ago
Tooltip.php
2 weeks ago
WPEditor.php
2 weeks ago
Alert.php
243 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 | * // Default (neutral) alert |
| 24 | * Alert::make() |
| 25 | * ->text( 'Your changes have been saved.' ) |
| 26 | * ->render(); |
| 27 | * |
| 28 | * // Success alert with icon |
| 29 | * Alert::make() |
| 30 | * ->text( 'Course published successfully.' ) |
| 31 | * ->variant( Alert::SUCCESS ) |
| 32 | * ->icon( Icon::PRIME_CHECK_CIRCLE ) |
| 33 | * ->render(); |
| 34 | * |
| 35 | * // Info alert with action button |
| 36 | * Alert::make() |
| 37 | * ->text( 'You have an incomplete quiz. Resume where you left off.' ) |
| 38 | * ->variant( Alert::INFO ) |
| 39 | * ->icon( Icon::INFO ) |
| 40 | * ->action( '<button class="tutor-btn tutor-btn-primary tutor-btn-small">Resume</button>' ) |
| 41 | * ->render(); |
| 42 | * |
| 43 | * // Warning alert |
| 44 | * Alert::make() |
| 45 | * ->text( 'Your subscription expires in 3 days.' ) |
| 46 | * ->variant( Alert::WARNING ) |
| 47 | * ->icon( Icon::WARNING ) |
| 48 | * ->render(); |
| 49 | * |
| 50 | * // Error alert with custom icon size |
| 51 | * Alert::make() |
| 52 | * ->text( 'Payment failed. Please try again.' ) |
| 53 | * ->variant( Alert::ERROR ) |
| 54 | * ->icon( Icon::CLOSE_CIRCLE, 24, 24 ) |
| 55 | * ->render(); |
| 56 | * |
| 57 | * // Alert with extra HTML attributes |
| 58 | * Alert::make() |
| 59 | * ->text( 'Draft saved.' ) |
| 60 | * ->variant( Alert::SUCCESS ) |
| 61 | * ->attr( 'id', 'draft-alert' ) |
| 62 | * ->attr( 'x-show', 'showAlert' ) |
| 63 | * ->render(); |
| 64 | * ``` |
| 65 | * |
| 66 | * @since 4.0.0 |
| 67 | */ |
| 68 | class Alert extends BaseComponent { |
| 69 | |
| 70 | /** |
| 71 | * Variant constants. |
| 72 | * |
| 73 | * @since 4.0.0 |
| 74 | */ |
| 75 | public const DEFAULT = 'default'; |
| 76 | public const INFO = 'info'; |
| 77 | public const SUCCESS = 'success'; |
| 78 | public const WARNING = 'warning'; |
| 79 | public const ERROR = 'error'; |
| 80 | |
| 81 | /** |
| 82 | * Alert text content. |
| 83 | * |
| 84 | * @since 4.0.0 |
| 85 | * |
| 86 | * @var string |
| 87 | */ |
| 88 | protected $text = ''; |
| 89 | |
| 90 | /** |
| 91 | * Alert variant style. |
| 92 | * |
| 93 | * @since 4.0.0 |
| 94 | * |
| 95 | * @var string |
| 96 | */ |
| 97 | protected $variant = self::DEFAULT; |
| 98 | |
| 99 | /** |
| 100 | * The SVG icon markup or name. |
| 101 | * |
| 102 | * @since 4.0.0 |
| 103 | * |
| 104 | * @var string |
| 105 | */ |
| 106 | protected $icon = ''; |
| 107 | |
| 108 | /** |
| 109 | * Icon width. |
| 110 | * |
| 111 | * @var int |
| 112 | */ |
| 113 | protected $icon_width = 20; |
| 114 | |
| 115 | /** |
| 116 | * Icon height. |
| 117 | * |
| 118 | * @var int |
| 119 | */ |
| 120 | protected $icon_height = 20; |
| 121 | |
| 122 | /** |
| 123 | * Alert action HTML. |
| 124 | * |
| 125 | * @since 4.0.0 |
| 126 | * |
| 127 | * @var string |
| 128 | */ |
| 129 | protected $action = ''; |
| 130 | |
| 131 | /** |
| 132 | * Set alert text content. |
| 133 | * |
| 134 | * @since 4.0.0 |
| 135 | * |
| 136 | * @param string $text Alert text content. |
| 137 | * |
| 138 | * @return $this |
| 139 | */ |
| 140 | public function text( string $text ): self { |
| 141 | $this->text = $text; |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Set alert variant style. |
| 147 | * |
| 148 | * @since 4.0.0 |
| 149 | * |
| 150 | * @param string $variant Alert variant (default|info|success|warning|error). |
| 151 | * |
| 152 | * @return $this |
| 153 | */ |
| 154 | public function variant( string $variant ): self { |
| 155 | $this->variant = $variant; |
| 156 | return $this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Set the SVG icon for the alert. |
| 161 | * |
| 162 | * @since 4.0.0 |
| 163 | * |
| 164 | * @param string $icon SVG icon name or markup. |
| 165 | * @param int $width Optional. Icon width. |
| 166 | * @param int $height Optional. Icon height. |
| 167 | * |
| 168 | * @return $this |
| 169 | */ |
| 170 | public function icon( string $icon, int $width = 20, int $height = 20 ): self { |
| 171 | $this->icon = $icon; |
| 172 | $this->icon_width = $width; |
| 173 | $this->icon_height = $height; |
| 174 | return $this; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set alert action HTML. |
| 179 | * |
| 180 | * @since 4.0.0 |
| 181 | * |
| 182 | * @param string $action Alert action HTML. |
| 183 | * |
| 184 | * @return $this |
| 185 | */ |
| 186 | public function action( string $action ): self { |
| 187 | $this->action = $action; |
| 188 | return $this; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the final alert HTML. |
| 193 | * |
| 194 | * @since 4.0.0 |
| 195 | * |
| 196 | * @return string HTML output. |
| 197 | */ |
| 198 | public function get(): string { |
| 199 | $classes = sprintf( 'tutor-alert tutor-alert-%s', esc_attr( $this->variant ) ); |
| 200 | |
| 201 | // Merge with any custom class attribute. |
| 202 | $this->attributes['class'] = trim( "{$classes} " . ( $this->attributes['class'] ?? '' ) ); |
| 203 | |
| 204 | $attributes = $this->get_attributes_string(); |
| 205 | |
| 206 | // Build icon HTML if exists. |
| 207 | $icon_html = ''; |
| 208 | if ( ! empty( $this->icon ) ) { |
| 209 | if ( false !== strpos( $this->icon, '<svg' ) ) { |
| 210 | $icon_html = $this->icon; |
| 211 | } else { |
| 212 | ob_start(); |
| 213 | SvgIcon::make()->name( $this->icon )->width( $this->icon_width )->height( $this->icon_height )->render(); |
| 214 | $icon_html = ob_get_clean(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Build content wrapper. |
| 219 | $content_html = sprintf( |
| 220 | '<div class="tutor-alert-content"> |
| 221 | %s |
| 222 | <div class="tutor-alert-text">%s</div> |
| 223 | </div>', |
| 224 | ! empty( $icon_html ) ? '<div class="tutor-alert-icon">' . $icon_html . '</div>' : '', |
| 225 | $this->text // Allow HTML in text if needed, or we could esc_html here. Usually components like this might need small HTML in text. |
| 226 | ); |
| 227 | |
| 228 | // Build action wrapper. |
| 229 | $action_html = ! empty( $this->action ) |
| 230 | ? sprintf( '<div class="tutor-alert-action">%s</div>', $this->action ) |
| 231 | : ''; |
| 232 | |
| 233 | $this->component_string = sprintf( |
| 234 | '<div %s>%s%s</div>', |
| 235 | $attributes, |
| 236 | $content_html, |
| 237 | $action_html |
| 238 | ); |
| 239 | |
| 240 | return $this->component_string; |
| 241 | } |
| 242 | } |
| 243 |