| 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 |
use Code_Snippets\Snippet; |
| 26 |
|
| 27 |
class Logtivity_Code_Snippets |
| 28 |
{ |
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
add_action('code_snippets/delete_snippet', [$this, 'snippetDeleted'], 10, 2); |
| 32 |
add_action('code_snippets/create_snippet', [$this, 'snippetCreated'], 10, 2); |
| 33 |
add_action('code_snippets/update_snippet', [$this, 'snippetUpdated'], 10, 2); |
| 34 |
add_action('code_snippets/activate_snippet', [$this, 'snippetActivated'], 10, 2); |
| 35 |
add_action('code_snippets/deactivate_snippet', [$this, 'snippetDeactivated'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param int $snippetId |
| 40 |
* @param ?bool $network |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function snippetDeleted(int $snippetId, ?bool $network): void |
| 45 |
{ |
| 46 |
Logtivity::log() |
| 47 |
->setAction('Code Snippet Deleted') |
| 48 |
->setContext($snippetId) |
| 49 |
->addMeta('Network', $network ? 'Yes' : 'No') |
| 50 |
->send(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param Snippet $snippet |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function snippetCreated(Snippet $snippet): void |
| 59 |
{ |
| 60 |
$cloneId = sanitize_text_field($_GET['action'] ?? null) == 'clone' |
| 61 |
? sanitize_text_field($_GET['id'] ?? null) |
| 62 |
: null; |
| 63 |
|
| 64 |
$logger = Logtivity::log() |
| 65 |
->setAction('Code Snippet Created') |
| 66 |
->setContext($snippet->id) |
| 67 |
->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId); |
| 68 |
|
| 69 |
$this->logWithMeta($logger, $snippet); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param Snippet $snippet |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function snippetUpdated(Snippet $snippet): void |
| 78 |
{ |
| 79 |
$logger = Logtivity::log() |
| 80 |
->setAction('Code Snippet Updated') |
| 81 |
->setContext($snippet->id); |
| 82 |
|
| 83 |
$this->logWithMeta($logger, $snippet); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param Snippet $snippet |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function snippetActivated(Snippet $snippet): void |
| 92 |
{ |
| 93 |
Logtivity::log() |
| 94 |
->setAction('Code Snippet Activated') |
| 95 |
->setContext($snippet->id) |
| 96 |
->addMeta('Snippet Name', $snippet->name) |
| 97 |
->addMeta('Network', $snippet->network == 0 ? 'No' : $snippet->network) |
| 98 |
->send(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @param int $snippetId |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function snippetDeactivated(int $snippetId): void |
| 107 |
{ |
| 108 |
Logtivity::log() |
| 109 |
->setAction('Code Snippet Deactivated') |
| 110 |
->setContext($snippetId) |
| 111 |
->send(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param Logtivity_Logger $logger |
| 116 |
* @param ?Snippet $snippet |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function logWithMeta(Logtivity_Logger $logger, ?Snippet $snippet = null): void |
| 121 |
{ |
| 122 |
$logger |
| 123 |
->addMeta('Snippet Name', $snippet->name) |
| 124 |
->addMeta('Active', $snippet->active ? 'Yes' : 'No') |
| 125 |
->addMeta('Network', $snippet->network == 0 ? 'No' : $snippet->network) |
| 126 |
->addMeta('Snippet Code', $snippet->code) |
| 127 |
->addMeta('Snippet Scope', $snippet->scope) |
| 128 |
->addMeta('Snippet Priority', $snippet->priority) |
| 129 |
->addMeta('Snippet Tags', $snippet->tags) |
| 130 |
->send(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
new Logtivity_Code_Snippets(); |
| 135 |
|