| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class UserParser extends BaseParser |
| 8 |
{ |
| 9 |
private $user; |
| 10 |
private $userId; |
| 11 |
|
| 12 |
public function __construct($data) |
| 13 |
{ |
| 14 |
if (Arr::has($data, 'user_id')) { |
| 15 |
$uId = Arr::get($data, 'user_id', null); |
| 16 |
} else { |
| 17 |
$uId = Arr::get($data, 'customer.user_id', null); |
| 18 |
} |
| 19 |
|
| 20 |
$this->userId = $uId; |
| 21 |
$this->setUser(); |
| 22 |
parent::__construct($data); |
| 23 |
} |
| 24 |
|
| 25 |
protected array $methodMap = [ |
| 26 |
'admin_email' => 'getAdminEmail', |
| 27 |
'site_url' => 'getSiteUrl', |
| 28 |
'site_name' => 'getSiteName', |
| 29 |
]; |
| 30 |
|
| 31 |
protected function setUser() |
| 32 |
{ |
| 33 |
$this->user = get_user_by('ID', $this->userId) ?: null; |
| 34 |
} |
| 35 |
|
| 36 |
public function parse($accessor = null, $code = null): ?string |
| 37 |
{ |
| 38 |
return $this->get($accessor,$code); |
| 39 |
} |
| 40 |
|
| 41 |
public function getID() |
| 42 |
{ |
| 43 |
return $this->userId; |
| 44 |
} |
| 45 |
|
| 46 |
public function getFirstName() |
| 47 |
{ |
| 48 |
return $this->getDataFromUser('first_name'); |
| 49 |
} |
| 50 |
|
| 51 |
public function getLastName() |
| 52 |
{ |
| 53 |
return $this->getDataFromUser('last_name'); |
| 54 |
} |
| 55 |
|
| 56 |
public function getDisplayName() |
| 57 |
{ |
| 58 |
return $this->getDataFromUser('display_name'); |
| 59 |
} |
| 60 |
public function getEmail() |
| 61 |
{ |
| 62 |
return $this->getDataFromUser('user_email'); |
| 63 |
} |
| 64 |
|
| 65 |
public function getUserEmail() |
| 66 |
{ |
| 67 |
return $this->getDataFromUser('user_email'); |
| 68 |
} |
| 69 |
|
| 70 |
private function getDataFromUser(string $key) |
| 71 |
{ |
| 72 |
if (empty($key) || empty($this->user)) { |
| 73 |
return ''; |
| 74 |
} |
| 75 |
|
| 76 |
if ($key === 'first_name' || $key === 'last_name') { |
| 77 |
return get_user_meta($this->userId, $key, true) ?: ''; |
| 78 |
} |
| 79 |
|
| 80 |
return $this->user->{$key} ?? ''; |
| 81 |
} |
| 82 |
} |
| 83 |
|