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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 / SvgIcon.php
tutor / components Last commit date
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