| 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 GetEntryController |
| 27 |
* |
| 28 |
* @package IvyForms\Controllers\Entry |
| 29 |
*/ |
| 30 |
class GetEntryController extends Controller |
| 31 |
{ |
| 32 |
private EntryService $entryService; |
| 33 |
|
| 34 |
public function __construct(EntryService $entryService) |
| 35 |
{ |
| 36 |
$this->entryService = $entryService; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param WP_REST_Request $data |
| 41 |
* |
| 42 |
* @return WP_REST_Response |
| 43 |
* |
| 44 |
* @throws NotFoundException |
| 45 |
* @throws InvalidArgumentException|ForbiddenException |
| 46 |
*/ |
| 47 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 48 |
{ |
| 49 |
// Verify the nonce |
| 50 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 51 |
|
| 52 |
// Check if the entry ID is provided |
| 53 |
if (empty($data->get_params())) { |
| 54 |
throw new InvalidArgumentException( |
| 55 |
BackendStrings::getExceptionStrings()['entry_id_required'] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$entryId = Sanitizer::sanitizeId($data->get_param('id')); |
| 60 |
|
| 61 |
$entry = $this->entryService->getEntryManager()->getEntry($entryId); |
| 62 |
$entryArray = $entry->toArray(); |
| 63 |
|
| 64 |
$entryFields = $this->entryService->getEntryFieldManager()->getEntryFields([$entryArray]); |
| 65 |
|
| 66 |
return new WP_REST_Response([ |
| 67 |
'entry' => $entryArray, |
| 68 |
'fields' => $entryFields, |
| 69 |
], 200); |
| 70 |
} |
| 71 |
} |
| 72 |
|