PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 1.2 1.3
ivyforms / backend / src / Controllers / Template / GetTemplateController.php

GetTemplateController.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/Controllers/Template/GetTemplateController.php

73 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Exceptions\InvalidArgumentException;
17 use IvyForms\Common\Sanitizer\Sanitizer;
18 use IvyForms\Controllers\Controller;
19 use IvyForms\Services\Template\TemplateService;
20 use WP_REST_Request;
21 use WP_REST_Response;
22 use IvyForms\Services\Translations\BackendStrings;
23
24 /**
25 * Class GetTemplateController
26 *
27 * @package IvyForms\Controllers\Template
28 */
29 class GetTemplateController extends Controller
30 {
31 private TemplateService $templateService;
32
33 public function __construct(TemplateService $templateService)
34 {
35 $this->templateService = $templateService;
36 }
37
38 /**
39 * @param WP_REST_Request<array<string, mixed>> $data
40 * @return WP_REST_Response
41 * @throws ForbiddenException
42 * @throws InvalidArgumentException
43 */
44 public function handle(WP_REST_Request $data): WP_REST_Response
45 {
46 // Verify the nonce
47 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
48
49 // TODO Add validation for template_id with Sanitizer
50 //$templateId = Sanitizer::sanitizeText($data->get_param('id'));
51 $templateId = sanitize_text_field($data->get_param('id') ?? '');
52
53 if (empty($templateId)) {
54 throw new InvalidArgumentException(
55 BackendStrings::getTemplateStrings()['required_template_id']
56 );
57 }
58
59 $template = $this->templateService->getTemplateById($templateId);
60
61 if (!$template) {
62 throw new InvalidArgumentException(
63 BackendStrings::getTemplateStrings()['template_not_found']
64 );
65 }
66
67 return new WP_REST_Response([
68 'success' => true,
69 'data' => $template
70 ], 200);
71 }
72 }
73