PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / routes / alert-dismissal-route.php

alert-dismissal-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.8, at src/routes/alert-dismissal-route.php

105 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Routes;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use Yoast\WP\SEO\Actions\Alert_Dismissal_Action;
8 use Yoast\WP\SEO\Conditionals\No_Conditionals;
9 use Yoast\WP\SEO\Main;
10
11 /**
12 * Class Alert_Dismissal_Route.
13 */
14 class Alert_Dismissal_Route implements Route_Interface {
15
16 use No_Conditionals;
17
18 /**
19 * Represents the alerts route prefix.
20 *
21 * @var string
22 */
23 const ROUTE_PREFIX = 'alerts';
24
25 /**
26 * Represents the dismiss route.
27 *
28 * @var string
29 */
30 const DISMISS_ROUTE = self::ROUTE_PREFIX . '/dismiss';
31
32 /**
33 * Represents the full dismiss route.
34 *
35 * @var string
36 */
37 const FULL_DISMISS_ROUTE = Main::API_V1_NAMESPACE . '/' . self::DISMISS_ROUTE;
38
39 /**
40 * Represents the alert dismissal action.
41 *
42 * @var Alert_Dismissal_Action
43 */
44 protected $alert_dismissal_action;
45
46 /**
47 * Constructs Alert_Dismissal_Route.
48 *
49 * @param Alert_Dismissal_Action $alert_dismissal_action The alert dismissal action.
50 */
51 public function __construct( Alert_Dismissal_Action $alert_dismissal_action ) {
52 $this->alert_dismissal_action = $alert_dismissal_action;
53 }
54
55 /**
56 * Registers routes with WordPress.
57 *
58 * @return void
59 */
60 public function register_routes() {
61 $dismiss_route_args = [
62 'methods' => 'POST',
63 'callback' => [ $this, 'dismiss' ],
64 'permission_callback' => [ $this, 'can_dismiss' ],
65 'args' => [
66 'key' => [
67 'validate_callback' => [ $this->alert_dismissal_action, 'is_allowed' ],
68 'required' => true,
69 ],
70 ],
71 ];
72
73 \register_rest_route( Main::API_V1_NAMESPACE, self::DISMISS_ROUTE, $dismiss_route_args );
74 }
75
76 /**
77 * Dismisses an alert.
78 *
79 * @param WP_REST_Request $request The request. This request should have a key param set.
80 *
81 * @return WP_REST_Response The response.
82 */
83 public function dismiss( WP_REST_Request $request ) {
84 $success = $this->alert_dismissal_action->dismiss( $request['key'] );
85 $status = $success === ( true ) ? 200 : 400;
86
87 return new WP_REST_Response(
88 (object) [
89 'success' => $success,
90 'status' => $status,
91 ],
92 $status
93 );
94 }
95
96 /**
97 * Whether or not the current user is allowed to dismiss alerts.
98 *
99 * @return bool Whether or not the current user is allowed to dismiss alerts.
100 */
101 public function can_dismiss() {
102 return \current_user_can( 'edit_posts' );
103 }
104 }
105