← All changes
|
backend/src/Controllers/Confirmation/UpdateConfirmationController.php
+16
-8
0.1.2
→
1.5
View file →
| @@ -1,20 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Controllers\Confirmation; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 | 6 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 12 | 7 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 8 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 13 | 9 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 | 10 | use IvyForms\Controllers\Controller; |
| 15 | 11 | use IvyForms\Factory\Confirmation\ConfirmationFactory; |
| 16 | 12 | use IvyForms\Services\Confirmation\ConfirmationService; |
| 13 | +use IvyForms\Services\Translations\BackendStrings; | |
| 17 | 14 | use WP_REST_Request; |
| 18 | 15 | use WP_REST_Response; |
| 19 | 16 | |
| 20 | 17 | /** |
| @@ -38,8 +35,9 @@ | ||
| 38 | 35 | * |
| 39 | 36 | * @throws QueryExecutionException |
| 40 | 37 | * @throws InvalidArgumentException |
| 41 | 38 | * @throws ForbiddenException |
| 39 | + * @throws ValidationException | |
| 42 | 40 | */ |
| 43 | 41 | public function handle(WP_REST_Request $data): WP_REST_Response |
| 44 | 42 | { |
| 45 | 43 | // Verify the nonce |
| @@ -45,13 +43,21 @@ | ||
| 45 | 43 | // Verify the nonce |
| 46 | 44 | Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 47 | 45 | // Validate and sanitize input data |
| 48 | 46 | if (empty($data->get_params())) { |
| 49 | - throw new InvalidArgumentException('Invalid request data.'); | |
| 47 | + throw new InvalidArgumentException( | |
| 48 | + BackendStrings::getExceptionStrings()['invalid_request_data'] | |
| 49 | + ); | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | $params = Sanitizer::sanitizeConfirmationData($data->get_params()); |
| 53 | 53 | |
| 54 | + if (empty(trim($params['name'] ?? ''))) { | |
| 55 | + throw new InvalidArgumentException( | |
| 56 | + BackendStrings::getSettingsFormBuilderStrings()['confirmation_name_required'] | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 54 | 60 | $confirmation = ConfirmationFactory::create($params); |
| 55 | 61 | |
| 56 | 62 | $confirmationId = $confirmation->getId(); |
| 57 | 63 | |
| @@ -57,13 +63,15 @@ | ||
| 57 | 63 | |
| 58 | 64 | $this->confirmationService->updateConfirmation($confirmationId, $confirmation); |
| 59 | 65 | |
| 60 | 66 | if (!$confirmation->getId()) { |
| 61 | - throw new QueryExecutionException('Failed to update confirmation.'); | |
| 67 | + throw new QueryExecutionException( | |
| 68 | + BackendStrings::getSettingsFormBuilderStrings()['failed_to_update_confirmation'] . '.' | |
| 69 | + ); | |
| 62 | 70 | } |
| 63 | 71 | |
| 64 | 72 | return new WP_REST_Response([ |
| 65 | - 'message' => 'OK', | |
| 73 | + 'message' => BackendStrings::getCommonStrings()['ok'], | |
| 66 | 74 | 'data' => $confirmation->toArray() |
| 67 | 75 | ], 200); |
| 68 | 76 | } |
| 69 | 77 | } |