wizard
1 month ago
class-dashboard-controller.php
1 month ago
class-license-resolve-controller.php
1 month ago
class-newsletter-signup-controller.php
1 month ago
class-notice-controller.php
1 month ago
class-plugin-reset-controller.php
1 month ago
class-rewrite-check-controller.php
1 month ago
class-settings-controller.php
1 month ago
class-troubleshooting-controller.php
1 month ago
class-settings-controller.php
587 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Admin\Controllers\PluginResetController; |
| 8 | use SuperbAddons\Config\Capabilities; |
| 9 | use SuperbAddons\Data\Controllers\CacheController; |
| 10 | use SuperbAddons\Data\Controllers\KeyController; |
| 11 | use SuperbAddons\Data\Controllers\OptionController; |
| 12 | use SuperbAddons\Data\Controllers\RestController; |
| 13 | use SuperbAddons\Data\Controllers\SettingsOptionKey; |
| 14 | use Exception; |
| 15 | use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController; |
| 16 | use SuperbAddons\Data\Controllers\CompatibilitySettingsOptionKey; |
| 17 | use SuperbAddons\Data\Controllers\LogController; |
| 18 | use SuperbAddons\Data\Utils\KeyException; |
| 19 | use SuperbAddons\Data\Utils\SettingsException; |
| 20 | use SuperbAddons\Gutenberg\Controllers\GutenbergController; |
| 21 | use SuperbAddons\Gutenberg\Controllers\GutenbergEnhancementsController; |
| 22 | use SuperbAddons\Gutenberg\Form\FormAccessControl; |
| 23 | use SuperbAddons\Gutenberg\Form\FormIntegrationHandler; |
| 24 | use SuperbAddons\Gutenberg\Form\FormPermissions; |
| 25 | use SuperbAddons\Gutenberg\Form\FormSettings; |
| 26 | |
| 27 | class SettingsController |
| 28 | { |
| 29 | const SETTINGS_ROUTE = '/settings'; |
| 30 | |
| 31 | public function __construct() |
| 32 | { |
| 33 | RestController::AddRoute(self::SETTINGS_ROUTE, array( |
| 34 | 'methods' => 'POST', |
| 35 | 'permission_callback' => array($this, 'SettingsCallbackPermissionCheck'), |
| 36 | 'callback' => array($this, 'SettingsRouteCallback'), |
| 37 | )); |
| 38 | } |
| 39 | |
| 40 | public function SettingsCallbackPermissionCheck() |
| 41 | { |
| 42 | // Restrict endpoint to only users who have the proper capability. |
| 43 | if (!current_user_can(Capabilities::ADMIN)) { |
| 44 | return new \WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401)); |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | public function SettingsRouteCallback($request) |
| 51 | { |
| 52 | if (!isset($request['action'])) { |
| 53 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 54 | } |
| 55 | switch ($request['action']) { |
| 56 | case 'submit_feedback': |
| 57 | return $this->SubmitFeedbackCallback($request); |
| 58 | case 'addkey': |
| 59 | return $this->RegisterKeyCallback($request); |
| 60 | case 'removekey': |
| 61 | return $this->RemoveKeyCallback(); |
| 62 | case SettingsOptionKey::LOGS_ENABLED: |
| 63 | case SettingsOptionKey::LOG_SHARE_ENABLED: |
| 64 | case 'clear_cache': |
| 65 | case 'clear_logs': |
| 66 | case 'view_logs': |
| 67 | case 'clear_restoration_points': |
| 68 | return $this->SaveSettingsCallback($request['action']); |
| 69 | case GutenbergEnhancementsController::HIGHLIGHTS_KEY: |
| 70 | case GutenbergEnhancementsController::HIGHLIGHTS_QUICKOPTIONS_KEY: |
| 71 | case GutenbergEnhancementsController::HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY: |
| 72 | case GutenbergEnhancementsController::RESPONSIVE_KEY: |
| 73 | case GutenbergEnhancementsController::ANIMATIONS_KEY: |
| 74 | case GutenbergEnhancementsController::CONDITIONS_KEY: |
| 75 | case GutenbergEnhancementsController::DYNAMIC_CONTENT_KEY: |
| 76 | case GutenbergEnhancementsController::NAVIGATION_KEY: |
| 77 | case GutenbergEnhancementsController::RICHTEXT_KEY: |
| 78 | case GutenbergEnhancementsController::SOCIAL_ICONS_KEY: |
| 79 | case GutenbergEnhancementsController::DASHBOARD_SHORTCUTS_KEY: |
| 80 | case GutenbergEnhancementsController::STICKY_KEY: |
| 81 | case GutenbergEnhancementsController::Z_INDEX_KEY: |
| 82 | case GutenbergEnhancementsController::PANEL_DEFAULT_STATE_KEY: |
| 83 | return GutenbergEnhancementsController::OptionsSaveCallback($request); |
| 84 | case CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING: |
| 85 | return $this->SaveCompatibilitySettingsCallback($request['action']); |
| 86 | case 'save_integration_key': |
| 87 | return $this->SaveIntegrationKeyCallback($request); |
| 88 | case 'remove_integration_key': |
| 89 | return $this->RemoveIntegrationKeyCallback($request); |
| 90 | case 'toggle_block': |
| 91 | return $this->ToggleBlockCallback($request); |
| 92 | case 'save_form_permissions': |
| 93 | return $this->SaveFormPermissionsCallback($request); |
| 94 | case 'save_default_email': |
| 95 | return $this->SaveDefaultEmailCallback($request); |
| 96 | case 'save_data_retention': |
| 97 | return $this->SaveDataRetentionCallback($request); |
| 98 | case 'save_captcha_key': |
| 99 | return $this->SaveCaptchaKeyCallback($request); |
| 100 | case 'remove_captcha_key': |
| 101 | return $this->RemoveCaptchaKeyCallback($request); |
| 102 | case 'get_integration_usage': |
| 103 | return $this->GetIntegrationUsageCallback($request); |
| 104 | case 'remove_all_data': |
| 105 | return $this->RemoveAllDataCallback($request); |
| 106 | default: |
| 107 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private function SubmitFeedbackCallback($request) |
| 112 | { |
| 113 | try { |
| 114 | if (!isset($request['spbaddons_reason']) || empty($request['spbaddons_reason'])) throw new SettingsException(__('Unable to send feedback. No feedback provided.', "superb-blocks")); |
| 115 | |
| 116 | if ($request['spbaddons_reason'] === 'other' && isset($request['spbaddons_other'])) { |
| 117 | $message = sanitize_text_field(wp_unslash($request['spbaddons_other'])); |
| 118 | } else { |
| 119 | $message = sanitize_text_field(wp_unslash($request['spbaddons_reason'])); |
| 120 | } |
| 121 | LogController::SendFeedback($message); |
| 122 | |
| 123 | return rest_ensure_response(['success' => true]); |
| 124 | } catch (SettingsException $s_ex) { |
| 125 | return rest_ensure_response(['success' => false, "text" => esc_html($s_ex->getMessage())]); |
| 126 | } catch (Exception $ex) { |
| 127 | LogController::HandleException($ex); |
| 128 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private function RegisterKeyCallback($request) |
| 133 | { |
| 134 | try { |
| 135 | KeyController::RegisterKey($request['key'], true); |
| 136 | return rest_ensure_response(['success' => true]); |
| 137 | } catch (KeyException $k_ex) { |
| 138 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 139 | } catch (Exception $ex) { |
| 140 | LogController::HandleException($ex); |
| 141 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private function RemoveKeyCallback() |
| 146 | { |
| 147 | try { |
| 148 | $removed = KeyController::RemoveKey(); |
| 149 | return rest_ensure_response(['success' => $removed]); |
| 150 | } catch (KeyException $k_ex) { |
| 151 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 152 | } catch (Exception $ex) { |
| 153 | LogController::HandleException($ex); |
| 154 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public static function GetSettings() |
| 159 | { |
| 160 | return OptionController::GetSettings(); |
| 161 | } |
| 162 | |
| 163 | public static function GetCompatibilitySettings() |
| 164 | { |
| 165 | return OptionController::GetCompatibilitySettings(); |
| 166 | } |
| 167 | |
| 168 | private function SaveSettingsCallback($action) |
| 169 | { |
| 170 | try { |
| 171 | $option_controller = new OptionController(); |
| 172 | $current_settings = OptionController::GetSettings(); |
| 173 | |
| 174 | switch ($action) { |
| 175 | case SettingsOptionKey::LOGS_ENABLED: |
| 176 | $current_settings[SettingsOptionKey::LOGS_ENABLED] = !$current_settings[SettingsOptionKey::LOGS_ENABLED]; |
| 177 | $option_controller->SaveSettings($current_settings); |
| 178 | break; |
| 179 | case SettingsOptionKey::LOG_SHARE_ENABLED: |
| 180 | $current_settings[SettingsOptionKey::LOG_SHARE_ENABLED] = !$current_settings[SettingsOptionKey::LOG_SHARE_ENABLED]; |
| 181 | $saved = $option_controller->SaveSettings($current_settings); |
| 182 | if ($saved) { |
| 183 | $current_settings[SettingsOptionKey::LOG_SHARE_ENABLED] ? LogController::MaybeSubscribeCron() : LogController::MaybeUnsubscribeCron(); |
| 184 | } |
| 185 | break; |
| 186 | case 'clear_cache': |
| 187 | $cleared = CacheController::ClearCacheAll(); |
| 188 | if (!$cleared) throw new SettingsException(__('Cache could not be cleared.', "superb-blocks")); |
| 189 | break; |
| 190 | case 'clear_logs': |
| 191 | $cleared = LogController::ClearLogs(); |
| 192 | if (!$cleared) throw new SettingsException(__('Logs could not be cleared.', "superb-blocks")); |
| 193 | break; |
| 194 | case 'view_logs': |
| 195 | return rest_ensure_response(['success' => true, 'content' => LogController::GetLogs()]); |
| 196 | case 'clear_restoration_points': |
| 197 | $cleared = WizardRestorationPointController::FullRestorationCleanup(); |
| 198 | if (!$cleared) throw new SettingsException(__('Restoration points could not be cleared.', "superb-blocks")); |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | return rest_ensure_response(['success' => true]); |
| 203 | } catch (SettingsException $s_ex) { |
| 204 | return rest_ensure_response(['success' => false, "text" => esc_html($s_ex->getMessage())]); |
| 205 | } catch (Exception $ex) { |
| 206 | LogController::HandleException($ex); |
| 207 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private function SaveCompatibilitySettingsCallback($action) |
| 212 | { |
| 213 | try { |
| 214 | $option_controller = new OptionController(); |
| 215 | $current_settings = OptionController::GetCompatibilitySettings(); |
| 216 | |
| 217 | switch ($action) { |
| 218 | case CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING: |
| 219 | $current_settings[CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING] = !$current_settings[CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING]; |
| 220 | $option_controller->SaveCompatibilitySettings($current_settings); |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | return rest_ensure_response(['success' => true]); |
| 225 | } catch (SettingsException $s_ex) { |
| 226 | return rest_ensure_response(['success' => false, "text" => esc_html($s_ex->getMessage())]); |
| 227 | } catch (Exception $ex) { |
| 228 | LogController::HandleException($ex); |
| 229 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private static function GetIntegrationOptionKey($integration) |
| 234 | { |
| 235 | $map = array( |
| 236 | 'mailchimp' => FormSettings::OPTION_MAILCHIMP_API_KEY, |
| 237 | 'brevo' => FormSettings::OPTION_BREVO_API_KEY, |
| 238 | ); |
| 239 | return isset($map[$integration]) ? $map[$integration] : false; |
| 240 | } |
| 241 | |
| 242 | private function SaveIntegrationKeyCallback($request) |
| 243 | { |
| 244 | try { |
| 245 | $integration = isset($request['integration']) ? sanitize_text_field(wp_unslash($request['integration'])) : ''; |
| 246 | $api_key = isset($request['api_key']) ? wp_unslash($request['api_key']) : ''; |
| 247 | |
| 248 | // Google Sheets uses a JSON Service Account key |
| 249 | if ($integration === 'google_sheets') { |
| 250 | return $this->SaveGoogleSheetsKeyCallback($api_key); |
| 251 | } |
| 252 | |
| 253 | $api_key = sanitize_text_field($api_key); |
| 254 | $option_key = self::GetIntegrationOptionKey($integration); |
| 255 | if (!$option_key) { |
| 256 | throw new SettingsException(__('Invalid integration.', "superb-blocks")); |
| 257 | } |
| 258 | if (empty($api_key)) { |
| 259 | throw new SettingsException(__('API key cannot be empty.', "superb-blocks")); |
| 260 | } |
| 261 | |
| 262 | // Save the key so the integration handler can use it for validation. |
| 263 | FormSettings::Set($option_key, $api_key); |
| 264 | |
| 265 | // Validate the key by attempting to fetch lists from the service. |
| 266 | if ($integration === 'mailchimp') { |
| 267 | $result = FormIntegrationHandler::GetMailchimpLists(); |
| 268 | } else { |
| 269 | $result = FormIntegrationHandler::GetBrevoLists(); |
| 270 | } |
| 271 | |
| 272 | if (is_wp_error($result)) { |
| 273 | // Validation failed — remove the key and return the error. |
| 274 | FormSettings::Remove($option_key); |
| 275 | throw new SettingsException($result->get_error_message()); |
| 276 | } |
| 277 | |
| 278 | return rest_ensure_response(array( |
| 279 | 'success' => true, |
| 280 | 'masked_key' => FormSettings::GetMasked($option_key), |
| 281 | )); |
| 282 | } catch (SettingsException $s_ex) { |
| 283 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 284 | } catch (Exception $ex) { |
| 285 | LogController::HandleException($ex); |
| 286 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private function SaveGoogleSheetsKeyCallback($json_string) |
| 291 | { |
| 292 | try { |
| 293 | if (empty($json_string)) { |
| 294 | throw new SettingsException(__('Service Account JSON key cannot be empty.', 'superb-blocks')); |
| 295 | } |
| 296 | |
| 297 | $json = json_decode($json_string, true); |
| 298 | if (!is_array($json)) { |
| 299 | throw new SettingsException(__('Invalid JSON format. Please paste the entire contents of your Service Account JSON key.', 'superb-blocks')); |
| 300 | } |
| 301 | |
| 302 | $client_email = isset($json['client_email']) ? sanitize_email($json['client_email']) : ''; |
| 303 | $private_key = isset($json['private_key']) ? $json['private_key'] : ''; |
| 304 | |
| 305 | if (empty($client_email) || empty($private_key)) { |
| 306 | throw new SettingsException(__('The JSON key must contain "client_email" and "private_key" fields.', 'superb-blocks')); |
| 307 | } |
| 308 | |
| 309 | // Validate email format |
| 310 | if (!is_email($client_email)) { |
| 311 | throw new SettingsException(__('Invalid client_email in the Service Account key.', 'superb-blocks')); |
| 312 | } |
| 313 | |
| 314 | // Store the extracted values |
| 315 | FormSettings::Set(FormSettings::OPTION_GOOGLE_SHEETS_CLIENT_EMAIL, $client_email); |
| 316 | FormSettings::Set(FormSettings::OPTION_GOOGLE_SHEETS_PRIVATE_KEY, $private_key); |
| 317 | |
| 318 | return rest_ensure_response(array( |
| 319 | 'success' => true, |
| 320 | 'masked_key' => $client_email, |
| 321 | )); |
| 322 | } catch (SettingsException $s_ex) { |
| 323 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 324 | } catch (Exception $ex) { |
| 325 | LogController::HandleException($ex); |
| 326 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | private function RemoveIntegrationKeyCallback($request) |
| 331 | { |
| 332 | try { |
| 333 | $integration = isset($request['integration']) ? sanitize_text_field(wp_unslash($request['integration'])) : ''; |
| 334 | |
| 335 | // Google Sheets has two option keys |
| 336 | if ($integration === 'google_sheets') { |
| 337 | FormSettings::Remove(FormSettings::OPTION_GOOGLE_SHEETS_CLIENT_EMAIL); |
| 338 | FormSettings::Remove(FormSettings::OPTION_GOOGLE_SHEETS_PRIVATE_KEY); |
| 339 | delete_transient('spb_google_token'); |
| 340 | return rest_ensure_response(array('success' => true)); |
| 341 | } |
| 342 | |
| 343 | $option_key = self::GetIntegrationOptionKey($integration); |
| 344 | if (!$option_key) { |
| 345 | throw new SettingsException(__('Invalid integration.', "superb-blocks")); |
| 346 | } |
| 347 | |
| 348 | FormSettings::Remove($option_key); |
| 349 | |
| 350 | return rest_ensure_response(array('success' => true)); |
| 351 | } catch (SettingsException $s_ex) { |
| 352 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 353 | } catch (Exception $ex) { |
| 354 | LogController::HandleException($ex); |
| 355 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | public static function GetRelevantCompatibilitySettings() |
| 360 | { |
| 361 | $relevant_settings = array(); |
| 362 | // Check if Spectra is active |
| 363 | if (class_exists('UAGB_Loader')) { |
| 364 | $relevant_settings[CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING] = true; |
| 365 | } |
| 366 | |
| 367 | return $relevant_settings; |
| 368 | } |
| 369 | |
| 370 | public static function IsCompatibilitySettingRelevantAndEnabled($compatibility_setting) |
| 371 | { |
| 372 | $relevant_settings = self::GetRelevantCompatibilitySettings(); |
| 373 | if (!isset($relevant_settings[$compatibility_setting])) return false; |
| 374 | |
| 375 | $compatibility_settings = self::GetCompatibilitySettings(); |
| 376 | return $compatibility_settings[$compatibility_setting]; |
| 377 | } |
| 378 | |
| 379 | private function ToggleBlockCallback($request) |
| 380 | { |
| 381 | try { |
| 382 | if (!isset($request['block'])) { |
| 383 | throw new SettingsException(__('No block specified.', 'superb-blocks')); |
| 384 | } |
| 385 | |
| 386 | $block_slug = sanitize_text_field($request['block']); |
| 387 | |
| 388 | // Validate the block is in the toggleable list |
| 389 | if (!in_array($block_slug, GutenbergController::TOGGLEABLE_BLOCKS, true)) { |
| 390 | throw new SettingsException(__('Invalid block.', 'superb-blocks')); |
| 391 | } |
| 392 | |
| 393 | $disabled = OptionController::GetDisabledBlocks(); |
| 394 | $key = array_search($block_slug, $disabled, true); |
| 395 | if ($key !== false) { |
| 396 | // Currently disabled, re-enable it |
| 397 | unset($disabled[$key]); |
| 398 | $disabled = array_values($disabled); |
| 399 | } else { |
| 400 | // Currently enabled, disable it |
| 401 | $disabled[] = $block_slug; |
| 402 | } |
| 403 | |
| 404 | OptionController::SaveDisabledBlocks($disabled); |
| 405 | |
| 406 | return rest_ensure_response(array('success' => true)); |
| 407 | } catch (SettingsException $s_ex) { |
| 408 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 409 | } catch (Exception $ex) { |
| 410 | LogController::HandleException($ex); |
| 411 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private function SaveFormPermissionsCallback($request) |
| 416 | { |
| 417 | try { |
| 418 | $raw = isset($request['permissions']) ? $request['permissions'] : ''; |
| 419 | $permissions = is_string($raw) ? json_decode(wp_unslash($raw), true) : $raw; |
| 420 | if (!is_array($permissions)) { |
| 421 | throw new SettingsException(__('Invalid permissions data.', 'superb-blocks')); |
| 422 | } |
| 423 | |
| 424 | FormPermissions::SaveAll($permissions); |
| 425 | |
| 426 | // Save access control toggle |
| 427 | $access_control_enabled = isset($request['access_control_enabled']) && $request['access_control_enabled'] === '1'; |
| 428 | update_option(FormAccessControl::OPTION_ENABLED, $access_control_enabled ? '1' : '', false); |
| 429 | |
| 430 | return rest_ensure_response(array('success' => true)); |
| 431 | } catch (SettingsException $s_ex) { |
| 432 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 433 | } catch (Exception $ex) { |
| 434 | LogController::HandleException($ex); |
| 435 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | private function SaveDefaultEmailCallback($request) |
| 440 | { |
| 441 | try { |
| 442 | $from_name = isset($request['from_name']) ? sanitize_text_field(wp_unslash($request['from_name'])) : ''; |
| 443 | $from_email = isset($request['from_email']) ? sanitize_email(wp_unslash($request['from_email'])) : ''; |
| 444 | |
| 445 | // Validate email if provided |
| 446 | if ($from_email !== '' && !is_email($from_email)) { |
| 447 | throw new SettingsException(__('Please enter a valid email address.', 'superb-blocks')); |
| 448 | } |
| 449 | |
| 450 | update_option('superbaddons_form_default_email', array( |
| 451 | 'from_name' => $from_name, |
| 452 | 'from_email' => $from_email, |
| 453 | )); |
| 454 | |
| 455 | return rest_ensure_response(array('success' => true)); |
| 456 | } catch (SettingsException $s_ex) { |
| 457 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 458 | } catch (Exception $ex) { |
| 459 | LogController::HandleException($ex); |
| 460 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | private function SaveDataRetentionCallback($request) |
| 465 | { |
| 466 | try { |
| 467 | $days = isset($request['days']) ? intval($request['days']) : 0; |
| 468 | |
| 469 | // Validate allowed values |
| 470 | $allowed = array(0, 30, 60, 90, 180, 365); |
| 471 | if (!in_array($days, $allowed, true)) { |
| 472 | throw new SettingsException(__('Invalid retention period.', 'superb-blocks')); |
| 473 | } |
| 474 | |
| 475 | update_option('superbaddons_form_data_retention', $days); |
| 476 | |
| 477 | return rest_ensure_response(array('success' => true)); |
| 478 | } catch (SettingsException $s_ex) { |
| 479 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 480 | } catch (Exception $ex) { |
| 481 | LogController::HandleException($ex); |
| 482 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | private static function GetCaptchaOptionKeys($provider) |
| 487 | { |
| 488 | $map = array( |
| 489 | 'hcaptcha' => array( |
| 490 | 'site_key' => FormSettings::OPTION_HCAPTCHA_SITE_KEY, |
| 491 | 'secret_key' => FormSettings::OPTION_HCAPTCHA_SECRET_KEY, |
| 492 | ), |
| 493 | 'recaptcha' => array( |
| 494 | 'site_key' => FormSettings::OPTION_RECAPTCHA_SITE_KEY, |
| 495 | 'secret_key' => FormSettings::OPTION_RECAPTCHA_SECRET_KEY, |
| 496 | ), |
| 497 | 'turnstile' => array( |
| 498 | 'site_key' => FormSettings::OPTION_TURNSTILE_SITE_KEY, |
| 499 | 'secret_key' => FormSettings::OPTION_TURNSTILE_SECRET_KEY, |
| 500 | ), |
| 501 | ); |
| 502 | return isset($map[$provider]) ? $map[$provider] : false; |
| 503 | } |
| 504 | |
| 505 | private function SaveCaptchaKeyCallback($request) |
| 506 | { |
| 507 | try { |
| 508 | $provider = isset($request['provider']) ? sanitize_text_field(wp_unslash($request['provider'])) : ''; |
| 509 | $site_key = isset($request['site_key']) ? sanitize_text_field(wp_unslash($request['site_key'])) : ''; |
| 510 | $secret_key = isset($request['secret_key']) ? sanitize_text_field(wp_unslash($request['secret_key'])) : ''; |
| 511 | |
| 512 | $keys = self::GetCaptchaOptionKeys($provider); |
| 513 | if (!$keys) { |
| 514 | throw new SettingsException(__('Invalid CAPTCHA provider.', 'superb-blocks')); |
| 515 | } |
| 516 | if (empty($site_key) || empty($secret_key)) { |
| 517 | throw new SettingsException(__('Both site key and secret key are required.', 'superb-blocks')); |
| 518 | } |
| 519 | |
| 520 | FormSettings::Set($keys['site_key'], $site_key); |
| 521 | FormSettings::Set($keys['secret_key'], $secret_key); |
| 522 | |
| 523 | return rest_ensure_response(array( |
| 524 | 'success' => true, |
| 525 | 'masked_secret' => FormSettings::GetMasked($keys['secret_key']), |
| 526 | )); |
| 527 | } catch (SettingsException $s_ex) { |
| 528 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 529 | } catch (Exception $ex) { |
| 530 | LogController::HandleException($ex); |
| 531 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | private function RemoveCaptchaKeyCallback($request) |
| 536 | { |
| 537 | try { |
| 538 | $provider = isset($request['provider']) ? sanitize_text_field(wp_unslash($request['provider'])) : ''; |
| 539 | $keys = self::GetCaptchaOptionKeys($provider); |
| 540 | if (!$keys) { |
| 541 | throw new SettingsException(__('Invalid CAPTCHA provider.', 'superb-blocks')); |
| 542 | } |
| 543 | |
| 544 | FormSettings::Remove($keys['site_key']); |
| 545 | FormSettings::Remove($keys['secret_key']); |
| 546 | |
| 547 | return rest_ensure_response(array('success' => true)); |
| 548 | } catch (SettingsException $s_ex) { |
| 549 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 550 | } catch (Exception $ex) { |
| 551 | LogController::HandleException($ex); |
| 552 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | private function GetIntegrationUsageCallback($request) |
| 557 | { |
| 558 | try { |
| 559 | return rest_ensure_response(array( |
| 560 | 'success' => true, |
| 561 | 'mailchimp' => FormSettings::CountFormsUsingIntegration('mailchimp'), |
| 562 | 'brevo' => FormSettings::CountFormsUsingIntegration('brevo'), |
| 563 | 'hcaptcha' => FormSettings::CountFormsUsingCaptchaProvider('hcaptcha'), |
| 564 | 'recaptcha' => FormSettings::CountFormsUsingCaptchaProvider('recaptcha'), |
| 565 | 'turnstile' => FormSettings::CountFormsUsingCaptchaProvider('turnstile'), |
| 566 | )); |
| 567 | } catch (Exception $ex) { |
| 568 | LogController::HandleException($ex); |
| 569 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | private function RemoveAllDataCallback($request) |
| 574 | { |
| 575 | try { |
| 576 | $include_submissions = isset($request['include_submissions']) && $request['include_submissions'] === '1'; |
| 577 | PluginResetController::RemoveAllPluginData($include_submissions); |
| 578 | return rest_ensure_response(array('success' => true)); |
| 579 | } catch (SettingsException $s_ex) { |
| 580 | return rest_ensure_response(array('success' => false, 'text' => esc_html($s_ex->getMessage()))); |
| 581 | } catch (Exception $ex) { |
| 582 | LogController::HandleException($ex); |
| 583 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 |