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 1.0.94, at vendor/wpfluent/framework/src/WPFluent/Http/Request/WPUserProxy.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Http\Request; |
| 4 | |
| 5 | use FluentCommunity\Framework\Support\Str; |
| 6 | |
| 7 | class WPUserProxy |
| 8 | { |
| 9 | /** |
| 10 | * The WP_User instance. |
| 11 | * @var \WP_User |
| 12 | */ |
| 13 | protected $wpUser; |
| 14 | |
| 15 | /** |
| 16 | * Methods to proxy. |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $passthrough = [ |
| 20 | 'can' => 'has_cap', |
| 21 | ]; |
| 22 | |
| 23 | /** |
| 24 | * Construct the proxy. |
| 25 | * |
| 26 | * @param \WP_User $wpUser |
| 27 | */ |
| 28 | public function __construct(\WP_User $wpUser) |
| 29 | { |
| 30 | $this->wpUser = $wpUser; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Checks if super admin (in multi-site) |
| 35 | * |
| 36 | * @return boolean |
| 37 | */ |
| 38 | public function isSuperAdmin() |
| 39 | { |
| 40 | return is_super_admin($this->wpUser->ID); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Check if the currently logged in user is an admin. |
| 45 | * |
| 46 | * @return boolean |
| 47 | */ |
| 48 | public function isAdmin() |
| 49 | { |
| 50 | return in_array('administrator', $this->roles); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get a property from the WP_User instance. |
| 55 | * |
| 56 | * @param string $key |
| 57 | * @return mixed |
| 58 | * @throws \OutOfBoundsException |
| 59 | */ |
| 60 | public function __get($key) |
| 61 | { |
| 62 | if (isset($this->wpUser->$key)) { |
| 63 | return $this->wpUser->$key; |
| 64 | } |
| 65 | |
| 66 | throw new \OutOfBoundsException( |
| 67 | "Property {$key} does not exist on WP_User" |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set a property on the WP_User instance. |
| 73 | * |
| 74 | * @param string $key |
| 75 | * @param mixed $value |
| 76 | */ |
| 77 | public function __set($key, $value) |
| 78 | { |
| 79 | $this->wpUser->$key = $value; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks if a property exists on the WP_User instance. |
| 84 | * |
| 85 | * @param string $key |
| 86 | * @return boolean |
| 87 | */ |
| 88 | public function __isset($key) |
| 89 | { |
| 90 | return isset($this->wpUser->$key); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Unsets a property from the WP_User instance. |
| 95 | * |
| 96 | * @param string $key |
| 97 | */ |
| 98 | public function __unset($key) |
| 99 | { |
| 100 | unset($this->wpUser->$key); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Handles method calls on the WP_User instance. |
| 105 | * |
| 106 | * @param string $method |
| 107 | * @param array $args |
| 108 | * @return mixed |
| 109 | * @throws \BadMethodCallException |
| 110 | */ |
| 111 | public function __call($method, $args) |
| 112 | { |
| 113 | if (!method_exists($this->wpUser, $method)) { |
| 114 | if (array_key_exists($method, $this->passthrough)) { |
| 115 | $method = $this->passthrough[$method]; |
| 116 | } else { |
| 117 | $method = Str::snake($method); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (method_exists($this->wpUser, $method)) { |
| 122 | return $this->wpUser->{$method}(...$args); |
| 123 | } |
| 124 | |
| 125 | throw new \BadMethodCallException( |
| 126 | "Method {$method} does not exist on WP_User" |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 |