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

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

353 lines 8.0 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 // 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 * @param ?int $userId
71 */
72 public function __construct(?int $userId = null)
73 {
74 $this->setUser($userId);
75
76 parent::__construct();
77 }
78
79 /**
80 * Way into class.
81 *
82 * @param ?string $action
83 * @param ?array|array[] $meta
84 * @param ?int $userId
85 *
86 * @return Logtivity_Logger
87 */
88 public static function log(?string $action = null, ?array $meta = null, ?int $userId = null): Logtivity_Logger
89 {
90 $logtivityLogger = new Logtivity_Logger($userId);
91
92 if ($action) {
93 $logtivityLogger->setAction($action);
94 }
95
96 if ($meta) {
97 // Convert legacy form to current
98 // @deprecated v3.1.8
99 $key = $meta['key'] ?? null;
100 $value = $meta['value'] ?? null;
101 if ($key && $value) {
102 $meta = [$key => $value];
103 }
104
105 if (logtivity_is_associative($meta)) {
106 // Associative array of keys and values
107 foreach ($meta as $key => $value) {
108 $logtivityLogger->addMeta($key, $value);
109 }
110 }
111 }
112
113 return $logtivityLogger;
114 }
115
116 /**
117 * @param string $action
118 *
119 * @return $this
120 */
121 public function setAction(string $action): self
122 {
123 $this->action = ucfirst(str_replace(['-', '_'], ' ', strtolower(trim($action))));
124
125 return $this;
126 }
127
128 /**
129 * @param ?string $context
130 *
131 * @return $this
132 */
133 public function setContext(?string $context): self
134 {
135 $this->context = $context;
136
137 return $this;
138 }
139
140 /**
141 * @param ?string $postType
142 *
143 * @return $this
144 */
145 public function setPostType(?string $postType): self
146 {
147 $this->post_type = $postType;
148
149 return $this;
150 }
151
152 /**
153 * @param int $postId
154 *
155 * @return $this
156 */
157 public function setPostId(int $postId): self
158 {
159 $this->post_id = $postId;
160
161 return $this;
162 }
163
164 /**
165 * @param string $key
166 * @param mixed $value
167 *
168 * @return $this
169 */
170 public function addMeta(string $key, $value): self
171 {
172 $this->meta[] = [
173 'key' => logtivity_logger_label($key),
174 'value' => logtivity_logger_value($value),
175 ];
176
177 return $this;
178 }
179
180 /**
181 * @param array $values
182 * @param string $key (optional)
183 *
184 * @return $this
185 */
186 public function addMetaArray(array $values, string $key = ''): self
187 {
188 if ($key) {
189 $title = str_pad(' ' . $key . ' ', strlen($key) + 8, '*', STR_PAD_BOTH);
190 $comment = str_pad(' ' . count($values) . ' ', strlen($key) + 8, '*', STR_PAD_BOTH);
191 $this->addMeta($title, $comment);
192 }
193
194 foreach ($values as $key => $value) {
195 $this->addMeta($key, $value);
196 }
197
198 return $this;
199 }
200
201 /**
202 * @param array $oldValues
203 * @param array $newValues
204 *
205 * @return $this
206 */
207 public function addMetaChanges(array $oldValues, array $newValues): self
208 {
209 $keys = array_unique(array_merge(array_keys($oldValues), array_keys($newValues)));
210 foreach ($keys as $key) {
211 $value = logtivity_logger_value($newValues[$key] ?? null);
212 $oldValue = logtivity_logger_value($oldValues[$key] ?? null);
213 if ($value != $oldValue) {
214 $this->addMeta($key, sprintf('%s => %s', $oldValue, $value));
215 }
216 }
217
218 return $this;
219 }
220
221 /**
222 * Add the meta if the first condition is true
223 *
224 * @param mixed $condition
225 * @param string $key
226 * @param mixed $value
227 *
228 * @return $this
229 */
230 public function addMetaIf($condition, string $key, $value): self
231 {
232 if ($condition) {
233 $this->addMeta($key, $value);
234 }
235
236 return $this;
237 }
238
239 /**
240 * Add to an array of user meta you would like to pass to this log.
241 *
242 * @param string $key
243 * @param mixed $value
244 *
245 * @return $this
246 */
247 public function addUserMeta(string $key, $value): self
248 {
249 $this->userMeta[$key] = $value;
250
251 return $this;
252 }
253
254 /**
255 * @return $this
256 */
257 public function addTrace(): self
258 {
259 $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
260 array_shift($stack);
261
262 $this->addMeta('Stack Trace', '**********');
263
264 foreach ($stack as $index => $stackItem) {
265 $file = str_replace(ABSPATH, '', $stackItem['file'] ?? 'NULL');
266 $line = $stackItem['line'] ?? 'NULL';
267 $this->addMeta($index, sprintf('%s: %s', $line, $file));
268 }
269
270 return $this;
271 }
272
273 /**
274 * @return $this
275 */
276 public function stop(): self
277 {
278 $this->active = false;
279
280 return $this;
281 }
282
283 /**
284 * Send the logged data to Logtivity
285 *
286 * @return void
287 */
288 public function send(): ?array
289 {
290 $this->maybeAddProfileLink();
291
292 do_action('wp_logtivity_instance', $this);
293
294 return $this->active
295 ? $this->makeRequest('/logs/store', $this->getData())
296 : null;
297 }
298
299 /**
300 * @return array
301 */
302 protected function getData(): array
303 {
304 return [
305 'action' => $this->action,
306 'context' => $this->context,
307 'post_type' => $this->post_type,
308 'post_id' => $this->post_id,
309 'meta' => $this->getMeta(),
310 'user_id' => $this->getUserID(),
311 'username' => $this->maybeGetUsersUsername(),
312 'user_meta' => $this->getUserMeta(),
313 'ip_address' => $this->maybeGetUsersIp(),
314 ];
315 }
316
317 /**
318 * Build the user meta array
319 *
320 * @return array
321 */
322 public function getUserMeta(): array
323 {
324 return (array)apply_filters('wp_logtivity_get_user_meta', $this->userMeta);
325 }
326
327 /**
328 * Build the meta array
329 *
330 * @return array
331 */
332 public function getMeta(): array
333 {
334 return (array)apply_filters('wp_logtivity_get_meta', $this->meta);
335 }
336
337 /**
338 * @return $this
339 */
340 protected function maybeAddProfileLink(): self
341 {
342 if (
343 $this->options->shouldStoreProfileLink()
344 && $this->user->isLoggedIn()
345 && ($profileLink = $this->user->profileLink())
346 ) {
347 $this->addUserMeta('Profile Link', $profileLink);
348 }
349
350 return $this;
351 }
352 }
353