| 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 |
trait Logtivity_User_Logger_Trait |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var Logtivity_Wp_User |
| 32 |
*/ |
| 33 |
public Logtivity_Wp_User $user; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param null|int|WP_User|Logtivity_Wp_User $user |
| 37 |
* |
| 38 |
* @return $this |
| 39 |
*/ |
| 40 |
public function setUser($user = null): self |
| 41 |
{ |
| 42 |
$this->user = new Logtivity_Wp_User($user); |
| 43 |
|
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return ?int |
| 49 |
*/ |
| 50 |
protected function getUserID(): ?int |
| 51 |
{ |
| 52 |
if ( |
| 53 |
$this->options->shouldStoreUserId() |
| 54 |
&& $this->user->isLoggedIn() |
| 55 |
) { |
| 56 |
return $this->user->id(); |
| 57 |
} |
| 58 |
|
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return ?string |
| 64 |
*/ |
| 65 |
protected function maybeGetUsersIp(): ?string |
| 66 |
{ |
| 67 |
if ($this->options->shouldStoreIp()) { |
| 68 |
$ipAddress = filter_var( |
| 69 |
($_SERVER['HTTP_CLIENT_IP'] ?? '') |
| 70 |
?: ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? '') |
| 71 |
?: $_SERVER['REMOTE_ADDR'] ?? '', |
| 72 |
FILTER_VALIDATE_IP |
| 73 |
); |
| 74 |
|
| 75 |
return $ipAddress ?: null; |
| 76 |
} |
| 77 |
|
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return ?string |
| 83 |
*/ |
| 84 |
protected function maybeGetUsersUsername(): ?string |
| 85 |
{ |
| 86 |
if ( |
| 87 |
$this->options->shouldStoreUsername() |
| 88 |
&& $this->user->isLoggedIn() |
| 89 |
) { |
| 90 |
return $this->user->userLogin(); |
| 91 |
} |
| 92 |
|
| 93 |
return null; |
| 94 |
} |
| 95 |
} |
| 96 |
|