PluginProbe
The Innovative Form Builder – IvyForms / 0.9.1
The Innovative Form Builder – IvyForms v0.9.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 0.9.1, at backend/src/Controllers/Entry/BulkUpdateEntryController.php

88 lines 2.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 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\InvalidArgumentException;
11 use IvyForms\Common\Sanitizer\Sanitizer;
12 use IvyForms\Controllers\Controller;
13 use IvyForms\Services\Entry\EntryService;
14 use IvyForms\Services\Translations\BackendStrings;
15 use WP_REST_Request;
16 use WP_REST_Response;
17
18 /**
19 * Class BulkUpdateEntryController
20 *
21 * @package IvyForms\Controllers\Entry
22 */
23 class BulkUpdateEntryController extends Controller
24 {
25 private EntryService $entryService;
26
27 public function __construct(EntryService $entryService)
28 {
29 $this->entryService = $entryService;
30 }
31
32 /**
33 * Handle bulk update request for entries
34 *
35 * @param WP_REST_Request $data
36 *
37 * @return WP_REST_Response
38 *
39 * @throws InvalidArgumentException
40 */
41 public function handle(WP_REST_Request $data): WP_REST_Response
42 {
43 // Verify the nonce
44 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
45
46 // Check if the entry data is provided
47 if (empty($data->get_params())) {
48 throw new InvalidArgumentException(
49 BackendStrings::getExceptionStrings()['invalid_request_data']
50 );
51 }
52
53 // Sanitize request data
54 $sanitized = Sanitizer::sanitizeBulkUpdateData([
55 'column' => $data->get_param('column'),
56 'value' => $data->get_param('value'),
57 'ids' => $data->get_param('ids'),
58 ]);
59
60 // Validate IDs
61 if (empty($sanitized['ids'])) {
62 return new WP_REST_Response([
63 'success' => false,
64 'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'],
65 ], 400);
66 }
67
68 // Validate column
69 if (empty($sanitized['column'])) {
70 return new WP_REST_Response([
71 'success' => false,
72 'message' => BackendStrings::getExceptionStrings()['table_or_attribute_not_allowed'],
73 ], 400);
74 }
75
76 $this->entryService->getEntryManager()->bulkUpdateColumn(
77 $sanitized['column'],
78 $sanitized['value'],
79 $sanitized['ids']
80 );
81
82 return new WP_REST_Response([
83 'success' => true,
84 'message' => BackendStrings::getExceptionStrings()['batch_update_successful'],
85 ], 200);
86 }
87 }
88