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

184 lines 3.6 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] = get_option($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 (!in_array($key, $this->settings)) {
63 return false;
64 }
65
66 return get_option($key);
67 }
68
69 /**
70 * Should we store the user id?
71 *
72 * @return bool
73 */
74 public function shouldStoreUserId()
75 {
76 return $this->getOption('logtivity_should_store_user_id');
77 }
78
79 /**
80 * Should we store the users IP?
81 *
82 * @return bool
83 */
84 public function shouldStoreIp()
85 {
86 return $this->getOption('logtivity_should_store_ip');
87 }
88
89 /**
90 * Should we store the users profile link?
91 *
92 * @return bool
93 */
94 public function shouldStoreProfileLink()
95 {
96 return $this->getOption('logtivity_should_log_profile_link');
97 }
98
99 /**
100 * Should we store the users username?
101 *
102 * @return bool
103 */
104 public function shouldStoreUsername()
105 {
106 return $this->getOption('logtivity_should_log_username');
107 }
108
109 /**
110 * Should we be logging the response from the API
111 *
112 * @return bool
113 */
114 public function shouldLogLatestResponse()
115 {
116 return $this->getOption('logtivity_enable_debug_mode');
117 }
118
119 /**
120 * Update the options for this plugin
121 *
122 * @param array $data
123 * @return void
124 */
125 public function update($data = null)
126 {
127 if ($data) {
128 foreach ($this->settings as $setting)
129 {
130 if (array_key_exists($setting, $data) && $this->validateSetting($setting, $data[$setting])) {
131 update_option($setting, $data[$setting]);
132 }
133 }
134 return;
135 }
136
137 foreach ($this->settings as $setting)
138 {
139 if (isset($_POST[$setting]) && $this->validateSetting($setting, $_POST[$setting])) {
140 update_option($setting, $_POST[$setting]);
141 }
142 }
143
144 $this->checkApiKey($_POST['logtivity_site_api_key'] ?? false);
145 }
146
147 public function checkApiKey($apiKey)
148 {
149 if (!$apiKey) {
150 update_option('logtivity_api_key_check', 'fail');
151 return;
152 }
153
154 $response = Logtivity::log()
155 ->setAction('Settings Updated')
156 ->setContext('Logtivity')
157 ->waitForResponse()
158 ->send();
159
160 if (strpos($response, 'Log Received') !== false) {
161 update_option('logtivity_api_key_check', 'success');
162 } else {
163 update_option('logtivity_api_key_check', 'fail');
164 }
165 }
166
167 /**
168 * Validate that the passed parameters are in the correct format
169 *
170 * @param string $setting
171 * @param string $value
172 * @return bool
173 */
174 protected function validateSetting($setting, $value)
175 {
176 $method = $this->rules[$setting];
177
178 if ($method == 'is_bool') {
179 return $method((bool) $value);
180 }
181
182 return $method($value);
183 }
184 }