| 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\Entry; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 12 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 13 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 |
use IvyForms\Controllers\Controller; |
| 15 |
use IvyForms\Services\Entry\EntryService; |
| 16 |
use IvyForms\Services\Permissions\RoutePermissionService; |
| 17 |
use IvyForms\Services\Translations\BackendStrings; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_REST_Response; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class GetEntryCountController |
| 23 |
* |
| 24 |
* @package IvyForms\Controllers\Entry |
| 25 |
*/ |
| 26 |
class GetEntryCountController extends Controller |
| 27 |
{ |
| 28 |
private EntryService $entryService; |
| 29 |
|
| 30 |
private RoutePermissionService $routePermissionService; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
EntryService $entryService, |
| 34 |
RoutePermissionService $routePermissionService |
| 35 |
) { |
| 36 |
$this->entryService = $entryService; |
| 37 |
$this->routePermissionService = $routePermissionService; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Fetches the count of entries for a given form ID or multiple form IDs. |
| 42 |
* |
| 43 |
* @param WP_REST_Request $data |
| 44 |
* |
| 45 |
* @return WP_REST_Response The response containing the count(s) of entries. |
| 46 |
* |
| 47 |
* @throws InvalidArgumentException|ForbiddenException |
| 48 |
* @throws ForbiddenException |
| 49 |
*/ |
| 50 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 51 |
{ |
| 52 |
// Verify the nonce |
| 53 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 54 |
|
| 55 |
$params = $data->get_params(); |
| 56 |
if (empty($params)) { |
| 57 |
throw new InvalidArgumentException( |
| 58 |
BackendStrings::getExceptionStrings()['form_id_required'] |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 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 |
|
| 87 |
$counts = $this->entryService->getEntryManager()->getEntryCountByFormIds($formIds); |
| 88 |
return new WP_REST_Response([ |
| 89 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 90 |
'data' => ['counts' => $counts], |
| 91 |
], 200); |
| 92 |
} |
| 93 |
} |
| 94 |
|