PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 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 All 27 releases
ivyforms / backend / src / Controllers / Form / SearchFormsController.php

SearchFormsController.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Controllers/Form/SearchFormsController.php

87 lines 2.9 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\Form;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Sanitizer\Sanitizer;
13 use IvyForms\Controllers\Controller;
14 use IvyForms\Services\Entry\EntryService;
15 use IvyForms\Services\Form\FormService;
16 use IvyForms\Services\Permissions\AdminFormListPermissionService;
17 use IvyForms\Services\Permissions\IvyFormsAccess;
18 use IvyForms\Services\Translations\BackendStrings;
19 use WP_REST_Request;
20 use WP_REST_Response;
21
22 class SearchFormsController extends Controller
23 {
24 private FormService $formService;
25
26 private EntryService $entryService;
27
28 private AdminFormListPermissionService $adminFormListPermissionService;
29
30 public function __construct(
31 FormService $formService,
32 EntryService $entryService,
33 AdminFormListPermissionService $adminFormListPermissionService
34 ) {
35 $this->formService = $formService;
36 $this->entryService = $entryService;
37 $this->adminFormListPermissionService = $adminFormListPermissionService;
38 }
39
40 /**
41 * @param WP_REST_Request $data
42 *
43 * @return WP_REST_Response
44 * @throws ForbiddenException
45 * @throws InvalidArgumentException
46 */
47 public function handle(WP_REST_Request $data): WP_REST_Response
48 {
49 // Verify the nonce
50 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
51
52 // Sanitize the filtering data
53 $params = Sanitizer::sanitizeSearchParams($data->get_query_params());
54
55 if (!IvyFormsAccess::hasImplicitElevatedAccess()) {
56 $allIdsUnique = $this->formService->getAllFormIds();
57 $visibleIds = $this->adminFormListPermissionService->filterFormIdsVisibleInAdminLists($allIdsUnique);
58 if ($visibleIds !== $allIdsUnique) {
59 $params['visibleFormIds'] = $visibleIds;
60 }
61 }
62
63 // Call the searchForms method
64 $formsArr = $this->formService->searchForms($params);
65
66 // Map the data to an array format
67 $forms = array_map(fn($form) => (array) $form, $formsArr['data']);
68
69 $filterCount = $this->formService->getFilterCount($params['shouldGetCount']);
70
71 // Collect form IDs for entry count
72 $formIds = array_column($forms, 'id');
73 $entryCounts = $this->entryService->getEntryManager()->getEntryCountByFormIds($formIds);
74
75 // Return the response with data, meta, filterCount, and entryCounts
76 return new WP_REST_Response([
77 'message' => BackendStrings::getCommonStrings()['ok'],
78 'data' => [
79 'data' => $forms,
80 'meta' => $formsArr['meta'],
81 'filterCount' => $filterCount,
82 'entryCounts' => $entryCounts,
83 ],
84 ], 200);
85 }
86 }
87