PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.9
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.9
3.3.9 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 All 67 releases
logtivity / Core / Admin / Logtivity_Wp_User.php

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

231 lines 4.8 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, [email protected]
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 'number' => 1,
80 'count_total' => false,
81 ]);
82
83 $this->user = $users ? array_shift($users) : null;
84
85 return $this;
86 }
87
88 /**
89 * @return ?int
90 */
91 public function id(): ?int
92 {
93 return $this->user->ID ?? null;
94 }
95
96 /**
97 * @return string
98 */
99 public function userLogin(): string
100 {
101 return $this->user->user_login ?? '';
102 }
103
104 /**
105 * @return string
106 */
107 public function email(): string
108 {
109 return $this->user->user_email ?? '';
110 }
111
112 /**
113 * @return ?string
114 */
115 public function name(): string
116 {
117 return trim($this->firstName() . ' ' . $this->lastName());
118 }
119
120 /**
121 * @return string
122 */
123 public function firstName(): string
124 {
125 return $this->meta('first_name');
126 }
127
128 /**
129 * @return string
130 */
131 public function lastName(): string
132 {
133 return $this->meta('last_name');
134 }
135
136 /**
137 * @return string
138 */
139 public function displayName(): string
140 {
141 return $this->user->display_name ?? '';
142 }
143
144 /**
145 * @return string
146 */
147 public function niceName(): string
148 {
149 return $this->user->user_nicename ?? '';
150 }
151
152 /**
153 * @return string
154 */
155 public function profileLink(): string
156 {
157 if ($this->user) {
158 return add_query_arg('user_id', $this->id(), self_admin_url('user-edit.php'));
159 }
160
161 return '';
162 }
163
164 /**
165 * @param string $metaKey
166 * @param bool $returnString (optional)
167 *
168 * @return mixed
169 */
170 public function meta(string $metaKey, bool $returnString = true)
171 {
172 return $this->user ? get_user_meta($this->user->ID, $metaKey, $returnString) : '';
173 }
174
175 /**
176 * Does the fetched user exist
177 *
178 * @return bool
179 */
180 public function exists(): bool
181 {
182 return (bool)$this->user;
183 }
184
185 /**
186 * @return bool
187 */
188 public function isLoggedIn(): bool
189 {
190 return ($this->user->ID ?? false);
191 }
192
193 /**
194 * @param string $role
195 *
196 * @return bool
197 */
198 public function hasRole(string $role): bool
199 {
200 return in_array($role, $this->getRoles());
201 }
202
203 /**
204 * @return array
205 */
206 public function getRoles(): array
207 {
208 return $this->user->roles ?? [];
209 }
210
211 /**
212 * Get the users first role
213 *
214 * @return string
215 */
216 public function getRole(): string
217 {
218 $roles = $this->getRoles();
219
220 return array_shift($roles) ?: '';
221 }
222
223 /**
224 * @return array
225 */
226 public function to_array(): array
227 {
228 return $this->user->to_array();
229 }
230 }
231