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.0 3.4.1 3.4.2 3.5.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 / Avatar.php
tutor / components Last commit date
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
Avatar.php
366 lines
1 <?php
2 /**
3 * Avatar Component Class.
4 *
5 * @package Tutor\Components
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 4.0.0
9 */
10
11 namespace Tutor\Components;
12
13 use Tutor\Components\Constants\Size;
14 use Tutor\Cache\TutorCache;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Class Avatar
20 *
21 * Responsible for rendering user avatars using either an image or initials.
22 * Supports various sizes, border styles, and radius options.
23 *
24 * Example usage:
25 *
26 * ```php
27 * // Avatar from a WP user ID (auto-resolves photo or initials)
28 * Avatar::make()
29 * ->user( $user_id )
30 * ->size( Size::SIZE_56 )
31 * ->render();
32 *
33 * // Avatar from a WP_User object
34 * Avatar::make()
35 * ->user( $user )
36 * ->size( Size::SIZE_40 )
37 * ->bordered()
38 * ->render();
39 *
40 * // Avatar with explicit image URL
41 * Avatar::make()
42 * ->src( 'https://example.com/avatar.jpg' )
43 * ->alt( 'John Doe' )
44 * ->size( Size::SIZE_32 )
45 * ->render();
46 *
47 * // Avatar with initials fallback only
48 * Avatar::make()
49 * ->initials( 'JD' )
50 * ->size( Size::SIZE_48 )
51 * ->render();
52 *
53 * // Avatar with square shape (no border-radius)
54 * Avatar::make()
55 * ->user( $user_id )
56 * ->size( Size::SIZE_64 )
57 * ->shape( 'square' )
58 * ->render();
59 *
60 * // Avatar with border and custom attributes
61 * Avatar::make()
62 * ->src( 'https://example.com/pic.jpg' )
63 * ->size( Size::SIZE_20 )
64 * ->bordered()
65 * ->attr( 'data-user-id', $user_id )
66 * ->render();
67 *
68 * // Retrieve HTML string without echoing
69 * $html = Avatar::make()->user( $user_id )->size( Size::SIZE_24 )->get();
70 * ```
71 *
72 * @since 4.0.0
73 */
74 class Avatar extends BaseComponent {
75
76 /**
77 * Avatar size (20, 24, etc).
78 *
79 * @since 4.0.0
80 *
81 * @see Size constants
82 *
83 * @var string
84 */
85 protected $size = Size::SIZE_56;
86
87 /**
88 * Avatar type (image or initials).
89 *
90 * @since 4.0.0
91 *
92 * @var string
93 */
94 protected $type = 'image';
95
96 /**
97 * Avatar image URL.
98 *
99 * @since 4.0.0
100 * @var string|null
101 */
102 protected $src = null;
103
104 /**
105 * User initials.
106 *
107 * @since 4.0.0
108 *
109 * @var string|null
110 */
111 protected $initials = null;
112
113 /**
114 * User object or ID.
115 *
116 * @since 4.0.0
117 *
118 * @var int|object|null
119 */
120 protected $user = null;
121
122 /**
123 * Avatar alt text.
124 *
125 * @since 4.0.0
126 *
127 * @var string|null
128 */
129 protected $alt = null;
130
131 /**
132 * Border enabled flag.
133 *
134 * @since 4.0.0
135 *
136 * @var bool
137 */
138 protected $bordered = false;
139
140 /**
141 * Avatar Shape
142 *
143 * @since 4.0.0
144 *
145 * @var string
146 */
147 protected $shape = '';
148
149 /**
150 * Set the avatar size.
151 *
152 * @since 4.0.0
153 *
154 * @param string $size Avatar size, see size constants.
155 *
156 * @return $this
157 */
158 public function size( string $size ): self {
159 $this->size = sanitize_html_class( $size );
160 return $this;
161 }
162
163 /**
164 * Set the avatar type (image or initials).
165 *
166 * @since 4.0.0
167 *
168 * @param string $type Avatar type.
169 *
170 * @return $this
171 */
172 public function type( string $type ): self {
173 $this->type = in_array( $type, array( 'image', 'initials' ), true ) ? $type : 'image';
174 return $this;
175 }
176
177 /**
178 * Set the avatar shape ('' or square).
179 *
180 * @since 4.0.0
181 *
182 * @param string $shape Avatar shape.
183 *
184 * @return $this
185 */
186 public function shape( string $shape = '' ): self {
187 $this->shape = $shape;
188 return $this;
189 }
190
191 /**
192 * Set the image URL for avatar.
193 *
194 * @since 4.0.0
195 *
196 * @param string $src Image URL.
197 * @return $this
198 */
199 public function src( string $src ): self {
200 $this->src = esc_url_raw( $src );
201 return $this;
202 }
203
204 /**
205 * Set initials for the avatar.
206 *
207 * @since 4.0.0
208 *
209 * @param string $initials User initials.
210 * @return $this
211 */
212 public function initials( string $initials ): self {
213 $this->initials = strtoupper( sanitize_text_field( $initials ) );
214 return $this;
215 }
216
217 /**
218 * Set alt text for the avatar.
219 *
220 * @since 4.0.0
221 *
222 * @param string $alt Alt text.
223 * @return $this
224 */
225 public function alt( string $alt ): self {
226 $this->alt = $alt;
227 return $this;
228 }
229
230 /**
231 * Set user for the avatar.
232 *
233 * @since 4.0.0
234 *
235 * @param int|object $user User ID or object.
236 * @return $this
237 */
238 public function user( $user ): self {
239 if ( ! $user ) {
240 return $this;
241 }
242
243 $user_id = is_object( $user ) ? $user->ID : (int) $user;
244 $cache_key = 'avatar_component_user_data_' . $user_id;
245 $cache_data = TutorCache::get( $cache_key );
246
247 if ( false !== $cache_data ) {
248 if ( $cache_data['src'] ) {
249 $this->src( $cache_data['src'] );
250 }
251 $this->type( $cache_data['type'] );
252 $this->initials( $cache_data['initials'] );
253 $this->alt( $cache_data['alt'] );
254 return $this;
255 }
256
257 if ( ! is_object( $user ) ) {
258 $user = get_userdata( $user_id );
259 }
260
261 if ( is_a( $user, 'WP_User' ) ) {
262 $profile_photo = get_user_meta( $user->ID, '_tutor_profile_photo', true );
263 $avatar_src = '';
264
265 if ( $profile_photo ) {
266 $url = wp_get_attachment_image_url( $profile_photo, 'thumbnail' );
267 if ( $url ) {
268 $avatar_src = $url;
269 }
270 }
271
272 // Generate initials.
273 $name = $user->display_name;
274 $arr = explode( ' ', trim( $name ) );
275 $first_char = ! empty( $arr[0] ) ? mb_substr( $arr[0], 0, 1, 'UTF-8' ) : '';
276 $second_char = ! empty( $arr[1] ) ? mb_substr( $arr[1], 0, 1, 'UTF-8' ) : '';
277 $initials = $first_char . $second_char;
278
279 $data = array(
280 'src' => $avatar_src,
281 'type' => $avatar_src ? 'image' : 'initials',
282 'initials' => $initials,
283 'alt' => $name,
284 );
285
286 // Store in cache.
287 TutorCache::set( $cache_key, $data );
288
289 // Apply to current instance.
290 $this->src( $data['src'] );
291 $this->type( $data['type'] );
292 $this->initials( $data['initials'] );
293 $this->alt( $data['alt'] );
294 }
295
296 return $this;
297 }
298
299 /**
300 * Enable or disable border.
301 *
302 * @since 4.0.0
303 *
304 * @param bool $bordered Whether avatar has border.
305 * @return $this
306 */
307 public function bordered( bool $bordered = true ): self {
308 $this->bordered = $bordered;
309 return $this;
310 }
311
312 /**
313 * Get the avatar HTML.
314 *
315 * @since 4.0.0
316 *
317 * @return string HTML output.
318 */
319 public function get(): string {
320 $classes = array(
321 'tutor-avatar',
322 'tutor-avatar-' . esc_attr( $this->size ),
323 );
324
325 if ( $this->bordered ) {
326 $classes[] = 'tutor-avatar-border';
327 }
328
329 if ( ! empty( $this->shape ) ) {
330 $classes[] = 'tutor-avatar-' . esc_attr( $this->shape );
331 }
332
333 $this->attributes['class'] = trim( implode( ' ', $classes ) . ' ' . ( $this->attributes['class'] ?? '' ) );
334
335 $attributes = $this->get_attributes_string();
336
337 if ( 'image' === $this->type && $this->src ) {
338 $initials_html = sprintf(
339 '<span class="tutor-avatar-initials">%s</span>',
340 esc_html( $this->initials ?? '' )
341 );
342
343 $image_html = sprintf(
344 '<img src="%1$s" alt="%2$s" class="tutor-avatar-image" loading="lazy" onerror="this.style.display=\'none\'" />',
345 esc_url( $this->src ),
346 esc_attr( $this->alt ?? $this->initials ?? _x( 'User Avatar', 'image alter text', 'tutor' ) )
347 );
348
349 $content = $initials_html . $image_html;
350 } else {
351 $content = sprintf(
352 '<span class="tutor-avatar-initials">%s</span>',
353 esc_html( $this->initials ?? '' )
354 );
355 }
356
357 $this->component_string = sprintf(
358 '<div %1$s>%2$s</div>',
359 $attributes,
360 $content
361 );
362
363 return $this->component_string;
364 }
365 }
366