Constants
3 weeks ago
Accordion.php
6 days ago
Alert.php
6 days ago
AttachmentCard.php
6 days ago
Avatar.php
6 days ago
Badge.php
6 days ago
BaseComponent.php
3 weeks ago
Button.php
6 days ago
ConfirmationModal.php
6 days ago
CourseFilter.php
6 days ago
DateFilter.php
6 days ago
DropdownFilter.php
6 days ago
EmptyState.php
6 days ago
FileUploader.php
6 days ago
InputField.php
6 days ago
Modal.php
6 days ago
Nav.php
6 days ago
Pagination.php
6 days ago
Popover.php
6 days ago
PreviewTrigger.php
6 days ago
Progress.php
6 days ago
SearchFilter.php
6 days ago
Sorting.php
6 days ago
StarRating.php
6 days ago
StarRatingInput.php
6 days ago
StatusSelect.php
6 days ago
SvgIcon.php
6 days ago
Table.php
6 days ago
Tabs.php
6 days ago
Tooltip.php
6 days ago
WPEditor.php
6 days 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 |