PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.8.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.8.0
1.9.0 1.8.0 1.7.1 1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 All 53 releases
fluent-cart / app / Services / ShortCodeParser / Parsers / UserParser.php

UserParser.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.8.0, at app/Services/ShortCodeParser/Parsers/UserParser.php

99 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Anything unlisted would fall through to Arr::get() on the whole parser data
32 // and could walk into the order, its customer and their WP user.
33 protected array $allowedAccessors = [
34 'ID', 'id', 'first_name', 'last_name', 'display_name', 'email', 'user_email', 'user_login',
35 ];
36
37 protected function setUser()
38 {
39 $this->user = get_user_by('ID', $this->userId) ?: null;
40 }
41
42 public function parse($accessor = null, $code = null): ?string
43 {
44 if (!in_array($accessor, $this->allowedAccessors, true)) {
45 return '';
46 }
47
48 return $this->get($accessor, $code);
49 }
50
51 public function getID()
52 {
53 return $this->userId;
54 }
55
56 public function getFirstName()
57 {
58 return $this->getDataFromUser('first_name');
59 }
60
61 public function getLastName()
62 {
63 return $this->getDataFromUser('last_name');
64 }
65
66 public function getDisplayName()
67 {
68 return $this->getDataFromUser('display_name');
69 }
70 public function getEmail()
71 {
72 return $this->getDataFromUser('user_email');
73 }
74
75 public function getUserEmail()
76 {
77 return $this->getDataFromUser('user_email');
78 }
79
80 // Half of the login credentials: admin previews only, never customer-facing renders.
81 public function getUserLogin()
82 {
83 return current_user_can('manage_options') ? $this->getDataFromUser('user_login') : '';
84 }
85
86 private function getDataFromUser(string $key)
87 {
88 if (empty($key) || empty($this->user)) {
89 return '';
90 }
91
92 if ($key === 'first_name' || $key === 'last_name') {
93 return get_user_meta($this->userId, $key, true) ?: '';
94 }
95
96 return $this->user->{$key} ?? '';
97 }
98 }
99