PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / trunk
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity vtrunk
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 1.6.1 All 66 releases
logtivity / Core / Admin / Logtivity_Admin.php

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

282 lines 7.6 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, 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.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 add_action('wp_ajax_nopriv_logtivity_update_settings', [$this, 'update']);
47
48 add_action('wp_ajax_logtivity_register_site', [$this, 'registerSite']);
49
50 add_filter('logtivity_hide_from_menu', [$this, 'shouldHidePluginFromUI']);
51 add_filter('all_plugins', [$this, 'maybeHideFromMenu']);
52
53 $this->options = new Logtivity_Options();
54 }
55
56 /**
57 * @return self
58 */
59 public static function init(): self
60 {
61 return new static();
62 }
63
64 /**
65 * @param array $plugins
66 *
67 * @return array
68 */
69 public function maybeHideFromMenu(array $plugins): array
70 {
71 if ($name = (new Logtivity_Options())->customPluginName()) {
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 $shouldHide = !array_key_exists('show_all', $_GET);
82
83 if ($shouldHide) {
84 $hiddenPlugins = [
85 'logtivity/logtivity.php',
86 ];
87
88 foreach ($hiddenPlugins as $hiddenPlugin) {
89 unset($plugins[$hiddenPlugin]);
90 }
91 }
92 return $plugins;
93 }
94
95 /**
96 * @param bool $value
97 *
98 * @return bool
99 */
100 public function shouldHidePluginFromUI(bool $value = false): bool
101 {
102 if (static::$shouldHidePluginFromUI = (new Logtivity_Options())->isPluginHiddenFromUI()) {
103 return static::$shouldHidePluginFromUI;
104 }
105
106 return $value;
107 }
108
109 /**
110 * Create the admin menus
111 *
112 * @return void
113 */
114 public function registerOptionsPage(): void
115 {
116 if (!apply_filters('logtivity_hide_from_menu', false)) {
117 add_menu_page(
118 ($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'),
119 ($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'),
120 Logtivity::ACCESS_LOGS,
121 ($this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity'),
122 [$this, 'showLogIndexPage'],
123 'dashicons-chart-area',
124 26
125 );
126 }
127
128 if (!apply_filters('logtivity_hide_settings_page', false)) {
129 add_submenu_page(
130 $this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity',
131 'Logtivity Settings',
132 'Settings',
133 Logtivity::ACCESS_SETTINGS,
134 'logtivity-settings',
135 [$this, 'showSettingsPage']
136 );
137 }
138
139 if ($this->options->getApiKey() == false) {
140 add_submenu_page(
141 $this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity',
142 'Register Site',
143 'Register Site',
144 Logtivity::ACCESS_SETTINGS,
145 'logtivity-register-site',
146 [$this, 'showRegisterSitePage']
147 );
148 }
149 }
150
151 /**
152 * Show the admin log index
153 *
154 * @return void
155 */
156 public function showLogIndexPage(): void
157 {
158 if (!current_user_can(Logtivity::ACCESS_LOGS)) {
159 wp_die(__('You do not have sufficient permissions to access this page.'));
160 }
161
162 $options = $this->options->getOptions();
163
164 echo logtivity_view('log-index', compact('options'));
165 }
166
167 /**
168 * Show the admin settings template
169 *
170 * @return void
171 */
172 public function showSettingsPage(): void
173 {
174 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
175 wp_die(__('You do not have sufficient permissions to access this page.'));
176 }
177
178 $options = $this->options->getOptions();
179
180 echo logtivity_view('settings', compact('options'));
181 }
182
183 /**
184 * Show the register by team API page
185 *
186 * @return void
187 */
188 public function showRegisterSitePage(): void
189 {
190 if (!current_user_can(Logtivity::ACCESS_SETTINGS)) {
191 wp_die(__('You do not have sufficient permissions to access this page.'));
192 }
193
194 $options = $this->options->getOptions();
195
196 echo logtivity_view('register', compact('options'));
197 }
198
199 /**
200 * @return void
201 */
202 public function update(): void
203 {
204 if (
205 !wp_verify_nonce(
206 sanitize_text_field($_POST['logtivity_update_settings'] ?? null),
207 'logtivity_update_settings'
208 )
209 ) {
210 wp_safe_redirect($this->settingsPageUrl());
211 exit();
212 }
213
214 $user = new Logtivity_Wp_User();
215
216 if (!$user->hasRole('administrator')) {
217 wp_safe_redirect($this->settingsPageUrl());
218 exit();
219 }
220
221 $this->options->update(
222 [
223 'logtivity_url_hash' => md5(home_url()),
224 ],
225 false
226 );
227
228 delete_transient('dismissed-logtivity-site-url-has-changed-notice');
229
230 $this->options->update();
231
232 (new Logtivity_Check_For_New_Settings())->checkForNewSettings();
233
234 wp_safe_redirect($this->settingsPageUrl());
235 exit();
236 }
237
238 /**
239 * ajax endpoint for registering with a team API Key
240 *
241 * @return void
242 */
243 public function registerSite(): void
244 {
245 try {
246 if (
247 wp_verify_nonce(
248 sanitize_text_field($_POST['logtivity_register_site'] ?? null),
249 'logtivity_register_site'
250 )
251 ) {
252 $teamApi = sanitize_text_field($_POST['logtivity_team_api_key'] ?? null);
253
254 $response = Logtivity::registerSite($teamApi);
255
256
257 if ($response instanceof WP_Error) {
258 wp_send_json_error($response);
259 } else {
260 wp_send_json_success($response);
261 }
262
263 } else {
264 wp_send_json_error('Invalid Request');
265 }
266
267 } catch (Throwable $error) {
268 wp_send_json_error($error->getMessage(), $error->getCode());
269 }
270
271 wp_die();
272 }
273
274 /**
275 * @return string
276 */
277 public function settingsPageUrl(): string
278 {
279 return admin_url('admin.php?page=logtivity-settings');
280 }
281 }
282