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