| 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\Notification; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 12 |
use IvyForms\Controllers\Controller; |
| 13 |
use IvyForms\Services\Notification\NotificationService; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
use IvyForms\Services\Translations\BackendStrings; |
| 17 |
|
| 18 |
class SearchNotificationsController extends Controller |
| 19 |
{ |
| 20 |
private NotificationService $notificationService; |
| 21 |
|
| 22 |
public function __construct( |
| 23 |
NotificationService $notificationService |
| 24 |
) { |
| 25 |
$this->notificationService = $notificationService; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @param WP_REST_Request $data |
| 30 |
* |
| 31 |
* @return WP_REST_Response |
| 32 |
* @throws ForbiddenException |
| 33 |
*/ |
| 34 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 35 |
{ |
| 36 |
// Verify the nonce |
| 37 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 38 |
|
| 39 |
// Sanitize the filtering data |
| 40 |
$params = Sanitizer::sanitizeSearchParams($data->get_query_params()); |
| 41 |
|
| 42 |
// Call the searchNotifications method |
| 43 |
$notificationsArr = $this->notificationService->searchNotifications($params); |
| 44 |
|
| 45 |
// Map the data to an array format |
| 46 |
$notifications = array_map(fn($notification) => (array) $notification, $notificationsArr['data']); |
| 47 |
|
| 48 |
// Return the response with data and meta |
| 49 |
return new WP_REST_Response([ |
| 50 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 51 |
'data' => [ |
| 52 |
'data' => $notifications, |
| 53 |
'meta' => $notificationsArr['meta'], |
| 54 |
], |
| 55 |
], 200); |
| 56 |
} |
| 57 |
} |
| 58 |
|