class-myyoast-connect.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Endpoint
  6. */
  7. /**
  8. * Represents an implementation of the WPSEO_Endpoint interface to register one or multiple endpoints.
  9. */
  10. class WPSEO_Endpoint_MyYoast_Connect implements WPSEO_Endpoint {
  11. /**
  12. * The namespace to use.
  13. *
  14. * @var string
  15. */
  16. const REST_NAMESPACE = 'yoast/v1/myyoast';
  17. /**
  18. * Registers the REST routes that are available on the endpoint.
  19. *
  20. * @codeCoverageIgnore
  21. *
  22. * @return void
  23. */
  24. public function register() {
  25. register_rest_route(
  26. self::REST_NAMESPACE,
  27. 'connect',
  28. [
  29. 'methods' => 'POST',
  30. 'callback' => [ $this, 'handle_request' ],
  31. 'permission_callback' => [ $this, 'can_retrieve_data' ],
  32. ]
  33. );
  34. }
  35. /**
  36. * Determines whether or not data can be retrieved for the registered endpoints.
  37. *
  38. * @param WP_REST_Request $request The current request.
  39. *
  40. * @return WP_REST_Response The response.
  41. */
  42. public function handle_request( WP_REST_Request $request ) {
  43. if ( $request->get_param( 'url' ) !== $this->get_home_url() ) {
  44. return new WP_REST_Response(
  45. 'Bad request: URL mismatch.',
  46. 403
  47. );
  48. }
  49. if ( $request->get_param( 'clientId' ) !== $this->get_client_id() ) {
  50. return new WP_REST_Response(
  51. 'Bad request: ClientID mismatch.',
  52. 403
  53. );
  54. }
  55. $client_secret = $request->get_param( 'clientSecret' );
  56. if ( empty( $client_secret ) ) {
  57. return new WP_REST_Response(
  58. 'Bad request: ClientSecret missing.',
  59. 403
  60. );
  61. }
  62. $this->save_secret( $client_secret );
  63. return new WP_REST_Response( 'Connection successful established.' );
  64. }
  65. /**
  66. * Determines whether or not data can be retrieved for the registered endpoints.
  67. *
  68. * @return bool Whether or not data can be retrieved.
  69. */
  70. public function can_retrieve_data() {
  71. return true;
  72. }
  73. /**
  74. * Saves the client secret.
  75. *
  76. * @codeCoverageIgnore
  77. *
  78. * @param string $client_secret The secret to save.
  79. *
  80. * @return void
  81. */
  82. protected function save_secret( $client_secret ) {
  83. $this->get_client()->save_configuration(
  84. [
  85. 'secret' => $client_secret,
  86. ]
  87. );
  88. }
  89. /**
  90. * Retrieves the current client ID.
  91. *
  92. * @codeCoverageIgnore
  93. *
  94. * @return array The client ID.
  95. */
  96. protected function get_client_id() {
  97. $config = $this->get_client()->get_configuration();
  98. return $config['clientId'];
  99. }
  100. /**
  101. * Retrieves an instance of the client.
  102. *
  103. * @codeCoverageIgnore
  104. *
  105. * @return WPSEO_MyYoast_Client Instance of client.
  106. */
  107. protected function get_client() {
  108. static $client;
  109. if ( ! $client ) {
  110. $client = new WPSEO_MyYoast_Client();
  111. }
  112. return $client;
  113. }
  114. /**
  115. * Wraps the method for retrieving the home URL.
  116. *
  117. * @codeCoverageIgnore
  118. *
  119. * @return string Home URL.
  120. */
  121. protected function get_home_url() {
  122. return WPSEO_Utils::get_home_url();
  123. }
  124. }