← All changes
|
backend/src/Controllers/Entry/GetEntryCountController.php
+34
-9
0.6.1-backup
→
1.5
View file →
| @@ -6,13 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\Controllers\Entry; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 | 11 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 18 | 13 | use IvyForms\Common\Sanitizer\Sanitizer; |
| @@ -17,8 +12,9 @@ | ||
| 17 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 18 | 13 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 19 | 14 | use IvyForms\Controllers\Controller; |
| 20 | 15 | use IvyForms\Services\Entry\EntryService; |
| 16 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 21 | 17 | use IvyForms\Services\Translations\BackendStrings; |
| 22 | 18 | use WP_REST_Request; |
| 23 | 19 | use WP_REST_Response; |
| 24 | 20 | |
| @@ -30,11 +26,16 @@ | ||
| 30 | 26 | class GetEntryCountController extends Controller |
| 31 | 27 | { |
| 32 | 28 | private EntryService $entryService; |
| 33 | 29 | |
| 34 | - public function __construct(EntryService $entryService) | |
| 35 | - { | |
| 36 | - $this->entryService = $entryService; | |
| 30 | + private RoutePermissionService $routePermissionService; | |
| 31 | + | |
| 32 | + public function __construct( | |
| 33 | + EntryService $entryService, | |
| 34 | + RoutePermissionService $routePermissionService | |
| 35 | + ) { | |
| 36 | + $this->entryService = $entryService; | |
| 37 | + $this->routePermissionService = $routePermissionService; | |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | /** |
| 40 | 41 | * Fetches the count of entries for a given form ID or multiple form IDs. |
| @@ -57,9 +58,33 @@ | ||
| 57 | 58 | BackendStrings::getExceptionStrings()['form_id_required'] |
| 58 | 59 | ); |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | - $formIds = Sanitizer::sanitizeIds($data->get_param('id')) ; | |
| 62 | + $idParam = $data->get_param('id'); | |
| 63 | + if ($idParam === null || $idParam === '') { | |
| 64 | + throw new InvalidArgumentException( | |
| 65 | + BackendStrings::getExceptionStrings()['form_id_required'] | |
| 66 | + ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + $formIds = array_values(array_filter( | |
| 70 | + Sanitizer::sanitizeIds(is_array($idParam) ? $idParam : [$idParam]), | |
| 71 | + static function (int $formId): bool { | |
| 72 | + return $formId > 0; | |
| 73 | + } | |
| 74 | + )); | |
| 75 | + if ($formIds === []) { | |
| 76 | + throw new InvalidArgumentException( | |
| 77 | + BackendStrings::getExceptionStrings()['form_id_required'] | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + foreach ($formIds as $formId) { | |
| 82 | + if (!$this->routePermissionService->canViewEntries($formId)) { | |
| 83 | + throw new ForbiddenException('forbidden'); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 62 | 87 | $counts = $this->entryService->getEntryManager()->getEntryCountByFormIds($formIds); |
| 63 | 88 | return new WP_REST_Response([ |
| 64 | 89 | 'message' => BackendStrings::getCommonStrings()['ok'], |
| 65 | 90 | 'data' => ['counts' => $counts], |