| @@ -154,9 +154,9 @@ | ||
| 154 | 154 | * @return bool |
| 155 | 155 | */ |
| 156 | 156 | public function shouldLogLatestResponse(): bool |
| 157 | 157 | { |
| 158 | - return $this->getOption('logtivity_enable_debug_mode') || $this->shouldCheckInWithApi(); | |
| 158 | + return $this->shouldCheckInWithApi(); | |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | 162 | * @return bool |
| @@ -162,15 +162,13 @@ | ||
| 162 | 162 | * @return bool |
| 163 | 163 | */ |
| 164 | 164 | public function shouldCheckInWithApi(): bool |
| 165 | 165 | { |
| 166 | - if ($this->getOption('logtivity_enable_debug_mode') == false) { | |
| 167 | - $lastCheckin = get_option('logtivity_last_settings_check_in_at'); | |
| 168 | - $lastCheckin = $lastCheckin['date'] ?? null; | |
| 166 | + $lastCheckin = get_option('logtivity_last_settings_check_in_at'); | |
| 167 | + $lastCheckin = $lastCheckin['date'] ?? null; | |
| 169 | 168 | |
| 170 | - if ($lastCheckin) { | |
| 171 | - return time() - strtotime($lastCheckin) > $this->checkinDelay; | |
| 172 | - } | |
| 169 | + if ($lastCheckin) { | |
| 170 | + return time() - strtotime($lastCheckin) > $this->checkinDelay; | |
| 173 | 171 | } |
| 174 | 172 | |
| 175 | 173 | return true; |
| 176 | 174 | } |
| @@ -224,10 +222,22 @@ | ||
| 224 | 222 | * @return void |
| 225 | 223 | */ |
| 226 | 224 | public function update(array $data = [], bool $checkApiKey = true): void |
| 227 | 225 | { |
| 228 | - $settings = array_intersect_key($data ?: $_POST, $this->settings); | |
| 226 | + if (!$data) { | |
| 227 | + if (!current_user_can(Logtivity::ACCESS_SETTINGS)) { | |
| 228 | + return; | |
| 229 | + } | |
| 230 | + | |
| 231 | + check_ajax_referer('logtivity_update_settings', 'logtivity_update_settings'); | |
| 232 | + | |
| 233 | + $data = wp_unslash($_POST); | |
| 234 | + } | |
| 235 | + | |
| 236 | + $settings = array_intersect_key($data, $this->settings); | |
| 229 | 237 | foreach ($settings as $key => $value) { |
| 238 | + $value = $this->sanitizeSetting($key, $value); | |
| 239 | + | |
| 230 | 240 | if ($this->validateSetting($key, $value)) { |
| 231 | 241 | update_option($key, $value); |
| 232 | 242 | } |
| 233 | 243 | } |
| @@ -232,11 +242,29 @@ | ||
| 232 | 242 | } |
| 233 | 243 | } |
| 234 | 244 | |
| 235 | 245 | if ($checkApiKey) { |
| 236 | - $this->checkApiKey($data['logtivity_site_api_key'] | |
| 237 | - ?? sanitize_text_field($_POST['logtivity_site_api_key'] ?? null)); | |
| 246 | + $this->checkApiKey($settings['logtivity_site_api_key'] ?? null); | |
| 238 | 247 | } |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Sanitize setting values before saving. | |
| 252 | + * | |
| 253 | + * @param string $key | |
| 254 | + * @param mixed $value | |
| 255 | + * | |
| 256 | + * @return mixed | |
| 257 | + */ | |
| 258 | + protected function sanitizeSetting(string $key, $value) | |
| 259 | + { | |
| 260 | + $setting = $this->settings[$key] ?? null; | |
| 261 | + | |
| 262 | + if (($setting['rule'] ?? null) === 'is_string' && is_scalar($value)) { | |
| 263 | + return sanitize_textarea_field((string)$value); | |
| 264 | + } | |
| 265 | + | |
| 266 | + return $value; | |
| 239 | 267 | } |
| 240 | 268 | |
| 241 | 269 | /** |
| 242 | 270 | * Validate that the passed parameters are in the correct format |