| @@ -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,16 +410,44 @@ | ||
| 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 | - public function hasCourseManageAccess() | |
| 417 | + public function hasCommunityModeratorAccess() | |
| 327 | 418 | { |
| 328 | - return !!array_intersect(['admin', 'course_creator', 'course_admin'], $this->getCommunityRoles()); | |
| 419 | + $permissions = $this->getPermissions(true); | |
| 420 | + | |
| 421 | + return Arr::isTrue($permissions, 'community_moderator'); | |
| 329 | 422 | } |
| 330 | 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 | + | |
| 331 | 450 | public function getSpaceRole($space) |
| 332 | 451 | { |
| 333 | 452 | $globalRoles = $this->getCommunityRoles(); |
| 334 | 453 | |
| @@ -338,22 +457,23 @@ | ||
| 338 | 457 | } |
| 339 | 458 | } |
| 340 | 459 | |
| 341 | 460 | if ($space) { |
| 342 | - $spacePivot = SpaceUserPivot::where('space_id', $space->id) | |
| 343 | - ->where('user_id', $this->ID) | |
| 344 | - ->first(); | |
| 461 | + $membership = $space->getMembership($this->ID); | |
| 345 | 462 | |
| 346 | - if ($spacePivot) { | |
| 347 | - if ($spacePivot->role != 'member') { | |
| 348 | - return $spacePivot->role; | |
| 463 | + if ($membership) { | |
| 464 | + $role = $membership->pivot->role; | |
| 465 | + $status = $membership->pivot->status; | |
| 466 | + | |
| 467 | + if ($role != 'member') { | |
| 468 | + return $role; | |
| 349 | 469 | } |
| 350 | 470 | |
| 351 | 471 | if (!in_array('moderator', $globalRoles)) { |
| 352 | - if ($spacePivot->status == 'pending') { | |
| 472 | + if ($status == 'pending') { | |
| 353 | 473 | return 'pending'; |
| 354 | 474 | } |
| 355 | - return $spacePivot->role; | |
| 475 | + return $role; | |
| 356 | 476 | } |
| 357 | 477 | } |
| 358 | 478 | } |
| 359 | 479 | |
| @@ -367,15 +487,17 @@ | ||
| 367 | 487 | public function cacheAccessSpaces() |
| 368 | 488 | { |
| 369 | 489 | $globalRoles = $this->getCommunityRoles(); |
| 370 | 490 | |
| 371 | - if (array_intersect($globalRoles, ['admin', 'moderator'])) { | |
| 372 | - $spaces = Space::all(); | |
| 491 | + if (array_intersect($globalRoles, [ 'admin', 'moderator' ])) { | |
| 492 | + $spaces = BaseSpace::onlyMain()->get(); | |
| 373 | 493 | } else { |
| 374 | - $spaces = Space::whereHas('members', function ($query) { | |
| 375 | - $query->where('user_id', $this->ID) | |
| 376 | - ->where('status', 'active'); | |
| 377 | - })->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(); | |
| 378 | 500 | } |
| 379 | 501 | |
| 380 | 502 | $spaceIds = $spaces->pluck('id')->toArray(); |
| 381 | 503 | |
| @@ -387,30 +509,31 @@ | ||
| 387 | 509 | protected function getRolePermissions($roles) |
| 388 | 510 | { |
| 389 | 511 | if (!$roles) { |
| 390 | 512 | return apply_filters('fluent_community/user/permissions', [ |
| 391 | - 'read' => true | |
| 513 | + 'read' => true, | |
| 392 | 514 | ], $roles, $this); |
| 393 | 515 | } |
| 394 | 516 | |
| 395 | 517 | $isAdmin = in_array('admin', $roles); |
| 396 | - $isModerator = array_intersect($roles, ['admin', 'moderator']); | |
| 518 | + $isModerator = (bool)array_intersect($roles, [ 'admin', 'moderator' ]); | |
| 397 | 519 | |
| 398 | 520 | $permissions = [ |
| 399 | - 'community_admin' => $isAdmin ? 'admin' : false, | |
| 521 | + 'admin' => $isAdmin, | |
| 522 | + 'super_admin' => Helper::isSuperAdmin(), | |
| 523 | + 'community_admin' => $isAdmin, | |
| 400 | 524 | 'community_moderator' => $isModerator, |
| 401 | - 'super_admin' => Helper::isSiteAdmin(), | |
| 402 | 525 | 'delete_any_feed' => $isModerator, |
| 403 | 526 | 'edit_any_feed' => $isModerator, |
| 404 | 527 | 'delete_any_comment' => $isModerator, |
| 405 | 528 | 'edit_any_comment' => $isModerator, |
| 406 | - 'read' => true | |
| 529 | + 'read' => true, | |
| 407 | 530 | ]; |
| 408 | 531 | |
| 409 | 532 | if ($isAdmin || in_array('course_admin', $roles)) { |
| 410 | 533 | $permissions['course_creator'] = true; |
| 411 | 534 | $permissions['course_admin'] = true; |
| 412 | - } else if (in_array('course_creatror', $roles)) { | |
| 535 | + } elseif (in_array('course_creator', $roles)) { | |
| 413 | 536 | $permissions['course_creator'] = true; |
| 414 | 537 | } |
| 415 | 538 | |
| 416 | 539 | return apply_filters('fluent_community/user/permissions', $permissions, $roles, $this); |
| @@ -417,19 +540,17 @@ | ||
| 417 | 540 | } |
| 418 | 541 | |
| 419 | 542 | public function getPermissions($cached = true) |
| 420 | 543 | { |
| 421 | - static $permissions; | |
| 544 | + static $permissions = []; | |
| 422 | 545 | |
| 423 | - if ($permissions && $cached) { | |
| 424 | - return $permissions; | |
| 546 | + if ($cached && isset($permissions[$this->ID])) { | |
| 547 | + return $permissions[$this->ID]; | |
| 425 | 548 | } |
| 426 | 549 | |
| 427 | 550 | $roles = $this->getCommunityRoles(); |
| 428 | 551 | |
| 429 | - $permissions = $this->getRolePermissions($roles); | |
| 430 | - | |
| 431 | - return $permissions; | |
| 552 | + return $permissions[$this->ID] = $this->getRolePermissions($roles); | |
| 432 | 553 | } |
| 433 | 554 | |
| 434 | 555 | public function getSpacePermissions($space) |
| 435 | 556 | { |
| @@ -437,67 +558,97 @@ | ||
| 437 | 558 | return []; |
| 438 | 559 | } |
| 439 | 560 | |
| 440 | 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 | + | |
| 441 | 566 | $isRestrictedPost = Arr::get($space->settings, 'restricted_post_only') == 'yes'; |
| 567 | + $isVerifiedPostOnly = Arr::get($space->settings, 'verified_post_only') == 'yes'; | |
| 442 | 568 | |
| 569 | + $documentAccess = Arr::get($space->settings, 'document_access'); | |
| 570 | + $mediaAccess = Arr::get($space->settings, 'media_access'); | |
| 571 | + | |
| 443 | 572 | if (!$role) { |
| 444 | 573 | $permissions = [ |
| 445 | - 'can_create_post' => false, | |
| 446 | - 'registered' => true, | |
| 447 | - 'can_view_posts' => true, | |
| 448 | - 'can_view_members' => true, | |
| 449 | - 'is_pending' => false, | |
| 450 | - 'is_non_member' => true, | |
| 451 | - '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' ]), | |
| 452 | 584 | ]; |
| 453 | 585 | |
| 454 | 586 | if ($space->privacy === 'secret' || $space->privacy === 'private') { |
| 455 | 587 | $permissions['can_view_posts'] = false; |
| 456 | 588 | $permissions['can_view_members'] = false; |
| 589 | + $permissions['can_view_documents'] = false; | |
| 590 | + $permissions['can_view_media'] = false; | |
| 457 | 591 | } |
| 458 | - } else if ($role == 'pending') { | |
| 592 | + } elseif ($role == 'pending') { | |
| 459 | 593 | $permissions = [ |
| 460 | - 'can_create_post' => false, | |
| 461 | - 'registered' => true, | |
| 462 | - 'can_view_posts' => true, | |
| 463 | - 'can_view_members' => true, | |
| 464 | - 'is_pending' => true, | |
| 465 | - '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' ]), | |
| 466 | 603 | ]; |
| 467 | 604 | |
| 468 | 605 | if ($space->privacy === 'secret' || $space->privacy === 'private') { |
| 469 | 606 | $permissions['can_view_posts'] = false; |
| 470 | 607 | $permissions['can_view_members'] = false; |
| 608 | + $permissions['can_view_documents'] = false; | |
| 609 | + $permissions['can_view_media'] = false; | |
| 471 | 610 | } |
| 472 | - } else if ($role == 'member' || $role == 'student') { | |
| 611 | + } elseif ($role == 'member' || $role == 'student') { | |
| 473 | 612 | $permissions = [ |
| 474 | - 'can_create_post' => $isRestrictedPost ? false : true, | |
| 475 | - 'registered' => true, | |
| 476 | - 'can_view_posts' => true, | |
| 477 | - 'can_view_members' => true, | |
| 478 | - 'can_comment' => true, | |
| 479 | - '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, | |
| 480 | 622 | ]; |
| 481 | 623 | } else { |
| 624 | + $isMod = in_array($role, [ 'admin', 'moderator' ]); | |
| 625 | + $isAdmin = $role === 'admin'; | |
| 482 | 626 | $permissions = [ |
| 483 | - 'can_create_post' => $isRestrictedPost ? in_array($role, ['admin', 'moderator']) : true, | |
| 484 | - 'can_view_posts' => true, | |
| 485 | - 'can_view_members' => true, | |
| 486 | - 'registered' => true, | |
| 487 | - 'community_admin' => $role === 'admin', | |
| 488 | - 'community_moderator' => in_array($role, ['admin', 'moderator']), | |
| 489 | - 'edit_any_feed' => $role === 'admin', | |
| 490 | - 'delete_any_feed' => in_array($role, ['admin', 'moderator']), | |
| 491 | - 'super_admin' => Helper::isSiteAdmin(), | |
| 492 | - 'read' => true, | |
| 493 | - 'can_remove_member' => $role === 'admin', | |
| 494 | - 'can_add_member' => $role === 'admin', | |
| 495 | - 'can_comment' => in_array($role, ['admin', 'moderator']), | |
| 496 | - '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, | |
| 497 | 646 | ]; |
| 498 | 647 | } |
| 499 | 648 | |
| 649 | + $permissions['is_member'] = in_array($role, [ 'admin', 'moderator', 'member', 'student' ]); | |
| 650 | + | |
| 500 | 651 | return apply_filters('fluent_community/user/space/permissions', $permissions, $space, $role, $this); |
| 501 | 652 | } |
| 502 | 653 | |
| 503 | 654 | public function hasCommunityPermission($permission) |
| @@ -524,9 +675,9 @@ | ||
| 524 | 675 | |
| 525 | 676 | public function verifyCommunityPermission($permission) |
| 526 | 677 | { |
| 527 | 678 | if (!$this->hasCommunityPermission($permission)) { |
| 528 | - 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')); | |
| 529 | 680 | } |
| 530 | 681 | |
| 531 | 682 | return true; |
| 532 | 683 | } |
| @@ -533,9 +684,9 @@ | ||
| 533 | 684 | |
| 534 | 685 | public function verifySpacePermission($permission, $space) |
| 535 | 686 | { |
| 536 | 687 | if (!$space || !$this->hasSpacePermission($permission, $space)) { |
| 537 | - 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')); | |
| 538 | 689 | } |
| 539 | 690 | |
| 540 | 691 | return true; |
| 541 | 692 | } |
| @@ -541,12 +692,12 @@ | ||
| 541 | 692 | } |
| 542 | 693 | |
| 543 | 694 | public function canEditFeed($feed, $throwException = false) |
| 544 | 695 | { |
| 545 | - $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); | |
| 546 | 697 | |
| 547 | 698 | if (!$result && $throwException) { |
| 548 | - 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')); | |
| 549 | 700 | } |
| 550 | 701 | |
| 551 | 702 | return $result; |
| 552 | 703 | } |
| @@ -552,12 +703,12 @@ | ||
| 552 | 703 | } |
| 553 | 704 | |
| 554 | 705 | public function canDeleteFeed($feed, $throwException = false) |
| 555 | 706 | { |
| 556 | - $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); | |
| 557 | 708 | |
| 558 | 709 | if (!$result && $throwException) { |
| 559 | - 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')); | |
| 560 | 711 | } |
| 561 | 712 | return $result; |
| 562 | 713 | } |
| 563 | 714 | |
| @@ -569,8 +720,17 @@ | ||
| 569 | 720 | |
| 570 | 721 | return $this->hasCommunityPermission($permission); |
| 571 | 722 | } |
| 572 | 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 | + | |
| 573 | 733 | public function getUnreadNotificationCount() |
| 574 | 734 | { |
| 575 | 735 | return NotificationSubscriber::where('user_id', $this->ID) |
| 576 | 736 | ->unread() |
| @@ -588,44 +748,43 @@ | ||
| 588 | 748 | } |
| 589 | 749 | |
| 590 | 750 | public function isVerified() |
| 591 | 751 | { |
| 592 | - 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; | |
| 593 | 757 | } |
| 594 | 758 | |
| 595 | - public function syncXProfile($force = false) | |
| 759 | + public function syncXProfile($force = false, $useUserName = false) | |
| 596 | 760 | { |
| 597 | - $exist = $this->xprofile; | |
| 761 | + $exist = XProfile::where('user_id', $this->ID)->first(); | |
| 598 | 762 | |
| 599 | - if ($exist && !$force) { | |
| 763 | + if (($exist && !$force) || ($exist && Utility::getPrivacySetting('enable_user_sync') === 'no')) { | |
| 600 | 764 | return $exist; |
| 601 | 765 | } |
| 602 | 766 | |
| 603 | - $createdAt = $this->user_registered; | |
| 604 | - if (!$createdAt || $createdAt == '0000-00-00 00:00:00' || !strtotime($createdAt)) { | |
| 605 | - $createdAt = gmdate('Y-m-d H:i:s'); | |
| 606 | - } else { | |
| 607 | - $timeDiff = time() - current_time('timestamp'); | |
| 608 | - $createdAt = gmdate('Y-m-d H:i:s', strtotime($createdAt) - $timeDiff); | |
| 609 | - } | |
| 610 | - | |
| 611 | 767 | $data = [ |
| 612 | 768 | 'user_id' => $this->ID, |
| 613 | - 'username' => ProfileHelper::generateUserName($this->ID), | |
| 769 | + 'username' => ProfileHelper::generateUserName($this->ID, $useUserName), | |
| 614 | 770 | 'display_name' => $this->getDisplayName(), |
| 615 | - 'avatar' => $this->getPhotoAttribute(), | |
| 616 | 771 | 'is_verified' => $this->isVerified() ? 1 : 0, |
| 617 | - 'created_at' => $createdAt, | |
| 618 | 772 | 'short_description' => get_user_meta($this->ID, 'description', true), |
| 619 | 773 | 'meta' => [ |
| 620 | - 'website' => $this->user_url, | |
| 621 | - 'cover_photo' => get_user_meta($this->ID, '_fluent_cover_photo', true) | |
| 622 | - ] | |
| 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 | + ], | |
| 623 | 778 | ]; |
| 624 | 779 | |
| 625 | 780 | if ($exist) { |
| 626 | - unset($data['avatar']); | |
| 627 | - 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 | + | |
| 628 | 787 | $data['meta'] = wp_parse_args($exist->meta, $data['meta']); |
| 629 | 788 | $exist->fill($data); |
| 630 | 789 | $exist->save(); |
| 631 | 790 | return $exist; |
| @@ -630,22 +789,33 @@ | ||
| 630 | 789 | $exist->save(); |
| 631 | 790 | return $exist; |
| 632 | 791 | } |
| 633 | 792 | |
| 634 | - | |
| 635 | 793 | $counter = 1; |
| 636 | 794 | $initialUserName = $data['username']; |
| 637 | 795 | while (XProfile::where('username', $initialUserName)->first()) { |
| 638 | 796 | $initialUserName = $data['username'] . '_' . $counter; |
| 639 | - $counter++; | |
| 797 | + ++$counter; | |
| 640 | 798 | } |
| 641 | 799 | |
| 642 | 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 | + } | |
| 643 | 805 | |
| 644 | - return XProfile::create($data); | |
| 806 | + $xprofile = XProfile::create($data); | |
| 807 | + $this->load('xprofile'); | |
| 808 | + | |
| 809 | + return $xprofile; | |
| 645 | 810 | } |
| 646 | 811 | |
| 647 | 812 | public function getUserMeta($metaKey, $default = null) |
| 648 | 813 | { |
| 649 | 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); | |
| 650 | 820 | } |
| 651 | 821 | } |