PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.8.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.8.0
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.0, at app/Models/User.php

808 lines 25.7 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
555 $documentAccess = Arr::get($space->settings, 'document_access');
556 $mediaAccess = Arr::get($space->settings, 'media_access');
557
558 if (!$role) {
559 $permissions = [
560 'can_create_post' => false,
561 'registered' => true,
562 'can_comment' => false,
563 'can_view_posts' => true,
564 'can_view_members' => $space->canViewMembers($this),
565 'is_pending' => false,
566 'is_non_member' => true,
567 'can_view_info' => $space->privacy !== 'secret',
568 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
569 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
570 ];
571
572 if ($space->privacy === 'secret' || $space->privacy === 'private') {
573 $permissions['can_view_posts'] = false;
574 $permissions['can_view_members'] = false;
575 $permissions['can_view_documents'] = false;
576 $permissions['can_view_media'] = false;
577 }
578 } elseif ($role == 'pending') {
579 $permissions = [
580 'can_create_post' => false,
581 'registered' => true,
582 'can_view_posts' => true,
583 'can_comment' => false,
584 'can_view_members' => $space->canViewMembers($this),
585 'is_pending' => true,
586 'can_view_info' => $space->privacy !== 'secret',
587 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
588 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
589 ];
590
591 if ($space->privacy === 'secret' || $space->privacy === 'private') {
592 $permissions['can_view_posts'] = false;
593 $permissions['can_view_members'] = false;
594 $permissions['can_view_documents'] = false;
595 $permissions['can_view_media'] = false;
596 }
597 } elseif ($role == 'member' || $role == 'student') {
598 $permissions = [
599 'can_create_post' => $isRestrictedPost ? false : true,
600 'registered' => true,
601 'can_view_posts' => true,
602 'can_view_members' => $space->canViewMembers($this),
603 'can_comment' => true,
604 'can_view_info' => true,
605 'can_view_documents' => $hasDocuments,
606 'can_upload_documents' => $hasDocuments && Arr::get($space->settings, 'document_upload') == 'members_only',
607 'can_view_media' => $hasMediaGallery,
608 ];
609 } else {
610 $isMod = in_array($role, [ 'admin', 'moderator' ]);
611 $isAdmin = $role === 'admin';
612 $permissions = [
613 'can_create_post' => $isRestrictedPost ? $isMod : true,
614 'can_view_posts' => true,
615 'can_view_members' => $isAdmin || $space->canViewMembers($this),
616 'registered' => true,
617 'community_admin' => $isAdmin,
618 'community_moderator' => $isMod,
619 'edit_any_feed' => $isMod,
620 'delete_any_feed' => $isMod,
621 'edit_any_comment' => $isMod,
622 'delete_any_comment' => $isMod,
623 'super_admin' => Helper::isSuperAdmin(),
624 'read' => true,
625 'can_remove_member' => $isAdmin,
626 'can_add_member' => $isAdmin,
627 'can_comment' => $isMod,
628 'can_view_info' => true,
629 'can_view_documents' => $hasDocuments,
630 'can_upload_documents' => $hasDocuments && $isMod,
631 'can_view_media' => $hasMediaGallery,
632 ];
633 }
634
635 $permissions['is_member'] = in_array($role, [ 'admin', 'moderator', 'member', 'student' ]);
636
637 return apply_filters('fluent_community/user/space/permissions', $permissions, $space, $role, $this);
638 }
639
640 public function hasCommunityPermission($permission)
641 {
642 $permissions = $this->getPermissions();
643
644 if (isset($permissions[$permission])) {
645 return $permissions[$permission];
646 }
647
648 return false;
649 }
650
651 public function hasSpacePermission($permission, $space)
652 {
653 $permissions = $this->getSpacePermissions($space);
654
655 if (isset($permissions[$permission])) {
656 return $permissions[$permission];
657 }
658
659 return false;
660 }
661
662 public function verifyCommunityPermission($permission)
663 {
664 if (!$this->hasCommunityPermission($permission)) {
665 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
666 }
667
668 return true;
669 }
670
671 public function verifySpacePermission($permission, $space)
672 {
673 if (!$space || !$this->hasSpacePermission($permission, $space)) {
674 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
675 }
676
677 return true;
678 }
679
680 public function canEditFeed($feed, $throwException = false)
681 {
682 $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('edit_any_feed') || $this->hasSpacePermission('edit_any_feed', $feed->space);
683
684 if (!$result && $throwException) {
685 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
686 }
687
688 return $result;
689 }
690
691 public function canDeleteFeed($feed, $throwException = false)
692 {
693 $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('delete_any_feed') || $this->hasSpacePermission('delete_any_feed', $feed->space);
694
695 if (!$result && $throwException) {
696 throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
697 }
698 return $result;
699 }
700
701 public function can($permission, $space = null)
702 {
703 if ($space) {
704 return $this->hasSpacePermission($permission, $space);
705 }
706
707 return $this->hasCommunityPermission($permission);
708 }
709
710 public function hasPermissionOrInCurrentSpace($permission, $space = null)
711 {
712 if ($this->hasCommunityPermission($permission)) {
713 return true;
714 }
715
716 return $space && $this->hasSpacePermission($permission, $space);
717 }
718
719 public function getUnreadNotificationCount()
720 {
721 return NotificationSubscriber::where('user_id', $this->ID)
722 ->unread()
723 ->count();
724 }
725
726 public function getUnreadNotificationFeedIds()
727 {
728 $ids = Notification::whereHas('subscribers', function ($query) {
729 return $query->where('user_id', $this->ID)
730 ->unread();
731 })->pluck('feed_id')->toArray();
732
733 return array_values(array_unique($ids));
734 }
735
736 public function isVerified()
737 {
738 if ($this->xprofile) {
739 return (bool) $this->xprofile->is_verified;
740 }
741
742 return false;
743 }
744
745 public function syncXProfile($force = false, $useUserName = false)
746 {
747 $exist = XProfile::where('user_id', $this->ID)->first();
748
749 if (($exist && !$force) || ($exist && Utility::getPrivacySetting('enable_user_sync') === 'no')) {
750 return $exist;
751 }
752
753 $data = [
754 'user_id' => $this->ID,
755 'username' => ProfileHelper::generateUserName($this->ID, $useUserName),
756 'display_name' => $this->getDisplayName(),
757 'is_verified' => $this->isVerified() ? 1 : 0,
758 'short_description' => get_user_meta($this->ID, 'description', true),
759 'meta' => [
760 'website' => $this->user_url,
761 'cover_photo' => get_user_meta($this->ID, '_fluent_cover_photo', true),
762 'short_description_rendered' => wp_kses_post(FeedsHelper::mdToHtml(get_user_meta($this->ID, 'description', true))),
763 ],
764 ];
765
766 if ($exist) {
767 $data = array_diff_key($data, [ 'avatar' => true, 'username' => true ]);
768
769 if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
770 $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
771 }
772
773 $data['meta'] = wp_parse_args($exist->meta, $data['meta']);
774 $exist->fill($data);
775 $exist->save();
776 return $exist;
777 }
778
779 $counter = 1;
780 $initialUserName = $data['username'];
781 while (XProfile::where('username', $initialUserName)->first()) {
782 $initialUserName = $data['username'] . '_' . $counter;
783 ++$counter;
784 }
785
786 $data['username'] = $initialUserName;
787 $data['status'] = 'active';
788 if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
789 $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
790 }
791
792 $xprofile = XProfile::create($data);
793 $this->load('xprofile');
794
795 return $xprofile;
796 }
797
798 public function getUserMeta($metaKey, $default = null)
799 {
800 return get_user_meta($this->ID, $metaKey, true) ?? $default;
801 }
802
803 public function getWpUser()
804 {
805 return get_user_by('ID', $this->ID);
806 }
807 }
808