Constants
23 hours ago
Accordion.php
23 hours ago
Alert.php
23 hours ago
AttachmentCard.php
23 hours ago
Avatar.php
23 hours ago
Badge.php
23 hours ago
BaseComponent.php
23 hours ago
Button.php
23 hours ago
ConfirmationModal.php
23 hours ago
CourseFilter.php
23 hours ago
DateFilter.php
23 hours ago
DropdownFilter.php
23 hours ago
EmptyState.php
23 hours ago
FileUploader.php
23 hours ago
InputField.php
23 hours ago
Modal.php
23 hours ago
Nav.php
23 hours ago
Pagination.php
23 hours ago
Popover.php
23 hours ago
PreviewTrigger.php
23 hours ago
Progress.php
23 hours ago
SearchFilter.php
23 hours ago
Sorting.php
23 hours ago
StarRating.php
23 hours ago
StarRatingInput.php
23 hours ago
StatusSelect.php
23 hours ago
SvgIcon.php
23 hours ago
Table.php
23 hours ago
Tabs.php
23 hours ago
Tooltip.php
23 hours ago
WPEditor.php
23 hours ago
SvgIcon.php
285 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SvgIcon Component Class. |
| 4 | * |
| 5 | * Provides a fluent builder for rendering SVG icons. |
| 6 | * |
| 7 | * @package Tutor\Components |
| 8 | * @author Themeum |
| 9 | * @link https://themeum.com |
| 10 | * @since 4.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Tutor\Components; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * SvgIcon Component Class. |
| 19 | * |
| 20 | * Example usage: |
| 21 | * ``` |
| 22 | * SvgIcon::make() |
| 23 | * ->name( Icon::CHECK ) |
| 24 | * ->size( 24 ) |
| 25 | * ->render(); |
| 26 | * |
| 27 | * SvgIcon::make() |
| 28 | * ->name( Icon::DELETE ) |
| 29 | * ->size( 16 ) |
| 30 | * ->attr( 'class', 'tutor-icon-secondary' ) |
| 31 | * ->render(); |
| 32 | * |
| 33 | * SvgIcon::make() |
| 34 | * ->name( Icon::DELETE ) |
| 35 | * ->size( 16 ) |
| 36 | * ->color( Color::SECONDARY ) |
| 37 | * ->render(); |
| 38 | * ``` |
| 39 | * |
| 40 | * Note: The color() property only works with the version 4 design system tokens. |
| 41 | * |
| 42 | * @since 4.0.0 |
| 43 | */ |
| 44 | class SvgIcon extends BaseComponent { |
| 45 | |
| 46 | /** |
| 47 | * Icon name. |
| 48 | * |
| 49 | * @since 4.0.0 |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $name = ''; |
| 54 | |
| 55 | /** |
| 56 | * Icon width. |
| 57 | * |
| 58 | * @since 4.0.0 |
| 59 | * |
| 60 | * @var int |
| 61 | */ |
| 62 | protected $width = 16; |
| 63 | |
| 64 | /** |
| 65 | * Icon height. |
| 66 | * |
| 67 | * @since 4.0.0 |
| 68 | * |
| 69 | * @var int |
| 70 | */ |
| 71 | protected $height = 16; |
| 72 | /** |
| 73 | * Icon color. |
| 74 | * |
| 75 | * Note: this only works with the version 4 design system tokens. |
| 76 | * |
| 77 | * @since 4.0.0 |
| 78 | * |
| 79 | * @var string |
| 80 | */ |
| 81 | protected $color = ''; |
| 82 | |
| 83 | /** |
| 84 | * Ignore kids mode variant. |
| 85 | * |
| 86 | * @since 4.0.0 |
| 87 | * |
| 88 | * @var bool |
| 89 | */ |
| 90 | protected $ignore_kids = false; |
| 91 | |
| 92 | /** |
| 93 | * Flip the icon in RTL. |
| 94 | * |
| 95 | * @since 4.0.0 |
| 96 | * |
| 97 | * @var bool |
| 98 | */ |
| 99 | protected $flip_rtl = false; |
| 100 | |
| 101 | /** |
| 102 | * Set icon name. |
| 103 | * |
| 104 | * @since 4.0.0 |
| 105 | * |
| 106 | * @param string $name Icon name. |
| 107 | * |
| 108 | * @return $this |
| 109 | */ |
| 110 | public function name( string $name ): self { |
| 111 | $this->name = $name; |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Set icon width. |
| 117 | * |
| 118 | * @since 4.0.0 |
| 119 | * |
| 120 | * @param int $width Icon width. |
| 121 | * |
| 122 | * @return $this |
| 123 | */ |
| 124 | public function width( int $width ): self { |
| 125 | $this->width = $width; |
| 126 | return $this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Set icon height. |
| 131 | * |
| 132 | * @since 4.0.0 |
| 133 | * |
| 134 | * @param int $height Icon height. |
| 135 | * |
| 136 | * @return $this |
| 137 | */ |
| 138 | public function height( int $height ): self { |
| 139 | $this->height = $height; |
| 140 | return $this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set icon size (both width and height). |
| 145 | * |
| 146 | * @since 4.0.0 |
| 147 | * |
| 148 | * @param int $size Icon size. |
| 149 | * |
| 150 | * @return $this |
| 151 | */ |
| 152 | public function size( int $size ): self { |
| 153 | $this->width = $size; |
| 154 | $this->height = $size; |
| 155 | return $this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set icon color. |
| 160 | * |
| 161 | * Note: this only works with the version 4 design system tokens. |
| 162 | * |
| 163 | * @since 4.0.0 |
| 164 | * |
| 165 | * @param string $color Icon color (use \Tutor\Components\Constants\Color). |
| 166 | * |
| 167 | * @return $this |
| 168 | */ |
| 169 | public function color( string $color ): self { |
| 170 | $this->color = $color; |
| 171 | return $this; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Set to ignore kids variant. |
| 176 | * |
| 177 | * @since 4.0.0 |
| 178 | * |
| 179 | * @param bool $ignore_kids Ignore kids mode variant. |
| 180 | * |
| 181 | * @return $this |
| 182 | */ |
| 183 | public function ignore_kids( bool $ignore_kids = true ): self { |
| 184 | $this->ignore_kids = $ignore_kids; |
| 185 | return $this; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Flip the icon in RTL. |
| 190 | * |
| 191 | * Use this only for directional icons such as arrows, |
| 192 | * chevrons and pagination controls. |
| 193 | * |
| 194 | * @since 4.0.0 |
| 195 | * |
| 196 | * @return $this |
| 197 | */ |
| 198 | public function flip_rtl(): self { |
| 199 | $this->flip_rtl = true; |
| 200 | return $this; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Set custom HTML attribute. |
| 205 | * |
| 206 | * @since 4.0.0 |
| 207 | * |
| 208 | * @param string $key Attribute name. |
| 209 | * @param string $value Attribute value. |
| 210 | * |
| 211 | * @return $this |
| 212 | */ |
| 213 | public function attr( $key, $value ) { |
| 214 | $this->attributes[ $key ] = esc_attr( $value ); |
| 215 | return $this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Get the final icon HTML. |
| 220 | * |
| 221 | * @since 4.0.0 |
| 222 | * |
| 223 | * @return string HTML output. |
| 224 | */ |
| 225 | public function get(): string { |
| 226 | if ( empty( $this->name ) ) { |
| 227 | return ''; |
| 228 | } |
| 229 | |
| 230 | $icon_path = tutor()->path . 'assets/icons/' . $this->name . '.svg'; |
| 231 | |
| 232 | // If learning mode is kids, check kids folder first. |
| 233 | if ( tutor_utils()->is_kids_mode() && ! $this->ignore_kids ) { |
| 234 | $kids_path = tutor()->path . 'assets/icons/kids/' . $this->name . '.svg'; |
| 235 | if ( file_exists( $kids_path ) ) { |
| 236 | $icon_path = $kids_path; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if ( ! file_exists( $icon_path ) ) { |
| 241 | return ''; |
| 242 | } |
| 243 | |
| 244 | $svg = file_get_contents( $icon_path ); |
| 245 | if ( ! $svg ) { |
| 246 | return ''; |
| 247 | } |
| 248 | |
| 249 | preg_match( '/<svg[^>]*viewBox="([^"]+)"[^>]*>(.*?)<\/svg>/is', $svg, $matches ); |
| 250 | if ( ! $matches ) { |
| 251 | return ''; |
| 252 | } |
| 253 | |
| 254 | list( $svg_tag, $view_box, $inner_svg ) = $matches; |
| 255 | |
| 256 | $this->attributes['width'] = $this->width; |
| 257 | $this->attributes['height'] = $this->height; |
| 258 | $this->attributes['viewBox'] = $view_box; |
| 259 | $this->attributes['fill'] = $this->attributes['fill'] ?? 'none'; |
| 260 | $this->attributes['role'] = $this->attributes['role'] ?? 'presentation'; |
| 261 | $this->attributes['aria-hidden'] = $this->attributes['aria-hidden'] ?? 'true'; |
| 262 | |
| 263 | if ( ! empty( $this->color ) ) { |
| 264 | $color_class = "tutor-icon-{$this->color}"; |
| 265 | $this->attributes['class'] = trim( ( $this->attributes['class'] ?? '' ) . " {$color_class}" ); |
| 266 | } |
| 267 | |
| 268 | if ( $this->flip_rtl ) { |
| 269 | $this->attributes['class'] = trim( |
| 270 | ( $this->attributes['class'] ?? '' ) . ' tutor-icon-flip-rtl' |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | $attributes = $this->get_attributes_string(); |
| 275 | |
| 276 | $this->component_string = sprintf( |
| 277 | '<svg %s>%s</svg>', |
| 278 | $attributes, |
| 279 | $inner_svg |
| 280 | ); |
| 281 | |
| 282 | return $this->component_string; |
| 283 | } |
| 284 | } |
| 285 |