PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.2
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.2
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_Options.php

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

294 lines 8.2 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-2025 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 class Logtivity_Options
29 {
30 /**
31 * Checkin for settings updates in designated seconds
32 *
33 * @var int
34 */
35 public int $checkinDelay = 10 * MINUTE_IN_SECONDS;
36
37 /**
38 * The option keys that we can save to the options table
39 *
40 * @var array
41 */
42 protected array $settings = [
43 'logtivity_api_key_check' => ['rule' => 'is_string', 'default' => null],
44 'logtivity_app_verify_url' => ['rule' => 'is_bool', 'default' => 1],
45 'logtivity_custom_plugin_name' => ['rule' => 'is_string', 'default' => null],
46 'logtivity_disable_default_logging' => ['rule' => 'is_bool', 'default' => null],
47 'logtivity_disable_error_logging' => ['rule' => 'is_bool', 'default' => null],
48 'logtivity_disable_individual_logs' => ['rule' => 'is_string', 'default' => null],
49 'logtivity_disabled_error_levels' => ['rule' => 'is_array', 'default' => null],
50 'logtivity_enable_debug_mode' => ['rule' => 'is_bool', 'default' => null],
51 'logtivity_enable_options_table_logging' => ['rule' => 'is_bool', 'default' => null],
52 'logtivity_enable_post_meta_logging' => ['rule' => 'is_bool', 'default' => null],
53 'logtivity_enable_white_label_mode' => ['rule' => 'is_bool', 'default' => null],
54 'logtivity_global_disabled_logs' => ['rule' => 'is_string', 'default' => null],
55 'logtivity_hide_plugin_from_ui' => ['rule' => 'is_bool', 'default' => null],
56 'logtivity_latest_response' => ['rule' => 'is_array', 'default' => null],
57 'logtivity_should_log_profile_link' => ['rule' => 'is_bool', 'default' => 1],
58 'logtivity_should_log_username' => ['rule' => 'is_bool', 'default' => 1],
59 'logtivity_should_store_ip' => ['rule' => 'is_bool', 'default' => 1],
60 'logtivity_should_store_user_id' => ['rule' => 'is_bool', 'default' => 1],
61 'logtivity_site_api_key' => ['rule' => 'is_string', 'default' => null],
62 'logtivity_url_hash' => ['rule' => 'is_string', 'default' => null],
63 ];
64
65 /**
66 * Get the admin settings or the plugin
67 *
68 * @return array
69 */
70 public function getOptions(): array
71 {
72 $options = [];
73
74 foreach ($this->settings as $setting => $rules) {
75 $options[$setting] = $this->getOption($setting);
76 }
77
78 return $options;
79 }
80
81 /**
82 * Get an option from the database
83 *
84 * @param string $key
85 *
86 * @return mixed
87 */
88 public function getOption(string $key)
89 {
90 if (has_filter($key)) {
91 return apply_filters($key, false);
92 }
93
94 $setting = $this->settings[$key] ?? null;
95 if ($setting) {
96 $value = get_option($key, $setting['default'] ?? null);
97 }
98
99 return $value ?? null;
100 }
101
102 /**
103 * Get the API Key for the site
104 *
105 * @return string
106 */
107 public function getApiKey(): string
108 {
109 return (string)$this->getOption('logtivity_site_api_key');
110 }
111
112 /**
113 * Should we store the user id?
114 *
115 * @return bool
116 */
117 public function shouldStoreUserId(): bool
118 {
119 return (bool)$this->getOption('logtivity_should_store_user_id');
120 }
121
122 /**
123 * Should we store the users IP?
124 *
125 * @return bool
126 */
127 public function shouldStoreIp(): bool
128 {
129 return (bool)$this->getOption('logtivity_should_store_ip');
130 }
131
132 /**
133 * Should we store the users profile link?
134 *
135 * @return bool
136 */
137 public function shouldStoreProfileLink(): bool
138 {
139 return (bool)$this->getOption('logtivity_should_log_profile_link');
140 }
141
142 /**
143 * Should we store the user's username?
144 *
145 * @return bool
146 */
147 public function shouldStoreUsername(): bool
148 {
149 return (bool)$this->getOption('logtivity_should_log_username');
150 }
151
152 /**
153 * Get the error levels that are disabled
154 *
155 * @return array
156 */
157 public function disabledErrorLevels(): array
158 {
159 $result = (array)$this->getOption('logtivity_disabled_error_levels');
160
161 return array_keys(array_filter($result));
162 }
163
164 /**
165 * Should we be logging the response from the API
166 *
167 * @return bool
168 */
169 public function shouldLogLatestResponse(): bool
170 {
171 return $this->getOption('logtivity_enable_debug_mode') || $this->shouldCheckInWithApi();
172 }
173
174 /**
175 * @return bool
176 */
177 public function shouldCheckInWithApi(): bool
178 {
179 if ($this->getOption('logtivity_enable_debug_mode') == false) {
180 $lastCheckin = get_option('logtivity_last_settings_check_in_at');
181 $lastCheckin = $lastCheckin['date'] ?? null;
182
183 if ($lastCheckin) {
184 return time() - strtotime($lastCheckin) > $this->checkinDelay;
185 }
186 }
187
188 return true;
189 }
190
191 /**
192 * @return string
193 */
194 public function urlHash(): string
195 {
196 return (string)$this->getOption('logtivity_url_hash');
197 }
198
199 /**
200 * @return string
201 */
202 public function disabledLogs(): string
203 {
204 return (string)$this->getOption('logtivity_global_disabled_logs');
205 }
206
207 /**
208 * @return bool
209 */
210 public function isWhiteLabelMode(): bool
211 {
212 return (bool)$this->getOption('logtivity_enable_white_label_mode');
213 }
214
215 /**
216 * @return bool
217 */
218 public function isPluginHiddenFromUI(): bool
219 {
220 return (bool)$this->getOption('logtivity_hide_plugin_from_ui');
221 }
222
223 /**
224 * @return string
225 */
226 public function customPluginName(): string
227 {
228 return (string)$this->getOption('logtivity_custom_plugin_name');
229 }
230
231 /**
232 * Update the options for this plugin
233 *
234 * @param array $data
235 * @param bool $checkApiKey
236 *
237 * @return void
238 */
239 public function update(array $data = [], bool $checkApiKey = true): void
240 {
241 $settings = array_intersect_key($data ?: $_POST, $this->settings);
242 foreach ($settings as $key => $value) {
243 if ($this->validateSetting($key, $value)) {
244 update_option($key, $value);
245 }
246 }
247
248 if ($checkApiKey) {
249 $this->checkApiKey($data['logtivity_site_api_key'] ?? $_POST['logtivity_site_api_key'] ?? null);
250 }
251 }
252
253 /**
254 * Validate that the passed parameters are in the correct format
255 *
256 * @param string $key
257 * @param mixed $value
258 *
259 * @return bool
260 */
261 protected function validateSetting(string $key, $value): bool
262 {
263 $setting = $this->settings[$key] ?? null;
264
265 if ($setting) {
266 $method = $setting['rule'] ?? null;
267
268 if ($method == 'is_bool') {
269 return $method((bool)$value);
270 }
271
272 return $method($value);
273 }
274
275 return true;
276 }
277
278 /**
279 * @param ?string $apiKey
280 *
281 * @return void
282 */
283 public function checkApiKey(?string $apiKey): void
284 {
285 if ($apiKey) {
286 Logtivity::log()
287 ->setAction('Settings Updated')
288 ->setContext('Logtivity')
289 ->waitForResponse()
290 ->send();
291 }
292 }
293 }
294