class-link-reindex-post-endpoint.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links\Reindex
  6. */
  7. /**
  8. * Class WPSEO_Link_Reindex_Post_Endpoint.
  9. */
  10. class WPSEO_Link_Reindex_Post_Endpoint {
  11. /**
  12. * Holds the namespace of the rest route.
  13. *
  14. * @var string
  15. */
  16. const REST_NAMESPACE = 'yoast/v1';
  17. /**
  18. * Holds the route of the endpoint to reindex the posts.
  19. *
  20. * @var string
  21. */
  22. const ENDPOINT_QUERY = 'reindex_posts';
  23. /**
  24. * Holds the name of the capability needed to reindex the posts.
  25. *
  26. * @var string
  27. */
  28. const CAPABILITY_RETRIEVE = 'edit_posts';
  29. /**
  30. * Holds the link reindex post service instance.
  31. *
  32. * @var WPSEO_Link_Reindex_Post_Service
  33. */
  34. protected $service;
  35. /**
  36. * WPSEO_Link_Reindex_Post_Endpoint constructor.
  37. *
  38. * @param WPSEO_Link_Reindex_Post_Service $service The service to handle the requests to the endpoint.
  39. */
  40. public function __construct( WPSEO_Link_Reindex_Post_Service $service ) {
  41. $this->service = $service;
  42. }
  43. /**
  44. * Register the REST endpoint to WordPress.
  45. */
  46. public function register() {
  47. $route_args = [
  48. 'methods' => 'GET',
  49. 'callback' => [ $this->service, 'reindex' ],
  50. 'permission_callback' => [ $this, 'can_retrieve_data' ],
  51. ];
  52. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_QUERY, $route_args );
  53. }
  54. /**
  55. * Determines if the current user is allowed to use this endpoint.
  56. *
  57. * @return bool
  58. */
  59. public function can_retrieve_data() {
  60. return current_user_can( self::CAPABILITY_RETRIEVE );
  61. }
  62. }