PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.8.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.8.1
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Models / User.php

User.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.8.1, at app/Models/User.php

809 lines 25.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Models;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Services\FeedsHelper;
7 use FluentCommunity\App\Services\Helper;
8 use FluentCommunity\App\Services\ProfileHelper;
9 use FluentCommunity\Framework\Support\Arr;
10 use FluentCommunity\Modules\Course\Model\Course;
11 use FluentCommunityPro\App\Models\Follow;
12 use FluentCrm\App\Models\Subscriber;
13
14 /**
15 * User Model - DB Model for WordPress Users Table
16 *
17 * Database Model
18 *
19 * @package FluentCommunity\App\Models
20 *
21 * @version 1.0.0
22 *
23 * @property int $ID
24 * @property string $user_login
25 * @property string $user_pass
26 * @property string $user_nicename
27 * @property string $user_email
28 * @property string $user_url
29 * @property string $user_registered
30 * @property string $user_activation_key
31 * @property int $user_status
32 * @property string $display_name
33 * @property-read string $photo
34 * @property-read XProfile|null $xprofile
35 * @property string|null $username
36 */
37 class User extends Model
38 {
39 protected $table = 'users';
40
41 protected $primaryKey = 'ID';
42
43 protected $hidden = [ 'user_pass', 'user_activation_key' ];
44
45 protected $appends = [ 'photo' ];
46
47 public $timestamps = false;
48
49 protected $searchable = [
50 'display_name',
51 'user_email',
52 ];
53
54 public function scopeSearchBy($query, $search)
55 {
56 if ($search) {
57 $fields = $this->searchable;
58 $query->where(function ($query) use ($fields, $search) {
59 $query->where(array_shift($fields), 'LIKE', "%$search%");
60 foreach ($fields as $field) {
61 $query->orWhere($field, 'LIKE', "$search%");
62 }
63 });
64 }
65
66 return $query;
67 }
68
69 public function scopeMentionBy($query, $search)
70 {
71 if ($search) {
72 $fields = [
73 'display_name',
74 'user_login',
75 ];
76 $query->where(function ($query) use ($fields, $search) {
77 $query->where(array_shift($fields), 'LIKE', "$search%");
78 foreach ($fields as $field) {
79 $query->orWhere($field, 'LIKE', "$search%");
80 }
81 });
82 }
83
84 return $query;
85 }
86
87 /**
88 * Accessor to get dynamic photo attribute
89 * @return string
90 */
91 public function getPhotoAttribute()
92 {
93 if ($photo = get_user_meta($this->ID, '_fcom_user_photo', true)) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
94 return $photo;
95 }
96
97 if ($contact = $this->getContact()) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
98 return $contact->photo;
99 }
100
101 if (empty($this->attributes['user_email'])) {
102 if (defined('FLUENTCRM')) {
103 $contact = Subscriber::where('user_id', $this->ID)->first();
104 if ($contact) {
105 return $contact->photo;
106 }
107 }
108 return '';
109 }
110
111 if (Utility::getPrivacySetting('enable_gravatar') == 'no') {
112 return apply_filters('fluent_community/default_avatar', FLUENT_COMMUNITY_PLUGIN_URL . 'assets/images/placeholder.png', $this->ID);
113 }
114
115 $hash = md5(strtolower(trim($this->attributes['user_email'])));
116
117 /**
118 * Gravatar URL by Email
119 *
120 * @return string $gravatar url of the gravatar image
121 */
122 $name = $this->attributes['display_name'];
123
124 $fallback = '';
125 if ($name) {
126 $fallback = '&d=https%3A%2F%2Fui-avatars.com%2Fapi%2F' . urlencode($name) . '/128';
127 }
128
129 return apply_filters('fluent_crm/get_avatar',
130 "https://www.gravatar.com/avatar/{$hash}?s=128" . $fallback,
131 $this->attributes['user_email']
132 );
133 }
134
135 public function getIsVerifiedAttribute()
136 {
137 return $this->isVerified();
138 }
139
140 public function getContact()
141 {
142 if (!defined('FLUENTCRM')) {
143 return null;
144 }
145
146 if ($this->user_email) {
147 return Subscriber::where('user_id', $this->ID)
148 ->orWhere('email', $this->user_email)
149 ->first();
150 }
151
152 return Subscriber::where('user_id', $this->ID)
153 ->first();
154 }
155
156 public function xprofile()
157 {
158 return $this->belongsTo(XProfile::class, 'ID', 'user_id');
159 }
160
161 // Relationship: Users this user follows
162 public function follows()
163 {
164 return $this->hasMany(Follow::class, 'follower_id', 'ID');
165
166 }
167
168 // Relationship: Users following this user
169 public function followers()
170 {
171 return $this->hasMany(Follow::class, 'followed_id', 'ID');
172 }
173
174 public function usermeta()
175 {
176 return $this->hasMany(UserMeta::class, 'user_id', 'ID');
177 }
178
179 public function messages()
180 {
181 return $this->hasMany(\FluentMessaging\App\Models\Message::class, 'user_id', 'ID');
182 }
183
184 public function getGeneralData()
185 {
186 $user = get_user_by('ID', $this->ID);
187
188 $fullName = '';
189 if ($user->first_name || $user->last_name) {
190 $fullName = trim($user->first_name . ' ' . $user->last_name);
191 }
192
193 if (!$fullName) {
194 $fullName = $user->display_name;
195 }
196
197 $contact = $this->getContact();
198
199 return [
200 'is_contact' => (bool)$contact,
201 'first_name' => $user->first_name,
202 'last_name' => $user->last_name,
203 'full_name' => $fullName,
204 'display_name' => $fullName,
205 'bio' => $user->description,
206 'website' => $user->user_url,
207 'id' => $user->ID,
208 'user_id' => $user->ID,
209 'created_at' => $user->user_registered,
210 'photo' => $this->photo,
211 'username' => $this->username,
212 'is_verified' => $this->isVerified(),
213 ];
214 }
215
216 public function spaces()
217 {
218 return $this->belongsToMany(BaseSpace::class, 'fcom_space_user', 'user_id', 'space_id')
219 ->withPivot([ 'role', 'status', 'created_at' ]);
220 }
221
222 public function courses()
223 {
224 return $this->belongsToMany(Course::class, 'fcom_space_user', 'user_id', 'space_id')
225 ->withPivot([ 'role', 'created_at' ]);
226 }
227
228 public function notificationSubscriptions()
229 {
230 return $this->hasMany(NotificationSubscription::class, 'user_id');
231 }
232
233 public function space_pivot()
234 {
235 return $this->belongsTo(SpaceUserPivot::class, 'ID', 'user_id')->withoutGlobalScopes();
236 }
237
238 public function notification_records()
239 {
240 return $this->hasMany(NotificationSubscriber::class, 'user_id', 'ID')->withoutGlobalScopes();
241 }
242
243 public function crm_contact()
244 {
245 return $this->belongsTo(Contact::class, 'ID', 'user_id')->withoutGlobalScopes();
246 }
247
248 public function community_role()
249 {
250 return $this->belongsTo(Meta::class, 'ID', 'object_id')
251 ->where('object_type', 'user')
252 ->where('meta_key', '_user_community_roles');
253 }
254
255 public function updateCustomData($updateData, $removeSrc = false)
256 {
257 if (isset($updateData['first_name']) && Utility::getPrivacySetting('enable_user_sync') === 'yes') {
258 $firstName = sanitize_text_field($updateData['first_name']);
259 $lastName = sanitize_text_field($updateData['last_name']);
260
261 $userData = [
262 'first_name' => $firstName,
263 'last_name' => $lastName,
264 'display_name' => trim($firstName . ' ' . $lastName),
265 'description' => isset($updateData['short_description']) ? sanitize_textarea_field($updateData['short_description']) : '',
266 'user_url' => isset($updateData['website']) ? esc_url($updateData['website']) : '',
267 'ID' => $this->ID,
268 ];
269
270 wp_update_user($userData);
271
272 if ($contact = $this->getContact()) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
273 $contact->fill(array_filter([
274 'first_name' => $firstName,
275 'last_name' => $lastName,
276 ]));
277
278 $dirtyFields = $contact->getDirty();
279 if ($dirtyFields) {
280 $contact->save();
281 do_action('fluent_crm/contact_updated', $contact, $dirtyFields);
282 }
283
284 }
285 }
286
287 return $this;
288 }
289
290 // WP account real name (first + last, falling back to wp_users.display_name). Not the public community name — use getPublicDisplayName() for anything shown to other members.
291 public function getDisplayName()
292 {
293 $user = get_user_by('ID', $this->ID);
294 $name = '';
295 if ($user->first_name || $user->last_name) {
296 $name = trim($user->first_name . ' ' . $user->last_name);
297 }
298 if ($name) {
299 return $name;
300 }
301
302 return $user->display_name;
303 }
304
305 // Public community name (xprofile.display_name, falling back to wp_users.display_name). Use this for actor names in notifications/emails to avoid leaking the legal name.
306 public function getPublicDisplayName()
307 {
308 $name = $this->xprofile ? $this->xprofile->display_name : '';
309 if (!$name) {
310 $name = $this->display_name;
311 }
312
313 return apply_filters('fluent_community/public_display_name', $name, $this);
314 }
315
316 public function getCustomMeta($key, $default = null)
317 {
318 $exist = Meta::where('object_type', 'user')
319 ->where('object_id', $this->ID)
320 ->where('meta_key', $key)
321 ->first();
322
323 if ($exist && $exist->value) {
324 return $exist->value;
325 }
326
327 return $default;
328 }
329
330 public function updateCustomMeta($key, $value)
331 {
332 $exist = Meta::where('object_type', 'user')
333 ->where('object_id', $this->ID)
334 ->where('meta_key', $key)
335 ->first();
336
337 if ($exist) {
338 $exist->value = $value;
339 $exist->save();
340 return $exist;
341 }
342
343 return Meta::create([
344 'object_type' => 'user',
345 'object_id' => $this->ID,
346 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
347 'value' => $value,
348 ]);
349 }
350
351 public function isNotMemberOfAnySpace()
352 {
353 return $this->spaces()->count() == 0;
354 }
355
356 public function getSpaceIds($cached = true)
357 {
358 if ($cached) {
359 $ids = get_user_meta($this->ID, '_fcom_space_ids', true);
360 if (!$ids || !is_array($ids)) {
361 $ids = [];
362 }
363 return $ids;
364 }
365
366 $this->cacheAccessSpaces();
367
368 return get_user_meta($this->ID, '_fcom_space_ids', true);
369 }
370
371 public function getJoinedSpaceIds()
372 {
373 $globalRoles = $this->getCommunityRoles();
374
375 if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) {
376 return BaseSpace::onlyMain()->pluck('id')->toArray();
377 }
378
379 return BaseSpace::onlyMain()->whereHas('members', function ($query) {
380 $query->where('user_id', $this->ID)
381 ->where('status', 'active');
382 })->pluck('id')->toArray();
383 }
384
385 public function getCommunityRoles()
386 {
387 if (Helper::isSuperAdmin($this->ID)) {
388 return [ 'admin' ];
389 }
390
391 return (array)$this->getCustomMeta('_user_community_roles', []);
392 }
393
394 public function isCommunityAdmin()
395 {
396 return in_array('admin', $this->getCommunityRoles());
397 }
398
399 public function isCommunityModerator()
400 {
401 return (bool)array_intersect([ 'moderator', 'admin' ], $this->getCommunityRoles());
402 }
403
404 public function hasCommunityModeratorAccess()
405 {
406 $permissions = $this->getPermissions(true);
407
408 return Arr::isTrue($permissions, 'community_moderator');
409 }
410
411 public function hasCommunityAdminAccess()
412 {
413 $permissions = $this->getPermissions(true);
414
415 return Arr::isTrue($permissions, 'community_admin');
416 }
417
418 public function hasCourseCreatorAccess()
419 {
420 $permissions = $this->getPermissions(true);
421
422 return Arr::isTrue($permissions, 'course_creator');
423 }
424
425 public function isSpaceModerator()
426 {
427 return $this->hasCourseCreatorAccess() || $this->hasCommunityModeratorAccess();
428 }
429
430 public function hasSpaceManageAccess()
431 {
432 $permissions = array_filter($this->getPermissions(true));
433
434 return (bool)array_intersect([ 'community_admin', 'course_admin' ], array_keys($permissions));
435 }
436
437 public function getSpaceRole($space)
438 {
439 $globalRoles = $this->getCommunityRoles();
440
441 if ($globalRoles) {
442 if (in_array('admin', $globalRoles)) {
443 return 'admin';
444 }
445 }
446
447 if ($space) {
448 $membership = $space->getMembership($this->ID);
449
450 if ($membership) {
451 $role = $membership->pivot->role;
452 $status = $membership->pivot->status;
453
454 if ($role != 'member') {
455 return $role;
456 }
457
458 if (!in_array('moderator', $globalRoles)) {
459 if ($status == 'pending') {
460 return 'pending';
461 }
462 return $role;
463 }
464 }
465 }
466
467 if (in_array('moderator', $globalRoles)) {
468 return 'moderator';
469 }
470
471 return '';
472 }
473
474 public function cacheAccessSpaces()
475 {
476 $globalRoles = $this->getCommunityRoles();
477
478 if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) {
479 $spaces = BaseSpace::onlyMain()->get();
480 } else {
481 $spaces = BaseSpace::onlyMain()->where(function ($spaceQuery) {
482 $spaceQuery->whereHas('members', function ($memberQuery) {
483 $memberQuery->where('user_id', $this->ID)
484 ->where('status', 'active');
485 })->orWhere('privacy', 'public');
486 })->get();
487 }
488
489 $spaceIds = $spaces->pluck('id')->toArray();
490
491 update_user_meta($this->ID, '_fcom_space_ids', $spaceIds);
492
493 return $spaces;
494 }
495
496 protected function getRolePermissions($roles)
497 {
498 if (!$roles) {
499 return apply_filters('fluent_community/user/permissions', [
500 'read' => true,
501 ], $roles, $this);
502 }
503
504 $isAdmin = in_array('admin', $roles);
505 $isModerator = (bool)array_intersect($roles, [ 'admin', 'moderator' ]);
506
507 $permissions = [
508 'admin' => $isAdmin,
509 'super_admin' => Helper::isSuperAdmin(),
510 'community_admin' => $isAdmin,
511 'community_moderator' => $isModerator,
512 'delete_any_feed' => $isModerator,
513 'edit_any_feed' => $isModerator,
514 'delete_any_comment' => $isModerator,
515 'edit_any_comment' => $isModerator,
516 'read' => true,
517 ];
518
519 if ($isAdmin || in_array('course_admin', $roles)) {
520 $permissions['course_creator'] = true;
521 $permissions['course_admin'] = true;
522 } elseif (in_array('course_creator', $roles)) {
523 $permissions['course_creator'] = true;
524 }
525
526 return apply_filters('fluent_community/user/permissions', $permissions, $roles, $this);
527 }
528
529 public function getPermissions($cached = true)
530 {
531 static $permissions = [];
532
533 if ($cached && isset($permissions[$this->ID])) {
534 return $permissions[$this->ID];
535 }
536
537 $roles = $this->getCommunityRoles();
538
539 return $permissions[$this->ID] = $this->getRolePermissions($roles);
540 }
541
542 public function getSpacePermissions($space)
543 {
544 if (!$space) {
545 return [];
546 }
547
548 $role = $this->getSpaceRole($space);
549
550 $hasDocuments = defined('FLUENT_COMMUNITY_PRO') && Arr::get($space->settings, 'document_library') == 'yes';
551 $hasMediaGallery = defined('FLUENT_COMMUNITY_PRO') && Arr::get($space->settings, 'media_gallery') == 'yes';
552
553 $isRestrictedPost = Arr::get($space->settings, 'restricted_post_only') == 'yes';
554 $isVerifiedPostOnly = Arr::get($space->settings, 'verified_post_only') == 'yes';
555
556 $documentAccess = Arr::get($space->settings, 'document_access');
557 $mediaAccess = Arr::get($space->settings, 'media_access');
558
559 if (!$role) {
560 $permissions = [
561 'can_create_post' => false,
562 'registered' => true,
563 'can_comment' => false,
564 'can_view_posts' => true,
565 'can_view_members' => $space->canViewMembers($this),
566 'is_pending' => false,
567 'is_non_member' => true,
568 'can_view_info' => $space->privacy !== 'secret',
569 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
570 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
571 ];
572
573 if ($space->privacy === 'secret' || $space->privacy === 'private') {
574 $permissions['can_view_posts'] = false;
575 $permissions['can_view_members'] = false;
576 $permissions['can_view_documents'] = false;
577 $permissions['can_view_media'] = false;
578 }
579 } elseif ($role == 'pending') {
580 $permissions = [
581 'can_create_post' => false,
582 'registered' => true,
583 'can_view_posts' => true,
584 'can_comment' => false,
585 'can_view_members' => $space->canViewMembers($this),
586 'is_pending' => true,
587 'can_view_info' => $space->privacy !== 'secret',
588 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
589 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
590 ];
591
592 if ($space->privacy === 'secret' || $space->privacy === 'private') {
593 $permissions['can_view_posts'] = false;
594 $permissions['can_view_members'] = false;
595 $permissions['can_view_documents'] = false;
596 $permissions['can_view_media'] = false;
597 }
598 } elseif ($role == 'member' || $role == 'student') {
599 $permissions = [
600 'can_create_post' => $isRestrictedPost ? false : (!$isVerifiedPostOnly || $this->isVerified()),
601 'registered' => true,
602 'can_view_posts' => true,
603 'can_view_members' => $space->canViewMembers($this),
604 'can_comment' => true,
605 'can_view_info' => true,
606 'can_view_documents' => $hasDocuments,
607 'can_upload_documents' => $hasDocuments && Arr::get($space->settings, 'document_upload') == 'members_only',
608 'can_view_media' => $hasMediaGallery,
609 ];
610 } else {
611 $isMod = in_array($role, [ 'admin', 'moderator' ]);
612 $isAdmin = $role === 'admin';
613 $permissions = [
614 'can_create_post' => $isRestrictedPost ? $isMod : true,
615 'can_view_posts' => true,
616 'can_view_members' => $isAdmin || $space->canViewMembers($this),
617 'registered' => true,
618 'community_admin' => $isAdmin,
619 'community_moderator' => $isMod,
620 'edit_any_feed' => $isMod,
621 'delete_any_feed' => $isMod,
622 'edit_any_comment' => $isMod,
623 'delete_any_comment' => $isMod,
624 'super_admin' => Helper::isSuperAdmin(),
625 'read' => true,
626 'can_remove_member' => $isAdmin,
627 'can_add_member' => $isAdmin,
628 'can_comment' => $isMod,
629 'can_view_info' => true,
630 'can_view_documents' => $hasDocuments,
631 'can_upload_documents' => $hasDocuments && $isMod,
632 'can_view_media' => $hasMediaGallery,
633 ];
634 }
635
636 $permissions['is_member'] = in_array($role, [ 'admin', 'moderator', 'member', 'student' ]);
637
638 return apply_filters('fluent_community/user/space/permissions', $permissions, $space, $role, $this);
639 }
640
641 public function hasCommunityPermission($permission)
642 {
643 $permissions = $this->getPermissions();
644
645 if (isset($permissions[$permission])) {
646 return $permissions[$permission];
647 }
648
649 return false;
650 }
651
652 public function hasSpacePermission($permission, $space)
653 {
654 $permissions = $this->getSpacePermissions($space);
655
656 if (isset($permissions[$permission])) {
657 return $permissions[$permission];
658 }
659
660 return false;
661 }
662
663 public function verifyCommunityPermission($permission)
664 {
665 if (!$this->hasCommunityPermission($permission)) {
666 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
667 }
668
669 return true;
670 }
671
672 public function verifySpacePermission($permission, $space)
673 {
674 if (!$space || !$this->hasSpacePermission($permission, $space)) {
675 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
676 }
677
678 return true;
679 }
680
681 public function canEditFeed($feed, $throwException = false)
682 {
683 $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('edit_any_feed') || $this->hasSpacePermission('edit_any_feed', $feed->space);
684
685 if (!$result && $throwException) {
686 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
687 }
688
689 return $result;
690 }
691
692 public function canDeleteFeed($feed, $throwException = false)
693 {
694 $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('delete_any_feed') || $this->hasSpacePermission('delete_any_feed', $feed->space);
695
696 if (!$result && $throwException) {
697 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
698 }
699 return $result;
700 }
701
702 public function can($permission, $space = null)
703 {
704 if ($space) {
705 return $this->hasSpacePermission($permission, $space);
706 }
707
708 return $this->hasCommunityPermission($permission);
709 }
710
711 public function hasPermissionOrInCurrentSpace($permission, $space = null)
712 {
713 if ($this->hasCommunityPermission($permission)) {
714 return true;
715 }
716
717 return $space && $this->hasSpacePermission($permission, $space);
718 }
719
720 public function getUnreadNotificationCount()
721 {
722 return NotificationSubscriber::where('user_id', $this->ID)
723 ->unread()
724 ->count();
725 }
726
727 public function getUnreadNotificationFeedIds()
728 {
729 $ids = Notification::whereHas('subscribers', function ($query) {
730 return $query->where('user_id', $this->ID)
731 ->unread();
732 })->pluck('feed_id')->toArray();
733
734 return array_values(array_unique($ids));
735 }
736
737 public function isVerified()
738 {
739 if ($this->xprofile) {
740 return (bool) $this->xprofile->is_verified;
741 }
742
743 return false;
744 }
745
746 public function syncXProfile($force = false, $useUserName = false)
747 {
748 $exist = XProfile::where('user_id', $this->ID)->first();
749
750 if (($exist && !$force) || ($exist && Utility::getPrivacySetting('enable_user_sync') === 'no')) {
751 return $exist;
752 }
753
754 $data = [
755 'user_id' => $this->ID,
756 'username' => ProfileHelper::generateUserName($this->ID, $useUserName),
757 'display_name' => $this->getDisplayName(),
758 'is_verified' => $this->isVerified() ? 1 : 0,
759 'short_description' => get_user_meta($this->ID, 'description', true),
760 'meta' => [
761 'website' => $this->user_url,
762 'cover_photo' => get_user_meta($this->ID, '_fluent_cover_photo', true),
763 'short_description_rendered' => wp_kses_post(FeedsHelper::mdToHtml(get_user_meta($this->ID, 'description', true))),
764 ],
765 ];
766
767 if ($exist) {
768 $data = array_diff_key($data, [ 'avatar' => true, 'username' => true ]);
769
770 if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
771 $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
772 }
773
774 $data['meta'] = wp_parse_args($exist->meta, $data['meta']);
775 $exist->fill($data);
776 $exist->save();
777 return $exist;
778 }
779
780 $counter = 1;
781 $initialUserName = $data['username'];
782 while (XProfile::where('username', $initialUserName)->first()) {
783 $initialUserName = $data['username'] . '_' . $counter;
784 ++$counter;
785 }
786
787 $data['username'] = $initialUserName;
788 $data['status'] = 'active';
789 if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
790 $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
791 }
792
793 $xprofile = XProfile::create($data);
794 $this->load('xprofile');
795
796 return $xprofile;
797 }
798
799 public function getUserMeta($metaKey, $default = null)
800 {
801 return get_user_meta($this->ID, $metaKey, true) ?? $default;
802 }
803
804 public function getWpUser()
805 {
806 return get_user_by('ID', $this->ID);
807 }
808 }
809