PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/src/routes/wincher-route.php

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