PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 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 All 68 releases
logtivity / Loggers / Core / Logtivity_User.php

Logtivity_User.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Loggers/Core/Logtivity_User.php

257 lines 7.3 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_User extends Logtivity_Abstract_Logger
26 {
27 /**
28 * @var array
29 */
30 protected array $meta = [];
31
32 /**
33 * @var WP_User[]
34 */
35 protected array $user = [];
36
37 /**
38 * @inheritDoc
39 */
40 protected function registerHooks(): void
41 {
42 add_action('wp_login', [$this, 'wp_login'], 10, 2);
43 add_action('wp_logout', [$this, 'wp_logout'], 10, 1);
44 add_action('user_register', [$this, 'user_register'], 10, 2);
45 add_action('deleted_user', [$this, 'deleted_user'], 10, 3);
46
47 add_action('personal_options_update', [$this, 'saveOriginalMetadata'], 10, 1); // Profile page
48 add_action('edit_user_profile_update', [$this, 'saveOriginalMetadata'], 10, 1); // Editing other user
49 add_action('update_user_meta', [$this, 'saveOriginalUser'], 10, 4);
50 add_action('profile_update', [$this, 'profile_update'], 10, 3);
51 add_action('retrieve_password', [$this, 'retrieve_password'], 10, 1);
52 add_action('wp_set_password', [$this, 'wp_set_password'], 10, 3);
53
54 // Triggered from bulk updates
55 add_action('set_user_role', [$this, 'set_user_role'], 10, 3);
56 }
57
58 /**
59 * @param string $username
60 * @param WP_User $user
61 *
62 * @return void
63 */
64 public function wp_login($username, $user): void
65 {
66 $log = Logtivity::log()->setUser($user);
67
68 $log->setAction('User Logged In')->setContext($log->user->getRole())->send();
69 }
70
71 /**
72 * @param int $userId
73 *
74 * @return void
75 */
76 public function wp_logout($userId): void
77 {
78 if ($userId) {
79 $logger = Logtivity::log()->setUser($userId);
80
81 $logger
82 ->setAction('User Logged Out')
83 ->setContext($logger->user->getRole())
84 ->send();
85 }
86 }
87
88 /**
89 * @param int $userId
90 *
91 * @return void
92 */
93 public function user_register($userId): void
94 {
95 $log = Logtivity::log();
96
97 if (is_user_logged_in() == false) {
98 $log->setUser($userId);
99 $user = $log->user;
100 } else {
101 $user = new Logtivity_WP_User($userId);
102 }
103
104 $meta = $this->getMetadata('user', $userId);
105
106 Logtivity::log('User Created')
107 ->setContext($user->getRole())
108 ->addMetaArray($this->sanitizeDataFields($user->to_array()))
109 ->addMetaArray($this->sanitizeDataFields($meta))
110 ->send();
111 }
112
113 /**
114 * @param int $userId
115 * @param ?int $reassign
116 * @param WP_User $user
117 *
118 * @return void
119 */
120 public function deleted_user($userId, $reassign, $user): void
121 {
122 $user = new Logtivity_WP_User($user);
123 $assignToUser = $reassign ? get_user($reassign) : null;
124 $meta = $this->getMetadata('user', $userId);
125
126 Logtivity::log('User Deleted')
127 ->setContext($user->getRole())
128 ->addMetaIf($assignToUser, 'Reassign Content To', $assignToUser->user_login)
129 ->addMetaArray($this->sanitizeDataFields($user->to_array()))
130 ->addMetaArray($this->sanitizeDataFields($meta))
131 ->send();
132 }
133
134 /**
135 * @param int $userId
136 * @param WP_User $oldUser
137 *
138 * @return void
139 */
140 public function profile_update($userId, $oldUser): void
141 {
142 $user = new Logtivity_Wp_User($userId);
143
144 $oldUserdata = $oldUser->to_array();
145 $userdata = $user->to_array();
146
147 $oldMeta = $this->meta[$userId] ?? [];
148 $meta = $this->getMetadata('user', $userId);
149
150 $activationKey = $userdata['user_activation_key'];
151 $passwordChange = $oldUserdata['user_pass'] != $userdata['user_pass'];
152
153 $oldUserdata = $this->sanitizeDataFields($oldUserdata);
154 $userdata = $this->sanitizeDataFields($userdata);
155 if ($passwordChange || $oldUserdata != $userdata || $oldMeta != $meta) {
156 Logtivity::log('User Updated')
157 ->setContext($user->getRole())
158 ->addMeta('User ID', $user->id())
159 ->addMeta('Username', $user->userLogin())
160 ->addMeta('Role', $user->getRole())
161 ->addMetaIf($passwordChange, 'Password Changed', 'Yes')
162 ->addMetaIf($activationKey, 'Activation Pending', 'Yes')
163 ->addMetaChanges($oldUserdata, $userdata)
164 ->addMetaChanges($oldMeta, $meta)
165 ->send();
166 }
167 }
168
169 /**
170 * @param string $username
171 *
172 * @return void
173 */
174 public function retrieve_password($username): void
175 {
176 $user = get_user_by('login', $username);
177 if ($user) {
178 $this->saveOriginalUser(null, $user->ID);
179 }
180
181 Logtivity::log('Password Reset Sent')
182 ->setContext($username)
183 ->send();
184 }
185
186 /**
187 * @param string $password
188 * @param int $userId
189 * @param WP_User $oldUser
190 *
191 * @return void
192 */
193 public function wp_set_password($password, $userId, $oldUser): void
194 {
195 $this->saveOriginalMetadata($userId);
196
197 $this->profile_update($userId, $oldUser);
198 }
199
200 /**
201 * Cover bulk update of role changes
202 *
203 * @param int $userId
204 *
205 * @return void
206 */
207 public function set_user_role($userId): void
208 {
209 if ($this->user[$userId] ?? null) {
210 $this->profile_update($userId, $this->user[$userId]);
211 }
212 }
213
214 /**
215 * @param ?int $metaId
216 * @param int $objectId
217 *
218 * @return void
219 */
220 public function saveOriginalUser($metaId, $objectId): void
221 {
222 if (empty($this->meta[$objectId])) {
223 $this->saveOriginalMetadata($objectId);
224 $this->user[$objectId] = get_user($objectId);
225 }
226 }
227
228 /**
229 * @param int $userId
230 *
231 * @return void
232 */
233 public function saveOriginalMetadata(int $userId): void
234 {
235 if (empty($this->meta[$userId])) {
236 $this->meta[$userId] = $this->getMetadata('user', $userId);
237 }
238 }
239
240 /**
241 * @inheritDoc
242 */
243 protected function sanitizeDataFields(array $fields, array $ignoredKeys = []): array
244 {
245 if (array_key_exists('user_pass', $fields)) {
246 unset($fields['user_pass']);
247 }
248 if (array_key_exists('user_activation_key', $fields)) {
249 unset($fields['user_activation_key']);
250 }
251
252 return parent::sanitizeDataFields($fields);
253 }
254 }
255
256 new Logtivity_User();
257