| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Http\Request; |
| 4 |
|
| 5 |
use FluentBoards\Framework\Support\Str; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class FluentBoards\Framework\Framework\Http\Request\WPUserProxy |
| 9 |
* |
| 10 |
* @method void __construct() Constructs the class |
| 11 |
* @method void init() Initializes the object |
| 12 |
* @method mixed getDataBy(string $field, mixed $value) Retrieves data by field |
| 13 |
* @method bool __isset(string $name) Checks if a property is set |
| 14 |
* @method mixed __get(string $name) Dynamically gets a property |
| 15 |
* @method void __set(string $name, mixed $value) Dynamically sets a property |
| 16 |
* @method void __unset(string $name) Unsets a property |
| 17 |
* @method bool exists() Checks existence of something |
| 18 |
* @method mixed get(string $key) Gets a value by key |
| 19 |
* @method bool hasProp(string $prop) Checks if a property exists |
| 20 |
* @method array getRoleCaps() Gets role capabilities |
| 21 |
* @method void addRole(string $role, string $displayName, array $caps) Adds a role |
| 22 |
* @method void removeRole(string $role) Removes a role |
| 23 |
* @method void setRole(string $role) Sets a role |
| 24 |
* @method void levelReduction() Reduces levels |
| 25 |
* @method void updateUserLevelFromCaps() Updates user level from capabilities |
| 26 |
* @method void addCap(string $cap) Adds a capability |
| 27 |
* @method void removeCap(string $cap) Removes a capability |
| 28 |
* @method void removeAllCaps() Removes all capabilities |
| 29 |
* @method bool hasCap(string $cap) Checks if the object has a capability |
| 30 |
* @method string translateLevelToCap(int $level) Translates a level to a capability |
| 31 |
* @method self forBlog(int $blogId) Switches context to a blog |
| 32 |
* @method self forSite(int $siteId) Switches context to a site |
| 33 |
* @method int getSiteId() Gets the site ID |
| 34 |
*/ |
| 35 |
class WPUserProxy |
| 36 |
{ |
| 37 |
/** |
| 38 |
* The WP_User instance. |
| 39 |
* @var \WP_User |
| 40 |
*/ |
| 41 |
protected $wpUser; |
| 42 |
|
| 43 |
/** |
| 44 |
* Methods to proxy. |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
protected $passthrough = [ |
| 48 |
'can' => 'has_cap', |
| 49 |
]; |
| 50 |
|
| 51 |
/** |
| 52 |
* User Meta. |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
protected $meta = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Construct the proxy. |
| 59 |
* |
| 60 |
* @param \WP_User $wpUser |
| 61 |
*/ |
| 62 |
public function __construct($wpUser) |
| 63 |
{ |
| 64 |
if (is_int($wpUser)) { |
| 65 |
$wpUser = get_user_by('id', $wpUser); |
| 66 |
} elseif (is_string($wpUser) && str_contains($wpUser, '@')) { |
| 67 |
$wpUser = get_user_by('email', $wpUser); |
| 68 |
} |
| 69 |
|
| 70 |
if (!$wpUser instanceof \WP_User) { |
| 71 |
throw new \InvalidArgumentException('Invalid user'); |
| 72 |
} |
| 73 |
|
| 74 |
$this->wpUser = $wpUser; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Retrieve the ID of the current user. |
| 79 |
* |
| 80 |
* @return int |
| 81 |
*/ |
| 82 |
public function id() |
| 83 |
{ |
| 84 |
return $this->wpUser->ID; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Retrieve the email of the current user. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function email() |
| 93 |
{ |
| 94 |
return $this->wpUser->user_email; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieve the user_login of the current user. |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public function login() |
| 103 |
{ |
| 104 |
return $this->wpUser->user_login; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Retrieve the user_nicename of the current user. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public function nicename() |
| 113 |
{ |
| 114 |
return $this->wpUser->user_nicename; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Retrieve the user_status of the current user. |
| 119 |
* |
| 120 |
* @return int |
| 121 |
*/ |
| 122 |
public function status() |
| 123 |
{ |
| 124 |
return $this->wpUser->user_status; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Retrieve the display_name of the current user. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public function displayName() |
| 133 |
{ |
| 134 |
return $this->wpUser->display_name; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the roles of the current user. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function getRoles() |
| 143 |
{ |
| 144 |
return $this->wpUser->roles; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get the permissions of the current user. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function getPermissions() |
| 153 |
{ |
| 154 |
$getPermissions = []; |
| 155 |
|
| 156 |
foreach ($this->wpUser->get_role_caps() as $permission => $value) { |
| 157 |
if ($value) $permissions[] = $permission; |
| 158 |
} |
| 159 |
|
| 160 |
return $permissions; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get the meta value(s) of the current user. |
| 165 |
* |
| 166 |
* @return mixed |
| 167 |
*/ |
| 168 |
public function getMeta($metaKey = null, $default = null) |
| 169 |
{ |
| 170 |
if (is_null($this->meta)) { |
| 171 |
foreach (get_user_meta($this->wpUser->ID) as $key => $value) { |
| 172 |
if ($key === 'session_tokens') continue; |
| 173 |
$this->meta[$key] = maybe_unserialize($value[0]); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (!$metaKey) { |
| 178 |
return $this->meta; |
| 179 |
} |
| 180 |
|
| 181 |
return $this->meta[$metaKey] ?: $default; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Set the meta value of the current user. |
| 186 |
* |
| 187 |
* @param string $key |
| 188 |
* @param mixed $value |
| 189 |
* @return bool |
| 190 |
*/ |
| 191 |
public function setMeta($key, $value) |
| 192 |
{ |
| 193 |
if (is_array($value) || is_object($value)) { |
| 194 |
$value = maybe_serialize($value); |
| 195 |
} |
| 196 |
|
| 197 |
return update_user_meta($this->wpUser->ID, $key, $value); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Get the underlying WP_User instance. |
| 202 |
* @return [type] [description] |
| 203 |
*/ |
| 204 |
public function toBase() |
| 205 |
{ |
| 206 |
return $this->wpUser; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Checks if super admin (in multi-site) |
| 211 |
* |
| 212 |
* @return boolean |
| 213 |
*/ |
| 214 |
public function isSuperAdmin() |
| 215 |
{ |
| 216 |
return is_super_admin($this->wpUser->ID); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Check if the currently logged in user is an admin. |
| 221 |
* |
| 222 |
* @return boolean |
| 223 |
*/ |
| 224 |
public function isAdmin() |
| 225 |
{ |
| 226 |
return in_array('administrator', $this->roles); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get a property from the WP_User instance. |
| 231 |
* |
| 232 |
* @param string $key |
| 233 |
* @return mixed |
| 234 |
* @throws \OutOfBoundsException |
| 235 |
*/ |
| 236 |
public function __get($key) |
| 237 |
{ |
| 238 |
return $this->wpUser->{$key}; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Set a property on the WP_User instance. |
| 243 |
* |
| 244 |
* @param string $key |
| 245 |
* @param mixed $value |
| 246 |
*/ |
| 247 |
public function __set($key, $value) |
| 248 |
{ |
| 249 |
$this->wpUser->{$key} = $value; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Checks if a property exists on the WP_User instance. |
| 254 |
* |
| 255 |
* @param string $key |
| 256 |
* @return boolean |
| 257 |
*/ |
| 258 |
public function __isset($key) |
| 259 |
{ |
| 260 |
return isset($this->wpUser->$key); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Unsets a property from the WP_User instance. |
| 265 |
* |
| 266 |
* @param string $key |
| 267 |
*/ |
| 268 |
public function __unset($key) |
| 269 |
{ |
| 270 |
unset($this->wpUser->$key); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Handles method calls on the WP_User instance. |
| 275 |
* |
| 276 |
* @param string $method |
| 277 |
* @param array $args |
| 278 |
* @return mixed |
| 279 |
* @throws \BadMethodCallException |
| 280 |
*/ |
| 281 |
public function __call($method, $args) |
| 282 |
{ |
| 283 |
if (!method_exists($this->wpUser, $method)) { |
| 284 |
if (array_key_exists($method, $this->passthrough)) { |
| 285 |
$method = $this->passthrough[$method]; |
| 286 |
} else { |
| 287 |
$method = Str::snake($method); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (method_exists($this->wpUser, $method)) { |
| 292 |
return $this->wpUser->{$method}(...$args); |
| 293 |
} |
| 294 |
|
| 295 |
throw new \BadMethodCallException( |
| 296 |
"Method {$method} does not exist on WP_User" |
| 297 |
); |
| 298 |
} |
| 299 |
} |
| 300 |
|