← All changes
|
backend/src/Controllers/Deactivation/SubmitDeactivationFeedbackController.php
+109
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,109 @@ | ||
| 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\Deactivation; | |
| 9 | + | |
| 10 | +use IvyForms\Controllers\Controller; | |
| 11 | +use WP_REST_Request; | |
| 12 | +use WP_REST_Response; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class SubmitDeactivationFeedbackController | |
| 16 | + * | |
| 17 | + * @package IvyForms\Controllers\Deactivation | |
| 18 | + */ | |
| 19 | +class SubmitDeactivationFeedbackController extends Controller | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * External API URL for storing feedback | |
| 23 | + * Change this to your actual server API endpoint | |
| 24 | + */ | |
| 25 | + private static string $feedbackApiUrl = | |
| 26 | + 'https://wpreportbuilder.com/wp-json/wpreportbuilder/v1/ivyforms-submissions'; | |
| 27 | + | |
| 28 | + public function __construct() | |
| 29 | + { | |
| 30 | + // No need for wpdb in this version | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Handle deactivation feedback submission | |
| 35 | + * | |
| 36 | + * @param WP_REST_Request<array<string, mixed>> $request The REST request containing feedback data. | |
| 37 | + * * | |
| 38 | + * * @return WP_REST_Response The REST response with the result of the submission. | |
| 39 | + */ | |
| 40 | + public function handle(WP_REST_Request $request): WP_REST_Response | |
| 41 | + { | |
| 42 | + // Sanitize and validate data | |
| 43 | + $apiVersion = sanitize_text_field($request->get_param('api_version') ?? ''); | |
| 44 | + $feedbackKey = sanitize_text_field($request->get_param('feedback_key') ?? ''); | |
| 45 | + $feedback = sanitize_textarea_field($request->get_param('feedback') ?? ''); | |
| 46 | + | |
| 47 | + // Validate that at least feedbackKey is provided | |
| 48 | + if (empty($feedbackKey)) { | |
| 49 | + return new WP_REST_Response( | |
| 50 | + ['error' => 'Missing required feedback_key parameter'], | |
| 51 | + 400 | |
| 52 | + ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Prepare data to send to external API | |
| 56 | + $feedbackData = [ | |
| 57 | + 'api_version' => $apiVersion, | |
| 58 | + 'feedback_key' => $feedbackKey, | |
| 59 | + 'feedback' => $feedback, | |
| 60 | + ]; | |
| 61 | + | |
| 62 | + // Send feedback to external API server | |
| 63 | + $response = wp_remote_post(self::$feedbackApiUrl, [ | |
| 64 | + 'method' => 'POST', | |
| 65 | + 'timeout' => 10, | |
| 66 | + 'redirection' => 5, | |
| 67 | + 'httpversion' => '1.0', | |
| 68 | + 'blocking' => true, | |
| 69 | + 'headers' => [ | |
| 70 | + 'Content-Type' => 'application/json', | |
| 71 | + ], | |
| 72 | + 'body' => wp_json_encode($feedbackData), | |
| 73 | + ]); | |
| 74 | + | |
| 75 | + // Handle response errors | |
| 76 | + if (is_wp_error($response)) { | |
| 77 | + return new WP_REST_Response( | |
| 78 | + [ | |
| 79 | + 'error' => 'Failed to send feedback to server', | |
| 80 | + 'message' => $response->get_error_message(), | |
| 81 | + ], | |
| 82 | + 500 | |
| 83 | + ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + // Check HTTP response code | |
| 87 | + $httpCode = wp_remote_retrieve_response_code($response); | |
| 88 | + if ($httpCode !== 200 && $httpCode !== 201) { | |
| 89 | + $responseBody = wp_remote_retrieve_body($response); | |
| 90 | + return new WP_REST_Response( | |
| 91 | + [ | |
| 92 | + 'error' => 'Server returned an error', | |
| 93 | + 'http_code' => $httpCode, | |
| 94 | + 'server_response' => $responseBody, | |
| 95 | + ], | |
| 96 | + 500 | |
| 97 | + ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Success response | |
| 101 | + return new WP_REST_Response( | |
| 102 | + [ | |
| 103 | + 'success' => true, | |
| 104 | + 'message' => 'Feedback sent to server successfully', | |
| 105 | + ], | |
| 106 | + 200 | |
| 107 | + ); | |
| 108 | + } | |
| 109 | +} | |