PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 / general / user-interface / opt-in-route.php

opt-in-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/general/user-interface/opt-in-route.php

151 lines 3.7 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\General\User_Interface;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use Yoast\WP\SEO\Conditionals\User_Can_Manage_Wpseo_Options_Conditional;
8 use Yoast\WP\SEO\Helpers\Capability_Helper;
9 use Yoast\WP\SEO\Helpers\User_Helper;
10 use Yoast\WP\SEO\Main;
11 use Yoast\WP\SEO\Routes\Route_Interface;
12
13 /**
14 * Registers a route to get dismiss opt in notification.
15 */
16 class Opt_In_Route implements Route_Interface {
17
18 /**
19 * The namespace for this route.
20 *
21 * @var string
22 */
23 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
24
25 /**
26 * The prefix for this route.
27 *
28 * @var string
29 */
30 public const SEEN_ROUTE = '/seen-opt-in-notification';
31
32 /**
33 * Holds the user helper.
34 *
35 * @var User_Helper
36 */
37 private $user_helper;
38
39 /**
40 * Holds the capability helper instance.
41 *
42 * @var Capability_Helper
43 */
44 private $capability_helper;
45
46 /**
47 * Returns the conditionals based on which this integration should be active.
48 *
49 * @return array<User_Can_Manage_Wpseo_Options_Conditional> The array of conditionals.
50 */
51 public static function get_conditionals() {
52 return [
53 User_Can_Manage_Wpseo_Options_Conditional::class,
54 ];
55 }
56
57 /**
58 * Constructs Opt_In_Route.
59 *
60 * @param User_Helper $user_helper The user helper.
61 * @param Capability_Helper $capability_helper The capability helper.
62 */
63 public function __construct( User_Helper $user_helper, Capability_Helper $capability_helper ) {
64 $this->user_helper = $user_helper;
65 $this->capability_helper = $capability_helper;
66 }
67
68 /**
69 * Registers routes with WordPress.
70 *
71 * @return void
72 */
73 public function register_routes() {
74 $opt_in_seen_route_args = [
75 'methods' => 'POST',
76 'callback' => [ $this, 'set_opt_in_seen' ],
77 'permission_callback' => [ $this, 'can_see_opt_in' ],
78 'args' => [
79 'key' => [
80 'required' => true,
81 'type' => 'string',
82 'sanitize_callback' => 'sanitize_text_field',
83 'validate_callback' => [ $this, 'validate_key' ],
84 ],
85 ],
86 ];
87
88 \register_rest_route( Main::API_V1_NAMESPACE, self::SEEN_ROUTE, $opt_in_seen_route_args );
89 }
90
91 /**
92 * Sets the opt-in notification as seen.
93 *
94 * @param WP_REST_Request $request The request. This request should have a key param set.
95 *
96 * @return WP_REST_Response The response.
97 */
98 public function set_opt_in_seen( $request ) {
99 $key = $request->get_param( 'key' );
100 $current_user_id = $this->user_helper->get_current_user_id();
101 $meta_key = '_yoast_wpseo_' . $key . '_opt_in_notification_seen';
102
103 // If already seen, return success immediately (update_user_meta returns false when the value is unchanged).
104 if ( $this->user_helper->get_meta( $current_user_id, $meta_key, true ) === '1' ) {
105 return new WP_REST_Response(
106 (object) [
107 'success' => true,
108 'status' => 200,
109 ],
110 200,
111 );
112 }
113
114 $result = $this->user_helper->update_meta( $current_user_id, $meta_key, true );
115 $success = $result !== false;
116 $status = ( $success ) ? 200 : 400;
117
118 return new WP_REST_Response(
119 (object) [
120 'success' => $success,
121 'status' => $status,
122 ],
123 $status,
124 );
125 }
126
127 /**
128 * Whether or not the current user is allowed to see the opt-in notification.
129 *
130 * @return bool Whether or not the current user is allowed to see the opt-in notification.
131 */
132 public function can_see_opt_in() {
133 return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
134 }
135
136 /**
137 * Validates the key parameter.
138 *
139 * @param string $key The key to validate.
140 *
141 * @return bool Whether the key is valid.
142 */
143 public function validate_key( $key ) {
144 $allowed_keys = [
145 'bulk_editor_tour',
146 ];
147
148 return \in_array( $key, $allowed_keys, true );
149 }
150 }
151