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.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 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