| 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 |
|