PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Features / Notifications / NotificationsEndpoints.php

NotificationsEndpoints.php in Metricool – Social media and site statistics 2.0.2, at app/Features/Notifications/NotificationsEndpoints.php

62 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Features\Notifications;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 use Metricool\Traits\HasRestAccess;
12 use Metricool\Traits\HasAllowlistControl;
13
14 class NotificationsEndpoints
15 {
16 use HasRestAccess;
17 use HasAllowlistControl;
18
19 private NotificationsService $service;
20
21 public function __construct(NotificationsService $service)
22 {
23 $this->service = $service;
24 }
25
26 public function register(): void
27 {
28 add_filter('metricool_rest_routes', [$this, 'addNotificationRoutes']);
29 }
30
31 /**
32 * Add the Notification routes to the REST API.
33 */
34 public function addNotificationRoutes(array $routes): array
35 {
36 if ($this->adminAccessAllowed() === false) {
37 return $routes;
38 }
39
40 $routes['get_notices'] = [
41 'methods' => \WP_REST_Server::READABLE,
42 'callback' => [$this, 'getNoticesCallback'],
43 ];
44
45 return $routes;
46 }
47
48 /**
49 * Return current Notices as a WP_REST_Response.
50 */
51 public function getNoticesCallback(\WP_REST_Request $request): \WP_REST_Response
52 {
53 $allNoticesAsArray = array_map(function ($notice) {
54 return $notice->toArray();
55 }, $this->service->getAllNotices());
56
57 return $this->sendHttpResponse(
58 array_values($allNoticesAsArray) // Keys should be removed
59 );
60 }
61 }
62