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.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 / Badge.php
tutor / components Last commit date
Constants 3 weeks ago Accordion.php 1 week ago Alert.php 1 week ago AttachmentCard.php 1 week ago Avatar.php 1 week ago Badge.php 1 week ago BaseComponent.php 3 weeks ago Button.php 1 week ago ConfirmationModal.php 1 week ago CourseFilter.php 1 week ago DateFilter.php 1 week ago DropdownFilter.php 1 week ago EmptyState.php 1 week ago FileUploader.php 1 week ago InputField.php 1 week ago Modal.php 1 week ago Nav.php 1 week ago Pagination.php 1 week ago Popover.php 1 week ago PreviewTrigger.php 1 week ago Progress.php 1 week ago SearchFilter.php 1 week ago Sorting.php 1 week ago StarRating.php 1 week ago StarRatingInput.php 1 week ago StatusSelect.php 1 week ago SvgIcon.php 1 week ago Table.php 1 week ago Tabs.php 1 week ago Tooltip.php 1 week ago WPEditor.php 1 week ago
Badge.php
277 lines
1 <?php
2 /**
3 * Badge Component Class.
4 *
5 * Provides a fluent builder for rendering badges with
6 * different variants and styles.
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 * Badge Component Class.
20 *
21 * Example usage:
22 * ```
23 * // Primary badge
24 * Badge::make()
25 * ->label( 'New' )
26 * ->variant( Badge::PRIMARY )
27 * ->render();
28 *
29 * // Badge with SVG icon markup
30 * Badge::make()
31 * ->label( 'Certified' )
32 * ->variant( Badge::SUCCESS )
33 * ->icon( '<svg>...</svg>' )
34 * ->render();
35 *
36 * // Badge with icon name (SvgIcon slug)
37 * Badge::make()
38 * ->label( 'Warning' )
39 * ->variant( Badge::WARNING )
40 * ->icon( Icon::WARNING, 14, 14 )
41 * ->render();
42 *
43 * // Badge with icon as URL (img tag is generated)
44 * Badge::make()
45 * ->label( 'Verified' )
46 * ->variant( Badge::SUCCESS_SOLID )
47 * ->icon( 'https://example.com/check.png', 16, 16 )
48 * ->render();
49 *
50 * // Rounded (pill) badge
51 * Badge::make()
52 * ->label( 'Points: 20' )
53 * ->variant( Badge::HIGHLIGHT )
54 * ->rounded()
55 * ->render();
56 *
57 * // Error / disabled badge without icon
58 * Badge::make()
59 * ->label( 'Inactive' )
60 * ->variant( Badge::DISABLED )
61 * ->render();
62 *
63 * // Badge with no variant (default style) and extra attrs
64 * Badge::make()
65 * ->label( 'Draft' )
66 * ->attr( 'data-status', 'draft' )
67 * ->render();
68 *
69 * // Retrieve HTML string without echoing
70 * $html = Badge::make()->label( 'Info' )->variant( Badge::INFO )->get();
71 * ```
72 *
73 * @since 4.0.0
74 */
75 class Badge extends BaseComponent {
76
77 /**
78 * Variant constants.
79 *
80 * @since 4.0.0
81 */
82 public const INFO = 'info';
83 public const PRIMARY = 'primary';
84 public const WARNING = 'warning';
85 public const SUCCESS = 'success';
86 public const SUCCESS_SOLID = 'success-solid';
87 public const ERROR = 'error';
88 public const HIGHLIGHT = 'highlight';
89 public const DISABLED = 'disabled';
90
91 /**
92 * Badge label text.
93 *
94 * @since 4.0.0
95 *
96 * @var string
97 */
98 protected $label = '';
99
100 /**
101 * Badge variant style.
102 *
103 * @since 4.0.0
104 *
105 * @see Variant constants.
106 *
107 * @var string
108 */
109 protected $variant = '';
110
111 /**
112 * Whether badge has rounded style.
113 *
114 * @since 4.0.0
115 *
116 * @var bool
117 */
118 protected $rounded = false;
119
120 /**
121 * Badge attributes.
122 *
123 * @since 4.0.0
124 *
125 * @var array
126 */
127 protected $attributes = array();
128
129 /**
130 * The SVG icon markup.
131 *
132 * @since 4.0.0
133 *
134 * @var string
135 */
136 protected $icon = '';
137
138 /**
139 * Sort Icon width.
140 *
141 * @var int
142 */
143 protected $icon_width = 16;
144
145 /**
146 * Sort Icon height.
147 *
148 * @var int
149 */
150 protected $icon_height = 16;
151
152 /**
153 * Prefix content (e.g., subdued text before label).
154 *
155 * @since 4.0.0
156 *
157 * @var string
158 */
159 protected $prefix = '';
160
161 /**
162 * Additional CSS classes.
163 *
164 * @since 4.0.0
165 *
166 * @var array
167 */
168 protected $extra_classes = array();
169
170 /**
171 * Set badge label text.
172 *
173 * @since 4.0.0
174 *
175 * @param string $label Badge label text.
176 *
177 * @return $this
178 */
179 public function label( $label ) {
180 $this->label = $label;
181 return $this;
182 }
183
184 /**
185 * Set badge variant style.
186 *
187 * @since 4.0.0
188 *
189 * @param string $variant Badge variant (primary|info|warning|success|success-solid|error|highlight).
190 *
191 * @return $this
192 */
193 public function variant( $variant ) {
194 $this->variant = esc_attr( $variant );
195 return $this;
196 }
197
198 /**
199 * Enable rounded style for badge.
200 *
201 * @since 4.0.0
202 *
203 * @return $this
204 */
205 public function rounded() {
206 $this->rounded = true;
207 return $this;
208 }
209
210 /**
211 * Set the SVG icon for the badge.
212 *
213 * @since 4.0.0
214 *
215 * @param string $icon SVG icon name or markup.
216 * @param int $width Optional. Icon width.
217 * @param int $height Optional. Icon height.
218 *
219 * @return $this
220 */
221 public function icon( string $icon, int $width = 16, int $height = 16 ): self {
222 $this->icon = $icon;
223 $this->icon_width = $width;
224 $this->icon_height = $height;
225 return $this;
226 }
227
228 /**
229 * Get the final badge HTML.
230 *
231 * @since 4.0.0
232 *
233 * @return string HTML output.
234 */
235 public function get(): string {
236 $classes = ! empty( $this->variant )
237 ? sprintf( 'tutor-badge tutor-badge-%s', esc_attr( $this->variant ) )
238 : 'tutor-badge';
239
240 if ( $this->rounded ) {
241 $classes .= ' tutor-badge-rounded';
242 }
243
244 // Merge with any custom class attribute.
245 $this->attributes['class'] = trim( "{$classes} " . ( $this->attributes['class'] ?? '' ) );
246
247 $attributes = $this->get_attributes_string();
248
249 // Build icon HTML if exists.
250 $icon_html = '';
251 if ( ! empty( $this->icon ) ) {
252 if ( false !== strpos( $this->icon, '<svg' ) ) {
253 $icon_html = $this->icon;
254 } elseif ( filter_var( $this->icon, FILTER_VALIDATE_URL ) !== false ) {
255 $icon_html = sprintf( '<img src="%s" width="%s" height="%s" alt="%s" />', esc_url( $this->icon ), esc_attr( $this->icon_width ), esc_attr( $this->icon_height ), esc_attr__( 'Badge Icon', 'tutor' ) );
256 } else {
257 $icon_html = SvgIcon::make()->name( $this->icon )->width( $this->icon_width )->height( $this->icon_height )->get();
258 }
259 }
260
261 // Build content.
262 $content = sprintf(
263 '%s%s',
264 $icon_html,
265 esc_html( $this->label )
266 );
267
268 $this->component_string = sprintf(
269 '<span %s>%s</span>',
270 $attributes,
271 $content
272 );
273
274 return $this->component_string;
275 }
276 }
277