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

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

80 lines 2.1 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\AdminNotices;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 use Metricool\Support\Helpers\Storage;
12 use Metricool\Traits\HasRestAccess;
13 use Metricool\Traits\HasAllowlistControl;
14
15 class AdminNoticesEndpoints
16 {
17 use HasRestAccess;
18 use HasAllowlistControl;
19
20 private AdminNoticesRepository $repository;
21
22 public function __construct(AdminNoticesRepository $repository)
23 {
24 $this->repository = $repository;
25 }
26
27 public function register(): void
28 {
29 add_filter('metricool_rest_routes', [$this, 'addRestRoutes']);
30 }
31
32 public function addRestRoutes(array $routes): array
33 {
34 $routes['/admin-notices/(?P<notice_id>[\w-]+)'] = [
35 'methods' => 'POST',
36 'callback' => [$this, 'handleNoticeAction'],
37 'middleware' => ['user_can:administrator'],
38 'args' => [
39 'notice_id' => [
40 'required' => true,
41 'type' => 'string',
42 'sanitize_callback' => 'sanitize_text_field',
43 ],
44 'action' => [
45 'required' => true,
46 'type' => 'string',
47 'enum' => [AbstractAdminNotice::DISMISS_NOTICE_ACTION, AbstractAdminNotice::SNOOZE_NOTICE_ACTION],
48 ],
49 ],
50 ];
51
52 return $routes;
53 }
54
55 public function handleNoticeAction(\WP_REST_Request $wpRestRequest): \WP_REST_Response
56 {
57 $request = new Storage(
58 $wpRestRequest->get_params()
59 );
60
61 $noticeId = $request->getString('notice_id');
62 $action = $request->getString('action');
63
64 $notice = $this->repository->find($noticeId);
65 if ($notice === null) {
66 return $this->sendHttpErrorResponse('Notice not found', null, 404);
67 }
68
69 if ($action === AbstractAdminNotice::DISMISS_NOTICE_ACTION) {
70 $notice->dismiss();
71 }
72
73 if ($action === AbstractAdminNotice::SNOOZE_NOTICE_ACTION) {
74 $notice->snooze();
75 }
76
77 return $this->sendHttpResponse(['success' => true]);
78 }
79 }
80