PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.9
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.9
3.3.9 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 All 67 releases
logtivity / Loggers / Code_Snippets / Logtivity_Code_Snippets.php

Logtivity_Code_Snippets.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.9, at Loggers/Code_Snippets/Logtivity_Code_Snippets.php

165 lines 4.9 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, [email protected]
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\Model;
26 use Code_Snippets\Snippet;
27
28 class Logtivity_Code_Snippets
29 {
30 public function __construct()
31 {
32 add_action('code_snippets/trash_snippet', [$this, 'snippetTrashed'], 10, 2);
33 add_action('code_snippets/delete_snippet', [$this, 'snippetDeleted'], 10, 2);
34 add_action('code_snippets/create_snippet', [$this, 'snippetCreated'], 10, 2);
35 add_action('code_snippets/update_snippet', [$this, 'snippetUpdated'], 10, 2);
36 add_action('code_snippets/activate_snippet', [$this, 'snippetActivated'], 10, 2);
37 add_action('code_snippets/deactivate_snippet', [$this, 'snippetDeactivated'], 10, 2);
38 }
39
40 /**
41 * @param Model\Snippet $snippet
42 *
43 * @return void
44 */
45 public function snippetTrashed($snippet): void
46 {
47 $logger = Logtivity::log()
48 ->setAction('Code Snippet Trashed')
49 ->setContext($snippet->id);
50
51 $this->logWithMeta($logger, $snippet);
52 }
53
54 /**
55 * @param int|Model\Snippet $snippet
56 * @param ?bool $network
57 *
58 * @return void
59 */
60 public function snippetDeleted($snippet, $network): void
61 {
62 $snippetId = $snippet->id ?? $snippet;
63
64 $logger = Logtivity::log()
65 ->setAction('Code Snippet Deleted')
66 ->setContext($snippetId);
67
68 if (is_object($snippet)) {
69 $this->logWithMeta($logger, $snippet);
70
71 } else {
72 $logger
73 ->addMeta('Network', $network ? 'Yes' : 'No')
74 ->send();
75 }
76 }
77
78 /**
79 * @param Snippet|Model\Snippet $snippet
80 * @param string $table
81 *
82 * @return void
83 */
84 public function snippetCreated($snippet, $table): void
85 {
86 // Will not work in newer versions of Code Snippets
87 $cloneId = sanitize_text_field($_GET['action'] ?? null) == 'clone'
88 ? sanitize_text_field($_GET['id'] ?? null)
89 : null;
90
91 $logger = Logtivity::log()
92 ->setAction('Code Snippet Created')
93 ->setContext($snippet->id)
94 ->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId)
95 ->addMetaIf((bool)$table, 'Table', $table);
96
97 $this->logWithMeta($logger, $snippet);
98 }
99
100 /**
101 * @param Snippet|Model\Snippet $snippet
102 * @param string $table
103 *
104 * @return void
105 */
106 public function snippetUpdated($snippet, $table): void
107 {
108 $logger = Logtivity::log()
109 ->setAction('Code Snippet Updated')
110 ->setContext($snippet->id)
111 ->addMetaIf((bool)$table, 'Table', $table);
112
113 $this->logWithMeta($logger, $snippet);
114 }
115
116 /**
117 * @param Snippet|Model\Snippet $snippet
118 *
119 * @return void
120 */
121 public function snippetActivated($snippet): void
122 {
123 Logtivity::log()
124 ->setAction('Code Snippet Activated')
125 ->setContext($snippet->id)
126 ->addMeta('Snippet Name', $snippet->name)
127 ->addMeta('Network', $snippet->network == 0 ? 'No' : $snippet->network)
128 ->send();
129 }
130
131 /**
132 * @param int $snippetId
133 *
134 * @return void
135 */
136 public function snippetDeactivated(int $snippetId): void
137 {
138 Logtivity::log()
139 ->setAction('Code Snippet Deactivated')
140 ->setContext($snippetId)
141 ->send();
142 }
143
144 /**
145 * @param Logtivity_Logger $logger
146 * @param Snippet|Model\Snippet $snippet
147 *
148 * @return void
149 */
150 public function logWithMeta(Logtivity_Logger $logger, object $snippet): void
151 {
152 $logger
153 ->addMeta('Snippet Name', $snippet->name)
154 ->addMeta('Active', $snippet->active ? 'Yes' : 'No')
155 ->addMeta('Network', $snippet->network == 0 ? 'No' : $snippet->network)
156 ->addMeta('Snippet Code', $snippet->code)
157 ->addMeta('Snippet Scope', $snippet->scope)
158 ->addMeta('Snippet Priority', $snippet->priority)
159 ->addMeta('Snippet Tags', $snippet->tags)
160 ->send();
161 }
162 }
163
164 new Logtivity_Code_Snippets();
165