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