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_Options.php

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

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