class-endpoint-indexable.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  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 or patch the indexable.
  19. *
  20. * @var string
  21. */
  22. const ENDPOINT_SINGULAR = 'indexables/(?P<object_type>\w+)/(?P<object_id>\d+)';
  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 name of the capability needed to store data using the endpoints.
  31. *
  32. * @var string
  33. */
  34. const CAPABILITY_STORE = 'manage_options';
  35. /**
  36. * The indexable service.
  37. *
  38. * @var WPSEO_Indexable_Service
  39. */
  40. private $service;
  41. /**
  42. * Sets the service provider.
  43. *
  44. * @param WPSEO_Indexable_Service $service The service provider.
  45. */
  46. public function __construct( WPSEO_Indexable_Service $service ) {
  47. $this->service = $service;
  48. }
  49. /**
  50. * Registers the routes for the endpoints.
  51. *
  52. * @return void
  53. */
  54. public function register() {
  55. $endpoints = [];
  56. $endpoints[] = new WPSEO_Endpoint_Factory(
  57. self::REST_NAMESPACE,
  58. self::ENDPOINT_SINGULAR,
  59. [ $this->service, 'get_indexable' ],
  60. [ $this, 'can_retrieve_data' ]
  61. );
  62. $endpoints[] = new WPSEO_Endpoint_Factory(
  63. self::REST_NAMESPACE,
  64. self::ENDPOINT_SINGULAR,
  65. [ $this->service, 'patch_indexable' ],
  66. [ $this, 'can_store_data' ],
  67. 'PATCH'
  68. );
  69. foreach ( $endpoints as $endpoint ) {
  70. $endpoint->register();
  71. }
  72. }
  73. /**
  74. * Determines whether or not data can be retrieved for the registered endpoints.
  75. *
  76. * @return bool Whether or not data can be retrieved.
  77. */
  78. public function can_retrieve_data() {
  79. return current_user_can( self::CAPABILITY_RETRIEVE );
  80. }
  81. /**
  82. * Determines whether or not data can be stored for the registered endpoints.
  83. *
  84. * @return bool Whether or not data can be stored.
  85. */
  86. public function can_store_data() {
  87. return current_user_can( self::CAPABILITY_STORE );
  88. }
  89. }