| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Notification; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 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\Notification\NotificationService; |
| 16 |
use IvyForms\Services\Translations\BackendStrings; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class GetNotificationsController |
| 22 |
* |
| 23 |
* @package IvyForms\Controllers\Notification |
| 24 |
*/ |
| 25 |
class GetNotificationsController extends Controller |
| 26 |
{ |
| 27 |
private NotificationService $notificationService; |
| 28 |
|
| 29 |
public function __construct(NotificationService $notificationService) |
| 30 |
{ |
| 31 |
$this->notificationService = $notificationService; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @param WP_REST_Request $data |
| 36 |
* |
| 37 |
* @return WP_REST_Response |
| 38 |
* @throws ForbiddenException |
| 39 |
* @throws InvalidArgumentException|NotFoundException |
| 40 |
*/ |
| 41 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 42 |
{ |
| 43 |
// Verify the nonce |
| 44 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 45 |
$formId = Sanitizer::sanitizeId($data->get_param('id')); |
| 46 |
|
| 47 |
|
| 48 |
// TODO: Update this logic to only require the form ID after templates. |
| 49 |
$notificationsArr = $formId |
| 50 |
? $this->notificationService->getNotificationsByFormId((int) $formId) |
| 51 |
: $this->notificationService->getAllNotifications(); |
| 52 |
|
| 53 |
$notifications = []; |
| 54 |
|
| 55 |
foreach ($notificationsArr as $notification) { |
| 56 |
$notifications[] = $notification->toArray(); |
| 57 |
} |
| 58 |
|
| 59 |
return new WP_REST_Response([ |
| 60 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 61 |
'data' => $notifications, |
| 62 |
], 200); |
| 63 |
} |
| 64 |
} |
| 65 |
|