PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.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
← All changes | app/Models/User.php +256 -108 1.0.982.10.0 View file →
@@ -1,12 +1,15 @@
1 1 <?php
2 2
3 3 namespace FluentCommunity\App\Models;
4 4
5 +use FluentCommunity\App\Functions\Utility;
6 +use FluentCommunity\App\Services\FeedsHelper;
5 7 use FluentCommunity\App\Services\Helper;
6 8 use FluentCommunity\App\Services\ProfileHelper;
7 9 use FluentCommunity\Framework\Support\Arr;
8 10 use FluentCommunity\Modules\Course\Model\Course;
11 +use FluentCommunityPro\App\Models\Follow;
9 12 use FluentCrm\App\Models\Subscriber;
10 13
11 14 /**
12 15 * User Model - DB Model for WordPress Users Table
@@ -12,11 +15,25 @@
12 15 * User Model - DB Model for WordPress Users Table
13 16 *
14 17 * Database Model
15 18 *
16 - * @package FluentCrm\App\Models
19 + * @package FluentCommunity\App\Models
17 20 *
18 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
19 36 */
20 37 class User extends Model
21 38 {
22 39 protected $table = 'users';
@@ -22,15 +39,17 @@
22 39 protected $table = 'users';
23 40
24 41 protected $primaryKey = 'ID';
25 42
26 - protected $hidden = ['user_pass', 'user_activation_key'];
43 + protected $hidden = [ 'user_pass', 'user_activation_key' ];
27 44
28 - protected $appends = ['photo'];
45 + protected $appends = [ 'photo' ];
29 46
47 + public $timestamps = false;
48 +
30 49 protected $searchable = [
31 50 'display_name',
32 - 'user_email'
51 + 'user_email',
33 52 ];
34 53
35 54 public function scopeSearchBy($query, $search)
36 55 {
@@ -51,9 +70,9 @@
51 70 {
52 71 if ($search) {
53 72 $fields = [
54 73 'display_name',
55 - 'user_login'
74 + 'user_login',
56 75 ];
57 76 $query->where(function ($query) use ($fields, $search) {
58 77 $query->where(array_shift($fields), 'LIKE', "$search%");
59 78 foreach ($fields as $field) {
@@ -70,13 +89,13 @@
70 89 * @return string
71 90 */
72 91 public function getPhotoAttribute()
73 92 {
74 - if ($photo = get_user_meta($this->ID, '_fcom_user_photo', true)) {
93 + if ($photo = get_user_meta($this->ID, '_fcom_user_photo', true)) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
75 94 return $photo;
76 95 }
77 96
78 - if ($contact = $this->getContact()) {
97 + if ($contact = $this->getContact()) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
79 98 return $contact->photo;
80 99 }
81 100
82 101 if (empty($this->attributes['user_email'])) {
@@ -88,8 +107,12 @@
88 107 }
89 108 return '';
90 109 }
91 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 +
92 115 $hash = md5(strtolower(trim($this->attributes['user_email'])));
93 116
94 117 /**
95 118 * Gravatar URL by Email
@@ -134,8 +157,31 @@
134 157 {
135 158 return $this->belongsTo(XProfile::class, 'ID', 'user_id');
136 159 }
137 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 +
138 184 public function getGeneralData()
139 185 {
140 186 $user = get_user_by('ID', $this->ID);
141 187
@@ -162,9 +208,9 @@
162 208 'user_id' => $user->ID,
163 209 'created_at' => $user->user_registered,
164 210 'photo' => $this->photo,
165 211 'username' => $this->username,
166 - 'is_verified' => $this->isVerified()
212 + 'is_verified' => $this->isVerified(),
167 213 ];
168 214 }
169 215
170 216 public function spaces()
@@ -169,22 +215,35 @@
169 215
170 216 public function spaces()
171 217 {
172 218 return $this->belongsToMany(BaseSpace::class, 'fcom_space_user', 'user_id', 'space_id')
173 - ->withPivot(['role', 'status', 'created_at']);
219 + ->withPivot([ 'role', 'status', 'created_at' ]);
174 220 }
175 221
176 222 public function courses()
177 223 {
178 224 return $this->belongsToMany(Course::class, 'fcom_space_user', 'user_id', 'space_id')
179 - ->withPivot(['role', 'created_at']);
225 + ->withPivot([ 'role', 'created_at' ]);
180 226 }
181 227
228 + /**
229 + * @deprecated 2.8.2 Use notificationPreferences() - preferences no longer live
230 + * in fcom_notification_users.
231 + */
182 232 public function notificationSubscriptions()
183 233 {
184 234 return $this->hasMany(NotificationSubscription::class, 'user_id');
185 235 }
186 236
237 + /**
238 + * Explicit notification preference overrides across every channel.
239 + * Recipient selection for email fan-outs is driven off this relation.
240 + */
241 + public function notificationPreferences()
242 + {
243 + return $this->hasMany(NotificationPreference::class, 'user_id', 'ID');
244 + }
245 +
187 246 public function space_pivot()
188 247 {
189 248 return $this->belongsTo(SpaceUserPivot::class, 'ID', 'user_id')->withoutGlobalScopes();
190 249 }
@@ -201,14 +260,15 @@
201 260
202 261 public function community_role()
203 262 {
204 263 return $this->belongsTo(Meta::class, 'ID', 'object_id')
264 + ->where('object_type', 'user')
205 265 ->where('meta_key', '_user_community_roles');
206 266 }
207 267
208 268 public function updateCustomData($updateData, $removeSrc = false)
209 269 {
210 - if (isset($updateData['first_name'])) {
270 + if (isset($updateData['first_name']) && Utility::getPrivacySetting('enable_user_sync') === 'yes') {
211 271 $firstName = sanitize_text_field($updateData['first_name']);
212 272 $lastName = sanitize_text_field($updateData['last_name']);
213 273
214 274 $userData = [
@@ -216,17 +276,17 @@
216 276 'last_name' => $lastName,
217 277 'display_name' => trim($firstName . ' ' . $lastName),
218 278 'description' => isset($updateData['short_description']) ? sanitize_textarea_field($updateData['short_description']) : '',
219 279 'user_url' => isset($updateData['website']) ? esc_url($updateData['website']) : '',
220 - 'ID' => $this->ID
280 + 'ID' => $this->ID,
221 281 ];
222 282
223 283 wp_update_user($userData);
224 284
225 - if ($contact = $this->getContact()) {
285 + if ($contact = $this->getContact()) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
226 286 $contact->fill(array_filter([
227 287 'first_name' => $firstName,
228 - 'last_name' => $lastName
288 + 'last_name' => $lastName,
229 289 ]));
230 290
231 291 $dirtyFields = $contact->getDirty();
232 292 if ($dirtyFields) {
@@ -234,14 +294,14 @@
234 294 do_action('fluent_crm/contact_updated', $contact, $dirtyFields);
235 295 }
236 296
237 297 }
238 -
239 298 }
240 299
241 300 return $this;
242 301 }
243 302
303 + // 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.
244 304 public function getDisplayName()
245 305 {
246 306 $user = get_user_by('ID', $this->ID);
247 307 $name = '';
@@ -250,11 +310,23 @@
250 310 }
251 311 if ($name) {
252 312 return $name;
253 313 }
314 +
254 315 return $user->display_name;
255 316 }
256 317
318 + // 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.
319 + public function getPublicDisplayName()
320 + {
321 + $name = $this->xprofile ? $this->xprofile->display_name : '';
322 + if (!$name) {
323 + $name = $this->display_name;
324 + }
325 +
326 + return apply_filters('fluent_community/public_display_name', $name, $this);
327 + }
328 +
257 329 public function getCustomMeta($key, $default = null)
258 330 {
259 331 $exist = Meta::where('object_type', 'user')
260 332 ->where('object_id', $this->ID)
@@ -283,13 +355,18 @@
283 355
284 356 return Meta::create([
285 357 'object_type' => 'user',
286 358 'object_id' => $this->ID,
287 - 'meta_key' => $key,
288 - 'value' => $value
359 + 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
360 + 'value' => $value,
289 361 ]);
290 362 }
291 363
364 + public function isNotMemberOfAnySpace()
365 + {
366 + return $this->spaces()->count() == 0;
367 + }
368 +
292 369 public function getSpaceIds($cached = true)
293 370 {
294 371 if ($cached) {
295 372 $ids = get_user_meta($this->ID, '_fcom_space_ids', true);
@@ -303,12 +380,26 @@
303 380
304 381 return get_user_meta($this->ID, '_fcom_space_ids', true);
305 382 }
306 383
384 + public function getJoinedSpaceIds()
385 + {
386 + $globalRoles = $this->getCommunityRoles();
387 +
388 + if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) {
389 + return BaseSpace::onlyMain()->pluck('id')->toArray();
390 + }
391 +
392 + return BaseSpace::onlyMain()->whereHas('members', function ($query) {
393 + $query->where('user_id', $this->ID)
394 + ->where('status', 'active');
395 + })->pluck('id')->toArray();
396 + }
397 +
307 398 public function getCommunityRoles()
308 399 {
309 - if (Helper::isSiteAdmin($this->ID)) {
310 - return ['admin'];
400 + if (Helper::isSuperAdmin($this->ID)) {
401 + return [ 'admin' ];
311 402 }
312 403
313 404 return (array)$this->getCustomMeta('_user_community_roles', []);
314 405 }
@@ -319,9 +410,9 @@
319 410 }
320 411
321 412 public function isCommunityModerator()
322 413 {
323 - return !!array_intersect(['moderator', 'admin'], $this->getCommunityRoles());
414 + return (bool)array_intersect([ 'moderator', 'admin' ], $this->getCommunityRoles());
324 415 }
325 416
326 417 public function hasCommunityModeratorAccess()
327 418 {
@@ -329,8 +420,15 @@
329 420
330 421 return Arr::isTrue($permissions, 'community_moderator');
331 422 }
332 423
424 + public function hasCommunityAdminAccess()
425 + {
426 + $permissions = $this->getPermissions(true);
427 +
428 + return Arr::isTrue($permissions, 'community_admin');
429 + }
430 +
333 431 public function hasCourseCreatorAccess()
334 432 {
335 433 $permissions = $this->getPermissions(true);
336 434
@@ -345,9 +443,9 @@
345 443 public function hasSpaceManageAccess()
346 444 {
347 445 $permissions = array_filter($this->getPermissions(true));
348 446
349 - return !!array_intersect(['community_admin', 'course_admin'], array_keys($permissions));
447 + return (bool)array_intersect([ 'community_admin', 'course_admin' ], array_keys($permissions));
350 448 }
351 449
352 450 public function getSpaceRole($space)
353 451 {
@@ -359,22 +457,23 @@
359 457 }
360 458 }
361 459
362 460 if ($space) {
363 - $spacePivot = SpaceUserPivot::where('space_id', $space->id)
364 - ->where('user_id', $this->ID)
365 - ->first();
461 + $membership = $space->getMembership($this->ID);
366 462
367 - if ($spacePivot) {
368 - if ($spacePivot->role != 'member') {
369 - return $spacePivot->role;
463 + if ($membership) {
464 + $role = $membership->pivot->role;
465 + $status = $membership->pivot->status;
466 +
467 + if ($role != 'member') {
468 + return $role;
370 469 }
371 470
372 471 if (!in_array('moderator', $globalRoles)) {
373 - if ($spacePivot->status == 'pending') {
472 + if ($status == 'pending') {
374 473 return 'pending';
375 474 }
376 - return $spacePivot->role;
475 + return $role;
377 476 }
378 477 }
379 478 }
380 479
@@ -388,15 +487,17 @@
388 487 public function cacheAccessSpaces()
389 488 {
390 489 $globalRoles = $this->getCommunityRoles();
391 490
392 - if (array_intersect($globalRoles, ['admin', 'moderator'])) {
393 - $spaces = Space::get();
491 + if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) {
492 + $spaces = BaseSpace::onlyMain()->get();
394 493 } else {
395 - $spaces = Space::whereHas('members', function ($query) {
396 - $query->where('user_id', $this->ID)
397 - ->where('status', 'active');
398 - })->orWhere('privacy', 'public')->get();
494 + $spaces = BaseSpace::onlyMain()->where(function ($spaceQuery) {
495 + $spaceQuery->whereHas('members', function ($memberQuery) {
496 + $memberQuery->where('user_id', $this->ID)
497 + ->where('status', 'active');
498 + })->orWhere('privacy', 'public');
499 + })->get();
399 500 }
400 501
401 502 $spaceIds = $spaces->pluck('id')->toArray();
402 503
@@ -408,18 +509,18 @@
408 509 protected function getRolePermissions($roles)
409 510 {
410 511 if (!$roles) {
411 512 return apply_filters('fluent_community/user/permissions', [
412 - 'read' => true
513 + 'read' => true,
413 514 ], $roles, $this);
414 515 }
415 516
416 517 $isAdmin = in_array('admin', $roles);
417 - $isModerator = !!array_intersect($roles, ['admin', 'moderator']);
518 + $isModerator = (bool)array_intersect($roles, [ 'admin', 'moderator' ]);
418 519
419 520 $permissions = [
420 521 'admin' => $isAdmin,
421 - 'super_admin' => Helper::isSiteAdmin(),
522 + 'super_admin' => Helper::isSuperAdmin(),
422 523 'community_admin' => $isAdmin,
423 524 'community_moderator' => $isModerator,
424 525 'delete_any_feed' => $isModerator,
425 526 'edit_any_feed' => $isModerator,
@@ -424,15 +525,15 @@
424 525 'delete_any_feed' => $isModerator,
425 526 'edit_any_feed' => $isModerator,
426 527 'delete_any_comment' => $isModerator,
427 528 'edit_any_comment' => $isModerator,
428 - 'read' => true
529 + 'read' => true,
429 530 ];
430 531
431 532 if ($isAdmin || in_array('course_admin', $roles)) {
432 533 $permissions['course_creator'] = true;
433 534 $permissions['course_admin'] = true;
434 - } else if (in_array('course_creatror', $roles)) {
535 + } elseif (in_array('course_creator', $roles)) {
435 536 $permissions['course_creator'] = true;
436 537 }
437 538
438 539 return apply_filters('fluent_community/user/permissions', $permissions, $roles, $this);
@@ -439,19 +540,17 @@
439 540 }
440 541
441 542 public function getPermissions($cached = true)
442 543 {
443 - static $permissions;
544 + static $permissions = [];
444 545
445 - if ($permissions && $cached) {
446 - return $permissions;
546 + if ($cached && isset($permissions[$this->ID])) {
547 + return $permissions[$this->ID];
447 548 }
448 549
449 550 $roles = $this->getCommunityRoles();
450 551
451 - $permissions = $this->getRolePermissions($roles);
452 -
453 - return $permissions;
552 + return $permissions[$this->ID] = $this->getRolePermissions($roles);
454 553 }
455 554
456 555 public function getSpacePermissions($space)
457 556 {
@@ -459,67 +558,97 @@
459 558 return [];
460 559 }
461 560
462 561 $role = $this->getSpaceRole($space);
562 +
563 + $hasDocuments = defined('FLUENT_COMMUNITY_PRO') && Arr::get($space->settings, 'document_library') == 'yes';
564 + $hasMediaGallery = defined('FLUENT_COMMUNITY_PRO') && Arr::get($space->settings, 'media_gallery') == 'yes';
565 +
463 566 $isRestrictedPost = Arr::get($space->settings, 'restricted_post_only') == 'yes';
567 + $isVerifiedPostOnly = Arr::get($space->settings, 'verified_post_only') == 'yes';
464 568
569 + $documentAccess = Arr::get($space->settings, 'document_access');
570 + $mediaAccess = Arr::get($space->settings, 'media_access');
571 +
465 572 if (!$role) {
466 573 $permissions = [
467 - 'can_create_post' => false,
468 - 'registered' => true,
469 - 'can_view_posts' => true,
470 - 'can_view_members' => $space->canViewMembers($this),
471 - 'is_pending' => false,
472 - 'is_non_member' => true,
473 - 'can_view_info' => $space->privacy !== 'secret'
574 + 'can_create_post' => false,
575 + 'registered' => true,
576 + 'can_comment' => false,
577 + 'can_view_posts' => true,
578 + 'can_view_members' => $space->canViewMembers($this),
579 + 'is_pending' => false,
580 + 'is_non_member' => true,
581 + 'can_view_info' => $space->privacy !== 'secret',
582 + 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
583 + 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
474 584 ];
475 585
476 586 if ($space->privacy === 'secret' || $space->privacy === 'private') {
477 587 $permissions['can_view_posts'] = false;
478 588 $permissions['can_view_members'] = false;
589 + $permissions['can_view_documents'] = false;
590 + $permissions['can_view_media'] = false;
479 591 }
480 - } else if ($role == 'pending') {
592 + } elseif ($role == 'pending') {
481 593 $permissions = [
482 - 'can_create_post' => false,
483 - 'registered' => true,
484 - 'can_view_posts' => true,
485 - 'can_view_members' => $space->canViewMembers($this),
486 - 'is_pending' => true,
487 - 'can_view_info' => $space->privacy !== 'secret'
594 + 'can_create_post' => false,
595 + 'registered' => true,
596 + 'can_view_posts' => true,
597 + 'can_comment' => false,
598 + 'can_view_members' => $space->canViewMembers($this),
599 + 'is_pending' => true,
600 + 'can_view_info' => $space->privacy !== 'secret',
601 + 'can_view_documents' => $hasDocuments && in_array($documentAccess, [ 'everybody', 'logged_in' ]),
602 + 'can_view_media' => $hasMediaGallery && in_array($mediaAccess, [ 'everybody', 'logged_in' ]),
488 603 ];
489 604
490 605 if ($space->privacy === 'secret' || $space->privacy === 'private') {
491 606 $permissions['can_view_posts'] = false;
492 607 $permissions['can_view_members'] = false;
608 + $permissions['can_view_documents'] = false;
609 + $permissions['can_view_media'] = false;
493 610 }
494 - } else if ($role == 'member' || $role == 'student') {
611 + } elseif ($role == 'member' || $role == 'student') {
495 612 $permissions = [
496 - 'can_create_post' => $isRestrictedPost ? false : true,
497 - 'registered' => true,
498 - 'can_view_posts' => true,
499 - 'can_view_members' => $space->canViewMembers($this),
500 - 'can_comment' => true,
501 - 'can_view_info' => true
613 + 'can_create_post' => $isRestrictedPost ? false : (!$isVerifiedPostOnly || $this->isVerified()),
614 + 'registered' => true,
615 + 'can_view_posts' => true,
616 + 'can_view_members' => $space->canViewMembers($this),
617 + 'can_comment' => true,
618 + 'can_view_info' => true,
619 + 'can_view_documents' => $hasDocuments,
620 + 'can_upload_documents' => $hasDocuments && Arr::get($space->settings, 'document_upload') == 'members_only',
621 + 'can_view_media' => $hasMediaGallery,
502 622 ];
503 623 } else {
624 + $isMod = in_array($role, [ 'admin', 'moderator' ]);
625 + $isAdmin = $role === 'admin';
504 626 $permissions = [
505 - 'can_create_post' => $isRestrictedPost ? in_array($role, ['admin', 'moderator']) : true,
506 - 'can_view_posts' => true,
507 - 'can_view_members' => $space->canViewMembers($this),
508 - 'registered' => true,
509 - 'community_admin' => $role === 'admin',
510 - 'community_moderator' => in_array($role, ['admin', 'moderator']),
511 - 'edit_any_feed' => $role === 'admin',
512 - 'delete_any_feed' => in_array($role, ['admin', 'moderator']),
513 - 'super_admin' => Helper::isSiteAdmin(),
514 - 'read' => true,
515 - 'can_remove_member' => $role === 'admin',
516 - 'can_add_member' => $role === 'admin',
517 - 'can_comment' => in_array($role, ['admin', 'moderator']),
518 - 'can_view_info' => true
627 + 'can_create_post' => $isRestrictedPost ? $isMod : true,
628 + 'can_view_posts' => true,
629 + 'can_view_members' => $isAdmin || $space->canViewMembers($this),
630 + 'registered' => true,
631 + 'community_admin' => $isAdmin,
632 + 'community_moderator' => $isMod,
633 + 'edit_any_feed' => $isMod,
634 + 'delete_any_feed' => $isMod,
635 + 'edit_any_comment' => $isMod,
636 + 'delete_any_comment' => $isMod,
637 + 'super_admin' => Helper::isSuperAdmin(),
638 + 'read' => true,
639 + 'can_remove_member' => $isAdmin,
640 + 'can_add_member' => $isAdmin,
641 + 'can_comment' => $isMod,
642 + 'can_view_info' => true,
643 + 'can_view_documents' => $hasDocuments,
644 + 'can_upload_documents' => $hasDocuments && $isMod,
645 + 'can_view_media' => $hasMediaGallery,
519 646 ];
520 647 }
521 648
649 + $permissions['is_member'] = in_array($role, [ 'admin', 'moderator', 'member', 'student' ]);
650 +
522 651 return apply_filters('fluent_community/user/space/permissions', $permissions, $space, $role, $this);
523 652 }
524 653
525 654 public function hasCommunityPermission($permission)
@@ -546,9 +675,9 @@
546 675
547 676 public function verifyCommunityPermission($permission)
548 677 {
549 678 if (!$this->hasCommunityPermission($permission)) {
550 - throw new \Exception('You do not have permission to do this action');
679 + throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
551 680 }
552 681
553 682 return true;
554 683 }
@@ -555,9 +684,9 @@
555 684
556 685 public function verifySpacePermission($permission, $space)
557 686 {
558 687 if (!$space || !$this->hasSpacePermission($permission, $space)) {
559 - throw new \Exception('You do not have permission to do this action');
688 + throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
560 689 }
561 690
562 691 return true;
563 692 }
@@ -563,12 +692,12 @@
563 692 }
564 693
565 694 public function canEditFeed($feed, $throwException = false)
566 695 {
567 - $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('edit_any_feed') || $this->hasSpacePermission('edit_any_feed', $feed->space_id);
696 + $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('edit_any_feed') || $this->hasSpacePermission('edit_any_feed', $feed->space);
568 697
569 698 if (!$result && $throwException) {
570 - throw new \Exception('You do not have permission to do this action');
699 + throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
571 700 }
572 701
573 702 return $result;
574 703 }
@@ -574,12 +703,12 @@
574 703 }
575 704
576 705 public function canDeleteFeed($feed, $throwException = false)
577 706 {
578 - $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('delete_any_feed') || $this->hasSpacePermission('delete_any_feed', $feed->space_id);
707 + $result = $feed->user_id == $this->ID || $this->hasCommunityPermission('delete_any_feed') || $this->hasSpacePermission('delete_any_feed', $feed->space);
579 708
580 709 if (!$result && $throwException) {
581 - throw new \Exception('You do not have permission to do this action');
710 + throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community'));
582 711 }
583 712 return $result;
584 713 }
585 714
@@ -591,8 +720,17 @@
591 720
592 721 return $this->hasCommunityPermission($permission);
593 722 }
594 723
724 + public function hasPermissionOrInCurrentSpace($permission, $space = null)
725 + {
726 + if ($this->hasCommunityPermission($permission)) {
727 + return true;
728 + }
729 +
730 + return $space && $this->hasSpacePermission($permission, $space);
731 + }
732 +
595 733 public function getUnreadNotificationCount()
596 734 {
597 735 return NotificationSubscriber::where('user_id', $this->ID)
598 736 ->unread()
@@ -610,44 +748,43 @@
610 748 }
611 749
612 750 public function isVerified()
613 751 {
614 - return get_user_meta($this->ID, '_fcom_is_verified', true) == 'yes';
752 + if ($this->xprofile) {
753 + return (bool) $this->xprofile->is_verified;
754 + }
755 +
756 + return false;
615 757 }
616 758
617 759 public function syncXProfile($force = false, $useUserName = false)
618 760 {
619 - $exist = $this->xprofile;
761 + $exist = XProfile::where('user_id', $this->ID)->first();
620 762
621 - if ($exist && !$force) {
763 + if (($exist && !$force) || ($exist && Utility::getPrivacySetting('enable_user_sync') === 'no')) {
622 764 return $exist;
623 765 }
624 766
625 - $createdAt = $this->user_registered;
626 - if (!$createdAt || $createdAt == '0000-00-00 00:00:00' || !strtotime($createdAt)) {
627 - $createdAt = gmdate('Y-m-d H:i:s');
628 - } else {
629 - $timeDiff = time() - current_time('timestamp');
630 - $createdAt = gmdate('Y-m-d H:i:s', strtotime($createdAt) - $timeDiff);
631 - }
632 -
633 767 $data = [
634 768 'user_id' => $this->ID,
635 769 'username' => ProfileHelper::generateUserName($this->ID, $useUserName),
636 770 'display_name' => $this->getDisplayName(),
637 - 'avatar' => $this->getPhotoAttribute(),
638 771 'is_verified' => $this->isVerified() ? 1 : 0,
639 - 'created_at' => $createdAt,
640 772 'short_description' => get_user_meta($this->ID, 'description', true),
641 773 'meta' => [
642 - 'website' => $this->user_url,
643 - 'cover_photo' => get_user_meta($this->ID, '_fluent_cover_photo', true)
644 - ]
774 + 'website' => $this->user_url,
775 + 'cover_photo' => get_user_meta($this->ID, '_fluent_cover_photo', true),
776 + 'short_description_rendered' => wp_kses_post(FeedsHelper::mdToHtml(get_user_meta($this->ID, 'description', true))),
777 + ],
645 778 ];
646 779
647 780 if ($exist) {
648 - unset($data['avatar']);
649 - unset($data['username']);
781 + $data = array_diff_key($data, [ 'avatar' => true, 'username' => true ]);
782 +
783 + if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
784 + $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
785 + }
786 +
650 787 $data['meta'] = wp_parse_args($exist->meta, $data['meta']);
651 788 $exist->fill($data);
652 789 $exist->save();
653 790 return $exist;
@@ -652,22 +789,33 @@
652 789 $exist->save();
653 790 return $exist;
654 791 }
655 792
656 -
657 793 $counter = 1;
658 794 $initialUserName = $data['username'];
659 795 while (XProfile::where('username', $initialUserName)->first()) {
660 796 $initialUserName = $data['username'] . '_' . $counter;
661 - $counter++;
797 + ++$counter;
662 798 }
663 799
664 800 $data['username'] = $initialUserName;
801 + $data['status'] = 'active';
802 + if (apply_filters('fluent/community/user_wp_user_registered_date', true, $this)) {
803 + $data['created_at'] = get_date_from_gmt($this->user_registered, 'Y-m-d H:i:s');
804 + }
665 805
666 - return XProfile::create($data);
806 + $xprofile = XProfile::create($data);
807 + $this->load('xprofile');
808 +
809 + return $xprofile;
667 810 }
668 811
669 812 public function getUserMeta($metaKey, $default = null)
670 813 {
671 814 return get_user_meta($this->ID, $metaKey, true) ?? $default;
815 + }
816 +
817 + public function getWpUser()
818 + {
819 + return get_user_by('ID', $this->ID);
672 820 }
673 821 }