PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / trunk
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity vtrunk
3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.6.1 All 66 releases
logtivity / Core / Admin / Logtivity_Wp_User.php

Logtivity_Wp_User.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity trunk, at Core/Admin/Logtivity_Wp_User.php

229 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, hello@logtivity.io
6 * @copyright 2024-2026 Logtivity. All rights reserved
7 * @license https://www.gnu.org/licenses/gpl.html GNU/GPL
8 *
9 * This file is part of Logtivity.
10 *
11 * Logtivity is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Logtivity is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Logtivity. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25 class Logtivity_Wp_User
26 {
27 /**
28 * @var ?WP_User
29 */
30 protected ?WP_User $user = null;
31
32 /**
33 * @param null|int|WP_User|Logtivity_Wp_User $user
34 * @param string $field
35 */
36 public function __construct($user = null, string $field = 'ID')
37 {
38 if (is_null($user)) {
39 if (function_exists('wp_get_current_user')) {
40 $this->user = wp_get_current_user();
41 }
42
43 } elseif ($user instanceof WP_User) {
44 $this->user = $user;
45
46 } elseif ($user instanceof static) {
47 $this->user = $user->user;
48
49 } elseif (is_scalar($user) && function_exists('get_user_by')) {
50 $this->user = get_user_by($field, $user) ?: null;
51 }
52 }
53
54 /**
55 * @param string $name
56 *
57 * @return ?mixed
58 */
59 public function __get(string $name)
60 {
61 if (property_exists($this, $name)) {
62 return $this->{$name};
63 }
64
65 return null;
66 }
67
68 /**
69 * @param string $field
70 * @param mixed $value
71 *
72 * @return $this
73 */
74 public function findByUserMeta(string $field, $value): self
75 {
76 $users = get_users([
77 'meta_key' => $field,
78 'meta_value' => $value,
79 ]);
80
81 $this->user = $users ? array_shift($users) : null;
82
83 return $this;
84 }
85
86 /**
87 * @return ?int
88 */
89 public function id(): ?int
90 {
91 return $this->user->ID ?? null;
92 }
93
94 /**
95 * @return string
96 */
97 public function userLogin(): string
98 {
99 return $this->user->user_login ?? '';
100 }
101
102 /**
103 * @return string
104 */
105 public function email(): string
106 {
107 return $this->user->user_email ?? '';
108 }
109
110 /**
111 * @return ?string
112 */
113 public function name(): string
114 {
115 return trim($this->firstName() . ' ' . $this->lastName());
116 }
117
118 /**
119 * @return string
120 */
121 public function firstName(): string
122 {
123 return $this->meta('first_name');
124 }
125
126 /**
127 * @return string
128 */
129 public function lastName(): string
130 {
131 return $this->meta('last_name');
132 }
133
134 /**
135 * @return string
136 */
137 public function displayName(): string
138 {
139 return $this->user->display_name ?? '';
140 }
141
142 /**
143 * @return string
144 */
145 public function niceName(): string
146 {
147 return $this->user->user_nicename ?? '';
148 }
149
150 /**
151 * @return string
152 */
153 public function profileLink(): string
154 {
155 if ($this->user) {
156 return add_query_arg('user_id', $this->id(), self_admin_url('user-edit.php'));
157 }
158
159 return '';
160 }
161
162 /**
163 * @param string $metaKey
164 * @param bool $returnString (optional)
165 *
166 * @return mixed
167 */
168 public function meta(string $metaKey, bool $returnString = true)
169 {
170 return $this->user ? get_user_meta($this->user->ID, $metaKey, $returnString) : '';
171 }
172
173 /**
174 * Does the fetched user exist
175 *
176 * @return bool
177 */
178 public function exists(): bool
179 {
180 return (bool)$this->user;
181 }
182
183 /**
184 * @return bool
185 */
186 public function isLoggedIn(): bool
187 {
188 return ($this->user->ID ?? false);
189 }
190
191 /**
192 * @param string $role
193 *
194 * @return bool
195 */
196 public function hasRole(string $role): bool
197 {
198 return in_array($role, $this->getRoles());
199 }
200
201 /**
202 * @return array
203 */
204 public function getRoles(): array
205 {
206 return $this->user->roles ?? [];
207 }
208
209 /**
210 * Get the users first role
211 *
212 * @return string
213 */
214 public function getRole(): string
215 {
216 $roles = $this->getRoles();
217
218 return array_shift($roles) ?: '';
219 }
220
221 /**
222 * @return array
223 */
224 public function to_array(): array
225 {
226 return $this->user->to_array();
227 }
228 }
229