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.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / components / Alert.php
tutor / components Last commit date
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