WowPress-Tailwind/theme/wowpress/Models/User.php

97 lines
2.3 KiB
PHP
Raw Normal View History

2024-04-22 15:38:22 +02:00
<?php
namespace WoWPress\Models;
use Wenprise\ORM\WP\User as WPUser;
2024-04-23 00:44:42 +02:00
class User extends WPUser
{
2024-04-22 15:38:22 +02:00
public static function logged_in(): User{
2024-06-11 12:19:18 +02:00
if(is_user_logged_in()){
return User::find(get_current_user_id());
}
return false;
}
2024-04-22 15:38:22 +02:00
2024-04-23 00:44:42 +02:00
public function characters()
{
2024-04-22 15:38:22 +02:00
return $this->hasMany(Character::class)->orderBy('rank');
}
2024-04-23 00:44:42 +02:00
public function raiders()
{
return $this->hasMany(Character::class, 'user_id')->where('raidchar', true)->orderBy('rank');
}
public static function getAvatar($avatar, $user_object)
{
if(is_numeric($user_object)){
$user = User::find($user_object);
if(!empty($user->ID)){
return $user->getAvatarAttribute();
}
}
if(!empty($user_object->user_id)){
$user = User::find($user_object->user_id);
if(!empty($user->ID)){
return $user->getAvatarAttribute();
}
}
return $avatar;
}
public function getAvatarAttribute(){
$chars = $this->characters;
if (!empty($chars->first())) {
return $chars->first()->avatar;
}
return $this->avatar_url;
}
2024-04-22 15:38:22 +02:00
2024-04-23 00:44:42 +02:00
public static function getUsername($username, $user_object)
{
if(is_numeric($user_object)){
$user = User::find($user_object);
if(!empty($user->ID)){
return $user->getUsernameAttribute();
}
}
if (!empty($user_object->user_id)) {
$user = User::find($user_object->user_id);
return $user->getUsernameAttribute();
}
if (!empty($user_object->ID)) {
$user = User::find($user_object->ID);
return $user->getUsernameAttribute();
}
return $username;
}
public function getUsernameAttribute()
{
$chars = $this->characters;
if (!empty($chars->first())) {
return $chars->first()->name;
}
return $this->user_nickname ?: $this->login;
}
public function getColorAttribute()
{
$chars = $this->characters;
if (!empty($chars->first())) {
return $chars->first()->color;
}
return "priest";
}
public function can($capability){
return user_can($this->ID,$capability);
}
2024-04-23 00:44:42 +02:00
}