← All changes
|
backend/src/Controllers/Settings/GetAllSettingsController.php
+143
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,143 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | + * @licence See COPYING.md for license details. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace IvyForms\Controllers\Settings; | |
| 9 | + | |
| 10 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 11 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 12 | +use IvyForms\Controllers\Controller; | |
| 13 | +use IvyForms\Infrastructure\WP\MCP\McpFeatureGate; | |
| 14 | +use IvyForms\Services\Settings\SettingsService; | |
| 15 | +use IvyForms\Services\Translations\BackendStrings; | |
| 16 | +use IvyForms\Services\Usage\UsageTrackingService; | |
| 17 | +use WP_REST_Request; | |
| 18 | +use WP_REST_Response; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Class GetAllSettingsController | |
| 22 | + * | |
| 23 | + * @package IvyForms\Controllers\Settings | |
| 24 | + */ | |
| 25 | +class GetAllSettingsController extends Controller | |
| 26 | +{ | |
| 27 | + private SettingsService $settingsService; | |
| 28 | + private UsageTrackingService $usageTrackingService; | |
| 29 | + | |
| 30 | + public function __construct( | |
| 31 | + SettingsService $settingsService, | |
| 32 | + UsageTrackingService $usageTrackingService | |
| 33 | + ) { | |
| 34 | + $this->settingsService = $settingsService; | |
| 35 | + $this->usageTrackingService = $usageTrackingService; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Get all settings organized by category | |
| 40 | + * | |
| 41 | + * @param WP_REST_Request $data | |
| 42 | + * | |
| 43 | + * @return WP_REST_Response | |
| 44 | + * | |
| 45 | + * @throws ForbiddenException | |
| 46 | + */ | |
| 47 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 48 | + { | |
| 49 | + // Verify the nonce | |
| 50 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 51 | + | |
| 52 | + // Get all settings from the service | |
| 53 | + $allSettings = $this->settingsService->getAllSettings(); | |
| 54 | + | |
| 55 | + // Organize settings structure for frontend (prepare for future extensions) | |
| 56 | + $organizedSettings = [ | |
| 57 | + 'security' => [ | |
| 58 | + 'recaptcha' => $allSettings['security']['recaptcha'] ?? null, | |
| 59 | + 'turnstile' => $allSettings['security']['turnstile'] ?? null, | |
| 60 | + 'hcaptcha' => $allSettings['security']['hcaptcha'] ?? null, | |
| 61 | + | |
| 62 | + // Example: If you wanted to include security status info, you could inject SecurityService: | |
| 63 | + // 'status' => [ | |
| 64 | + // 'activeCaptchaProvider' => $this->securityService->getActiveCaptchaProvider(), | |
| 65 | + // 'isCaptchaConfigured' => $this->securityService->isCaptchaConfigured(), | |
| 66 | + // ] | |
| 67 | + ], | |
| 68 | + 'general' => [ | |
| 69 | + 'wcagBackend' => $allSettings['general']['wcagBackend'] ?? false, | |
| 70 | + 'mcp_enabled' => McpFeatureGate::coerceStoredEnabled( | |
| 71 | + $allSettings['general']['mcp_enabled'] ?? true | |
| 72 | + ), | |
| 73 | + 'favoriteTemplates' => $allSettings['general']['favoriteTemplates'] ?? [], | |
| 74 | + 'delete_on_uninstall' => $allSettings['general']['delete_on_uninstall'] ?? false, | |
| 75 | + 'usageTrackingEnabled' => $this->usageTrackingService->isSettingEnabled(), | |
| 76 | + ], | |
| 77 | + 'integrations' => [ | |
| 78 | + 'wpdatatables' => array_merge( | |
| 79 | + $allSettings['integrations']['wpdatatables'] ?? ['enabled' => false], | |
| 80 | + [ | |
| 81 | + 'connected' => class_exists('WPDataTable') || defined('WDT_ROOT_PATH') | |
| 82 | + ] | |
| 83 | + ), | |
| 84 | + 'ameliabooking' => array_merge( | |
| 85 | + $allSettings['integrations']['ameliabooking'] ?? ['enabled' => false], | |
| 86 | + [ | |
| 87 | + 'connected' => class_exists('AmeliaBooking\Plugin') || defined('AMELIA_PATH') | |
| 88 | + ] | |
| 89 | + ), | |
| 90 | + 'square' => array_merge( | |
| 91 | + $allSettings['integrations']['square'] ?? ['enabled' => false], | |
| 92 | + [ | |
| 93 | + 'connected' => ! empty($allSettings['payment']['square']['connected']), | |
| 94 | + ] | |
| 95 | + ), | |
| 96 | + ], | |
| 97 | + 'payment' => [ | |
| 98 | + 'square' => self::sanitizeSquareSettingsForFrontend( | |
| 99 | + is_array($allSettings['payment']['square'] ?? null) | |
| 100 | + ? $allSettings['payment']['square'] | |
| 101 | + : [] | |
| 102 | + ), | |
| 103 | + ], | |
| 104 | + ]; | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Filter to allow Pro plugin to add additional settings data | |
| 108 | + * | |
| 109 | + * @param array $organizedSettings The organized settings array | |
| 110 | + * @param array $allSettings The raw settings from database | |
| 111 | + * | |
| 112 | + * @since 1.0.0 | |
| 113 | + */ | |
| 114 | + $organizedSettings = apply_filters('ivyforms/global/settings/get_all', $organizedSettings, $allSettings); | |
| 115 | + | |
| 116 | + return new WP_REST_Response([ | |
| 117 | + 'message' => BackendStrings::getSettingsStrings()['all_settings_retrieved'], | |
| 118 | + 'data' => $organizedSettings | |
| 119 | + ], 200); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * @param array<string, mixed> $squareSettings | |
| 124 | + * @return array<string, mixed> | |
| 125 | + */ | |
| 126 | + private static function sanitizeSquareSettingsForFrontend(array $squareSettings): array | |
| 127 | + { | |
| 128 | + $defaults = [ | |
| 129 | + 'connected' => false, | |
| 130 | + 'merchantId' => '', | |
| 131 | + 'merchantName' => '', | |
| 132 | + 'locationId' => '', | |
| 133 | + 'locationName' => '', | |
| 134 | + 'currency' => '', | |
| 135 | + 'disconnectedByWebhook' => false, | |
| 136 | + ]; | |
| 137 | + | |
| 138 | + $merged = array_merge($defaults, $squareSettings); | |
| 139 | + unset($merged['accessToken'], $merged['refreshToken']); | |
| 140 | + | |
| 141 | + return $merged; | |
| 142 | + } | |
| 143 | +} | |