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