class-endpoint-ryte.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\OnPage
  6. */
  7. /**
  8. * Represents an implementation of the WPSEO_Endpoint interface to register one or multiple endpoints.
  9. */
  10. class WPSEO_Endpoint_Ryte implements WPSEO_Endpoint {
  11. /**
  12. * The namespace of the REST route.
  13. *
  14. * @var string
  15. */
  16. const REST_NAMESPACE = 'yoast/v1';
  17. /**
  18. * The route of the ryte endpoint.
  19. *
  20. * @var string
  21. */
  22. const ENDPOINT_RETRIEVE = 'ryte';
  23. /**
  24. * The name of the capability needed to retrieve data using the endpoints.
  25. *
  26. * @var string
  27. */
  28. const CAPABILITY_RETRIEVE = 'manage_options';
  29. /**
  30. * Service to use.
  31. *
  32. * @var WPSEO_Ryte_Service
  33. */
  34. protected $service;
  35. /**
  36. * Constructs the WPSEO_Endpoint_Ryte class and sets the service to use.
  37. *
  38. * @param WPSEO_Ryte_Service $service Service to use.
  39. */
  40. public function __construct( WPSEO_Ryte_Service $service ) {
  41. $this->service = $service;
  42. }
  43. /**
  44. * Registers the REST routes that are available on the endpoint.
  45. */
  46. public function register() {
  47. // Register fetch config.
  48. $route_args = [
  49. 'methods' => 'GET',
  50. 'callback' => [ $this->service, 'get_statistics' ],
  51. 'permission_callback' => [ $this, 'can_retrieve_data' ],
  52. ];
  53. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_RETRIEVE, $route_args );
  54. }
  55. /**
  56. * Determines whether or not data can be retrieved for the registered endpoints.
  57. *
  58. * @return bool Whether or not data can be retrieved.
  59. */
  60. public function can_retrieve_data() {
  61. return current_user_can( self::CAPABILITY_RETRIEVE );
  62. }
  63. }