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

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