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 / Base / Logtivity_Abstract_Logger.php

Logtivity_Abstract_Logger.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity trunk, at Base/Logtivity_Abstract_Logger.php

138 lines 3.6 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 abstract class Logtivity_Abstract_Logger
26 {
27 public const LAST_LOGGED_KEY = 'logtivity_last_logged';
28 public const LAST_LOGGED_SECONDS = 5;
29
30 public function __construct()
31 {
32 $this->registerHooks();
33 }
34
35 /**
36 * @return void
37 */
38 abstract protected function registerHooks(): void;
39
40 /**
41 * @param string|int $id
42 * @param ?string $metaType
43 *
44 * @return bool
45 */
46 protected function recentlyLogged($id, ?string $metaType = null): bool
47 {
48 if ($metaType) {
49 /* Use a metadata table */
50 $lastLogged = (int)get_metadata($metaType, $id, static::LAST_LOGGED_KEY, true);
51 $loggedRecently = (time() - $lastLogged) < static::LAST_LOGGED_SECONDS;
52
53 } else {
54 /* Metadata not available, use transients */
55 $key = $this->getLastLoggedKey($id);
56
57 $loggedRecently = (bool)get_transient($key);
58 }
59
60 return $loggedRecently;
61 }
62
63 /**
64 * @param string|int $id
65 * @param ?string $metaType
66 *
67 * @return void
68 */
69 protected function setRecentlyLogged($id, ?string $metaType = null): void
70 {
71 if ($metaType) {
72 /* Use a metadata table */
73 update_metadata($metaType, $id, static::LAST_LOGGED_KEY, time());
74
75 } else {
76 /* Metadata not available, using transients */
77 $key = $this->getLastLoggedKey($id);
78
79 $className = explode('\\', static::class);
80
81 set_transient($key, $id . ': ' . array_pop($className), static::LAST_LOGGED_SECONDS);
82 }
83 }
84
85 /**
86 * @param string $id
87 *
88 * @return string
89 */
90 protected function getLastLoggedKey(string $id): string
91 {
92 $key = join(
93 ':',
94 [
95 static::class,
96 $id,
97 ]
98 );
99
100 return static::LAST_LOGGED_KEY . '_' . md5($key);
101 }
102
103 /**
104 * @param string $metaType
105 * @param int $parentId
106 *
107 * @return array
108 */
109 protected function getMetadata(string $metaType, int $parentId): array
110 {
111 return array_map(
112 function ($row) {
113 return logtivity_logger_value($row);
114 },
115 get_metadata($metaType, $parentId) ?: []
116 );
117 }
118
119 /**
120 * @param array $fields
121 * @param array $ignoredKeys
122 *
123 * @return array
124 */
125 protected function sanitizeDataFields(array $fields, array $ignoredKeys = []): array
126 {
127 $values = [];
128 foreach ($fields as $key => $value) {
129 if (in_array($key, $ignoredKeys) == false) {
130 $key = logtivity_logger_label($key);
131 $values[$key] = logtivity_logger_value($value);
132 }
133 }
134
135 return $values;
136 }
137 }
138