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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. const REST_NAMESPACE = 'yoast/v1';
  12. const ENDPOINT_QUERY = 'reindex_posts';
  13. const CAPABILITY_RETRIEVE = 'edit_posts';
  14. /** @var WPSEO_Link_Reindex_Post_Service */
  15. protected $service;
  16. /**
  17. * WPSEO_Link_Reindex_Post_Endpoint constructor.
  18. *
  19. * @param WPSEO_Link_Reindex_Post_Service $service The service to handle the requests to the endpoint.
  20. */
  21. public function __construct( WPSEO_Link_Reindex_Post_Service $service ) {
  22. $this->service = $service;
  23. }
  24. /**
  25. * Register the REST endpoint to WordPress.
  26. */
  27. public function register() {
  28. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_QUERY, array(
  29. 'methods' => 'GET',
  30. 'callback' => array(
  31. $this->service,
  32. 'reindex',
  33. ),
  34. 'permission_callback' => array(
  35. $this,
  36. 'can_retrieve_data',
  37. ),
  38. ) );
  39. }
  40. /**
  41. * Determines if the current user is allowed to use this endpoint.
  42. *
  43. * @return bool
  44. */
  45. public function can_retrieve_data() {
  46. return current_user_can( self::CAPABILITY_RETRIEVE );
  47. }
  48. }