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

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

282 lines 7.8 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.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->getOption('logtivity_enable_debug_mode') || $this->shouldCheckInWithApi();
159 }
160
161 /**
162 * @return bool
163 */
164 public function shouldCheckInWithApi(): bool
165 {
166 if ($this->getOption('logtivity_enable_debug_mode') == false) {
167 $lastCheckin = get_option('logtivity_last_settings_check_in_at');
168 $lastCheckin = $lastCheckin['date'] ?? null;
169
170 if ($lastCheckin) {
171 return time() - strtotime($lastCheckin) > $this->checkinDelay;
172 }
173 }
174
175 return true;
176 }
177
178 /**
179 * @return string
180 */
181 public function urlHash(): string
182 {
183 return (string)$this->getOption('logtivity_url_hash');
184 }
185
186 /**
187 * @return string
188 */
189 public function disabledLogs(): string
190 {
191 return (string)$this->getOption('logtivity_global_disabled_logs');
192 }
193
194 /**
195 * @return bool
196 */
197 public function isWhiteLabelMode(): bool
198 {
199 return (bool)$this->getOption('logtivity_enable_white_label_mode');
200 }
201
202 /**
203 * @return bool
204 */
205 public function isPluginHiddenFromUI(): bool
206 {
207 return (bool)$this->getOption('logtivity_hide_plugin_from_ui');
208 }
209
210 /**
211 * @return string
212 */
213 public function customPluginName(): string
214 {
215 return (string)$this->getOption('logtivity_custom_plugin_name');
216 }
217
218 /**
219 * Update the options for this plugin
220 *
221 * @param array $data
222 * @param bool $checkApiKey
223 *
224 * @return void
225 */
226 public function update(array $data = [], bool $checkApiKey = true): void
227 {
228 $settings = array_intersect_key($data ?: $_POST, $this->settings);
229 foreach ($settings as $key => $value) {
230 if ($this->validateSetting($key, $value)) {
231 update_option($key, $value);
232 }
233 }
234
235 if ($checkApiKey) {
236 $this->checkApiKey($data['logtivity_site_api_key']
237 ?? sanitize_text_field($_POST['logtivity_site_api_key'] ?? null));
238 }
239 }
240
241 /**
242 * Validate that the passed parameters are in the correct format
243 *
244 * @param string $key
245 * @param mixed $value
246 *
247 * @return bool
248 */
249 protected function validateSetting(string $key, $value): bool
250 {
251 $setting = $this->settings[$key] ?? null;
252
253 if ($setting) {
254 $method = $setting['rule'] ?? null;
255
256 if ($method == 'is_bool') {
257 return $method((bool)$value);
258 }
259
260 return $method($value);
261 }
262
263 return true;
264 }
265
266 /**
267 * @param ?string $apiKey
268 *
269 * @return void
270 */
271 public function checkApiKey(?string $apiKey): void
272 {
273 if ($apiKey) {
274 Logtivity::log()
275 ->setAction('Settings Updated')
276 ->setContext('Logtivity')
277 ->waitForResponse()
278 ->send();
279 }
280 }
281 }
282