PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.16.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.16.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.16.0, at Admin/Logtivity_Options.php

209 lines 4.2 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 ];
23
24 /**
25 * The option keys that we can save to the options table
26 *
27 * @var array
28 */
29 protected $rules = [
30 'logtivity_site_api_key' => 'is_string',
31 'logtivity_disable_default_logging' => 'is_bool',
32 'logtivity_should_store_user_id' => 'is_bool',
33 'logtivity_should_store_ip' => 'is_bool',
34 'logtivity_should_log_profile_link' => 'is_bool',
35 'logtivity_should_log_username' => 'is_bool',
36 'logtivity_enable_debug_mode' => 'is_bool',
37 'logtivity_latest_response' => 'is_array',
38 'logtivity_disable_individual_logs' => 'is_string',
39 ];
40
41 /**
42 * Get the admin settings or the plugin
43 *
44 * @return array
45 */
46 public function getOptions()
47 {
48 $options = [];
49
50 foreach ($this->settings as $setting) {
51 $options[$setting] = $this->getOption($setting);
52 }
53
54 return $options;
55 }
56
57 /**
58 * Get an option from the database
59 *
60 * @param string $key
61 * @return mixed
62 */
63 public function getOption($key)
64 {
65 if (has_filter($key)) {
66 return apply_filters($key, false);
67 }
68
69 if (!in_array($key, $this->settings)) {
70 return false;
71 }
72
73 return get_option($key);
74 }
75
76 /**
77 * Get the API key for the site
78 *
79 * @return string
80 */
81 public function getApiKey()
82 {
83 return $this->getOption('logtivity_site_api_key');
84 }
85
86 /**
87 * Should we store the user id?
88 *
89 * @return bool
90 */
91 public function shouldStoreUserId()
92 {
93 return $this->getOption('logtivity_should_store_user_id');
94 }
95
96 /**
97 * Should we store the users IP?
98 *
99 * @return bool
100 */
101 public function shouldStoreIp()
102 {
103 return $this->getOption('logtivity_should_store_ip');
104 }
105
106 /**
107 * Should we store the users profile link?
108 *
109 * @return bool
110 */
111 public function shouldStoreProfileLink()
112 {
113 return $this->getOption('logtivity_should_log_profile_link');
114 }
115
116 /**
117 * Should we store the users username?
118 *
119 * @return bool
120 */
121 public function shouldStoreUsername()
122 {
123 return $this->getOption('logtivity_should_log_username');
124 }
125
126 /**
127 * Should we be logging the response from the API
128 *
129 * @return bool
130 */
131 public function shouldLogLatestResponse()
132 {
133 return $this->getOption('logtivity_enable_debug_mode');
134 }
135
136 public function urlHash()
137 {
138 return $this->getOption('logtivity_url_hash');
139 }
140
141 /**
142 * Update the options for this plugin
143 *
144 * @param array $data
145 * @return void
146 */
147 public function update($data = [], $checkApiKey = true)
148 {
149 if (count($data)) {
150 foreach ($this->settings as $setting) {
151 if (array_key_exists($setting, $data) && $this->validateSetting($setting, $data[$setting])) {
152 update_option($setting, $data[$setting]);
153 }
154 }
155 } else {
156 foreach ($this->settings as $setting) {
157 if (isset($_POST[$setting]) && $this->validateSetting($setting, $_POST[$setting])) {
158 update_option($setting, $_POST[$setting]);
159 }
160 }
161 }
162
163 if ($checkApiKey) {
164 $this->checkApiKey($data['logtivity_site_api_key'] ?? $_POST['logtivity_site_api_key'] ?? false);
165 }
166 }
167
168 public function checkApiKey($apiKey)
169 {
170 if (!$apiKey) {
171 update_option('logtivity_api_key_check', 'fail');
172 return;
173 }
174
175 $response = Logtivity::log()
176 ->setAction('Settings Updated')
177 ->setContext('Logtivity')
178 ->waitForResponse()
179 ->send();
180
181 if (strpos($response, 'Log Received') !== false) {
182 update_option('logtivity_api_key_check', 'success');
183 } else {
184 update_option('logtivity_api_key_check', 'fail');
185 }
186 }
187
188 /**
189 * Validate that the passed parameters are in the correct format
190 *
191 * @param string $setting
192 * @param string $value
193 * @return bool
194 */
195 protected function validateSetting($setting, $value)
196 {
197 if (!isset($this->rules[$setting])) {
198 return true;
199 }
200
201 $method = $this->rules[$setting];
202
203 if ($method == 'is_bool') {
204 return $method((bool) $value);
205 }
206
207 return $method($value);
208 }
209 }