| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Integration; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 6 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 7 |
use IvyForms\Controllers\Controller; |
| 8 |
use IvyForms\Services\Integrations\IntegrationRegistry; |
| 9 |
use WP_REST_Request; |
| 10 |
use WP_REST_Response; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get Single Integration Controller |
| 14 |
* |
| 15 |
* Returns a specific integration by slug |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class GetIntegrationController extends Controller |
| 20 |
{ |
| 21 |
private IntegrationRegistry $registry; |
| 22 |
|
| 23 |
public function __construct(IntegrationRegistry $registry) |
| 24 |
{ |
| 25 |
$this->registry = $registry; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Handle the request |
| 30 |
* |
| 31 |
* @param WP_REST_Request<array<string, mixed>> $data |
| 32 |
* @return WP_REST_Response |
| 33 |
* @throws ForbiddenException |
| 34 |
*/ |
| 35 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 36 |
{ |
| 37 |
// Verify the nonce |
| 38 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 39 |
|
| 40 |
$slug = Sanitizer::sanitizeText($data->get_param('slug')); |
| 41 |
|
| 42 |
if (empty($slug)) { |
| 43 |
return new WP_REST_Response([ |
| 44 |
'success' => false, |
| 45 |
'message' => __('Integration slug is required', 'ivyforms'), |
| 46 |
], 400); |
| 47 |
} |
| 48 |
|
| 49 |
$integration = $this->registry->get($slug); |
| 50 |
|
| 51 |
if (!$integration) { |
| 52 |
return new WP_REST_Response([ |
| 53 |
'success' => false, |
| 54 |
'message' => sprintf(__('Integration "%s" not found', 'ivyforms'), $slug), |
| 55 |
], 404); |
| 56 |
} |
| 57 |
|
| 58 |
return new WP_REST_Response([ |
| 59 |
'success' => true, |
| 60 |
'data' => $integration, |
| 61 |
], 200); |
| 62 |
} |
| 63 |
} |
| 64 |
|