| 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\Template; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 17 |
use IvyForms\Controllers\Controller; |
| 18 |
use IvyForms\Services\Template\TemplateService; |
| 19 |
use WP_REST_Request; |
| 20 |
use WP_REST_Response; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class GetTemplatesController |
| 24 |
* |
| 25 |
* @package IvyForms\Controllers\Template |
| 26 |
*/ |
| 27 |
class GetTemplatesController extends Controller |
| 28 |
{ |
| 29 |
private TemplateService $templateService; |
| 30 |
|
| 31 |
public function __construct(TemplateService $templateService) |
| 32 |
{ |
| 33 |
$this->templateService = $templateService; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param WP_REST_Request<array<string, mixed>> $request |
| 38 |
* @return WP_REST_Response |
| 39 |
* @throws ForbiddenException |
| 40 |
*/ |
| 41 |
public function handle(WP_REST_Request $request): WP_REST_Response |
| 42 |
{ |
| 43 |
// Verify the nonce |
| 44 |
Sanitizer::verifyNonce($request->get_header('X-WP-Nonce')); |
| 45 |
|
| 46 |
$templates = $this->templateService->getAllTemplates(); |
| 47 |
$categories = $this->templateService->getTemplateCategories(); |
| 48 |
|
| 49 |
return new WP_REST_Response([ |
| 50 |
'success' => true, |
| 51 |
'data' => [ |
| 52 |
'templates' => $templates, |
| 53 |
'categories' => $categories |
| 54 |
] |
| 55 |
], 200); |
| 56 |
} |
| 57 |
} |
| 58 |
|