class-endpoint-file-size.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Endpoints
  6. */
  7. /**
  8. * Represents an implementation of the WPSEO_Endpoint interface to register one or multiple endpoints.
  9. */
  10. class WPSEO_Endpoint_File_Size 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 endpoint to retrieve the file size.
  19. *
  20. * @var string
  21. */
  22. const ENDPOINT_SINGULAR = 'file_size';
  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. * The service provider.
  31. *
  32. * @var WPSEO_File_Size_Service
  33. */
  34. private $service;
  35. /**
  36. * Sets the service provider.
  37. *
  38. * @param WPSEO_File_Size_Service $service The service provider.
  39. */
  40. public function __construct( WPSEO_File_Size_Service $service ) {
  41. $this->service = $service;
  42. }
  43. /**
  44. * Registers the routes for the endpoints.
  45. *
  46. * @return void
  47. */
  48. public function register() {
  49. $route_args = [
  50. 'methods' => 'GET',
  51. 'args' => [
  52. 'url' => [
  53. 'required' => true,
  54. 'type' => 'string',
  55. 'description' => 'The url to retrieve',
  56. ],
  57. ],
  58. 'callback' => [
  59. $this->service,
  60. 'get',
  61. ],
  62. 'permission_callback' => [
  63. $this,
  64. 'can_retrieve_data',
  65. ],
  66. ];
  67. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_SINGULAR, $route_args );
  68. }
  69. /**
  70. * Determines whether or not data can be retrieved for the registered endpoints.
  71. *
  72. * @return bool Whether or not data can be retrieved.
  73. */
  74. public function can_retrieve_data() {
  75. return current_user_can( self::CAPABILITY_RETRIEVE );
  76. }
  77. }