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.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 20 hours ago Accordion.php 20 hours ago Alert.php 20 hours ago AttachmentCard.php 20 hours ago Avatar.php 20 hours ago Badge.php 20 hours ago BaseComponent.php 20 hours ago Button.php 20 hours ago ConfirmationModal.php 20 hours ago CourseFilter.php 20 hours ago DateFilter.php 20 hours ago DropdownFilter.php 20 hours ago EmptyState.php 20 hours ago FileUploader.php 20 hours ago InputField.php 20 hours ago Modal.php 20 hours ago Nav.php 20 hours ago Pagination.php 20 hours ago Popover.php 20 hours ago PreviewTrigger.php 20 hours ago Progress.php 20 hours ago SearchFilter.php 20 hours ago Sorting.php 20 hours ago StarRating.php 20 hours ago StarRatingInput.php 20 hours ago StatusSelect.php 20 hours ago SvgIcon.php 20 hours ago Table.php 20 hours ago Tabs.php 20 hours ago Tooltip.php 20 hours ago WPEditor.php 20 hours ago
Badge.php
239 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 * Badge::make()
24 * ->label( 'Primary' )
25 * ->variant( Badge::PRIMARY )
26 * ->icon( '<svg>...</svg>' )
27 * ->render();
28 *
29 * Badge::make()
30 * ->label( 'Points: 20' )
31 * ->rounded()
32 * ->render();
33 * ```
34 *
35 * @since 4.0.0
36 */
37 class Badge extends BaseComponent {
38
39 /**
40 * Variant constants.
41 *
42 * @since 4.0.0
43 */
44 public const INFO = 'info';
45 public const PRIMARY = 'primary';
46 public const WARNING = 'warning';
47 public const SUCCESS = 'success';
48 public const SUCCESS_SOLID = 'success-solid';
49 public const ERROR = 'error';
50 public const HIGHLIGHT = 'highlight';
51 public const DISABLED = 'disabled';
52
53 /**
54 * Badge label text.
55 *
56 * @since 4.0.0
57 *
58 * @var string
59 */
60 protected $label = '';
61
62 /**
63 * Badge variant style.
64 *
65 * @since 4.0.0
66 *
67 * @see Variant constants.
68 *
69 * @var string
70 */
71 protected $variant = '';
72
73 /**
74 * Whether badge has rounded style.
75 *
76 * @since 4.0.0
77 *
78 * @var bool
79 */
80 protected $rounded = false;
81
82 /**
83 * Badge attributes.
84 *
85 * @since 4.0.0
86 *
87 * @var array
88 */
89 protected $attributes = array();
90
91 /**
92 * The SVG icon markup.
93 *
94 * @since 4.0.0
95 *
96 * @var string
97 */
98 protected $icon = '';
99
100 /**
101 * Sort Icon width.
102 *
103 * @var int
104 */
105 protected $icon_width = 16;
106
107 /**
108 * Sort Icon height.
109 *
110 * @var int
111 */
112 protected $icon_height = 16;
113
114 /**
115 * Prefix content (e.g., subdued text before label).
116 *
117 * @since 4.0.0
118 *
119 * @var string
120 */
121 protected $prefix = '';
122
123 /**
124 * Additional CSS classes.
125 *
126 * @since 4.0.0
127 *
128 * @var array
129 */
130 protected $extra_classes = array();
131
132 /**
133 * Set badge label text.
134 *
135 * @since 4.0.0
136 *
137 * @param string $label Badge label text.
138 *
139 * @return $this
140 */
141 public function label( $label ) {
142 $this->label = $label;
143 return $this;
144 }
145
146 /**
147 * Set badge variant style.
148 *
149 * @since 4.0.0
150 *
151 * @param string $variant Badge variant (primary|info|warning|success|success-solid|error|highlight).
152 *
153 * @return $this
154 */
155 public function variant( $variant ) {
156 $this->variant = esc_attr( $variant );
157 return $this;
158 }
159
160 /**
161 * Enable rounded style for badge.
162 *
163 * @since 4.0.0
164 *
165 * @return $this
166 */
167 public function rounded() {
168 $this->rounded = true;
169 return $this;
170 }
171
172 /**
173 * Set the SVG icon for the badge.
174 *
175 * @since 4.0.0
176 *
177 * @param string $icon SVG icon name or markup.
178 * @param int $width Optional. Icon width.
179 * @param int $height Optional. Icon height.
180 *
181 * @return $this
182 */
183 public function icon( string $icon, int $width = 16, int $height = 16 ): self {
184 $this->icon = $icon;
185 $this->icon_width = $width;
186 $this->icon_height = $height;
187 return $this;
188 }
189
190 /**
191 * Get the final badge HTML.
192 *
193 * @since 4.0.0
194 *
195 * @return string HTML output.
196 */
197 public function get(): string {
198 $classes = ! empty( $this->variant )
199 ? sprintf( 'tutor-badge tutor-badge-%s', esc_attr( $this->variant ) )
200 : 'tutor-badge';
201
202 if ( $this->rounded ) {
203 $classes .= ' tutor-badge-rounded';
204 }
205
206 // Merge with any custom class attribute.
207 $this->attributes['class'] = trim( "{$classes} " . ( $this->attributes['class'] ?? '' ) );
208
209 $attributes = $this->get_attributes_string();
210
211 // Build icon HTML if exists.
212 $icon_html = '';
213 if ( ! empty( $this->icon ) ) {
214 if ( false !== strpos( $this->icon, '<svg' ) ) {
215 $icon_html = $this->icon;
216 } elseif ( filter_var( $this->icon, FILTER_VALIDATE_URL ) !== false ) {
217 $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' ) );
218 } else {
219 $icon_html = SvgIcon::make()->name( $this->icon )->width( $this->icon_width )->height( $this->icon_height )->get();
220 }
221 }
222
223 // Build content.
224 $content = sprintf(
225 '%s%s',
226 $icon_html,
227 esc_html( $this->label )
228 );
229
230 $this->component_string = sprintf(
231 '<span %s>%s</span>',
232 $attributes,
233 $content
234 );
235
236 return $this->component_string;
237 }
238 }
239