← All changes
|
backend/src/Controllers/Template/GetTemplatesController.php
+52
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,52 @@ | ||
| 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 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 11 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 12 | +use IvyForms\Controllers\Controller; | |
| 13 | +use IvyForms\Services\Template\TemplateService; | |
| 14 | +use WP_REST_Request; | |
| 15 | +use WP_REST_Response; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * Class GetTemplatesController | |
| 19 | + * | |
| 20 | + * @package IvyForms\Controllers\Template | |
| 21 | + */ | |
| 22 | +class GetTemplatesController extends Controller | |
| 23 | +{ | |
| 24 | + private TemplateService $templateService; | |
| 25 | + | |
| 26 | + public function __construct(TemplateService $templateService) | |
| 27 | + { | |
| 28 | + $this->templateService = $templateService; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @param WP_REST_Request<array<string, mixed>> $request | |
| 33 | + * @return WP_REST_Response | |
| 34 | + * @throws ForbiddenException | |
| 35 | + */ | |
| 36 | + public function handle(WP_REST_Request $request): WP_REST_Response | |
| 37 | + { | |
| 38 | + // Verify the nonce | |
| 39 | + Sanitizer::verifyNonce($request->get_header('X-WP-Nonce')); | |
| 40 | + | |
| 41 | + $templates = $this->templateService->getAllTemplateMetas(); | |
| 42 | + $categories = $this->templateService->getTemplateCategories(); | |
| 43 | + | |
| 44 | + return new WP_REST_Response([ | |
| 45 | + 'success' => true, | |
| 46 | + 'data' => [ | |
| 47 | + 'templates' => $templates, | |
| 48 | + 'categories' => $categories | |
| 49 | + ] | |
| 50 | + ], 200); | |
| 51 | + } | |
| 52 | +} | |