| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Wp_User |
| 4 |
{ |
| 5 |
protected $user; |
| 6 |
|
| 7 |
public function __construct($user = null, $field = 'ID') |
| 8 |
{ |
| 9 |
if (is_null($user)) |
| 10 |
{ |
| 11 |
$this->user = wp_get_current_user(); |
| 12 |
} |
| 13 |
elseif ($user instanceof WP_User) |
| 14 |
{ |
| 15 |
$this->user = $user; |
| 16 |
} |
| 17 |
else |
| 18 |
{ |
| 19 |
$this->user = get_user_by($field, $user); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public function findByUserMeta($field, $value) |
| 24 |
{ |
| 25 |
$users = get_users([ |
| 26 |
'meta_key' => $field, |
| 27 |
'meta_value' => $value |
| 28 |
]); |
| 29 |
|
| 30 |
if (count($users)) |
| 31 |
{ |
| 32 |
$this->user = $users[0]; |
| 33 |
} |
| 34 |
else |
| 35 |
{ |
| 36 |
$this->user = false; |
| 37 |
} |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function id() |
| 43 |
{ |
| 44 |
return $this->user->ID; |
| 45 |
} |
| 46 |
|
| 47 |
public function userLogin() |
| 48 |
{ |
| 49 |
return $this->user->user_login; |
| 50 |
} |
| 51 |
|
| 52 |
public function email() |
| 53 |
{ |
| 54 |
return $this->user->user_email; |
| 55 |
} |
| 56 |
|
| 57 |
public function name() |
| 58 |
{ |
| 59 |
return $this->firstName() . ' ' . $this->lastName(); |
| 60 |
} |
| 61 |
|
| 62 |
public function firstName() |
| 63 |
{ |
| 64 |
return $this->meta('first_name'); |
| 65 |
} |
| 66 |
|
| 67 |
public function lastName() |
| 68 |
{ |
| 69 |
return $this->meta('last_name'); |
| 70 |
} |
| 71 |
|
| 72 |
public function displayName() |
| 73 |
{ |
| 74 |
return $this->user->display_name; |
| 75 |
} |
| 76 |
|
| 77 |
public function niceName() |
| 78 |
{ |
| 79 |
return $this->user->user_nicename; |
| 80 |
} |
| 81 |
|
| 82 |
public function profileLink() |
| 83 |
{ |
| 84 |
return add_query_arg( 'user_id', $this->id(), self_admin_url( 'user-edit.php' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get user meta |
| 89 |
* |
| 90 |
* @param string $meta_key |
| 91 |
* @param boolean $returnString |
| 92 |
* @return string/null |
| 93 |
*/ |
| 94 |
public function meta($meta_key, $returnString = true) |
| 95 |
{ |
| 96 |
$meta = get_user_meta($this->user->ID, $meta_key, $returnString); |
| 97 |
|
| 98 |
if ($meta != '') |
| 99 |
{ |
| 100 |
return $meta; |
| 101 |
} |
| 102 |
|
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Does the fetched user exist |
| 108 |
* |
| 109 |
* @return boolean |
| 110 |
*/ |
| 111 |
public function exists() |
| 112 |
{ |
| 113 |
return $this->user; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Is the user logged in |
| 118 |
* |
| 119 |
* @return boolean |
| 120 |
*/ |
| 121 |
public function isLoggedIn() |
| 122 |
{ |
| 123 |
if ($this->user->ID == 0) |
| 124 |
{ |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Handles one user role. Useful for determining in more detail difference between employer and |
| 133 |
* employer_premium for instance. |
| 134 |
* |
| 135 |
* @param string $role |
| 136 |
* @return boolean |
| 137 |
*/ |
| 138 |
public function hasRole($role) |
| 139 |
{ |
| 140 |
if ( in_array($role, $this->getRoles()) ) |
| 141 |
{ |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get the users roles |
| 150 |
* |
| 151 |
* @return array |
| 152 |
*/ |
| 153 |
public function getRoles() |
| 154 |
{ |
| 155 |
return $this->user->roles; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get the users first role |
| 160 |
* |
| 161 |
* @return string|false |
| 162 |
*/ |
| 163 |
public function getRole() |
| 164 |
{ |
| 165 |
foreach ($this->getRoles() as $role) { |
| 166 |
return $role; |
| 167 |
} |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|