PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 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 All 68 releases
logtivity / Core / Admin / Logtivity_Admin.php

Logtivity_Admin.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Core/Admin/Logtivity_Admin.php

304 lines 8.7 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 // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
26 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
27 // phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
28
29 class Logtivity_Admin
30 {
31 /**
32 * @var Logtivity_Options
33 */
34 protected Logtivity_Options $options;
35
36 /**
37 * @var bool
38 */
39 protected static bool $shouldHidePluginFromUI = false;
40
41 public function __construct()
42 {
43 add_action('admin_menu', [$this, 'registerOptionsPage']);
44
45 add_action('wp_ajax_logtivity_update_settings', [$this, 'update']);
46
47 add_action('wp_ajax_logtivity_register_site', [$this, 'registerSite']);
48
49 add_filter('logtivity_hide_from_menu', [$this, 'shouldHidePluginFromUI']);
50 add_filter('all_plugins', [$this, 'maybeHideFromMenu']);
51
52 $this->options = new Logtivity_Options();
53 }
54
55 /**
56 * @return self
57 */
58 public static function init(): self
59 {
60 return new static();
61 }
62
63 /**
64 * @param array $plugins
65 *
66 * @return array
67 */
68 public function maybeHideFromMenu(array $plugins): array
69 {
70 $name = (new Logtivity_Options())->customPluginName();
71 if ($name) {
72 if (isset($plugins['logtivity/logtivity.php'])) {
73 $plugins['logtivity/logtivity.php']['Name'] = $name;
74 }
75 }
76
77 if (!$this->shouldHidePluginFromUI()) {
78 return $plugins;
79 }
80
81 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- show_all only controls local admin list display.
82 $shouldHide = !array_key_exists('show_all', $_GET);
83
84 if ($shouldHide) {
85 $hiddenPlugins = [
86 'logtivity/logtivity.php',
87 ];
88
89 foreach ($hiddenPlugins as $hiddenPlugin) {
90 unset($plugins[$hiddenPlugin]);
91 }
92 }
93 return $plugins;
94 }
95
96 /**
97 * @param bool $value
98 *
99 * @return bool
100 */
101 public function shouldHidePluginFromUI(bool $value = false): bool
102 {
103 static::$shouldHidePluginFromUI = (new Logtivity_Options())->isPluginHiddenFromUI();
104
105 if (static::$shouldHidePluginFromUI) {
106 return static::$shouldHidePluginFromUI;
107 }
108
109 return $value;
110 }
111
112 /**
113 * Create the admin menus
114 *
115 * @return void
116 */
117 public function registerOptionsPage(): void
118 {
119 if (!apply_filters('logtivity_hide_from_menu', false)) {
120 add_menu_page(
121 ($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'),
122 ($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'),
123 Logtivity::ACCESS_LOGS,
124 ($this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity'),
125 [$this, 'showLogIndexPage'],
126 'dashicons-chart-area',
127 26
128 );
129 }
130
131 if (!apply_filters('logtivity_hide_settings_page', false)) {
132 add_submenu_page(
133 $this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity',
134 'Logtivity Settings',
135 'Settings',
136 Logtivity::ACCESS_SETTINGS,
137 'logtivity-settings',
138 [$this, 'showSettingsPage']
139 );
140 }
141
142 if ($this->options->getApiKey() == false) {
143 add_submenu_page(
144 $this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity',
145 'Register Site',
146 'Register Site',
147 Logtivity::ACCESS_SETTINGS,
148 'logtivity-register-site',
149 [$this, 'showRegisterSitePage']
150 );
151 }
152 }
153
154 /**
155 * Show the admin log index
156 *
157 * @return void
158 */
159 public function showLogIndexPage(): void
160 {
161 if (!current_user_can(Logtivity::ACCESS_LOGS)) {
162 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'logtivity'));
163 }
164
165 $options = $this->options->getOptions();
166
167 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- logtivity_view() renders trusted plugin templates.
168 echo logtivity_view('log-index', compact('options'));
169 }
170
171 /**
172 * Show the admin settings template
173 *
174 * @return void
175 */
176 public function showSettingsPage(): void
177 {
178 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
179 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'logtivity'));
180 }
181
182 $options = $this->options->getOptions();
183
184 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- logtivity_view() renders trusted plugin templates.
185 echo logtivity_view('settings', compact('options'));
186 }
187
188 /**
189 * Show the register by team API page
190 *
191 * @return void
192 */
193 public function showRegisterSitePage(): void
194 {
195 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
196 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'logtivity'));
197 }
198
199 $options = $this->options->getOptions();
200
201 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- logtivity_view() renders trusted plugin templates.
202 echo logtivity_view('register', compact('options'));
203 }
204
205 /**
206 * @return void
207 */
208 public function update(): void
209 {
210 $nonce = isset($_POST['logtivity_update_settings'])
211 ? sanitize_text_field(wp_unslash($_POST['logtivity_update_settings']))
212 : '';
213
214 if (
215 !wp_verify_nonce(
216 $nonce,
217 'logtivity_update_settings'
218 )
219 ) {
220 wp_safe_redirect($this->settingsPageUrl());
221 exit();
222 }
223
224 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
225 wp_safe_redirect($this->settingsPageUrl());
226 exit();
227 }
228
229 $this->options->update(
230 [
231 'logtivity_url_hash' => md5(home_url()),
232 ],
233 false
234 );
235
236 delete_transient('dismissed-logtivity-site-url-has-changed-notice');
237
238 $this->options->update();
239
240 if (!$this->options->getOption('logtivity_enable_debug_mode')) {
241 (new Logtivity_Debug_Log())->delete();
242 }
243
244 (new Logtivity_Check_For_New_Settings())->checkForNewSettings();
245
246 wp_safe_redirect($this->settingsPageUrl());
247 exit();
248 }
249
250 /**
251 * ajax endpoint for registering with a team API Key
252 *
253 * @return void
254 */
255 public function registerSite(): void
256 {
257 try {
258 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
259 wp_send_json_error('Unauthorized');
260 }
261
262 $nonce = isset($_POST['logtivity_register_site'])
263 ? sanitize_text_field(wp_unslash($_POST['logtivity_register_site']))
264 : '';
265
266 if (
267 wp_verify_nonce(
268 $nonce,
269 'logtivity_register_site'
270 )
271 ) {
272 $teamApi = isset($_POST['logtivity_team_api_key'])
273 ? sanitize_text_field(wp_unslash($_POST['logtivity_team_api_key']))
274 : '';
275
276 $response = Logtivity::registerSite($teamApi);
277
278
279 if ($response instanceof WP_Error) {
280 wp_send_json_error($response);
281 } else {
282 wp_send_json_success($response);
283 }
284
285 } else {
286 wp_send_json_error('Invalid Request');
287 }
288
289 } catch (Throwable $error) {
290 wp_send_json_error($error->getMessage(), $error->getCode());
291 }
292
293 wp_die();
294 }
295
296 /**
297 * @return string
298 */
299 public function settingsPageUrl(): string
300 {
301 return admin_url('admin.php?page=logtivity-settings');
302 }
303 }
304