PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.4
GiveWP – Donation Plugin and Fundraising Platform v4.15.4
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Promotions / WelcomeBanner / Endpoints / DismissWelcomeBannerRoute.php
DismissWelcomeBannerRoute.php
90 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 namespace Give\Promotions\WelcomeBanner\Endpoints;
4
5 use Give\API\RestRoute;
6 use WP_Error;
7 use WP_REST_Request;
8 use WP_REST_Response;
9
10 class DismissWelcomeBannerRoute implements RestRoute
11 {
12 /**
13 * @var string
14 * @since 3.0.0
15 */
16 protected $endpoint = 'welcome-banner/dismiss';
17
18 /**
19 * @inheritDoc
20 * @since 3.0.0
21 */
22 public function registerRoute(): void
23 {
24 register_rest_route(
25 'give-api/v2',
26 $this->endpoint,
27 [
28 [
29 'methods' => 'POST',
30 'callback' => [$this, 'handleRequest'],
31 'permission_callback' => [$this, 'permissionsCheck'],
32 'args' => [
33 'action' => [
34 'type' => 'string',
35 'required' => true,
36 ],
37 ],
38 ],
39 ]
40 );
41 }
42
43 /**
44 * @since 3.0.0
45 */
46 public function permissionsCheck()
47 {
48 if (!current_user_can('manage_options')) {
49 return new WP_Error(
50 'rest_forbidden',
51 __(
52 'You don\'t have permission to dismiss options. Only users with the "manage_options" capability can perform this action.',
53 'give'
54 ),
55 ['status' => $this->authorizationStatusCode()]
56 );
57 }
58
59 return true;
60 }
61
62 /**
63 * Sets up the proper HTTP status code for authorization.
64 * @return int
65 * @since 3.0.0
66 */
67 public function authorizationStatusCode(): int
68 {
69 if (is_user_logged_in()) {
70 return 403;
71 }
72
73 return 401;
74 }
75
76 /**
77 * @param WP_REST_Request $request
78 *
79 * @return WP_REST_Response
80 * @since 3.0.0
81 */
82 public function handleRequest(WP_REST_Request $request): WP_REST_Response
83 {
84 update_option($request->get_param('action'), time());
85
86 return new WP_REST_Response(['banner_dismissed' => $request->get_param('action')]);
87 }
88
89 }
90