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