PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 / wincher-route.php

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

278 lines 7.2 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\Wincher\Wincher_Account_Action;
8 use Yoast\WP\SEO\Actions\Wincher\Wincher_Keyphrases_Action;
9 use Yoast\WP\SEO\Actions\Wincher\Wincher_Login_Action;
10 use Yoast\WP\SEO\Conditionals\Wincher_Enabled_Conditional;
11 use Yoast\WP\SEO\Main;
12
13 /**
14 * Wincher_Route class.
15 */
16 class Wincher_Route implements Route_Interface {
17
18 /**
19 * The Wincher route prefix.
20 *
21 * @var string
22 */
23 const ROUTE_PREFIX = 'wincher';
24
25 /**
26 * The authorize route constant.
27 *
28 * @var string
29 */
30 const AUTHORIZATION_URL_ROUTE = self::ROUTE_PREFIX . '/authorization-url';
31
32 /**
33 * The authenticate route constant.
34 *
35 * @var string
36 */
37 const AUTHENTICATION_ROUTE = self::ROUTE_PREFIX . '/authenticate';
38
39 /**
40 * The track bulk keyphrases route constant.
41 *
42 * @var string
43 */
44 const KEYPHRASES_TRACK_ROUTE = self::ROUTE_PREFIX . '/keyphrases/track';
45
46 /**
47 * The keyphrases route constant.
48 *
49 * @var string
50 */
51 const TRACKED_KEYPHRASES_ROUTE = self::ROUTE_PREFIX . '/keyphrases';
52
53 /**
54 * The untrack keyphrase route constant.
55 *
56 * @var string
57 */
58 const UNTRACK_KEYPHRASE_ROUTE = self::ROUTE_PREFIX . '/keyphrases/untrack';
59
60 /**
61 * The login action.
62 *
63 * @var Wincher_Login_Action
64 */
65 private $login_action;
66
67 /**
68 * The account action.
69 *
70 * @var Wincher_Account_Action
71 */
72 private $account_action;
73
74 /**
75 * The keyphrases action.
76 *
77 * @var Wincher_Keyphrases_Action
78 */
79 private $keyphrases_action;
80
81 /**
82 * Returns the conditionals based in which this loadable should be active.
83 *
84 * @return array
85 */
86 public static function get_conditionals() {
87 return [ Wincher_Enabled_Conditional::class ];
88 }
89
90 /**
91 * Wincher_Route constructor.
92 *
93 * @param Wincher_Login_Action $login_action The login action.
94 * @param Wincher_Account_Action $account_action The account action.
95 * @param Wincher_Keyphrases_Action $keyphrases_action The keyphrases action.
96 */
97 public function __construct(
98 Wincher_Login_Action $login_action,
99 Wincher_Account_Action $account_action,
100 Wincher_Keyphrases_Action $keyphrases_action
101 ) {
102 $this->login_action = $login_action;
103 $this->account_action = $account_action;
104 $this->keyphrases_action = $keyphrases_action;
105 }
106
107 /**
108 * Registers routes with WordPress.
109 *
110 * @return void
111 */
112 public function register_routes() {
113 $authorize_route_args = [
114 'methods' => 'GET',
115 'callback' => [ $this, 'get_authorization_url' ],
116 'permission_callback' => [ $this, 'can_use_wincher' ],
117 ];
118 \register_rest_route( Main::API_V1_NAMESPACE, self::AUTHORIZATION_URL_ROUTE, $authorize_route_args );
119
120 $authentication_route_args = [
121 'methods' => 'POST',
122 'callback' => [ $this, 'authenticate' ],
123 'permission_callback' => [ $this, 'can_use_wincher' ],
124 'args' => [
125 'code' => [
126 'validate_callback' => [ $this, 'has_valid_code' ],
127 'required' => true,
128 ],
129 'websiteId' => [
130 'validate_callback' => [ $this, 'has_valid_website_id' ],
131 'required' => true,
132 ],
133 ],
134 ];
135
136 \register_rest_route( Main::API_V1_NAMESPACE, self::AUTHENTICATION_ROUTE, $authentication_route_args );
137
138 $track_keyphrases_route_args = [
139 'methods' => 'POST',
140 'callback' => [ $this, 'track_keyphrases' ],
141 'permission_callback' => [ $this, 'can_use_wincher' ],
142 'args' => [
143 'keyphrases' => [
144 'required' => true,
145 ],
146 ],
147 ];
148
149 \register_rest_route( Main::API_V1_NAMESPACE, self::KEYPHRASES_TRACK_ROUTE, $track_keyphrases_route_args );
150
151 $get_keyphrases_route_args = [
152 'methods' => 'POST',
153 'callback' => [ $this, 'get_tracked_keyphrases' ],
154 'permission_callback' => [ $this, 'can_use_wincher' ],
155 'args' => [
156 'keyphrases' => [
157 'required' => false,
158 ],
159 'permalink' => [
160 'required' => false,
161 ],
162 ],
163 ];
164
165 \register_rest_route( Main::API_V1_NAMESPACE, self::TRACKED_KEYPHRASES_ROUTE, $get_keyphrases_route_args );
166
167 $delete_keyphrase_route_args = [
168 'methods' => 'DELETE',
169 'callback' => [ $this, 'untrack_keyphrase' ],
170 'permission_callback' => [ $this, 'can_use_wincher' ],
171 ];
172
173 \register_rest_route( Main::API_V1_NAMESPACE, self::UNTRACK_KEYPHRASE_ROUTE, $delete_keyphrase_route_args );
174 }
175
176 /**
177 * Returns the authorization URL.
178 *
179 * @return WP_REST_Response The response.
180 */
181 public function get_authorization_url() {
182 $data = $this->login_action->get_authorization_url();
183 return new WP_REST_Response( $data, $data->status );
184 }
185
186 /**
187 * Authenticates with Wincher.
188 *
189 * @param WP_REST_Request $request The request. This request should have a code param set.
190 *
191 * @return WP_REST_Response The response.
192 */
193 public function authenticate( WP_REST_Request $request ) {
194 $data = $this
195 ->login_action
196 ->authenticate( $request['code'], (string) $request['websiteId'] );
197
198 return new WP_REST_Response( $data, $data->status );
199 }
200
201 /**
202 * Posts keyphrases to track.
203 *
204 * @param WP_REST_Request $request The request. This request should have a code param set.
205 *
206 * @return WP_REST_Response The response.
207 */
208 public function track_keyphrases( WP_REST_Request $request ) {
209 $limits = $this->account_action->check_limit();
210
211 if ( $limits->status !== 200 ) {
212 return new WP_REST_Response( $limits, $limits->status );
213 }
214
215 $data = $this->keyphrases_action->track_keyphrases( $request['keyphrases'], $limits );
216
217 return new WP_REST_Response( $data, $data->status );
218 }
219
220 /**
221 * Gets the tracked keyphrases via POST.
222 * This is done via POST, so we don't potentially run into URL limit issues when a lot of long keyphrases are tracked.
223 *
224 * @param WP_REST_Request $request The request. This request should have a code param set.
225 *
226 * @return WP_REST_Response The response.
227 */
228 public function get_tracked_keyphrases( WP_REST_Request $request ) {
229 $data = $this->keyphrases_action->get_tracked_keyphrases( $request['keyphrases'], $request['permalink'] );
230
231 return new WP_REST_Response( $data, $data->status );
232 }
233
234 /**
235 * Untracks the tracked keyphrase.
236 *
237 * @param WP_REST_Request $request The request. This request should have a code param set.
238 *
239 * @return WP_REST_Response The response.
240 */
241 public function untrack_keyphrase( WP_REST_Request $request ) {
242 $data = $this->keyphrases_action->untrack_keyphrase( $request['keyphraseID'] );
243
244 return new WP_REST_Response( $data, $data->status );
245 }
246
247 /**
248 * Checks if a valid code was returned.
249 *
250 * @param string $code The code to check.
251 *
252 * @return bool Whether the code is valid.
253 */
254 public function has_valid_code( $code ) {
255 return $code !== '';
256 }
257
258 /**
259 * Checks if a valid website_id was returned.
260 *
261 * @param int $website_id The website_id to check.
262 *
263 * @return bool Whether the website_id is valid.
264 */
265 public function has_valid_website_id( $website_id ) {
266 return ! empty( $website_id ) && \is_int( $website_id );
267 }
268
269 /**
270 * Whether the current user is allowed to publish post/pages and thus use the Wincher integration.
271 *
272 * @return bool Whether the current user is allowed to use Wincher.
273 */
274 public function can_use_wincher() {
275 return \current_user_can( 'publish_posts' ) || \current_user_can( 'publish_pages' );
276 }
277 }
278