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 / Loggers / Core / Logtivity_User.php

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

256 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, 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_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 if ($user = get_user_by('login', $username)) {
177 $this->saveOriginalUser(null, $user->ID);
178 }
179
180 Logtivity::log('Password Reset Sent')
181 ->setContext($username)
182 ->send();
183 }
184
185 /**
186 * @param string $password
187 * @param int $userId
188 * @param WP_User $oldUser
189 *
190 * @return void
191 */
192 public function wp_set_password($password, $userId, $oldUser): void
193 {
194 $this->saveOriginalMetadata($userId);
195
196 $this->profile_update($userId, $oldUser);
197 }
198
199 /**
200 * Cover bulk update of role changes
201 *
202 * @param int $userId
203 *
204 * @return void
205 */
206 public function set_user_role($userId): void
207 {
208 if ($this->user[$userId] ?? null) {
209 $this->profile_update($userId, $this->user[$userId]);
210 }
211 }
212
213 /**
214 * @param ?int $metaId
215 * @param int $objectId
216 *
217 * @return void
218 */
219 public function saveOriginalUser($metaId, $objectId): void
220 {
221 if (empty($this->meta[$objectId])) {
222 $this->saveOriginalMetadata($objectId);
223 $this->user[$objectId] = get_user($objectId);
224 }
225 }
226
227 /**
228 * @param int $userId
229 *
230 * @return void
231 */
232 public function saveOriginalMetadata(int $userId): void
233 {
234 if (empty($this->meta[$userId])) {
235 $this->meta[$userId] = $this->getMetadata('user', $userId);
236 }
237 }
238
239 /**
240 * @inheritDoc
241 */
242 protected function sanitizeDataFields(array $fields, array $ignoredKeys = []): array
243 {
244 if (array_key_exists('user_pass', $fields)) {
245 unset($fields['user_pass']);
246 }
247 if (array_key_exists('user_activation_key', $fields)) {
248 unset($fields['user_activation_key']);
249 }
250
251 return parent::sanitizeDataFields($fields);
252 }
253 }
254
255 new Logtivity_User();
256