| 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\QueryExecutionException; |
| 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 |
class UpdateEntryStatusController extends Controller |
| 26 |
{ |
| 27 |
private EntryService $entryService; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
EntryService $entryService |
| 31 |
) { |
| 32 |
$this->entryService = $entryService; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param WP_REST_Request $data |
| 37 |
* |
| 38 |
* @return WP_REST_Response |
| 39 |
* |
| 40 |
* @throws InvalidArgumentException |
| 41 |
* @throws ForbiddenException |
| 42 |
* @throws QueryExecutionException |
| 43 |
*/ |
| 44 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 45 |
{ |
| 46 |
// Verify the nonce |
| 47 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 48 |
|
| 49 |
// Check if the entry data is provided |
| 50 |
if (empty($data->get_params())) { |
| 51 |
throw new InvalidArgumentException( |
| 52 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
$entryId = Sanitizer::sanitizeId($data->get_params()['id']); |
| 57 |
|
| 58 |
if ($entryId <= 0) { |
| 59 |
throw new InvalidArgumentException( |
| 60 |
BackendStrings::getExceptionStrings()['invalid_entry_id'] |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
$status = sanitize_text_field($data->get_params()['value'] ?? ''); |
| 65 |
|
| 66 |
// Update the status in the database |
| 67 |
$this->entryService->getEntryManager()->updateEntryStatus($entryId, $status); |
| 68 |
|
| 69 |
return new WP_REST_Response([ |
| 70 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 71 |
'data' => [ |
| 72 |
'id' => $entryId, |
| 73 |
'status' => $status, |
| 74 |
] |
| 75 |
], 200); |
| 76 |
} |
| 77 |
} |
| 78 |
|