| 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 |
/** |
| 229 |
* @deprecated 2.8.2 Use notificationPreferences() - preferences no longer live |
| 230 |
* in fcom_notification_users. |
| 231 |
*/ |
| 232 |
public function notificationSubscriptions() |
| 233 |
{ |
| 234 |
return $this->hasMany(NotificationSubscription::class, 'user_id'); |
| 235 |
} |
| 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 |
|
| 246 |
public function space_pivot() |
| 247 |
{ |
| 248 |
return $this->belongsTo(SpaceUserPivot::class, 'ID', 'user_id')->withoutGlobalScopes(); |
| 249 |
} |
| 250 |
|
| 251 |
public function notification_records() |
| 252 |
{ |
| 253 |
return $this->hasMany(NotificationSubscriber::class, 'user_id', 'ID')->withoutGlobalScopes(); |
| 254 |
} |
| 255 |
|
| 256 |
public function crm_contact() |
| 257 |
{ |
| 258 |
return $this->belongsTo(Contact::class, 'ID', 'user_id')->withoutGlobalScopes(); |
| 259 |
} |
| 260 |
|
| 261 |
public function community_role() |
| 262 |
{ |
| 263 |
return $this->belongsTo(Meta::class, 'ID', 'object_id') |
| 264 |
->where('object_type', 'user') |
| 265 |
->where('meta_key', '_user_community_roles'); |
| 266 |
} |
| 267 |
|
| 268 |
public function updateCustomData($updateData, $removeSrc = false) |
| 269 |
{ |
| 270 |
if (isset($updateData['first_name']) && Utility::getPrivacySetting('enable_user_sync') === 'yes') { |
| 271 |
$firstName = sanitize_text_field($updateData['first_name']); |
| 272 |
$lastName = sanitize_text_field($updateData['last_name']); |
| 273 |
|
| 274 |
$userData = [ |
| 275 |
'first_name' => $firstName, |
| 276 |
'last_name' => $lastName, |
| 277 |
'display_name' => trim($firstName . ' ' . $lastName), |
| 278 |
'description' => isset($updateData['short_description']) ? sanitize_textarea_field($updateData['short_description']) : '', |
| 279 |
'user_url' => isset($updateData['website']) ? esc_url($updateData['website']) : '', |
| 280 |
'ID' => $this->ID, |
| 281 |
]; |
| 282 |
|
| 283 |
wp_update_user($userData); |
| 284 |
|
| 285 |
if ($contact = $this->getContact()) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure |
| 286 |
$contact->fill(array_filter([ |
| 287 |
'first_name' => $firstName, |
| 288 |
'last_name' => $lastName, |
| 289 |
])); |
| 290 |
|
| 291 |
$dirtyFields = $contact->getDirty(); |
| 292 |
if ($dirtyFields) { |
| 293 |
$contact->save(); |
| 294 |
do_action('fluent_crm/contact_updated', $contact, $dirtyFields); |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return $this; |
| 301 |
} |
| 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. |
| 304 |
public function getDisplayName() |
| 305 |
{ |
| 306 |
$user = get_user_by('ID', $this->ID); |
| 307 |
$name = ''; |
| 308 |
if ($user->first_name || $user->last_name) { |
| 309 |
$name = trim($user->first_name . ' ' . $user->last_name); |
| 310 |
} |
| 311 |
if ($name) { |
| 312 |
return $name; |
| 313 |
} |
| 314 |
|
| 315 |
return $user->display_name; |
| 316 |
} |
| 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 |
|
| 329 |
public function getCustomMeta($key, $default = null) |
| 330 |
{ |
| 331 |
$exist = Meta::where('object_type', 'user') |
| 332 |
->where('object_id', $this->ID) |
| 333 |
->where('meta_key', $key) |
| 334 |
->first(); |
| 335 |
|
| 336 |
if ($exist && $exist->value) { |
| 337 |
return $exist->value; |
| 338 |
} |
| 339 |
|
| 340 |
return $default; |
| 341 |
} |
| 342 |
|
| 343 |
public function updateCustomMeta($key, $value) |
| 344 |
{ |
| 345 |
$exist = Meta::where('object_type', 'user') |
| 346 |
->where('object_id', $this->ID) |
| 347 |
->where('meta_key', $key) |
| 348 |
->first(); |
| 349 |
|
| 350 |
if ($exist) { |
| 351 |
$exist->value = $value; |
| 352 |
$exist->save(); |
| 353 |
return $exist; |
| 354 |
} |
| 355 |
|
| 356 |
return Meta::create([ |
| 357 |
'object_type' => 'user', |
| 358 |
'object_id' => $this->ID, |
| 359 |
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 360 |
'value' => $value, |
| 361 |
]); |
| 362 |
} |
| 363 |
|
| 364 |
public function isNotMemberOfAnySpace() |
| 365 |
{ |
| 366 |
return $this->spaces()->count() == 0; |
| 367 |
} |
| 368 |
|
| 369 |
public function getSpaceIds($cached = true) |
| 370 |
{ |
| 371 |
if ($cached) { |
| 372 |
$ids = get_user_meta($this->ID, '_fcom_space_ids', true); |
| 373 |
if (!$ids || !is_array($ids)) { |
| 374 |
$ids = []; |
| 375 |
} |
| 376 |
return $ids; |
| 377 |
} |
| 378 |
|
| 379 |
$this->cacheAccessSpaces(); |
| 380 |
|
| 381 |
return get_user_meta($this->ID, '_fcom_space_ids', true); |
| 382 |
} |
| 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 |
|
| 398 |
public function getCommunityRoles() |
| 399 |
{ |
| 400 |
if (Helper::isSuperAdmin($this->ID)) { |
| 401 |
return [ 'admin' ]; |
| 402 |
} |
| 403 |
|
| 404 |
return (array)$this->getCustomMeta('_user_community_roles', []); |
| 405 |
} |
| 406 |
|
| 407 |
public function isCommunityAdmin() |
| 408 |
{ |
| 409 |
return in_array('admin', $this->getCommunityRoles()); |
| 410 |
} |
| 411 |
|
| 412 |
public function isCommunityModerator() |
| 413 |
{ |
| 414 |
return (bool)array_intersect([ 'moderator', 'admin' ], $this->getCommunityRoles()); |
| 415 |
} |
| 416 |
|
| 417 |
public function hasCommunityModeratorAccess() |
| 418 |
{ |
| 419 |
$permissions = $this->getPermissions(true); |
| 420 |
|
| 421 |
return Arr::isTrue($permissions, 'community_moderator'); |
| 422 |
} |
| 423 |
|
| 424 |
public function hasCommunityAdminAccess() |
| 425 |
{ |
| 426 |
$permissions = $this->getPermissions(true); |
| 427 |
|
| 428 |
return Arr::isTrue($permissions, 'community_admin'); |
| 429 |
} |
| 430 |
|
| 431 |
public function hasCourseCreatorAccess() |
| 432 |
{ |
| 433 |
$permissions = $this->getPermissions(true); |
| 434 |
|
| 435 |
return Arr::isTrue($permissions, 'course_creator'); |
| 436 |
} |
| 437 |
|
| 438 |
public function isSpaceModerator() |
| 439 |
{ |
| 440 |
return $this->hasCourseCreatorAccess() || $this->hasCommunityModeratorAccess(); |
| 441 |
} |
| 442 |
|
| 443 |
public function hasSpaceManageAccess() |
| 444 |
{ |
| 445 |
$permissions = array_filter($this->getPermissions(true)); |
| 446 |
|
| 447 |
return (bool)array_intersect([ 'community_admin', 'course_admin' ], array_keys($permissions)); |
| 448 |
} |
| 449 |
|
| 450 |
public function getSpaceRole($space) |
| 451 |
{ |
| 452 |
$globalRoles = $this->getCommunityRoles(); |
| 453 |
|
| 454 |
if ($globalRoles) { |
| 455 |
if (in_array('admin', $globalRoles)) { |
| 456 |
return 'admin'; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if ($space) { |
| 461 |
$membership = $space->getMembership($this->ID); |
| 462 |
|
| 463 |
if ($membership) { |
| 464 |
$role = $membership->pivot->role; |
| 465 |
$status = $membership->pivot->status; |
| 466 |
|
| 467 |
if ($role != 'member') { |
| 468 |
return $role; |
| 469 |
} |
| 470 |
|
| 471 |
if (!in_array('moderator', $globalRoles)) { |
| 472 |
if ($status == 'pending') { |
| 473 |
return 'pending'; |
| 474 |
} |
| 475 |
return $role; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if (in_array('moderator', $globalRoles)) { |
| 481 |
return 'moderator'; |
| 482 |
} |
| 483 |
|
| 484 |
return ''; |
| 485 |
} |
| 486 |
|
| 487 |
public function cacheAccessSpaces() |
| 488 |
{ |
| 489 |
$globalRoles = $this->getCommunityRoles(); |
| 490 |
|
| 491 |
if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) { |
| 492 |
$spaces = BaseSpace::onlyMain()->get(); |
| 493 |
} else { |
| 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(); |
| 500 |
} |
| 501 |
|
| 502 |
$spaceIds = $spaces->pluck('id')->toArray(); |
| 503 |
|
| 504 |
update_user_meta($this->ID, '_fcom_space_ids', $spaceIds); |
| 505 |
|
| 506 |
return $spaces; |
| 507 |
} |
| 508 |
|
| 509 |
protected function getRolePermissions($roles) |
| 510 |
{ |
| 511 |
if (!$roles) { |
| 512 |
return apply_filters('fluent_community/user/permissions', [ |
| 513 |
'read' => true, |
| 514 |
], $roles, $this); |
| 515 |
} |
| 516 |
|
| 517 |
$isAdmin = in_array('admin', $roles); |
| 518 |
$isModerator = (bool)array_intersect($roles, [ 'admin', 'moderator' ]); |
| 519 |
|
| 520 |
$permissions = [ |
| 521 |
'admin' => $isAdmin, |
| 522 |
'super_admin' => Helper::isSuperAdmin(), |
| 523 |
'community_admin' => $isAdmin, |
| 524 |
'community_moderator' => $isModerator, |
| 525 |
'delete_any_feed' => $isModerator, |
| 526 |
'edit_any_feed' => $isModerator, |
| 527 |
'delete_any_comment' => $isModerator, |
| 528 |
'edit_any_comment' => $isModerator, |
| 529 |
'read' => true, |
| 530 |
]; |
| 531 |
|
| 532 |
if ($isAdmin || in_array('course_admin', $roles)) { |
| 533 |
$permissions['course_creator'] = true; |
| 534 |
$permissions['course_admin'] = true; |
| 535 |
} elseif (in_array('course_creator', $roles)) { |
| 536 |
$permissions['course_creator'] = true; |
| 537 |
} |
| 538 |
|
| 539 |
return apply_filters('fluent_community/user/permissions', $permissions, $roles, $this); |
| 540 |
} |
| 541 |
|
| 542 |
public function getPermissions($cached = true) |
| 543 |
{ |
| 544 |
static $permissions = []; |
| 545 |
|
| 546 |
if ($cached && isset($permissions[$this->ID])) { |
| 547 |
return $permissions[$this->ID]; |
| 548 |
} |
| 549 |
|
| 550 |
$roles = $this->getCommunityRoles(); |
| 551 |
|
| 552 |
return $permissions[$this->ID] = $this->getRolePermissions($roles); |
| 553 |
} |
| 554 |
|
| 555 |
public function getSpacePermissions($space) |
| 556 |
{ |
| 557 |
if (!$space) { |
| 558 |
return []; |
| 559 |
} |
| 560 |
|
| 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 |
|
| 566 |
$isRestrictedPost = Arr::get($space->settings, 'restricted_post_only') == 'yes'; |
| 567 |
$isVerifiedPostOnly = Arr::get($space->settings, 'verified_post_only') == 'yes'; |
| 568 |
|
| 569 |
$documentAccess = Arr::get($space->settings, 'document_access'); |
| 570 |
$mediaAccess = Arr::get($space->settings, 'media_access'); |
| 571 |
|
| 572 |
if (!$role) { |
| 573 |
$permissions = [ |
| 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' ]), |
| 584 |
]; |
| 585 |
|
| 586 |
if ($space->privacy === 'secret' || $space->privacy === 'private') { |
| 587 |
$permissions['can_view_posts'] = false; |
| 588 |
$permissions['can_view_members'] = false; |
| 589 |
$permissions['can_view_documents'] = false; |
| 590 |
$permissions['can_view_media'] = false; |
| 591 |
} |
| 592 |
} elseif ($role == 'pending') { |
| 593 |
$permissions = [ |
| 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' ]), |
| 603 |
]; |
| 604 |
|
| 605 |
if ($space->privacy === 'secret' || $space->privacy === 'private') { |
| 606 |
$permissions['can_view_posts'] = false; |
| 607 |
$permissions['can_view_members'] = false; |
| 608 |
$permissions['can_view_documents'] = false; |
| 609 |
$permissions['can_view_media'] = false; |
| 610 |
} |
| 611 |
} elseif ($role == 'member' || $role == 'student') { |
| 612 |
$permissions = [ |
| 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, |
| 622 |
]; |
| 623 |
} else { |
| 624 |
$isMod = in_array($role, [ 'admin', 'moderator' ]); |
| 625 |
$isAdmin = $role === 'admin'; |
| 626 |
$permissions = [ |
| 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, |
| 646 |
]; |
| 647 |
} |
| 648 |
|
| 649 |
$permissions['is_member'] = in_array($role, [ 'admin', 'moderator', 'member', 'student' ]); |
| 650 |
|
| 651 |
return apply_filters('fluent_community/user/space/permissions', $permissions, $space, $role, $this); |
| 652 |
} |
| 653 |
|
| 654 |
public function hasCommunityPermission($permission) |
| 655 |
{ |
| 656 |
$permissions = $this->getPermissions(); |
| 657 |
|
| 658 |
if (isset($permissions[$permission])) { |
| 659 |
return $permissions[$permission]; |
| 660 |
} |
| 661 |
|
| 662 |
return false; |
| 663 |
} |
| 664 |
|
| 665 |
public function hasSpacePermission($permission, $space) |
| 666 |
{ |
| 667 |
$permissions = $this->getSpacePermissions($space); |
| 668 |
|
| 669 |
if (isset($permissions[$permission])) { |
| 670 |
return $permissions[$permission]; |
| 671 |
} |
| 672 |
|
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
public function verifyCommunityPermission($permission) |
| 677 |
{ |
| 678 |
if (!$this->hasCommunityPermission($permission)) { |
| 679 |
throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community')); |
| 680 |
} |
| 681 |
|
| 682 |
return true; |
| 683 |
} |
| 684 |
|
| 685 |
public function verifySpacePermission($permission, $space) |
| 686 |
{ |
| 687 |
if (!$space || !$this->hasSpacePermission($permission, $space)) { |
| 688 |
throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community')); |
| 689 |
} |
| 690 |
|
| 691 |
return true; |
| 692 |
} |
| 693 |
|
| 694 |
public function canEditFeed($feed, $throwException = false) |
| 695 |
{ |
| 696 |
$result = $feed->user_id == $this->ID || $this->hasCommunityPermission('edit_any_feed') || $this->hasSpacePermission('edit_any_feed', $feed->space); |
| 697 |
|
| 698 |
if (!$result && $throwException) { |
| 699 |
throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community')); |
| 700 |
} |
| 701 |
|
| 702 |
return $result; |
| 703 |
} |
| 704 |
|
| 705 |
public function canDeleteFeed($feed, $throwException = false) |
| 706 |
{ |
| 707 |
$result = $feed->user_id == $this->ID || $this->hasCommunityPermission('delete_any_feed') || $this->hasSpacePermission('delete_any_feed', $feed->space); |
| 708 |
|
| 709 |
if (!$result && $throwException) { |
| 710 |
throw new \Exception(esc_html__('You do not have permission to do this action', 'fluent-community')); |
| 711 |
} |
| 712 |
return $result; |
| 713 |
} |
| 714 |
|
| 715 |
public function can($permission, $space = null) |
| 716 |
{ |
| 717 |
if ($space) { |
| 718 |
return $this->hasSpacePermission($permission, $space); |
| 719 |
} |
| 720 |
|
| 721 |
return $this->hasCommunityPermission($permission); |
| 722 |
} |
| 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 |
|
| 733 |
public function getUnreadNotificationCount() |
| 734 |
{ |
| 735 |
return NotificationSubscriber::where('user_id', $this->ID) |
| 736 |
->unread() |
| 737 |
->count(); |
| 738 |
} |
| 739 |
|
| 740 |
public function getUnreadNotificationFeedIds() |
| 741 |
{ |
| 742 |
$ids = Notification::whereHas('subscribers', function ($query) { |
| 743 |
return $query->where('user_id', $this->ID) |
| 744 |
->unread(); |
| 745 |
})->pluck('feed_id')->toArray(); |
| 746 |
|
| 747 |
return array_values(array_unique($ids)); |
| 748 |
} |
| 749 |
|
| 750 |
public function isVerified() |
| 751 |
{ |
| 752 |
if ($this->xprofile) { |
| 753 |
return (bool) $this->xprofile->is_verified; |
| 754 |
} |
| 755 |
|
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
public function syncXProfile($force = false, $useUserName = false) |
| 760 |
{ |
| 761 |
$exist = XProfile::where('user_id', $this->ID)->first(); |
| 762 |
|
| 763 |
if (($exist && !$force) || ($exist && Utility::getPrivacySetting('enable_user_sync') === 'no')) { |
| 764 |
return $exist; |
| 765 |
} |
| 766 |
|
| 767 |
$data = [ |
| 768 |
'user_id' => $this->ID, |
| 769 |
'username' => ProfileHelper::generateUserName($this->ID, $useUserName), |
| 770 |
'display_name' => $this->getDisplayName(), |
| 771 |
'is_verified' => $this->isVerified() ? 1 : 0, |
| 772 |
'short_description' => get_user_meta($this->ID, 'description', true), |
| 773 |
'meta' => [ |
| 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 |
], |
| 778 |
]; |
| 779 |
|
| 780 |
if ($exist) { |
| 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 |
|
| 787 |
$data['meta'] = wp_parse_args($exist->meta, $data['meta']); |
| 788 |
$exist->fill($data); |
| 789 |
$exist->save(); |
| 790 |
return $exist; |
| 791 |
} |
| 792 |
|
| 793 |
$counter = 1; |
| 794 |
$initialUserName = $data['username']; |
| 795 |
while (XProfile::where('username', $initialUserName)->first()) { |
| 796 |
$initialUserName = $data['username'] . '_' . $counter; |
| 797 |
++$counter; |
| 798 |
} |
| 799 |
|
| 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 |
} |
| 805 |
|
| 806 |
$xprofile = XProfile::create($data); |
| 807 |
$this->load('xprofile'); |
| 808 |
|
| 809 |
return $xprofile; |
| 810 |
} |
| 811 |
|
| 812 |
public function getUserMeta($metaKey, $default = null) |
| 813 |
{ |
| 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); |
| 820 |
} |
| 821 |
} |
| 822 |
|