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 / Core / Services / Logtivity_Logger.php

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

380 lines 8.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, [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 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
26 // phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
27
28 class Logtivity_Logger extends Logtivity_Api
29 {
30 use Logtivity_User_Logger_Trait;
31
32 /**
33 * Can this instance log something
34 *
35 * @var bool
36 */
37 public bool $active = true;
38
39 /**
40 * @var ?string
41 */
42 public ?string $action = null;
43
44 /**
45 * @var ?string
46 */
47 public ?string $context = null;
48
49 /**
50 * @var ?string
51 */
52 public ?string $post_type = null;
53
54 /**
55 * @var ?int
56 */
57 public ?int $post_id = null;
58
59 /**
60 * @var array
61 */
62 public array $meta = [];
63
64 /**
65 * @var array
66 */
67 public array $userMeta = [];
68
69 /**
70 * @var bool
71 */
72 protected bool $includeInventory = false;
73
74 /**
75 * @param ?int $userId
76 */
77 public function __construct(?int $userId = null)
78 {
79 $this->setUser($userId);
80
81 parent::__construct();
82 }
83
84 /**
85 * Way into class.
86 *
87 * @param ?string $action
88 * @param ?array|array[] $meta
89 * @param ?int $userId
90 *
91 * @return Logtivity_Logger
92 */
93 public static function log(?string $action = null, ?array $meta = null, ?int $userId = null): Logtivity_Logger
94 {
95 $logtivityLogger = new Logtivity_Logger($userId);
96
97 if ($action) {
98 $logtivityLogger->setAction($action);
99 }
100
101 if ($meta) {
102 // Convert legacy form to current
103 // @deprecated v3.1.8
104 $key = $meta['key'] ?? null;
105 $value = $meta['value'] ?? null;
106 if ($key && $value) {
107 $meta = [$key => $value];
108 }
109
110 if (logtivity_is_associative($meta)) {
111 // Associative array of keys and values
112 foreach ($meta as $key => $value) {
113 $logtivityLogger->addMeta($key, $value);
114 }
115 }
116 }
117
118 return $logtivityLogger;
119 }
120
121 /**
122 * @param string $action
123 *
124 * @return $this
125 */
126 public function setAction(string $action): self
127 {
128 $this->action = ucfirst(str_replace(['-', '_'], ' ', strtolower(trim($action))));
129
130 return $this;
131 }
132
133 /**
134 * @param ?string $context
135 *
136 * @return $this
137 */
138 public function setContext(?string $context): self
139 {
140 $this->context = $context;
141
142 return $this;
143 }
144
145 /**
146 * @param ?string $postType
147 *
148 * @return $this
149 */
150 public function setPostType(?string $postType): self
151 {
152 $this->post_type = $postType;
153
154 return $this;
155 }
156
157 /**
158 * @param int $postId
159 *
160 * @return $this
161 */
162 public function setPostId(int $postId): self
163 {
164 $this->post_id = $postId;
165
166 return $this;
167 }
168
169 /**
170 * @param string $key
171 * @param mixed $value
172 *
173 * @return $this
174 */
175 public function addMeta(string $key, $value): self
176 {
177 $this->meta[] = [
178 'key' => logtivity_logger_label($key),
179 'value' => logtivity_logger_value($value),
180 ];
181
182 return $this;
183 }
184
185 /**
186 * @param array $values
187 * @param string $key (optional)
188 *
189 * @return $this
190 */
191 public function addMetaArray(array $values, string $key = ''): self
192 {
193 if ($key) {
194 $title = str_pad(' ' . $key . ' ', strlen($key) + 8, '*', STR_PAD_BOTH);
195 $comment = str_pad(' ' . count($values) . ' ', strlen($key) + 8, '*', STR_PAD_BOTH);
196 $this->addMeta($title, $comment);
197 }
198
199 foreach ($values as $key => $value) {
200 $this->addMeta($key, $value);
201 }
202
203 return $this;
204 }
205
206 /**
207 * @param array $oldValues
208 * @param array $newValues
209 *
210 * @return $this
211 */
212 public function addMetaChanges(array $oldValues, array $newValues): self
213 {
214 $keys = array_unique(array_merge(array_keys($oldValues), array_keys($newValues)));
215 foreach ($keys as $key) {
216 $value = logtivity_logger_value($newValues[$key] ?? null);
217 $oldValue = logtivity_logger_value($oldValues[$key] ?? null);
218 if ($value != $oldValue) {
219 $this->addMeta($key, sprintf('%s => %s', $oldValue, $value));
220 }
221 }
222
223 return $this;
224 }
225
226 /**
227 * Add the meta if the first condition is true
228 *
229 * @param mixed $condition
230 * @param string $key
231 * @param mixed $value
232 *
233 * @return $this
234 */
235 public function addMetaIf($condition, string $key, $value): self
236 {
237 if ($condition) {
238 $this->addMeta($key, $value);
239 }
240
241 return $this;
242 }
243
244 /**
245 * Add to an array of user meta you would like to pass to this log.
246 *
247 * @param string $key
248 * @param mixed $value
249 *
250 * @return $this
251 */
252 public function addUserMeta(string $key, $value): self
253 {
254 $this->userMeta[$key] = $value;
255
256 return $this;
257 }
258
259 /**
260 * @return $this
261 */
262 public function addTrace(): self
263 {
264 $stack = (new Exception())->getTrace();
265 array_shift($stack);
266 $stack = array_slice($stack, 0, 12);
267
268 $this->addMeta('Stack Trace', '**********');
269
270 foreach ($stack as $index => $stackItem) {
271 $file = str_replace(ABSPATH, '', $stackItem['file'] ?? 'NULL');
272 $line = $stackItem['line'] ?? 'NULL';
273 $this->addMeta($index, sprintf('%s: %s', $line, $file));
274 }
275
276 return $this;
277 }
278
279 /**
280 * Send the current plugin/theme inventory with this log so the API's stored
281 * copy stays in step with the change being logged. Captured at send time.
282 *
283 * @return $this
284 */
285 public function addInventory(): self
286 {
287 $this->includeInventory = true;
288
289 return $this;
290 }
291
292 /**
293 * @return $this
294 */
295 public function stop(): self
296 {
297 $this->active = false;
298
299 return $this;
300 }
301
302 /**
303 * Send the logged data to Logtivity
304 *
305 * @return void
306 */
307 public function send(): ?array
308 {
309 $this->maybeAddProfileLink();
310
311 do_action('wp_logtivity_instance', $this);
312
313 return $this->active
314 ? $this->makeRequest('/logs/store', $this->getData())
315 : null;
316 }
317
318 /**
319 * @return array
320 */
321 protected function getData(): array
322 {
323 $data = [
324 'action' => $this->action,
325 'context' => $this->context,
326 'post_type' => $this->post_type,
327 'post_id' => $this->post_id,
328 'meta' => $this->getMeta(),
329 'user_id' => $this->getUserID(),
330 'username' => $this->maybeGetUsersUsername(),
331 'user_meta' => $this->getUserMeta(),
332 'ip_address' => $this->maybeGetUsersIp(),
333 ];
334
335 if ($this->includeInventory) {
336 $data = array_merge($data, (new Logtivity_Site_Inventory())->get());
337 }
338
339 return $data;
340 }
341
342 /**
343 * Build the user meta array
344 *
345 * @return array
346 */
347 public function getUserMeta(): array
348 {
349 return (array)apply_filters('wp_logtivity_get_user_meta', $this->userMeta);
350 }
351
352 /**
353 * Build the meta array
354 *
355 * @return array
356 */
357 public function getMeta(): array
358 {
359 return (array)apply_filters('wp_logtivity_get_meta', $this->meta);
360 }
361
362 /**
363 * @return $this
364 */
365 protected function maybeAddProfileLink(): self
366 {
367 if (
368 $this->options->shouldStoreProfileLink()
369 && $this->user->isLoggedIn()
370 ) {
371 $profileLink = $this->user->profileLink();
372 if ($profileLink) {
373 $this->addUserMeta('Profile Link', $profileLink);
374 }
375 }
376
377 return $this;
378 }
379 }
380