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