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 / Entry / BulkUpdateEntryController.php

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

110 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Controllers\Entry;
4
5 use IvyForms\Common\Exceptions\ForbiddenException;
6 use IvyForms\Common\Exceptions\InvalidArgumentException;
7 use IvyForms\Common\Sanitizer\Sanitizer;
8 use IvyForms\Controllers\Controller;
9 use IvyForms\Services\Entry\EntryService;
10 use IvyForms\Services\Permissions\PermissionResourceResolver;
11 use IvyForms\Services\Permissions\RoutePermissionService;
12 use IvyForms\Services\Translations\BackendStrings;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 /**
17 * Class BulkUpdateEntryController
18 *
19 * @package IvyForms\Controllers\Entry
20 */
21 class BulkUpdateEntryController extends Controller
22 {
23 private EntryService $entryService;
24
25 private RoutePermissionService $routePermissionService;
26
27 private PermissionResourceResolver $permissionResourceResolver;
28
29 public function __construct(
30 EntryService $entryService,
31 RoutePermissionService $routePermissionService,
32 PermissionResourceResolver $permissionResourceResolver
33 ) {
34 $this->entryService = $entryService;
35 $this->routePermissionService = $routePermissionService;
36 $this->permissionResourceResolver = $permissionResourceResolver;
37 }
38
39 /**
40 * Handle bulk update request for entries
41 *
42 * @param WP_REST_Request $data
43 *
44 * @return WP_REST_Response
45 *
46 * @throws InvalidArgumentException
47 */
48 public function handle(WP_REST_Request $data): WP_REST_Response
49 {
50 // Verify the nonce
51 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
52
53 // Check if the entry data is provided
54 if (empty($data->get_params())) {
55 throw new InvalidArgumentException(
56 BackendStrings::getExceptionStrings()['invalid_request_data']
57 );
58 }
59
60 // Sanitize request data
61 $sanitized = Sanitizer::sanitizeBulkUpdateData([
62 'column' => $data->get_param('column'),
63 'value' => $data->get_param('value'),
64 'ids' => $data->get_param('ids'),
65 ]);
66
67 // Validate IDs
68 if (empty($sanitized['ids'])) {
69 return new WP_REST_Response([
70 'success' => false,
71 'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'],
72 ], 400);
73 }
74
75 // Validate column
76 if (empty($sanitized['column'])) {
77 return new WP_REST_Response([
78 'success' => false,
79 'message' => BackendStrings::getExceptionStrings()['table_or_attribute_not_allowed'],
80 ], 400);
81 }
82
83 $formIdsByEntryId = $this->permissionResourceResolver->formIdsFromEntryIds($sanitized['ids']);
84
85 foreach ($sanitized['ids'] as $entryId) {
86 $id = (int) $entryId;
87 if (!isset($formIdsByEntryId[$id])) {
88 throw new ForbiddenException('forbidden');
89 }
90 }
91
92 foreach (array_unique(array_values($formIdsByEntryId)) as $formId) {
93 if (!$this->routePermissionService->canEditEntries($formId)) {
94 throw new ForbiddenException('forbidden');
95 }
96 }
97
98 $this->entryService->getEntryManager()->bulkUpdateColumn(
99 $sanitized['column'],
100 $sanitized['value'],
101 $sanitized['ids']
102 );
103
104 return new WP_REST_Response([
105 'success' => true,
106 'message' => BackendStrings::getExceptionStrings()['batch_update_successful'],
107 ], 200);
108 }
109 }
110