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 |