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