fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Orm
/
UserProxyTrait.php
UserProxyTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.01, at vendor/wpfluent/framework/src/WPFluent/Database/Orm/UserProxyTrait.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Database\Orm; |
| 4 | |
| 5 | use WP_User; |
| 6 | use BadMethodCallException; |
| 7 | use InvalidArgumentException; |
| 8 | use FluentCommunity\Framework\Http\Request\WPUserProxy; |
| 9 | |
| 10 | trait UserProxyTrait |
| 11 | { |
| 12 | /** |
| 13 | * Resolve the WPUserProxy instance. |
| 14 | * |
| 15 | * Always returns a live WPUserProxy reflecting latest WP_User data. |
| 16 | * |
| 17 | * @return WPUserProxy |
| 18 | */ |
| 19 | protected function resolveWPUser() |
| 20 | { |
| 21 | $userIdentifier = $this->getKey() ?: wp_get_current_user(); |
| 22 | |
| 23 | return new WPUserProxy($userIdentifier); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Dual-purpose is() method: |
| 28 | * - is(string $role) - checks user role |
| 29 | * - is(User $model) - checks model identity |
| 30 | * |
| 31 | * @param string|self $value |
| 32 | * @return bool |
| 33 | */ |
| 34 | public function is($value) |
| 35 | { |
| 36 | if (is_string($value)) { |
| 37 | return $this->resolveWPUser()->is($value); |
| 38 | } |
| 39 | |
| 40 | if ($value instanceof static) { |
| 41 | return parent::is($value); |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Dynamically call methods on the WPUserProxy if not found in the model. |
| 49 | * |
| 50 | * @param string $method |
| 51 | * @param array $args |
| 52 | * @return mixed |
| 53 | * |
| 54 | * @throws BadMethodCallException|InvalidArgumentException |
| 55 | */ |
| 56 | public function __call($method, $args) |
| 57 | { |
| 58 | try { |
| 59 | return parent::__call($method, $args); |
| 60 | } catch (BadMethodCallException $e) { |
| 61 | try { |
| 62 | return $this->resolveWPUser()->$method(...$args); |
| 63 | } catch (BadMethodCallException $proxyError) { |
| 64 | throw $proxyError; |
| 65 | } catch (InvalidArgumentException $invalidUser) { |
| 66 | throw new BadMethodCallException( |
| 67 | "Call to undefined method " . static::class . "::{$method}()", |
| 68 | 0, |
| 69 | $invalidUser |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |