class-endpoint-indexable.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_Indexable implements WPSEO_Endpoint, WPSEO_Endpoint_Storable {
  11. const REST_NAMESPACE = 'yoast/v1';
  12. const ENDPOINT_SINGULAR = 'indexables/(?P<object_type>.*)/(?P<object_id>\d+)';
  13. const CAPABILITY_RETRIEVE = 'manage_options';
  14. const CAPABILITY_STORE = 'manage_options';
  15. /** @var WPSEO_Indexable_Service */
  16. private $service;
  17. /**
  18. * Sets the service provider.
  19. *
  20. * @param WPSEO_Indexable_Service $service The service provider.
  21. */
  22. public function __construct( WPSEO_Indexable_Service $service ) {
  23. $this->service = $service;
  24. }
  25. /**
  26. * Registers the routes for the endpoints.
  27. *
  28. * @return void
  29. */
  30. public function register() {
  31. // Register fetch config.
  32. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_SINGULAR, array(
  33. 'methods' => 'GET',
  34. 'callback' => array(
  35. $this->service,
  36. 'get_indexable',
  37. ),
  38. 'permission_callback' => array(
  39. $this,
  40. 'can_retrieve_data',
  41. ),
  42. ) );
  43. }
  44. /**
  45. * Determines whether or not data can be retrieved for the registered endpoints.
  46. *
  47. * @return bool Whether or not data can be retrieved.
  48. */
  49. public function can_retrieve_data() {
  50. return current_user_can( self::CAPABILITY_RETRIEVE );
  51. }
  52. /**
  53. * Determines whether or not data can be stored for the registered endpoints.
  54. *
  55. * @return bool Whether or not data can be stored.
  56. */
  57. public function can_store_data() {
  58. return current_user_can( self::CAPABILITY_STORE );
  59. }
  60. }