PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.17.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.17.0
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 1.6.0 All 67 releases
logtivity / Admin / Logtivity_Options.php

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

237 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Logtivity_Options
4 {
5 /**
6 * The option keys that we can save to the options table
7 *
8 * @var array
9 */
10 protected $settings = [
11 'logtivity_site_api_key',
12 'logtivity_disable_default_logging',
13 'logtivity_should_store_user_id',
14 'logtivity_should_store_ip',
15 'logtivity_should_log_profile_link',
16 'logtivity_should_log_username',
17 'logtivity_disable_individual_logs',
18 'logtivity_enable_debug_mode',
19 'logtivity_latest_response',
20 'logtivity_api_key_check',
21 'logtivity_url_hash',
22 'logtivity_global_disabled_logs',
23 'logtivity_enable_white_label_mode',
24 ];
25
26 /**
27 * The option keys that we can save to the options table
28 *
29 * @var array
30 */
31 protected $rules = [
32 'logtivity_site_api_key' => 'is_string',
33 'logtivity_disable_default_logging' => 'is_bool',
34 'logtivity_should_store_user_id' => 'is_bool',
35 'logtivity_should_store_ip' => 'is_bool',
36 'logtivity_should_log_profile_link' => 'is_bool',
37 'logtivity_should_log_username' => 'is_bool',
38 'logtivity_enable_debug_mode' => 'is_bool',
39 'logtivity_latest_response' => 'is_array',
40 'logtivity_disable_individual_logs' => 'is_string',
41 ];
42
43 /**
44 * Get the admin settings or the plugin
45 *
46 * @return array
47 */
48 public function getOptions()
49 {
50 $options = [];
51
52 foreach ($this->settings as $setting) {
53 $options[$setting] = $this->getOption($setting);
54 }
55
56 return $options;
57 }
58
59 /**
60 * Get an option from the database
61 *
62 * @param string $key
63 * @return mixed
64 */
65 public function getOption($key)
66 {
67 if (has_filter($key)) {
68 return apply_filters($key, false);
69 }
70
71 if (!in_array($key, $this->settings)) {
72 return false;
73 }
74
75 return get_option($key);
76 }
77
78 /**
79 * Get the API key for the site
80 *
81 * @return string
82 */
83 public function getApiKey()
84 {
85 return $this->getOption('logtivity_site_api_key');
86 }
87
88 /**
89 * Should we store the user id?
90 *
91 * @return bool
92 */
93 public function shouldStoreUserId()
94 {
95 return $this->getOption('logtivity_should_store_user_id');
96 }
97
98 /**
99 * Should we store the users IP?
100 *
101 * @return bool
102 */
103 public function shouldStoreIp()
104 {
105 return $this->getOption('logtivity_should_store_ip');
106 }
107
108 /**
109 * Should we store the users profile link?
110 *
111 * @return bool
112 */
113 public function shouldStoreProfileLink()
114 {
115 return $this->getOption('logtivity_should_log_profile_link');
116 }
117
118 /**
119 * Should we store the users username?
120 *
121 * @return bool
122 */
123 public function shouldStoreUsername()
124 {
125 return $this->getOption('logtivity_should_log_username');
126 }
127
128 /**
129 * Should we be logging the response from the API
130 *
131 * @return bool
132 */
133 public function shouldLogLatestResponse()
134 {
135 return $this->getOption('logtivity_enable_debug_mode') || $this->shouldCheckInWithApi();
136 }
137
138 /**
139 * Is it time to check in with the latest API response incase of any new global settings?
140 *
141 * @return bool
142 */
143 public function shouldCheckInWithApi()
144 {
145 $latestReponse = $this->getOption('logtivity_latest_response');
146
147 if (is_array($latestReponse) && isset($latestReponse['date'])) {
148 return time() - strtotime($latestReponse['date']) > 3601; // 1 hour
149 }
150
151 return true;
152 }
153
154 public function urlHash()
155 {
156 return $this->getOption('logtivity_url_hash');
157 }
158
159 public function disabledLogs()
160 {
161 return $this->getOption('logtivity_global_disabled_logs');
162 }
163
164 public function isWhiteLabelMode()
165 {
166 return $this->getOption('logtivity_enable_white_label_mode');
167 }
168
169 /**
170 * Update the options for this plugin
171 *
172 * @param array $data
173 * @return void
174 */
175 public function update($data = [], $checkApiKey = true)
176 {
177 if (count($data)) {
178 foreach ($this->settings as $setting) {
179 if (array_key_exists($setting, $data) && $this->validateSetting($setting, $data[$setting])) {
180 update_option($setting, $data[$setting]);
181 }
182 }
183 } else {
184 foreach ($this->settings as $setting) {
185 if (isset($_POST[$setting]) && $this->validateSetting($setting, $_POST[$setting])) {
186 update_option($setting, $_POST[$setting]);
187 }
188 }
189 }
190
191 if ($checkApiKey) {
192 $this->checkApiKey($data['logtivity_site_api_key'] ?? $_POST['logtivity_site_api_key'] ?? false);
193 }
194 }
195
196 public function checkApiKey($apiKey)
197 {
198 if (!$apiKey) {
199 update_option('logtivity_api_key_check', 'fail');
200 return;
201 }
202
203 $response = Logtivity::log()
204 ->setAction('Settings Updated')
205 ->setContext('Logtivity')
206 ->waitForResponse()
207 ->send();
208
209 if (strpos($response, 'Log Received') !== false) {
210 update_option('logtivity_api_key_check', 'success');
211 } else {
212 update_option('logtivity_api_key_check', 'fail');
213 }
214 }
215
216 /**
217 * Validate that the passed parameters are in the correct format
218 *
219 * @param string $setting
220 * @param string $value
221 * @return bool
222 */
223 protected function validateSetting($setting, $value)
224 {
225 if (!isset($this->rules[$setting])) {
226 return true;
227 }
228
229 $method = $this->rules[$setting];
230
231 if ($method == 'is_bool') {
232 return $method((bool) $value);
233 }
234
235 return $method($value);
236 }
237 }