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

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