class-configuration-endpoint.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Configuration_Endpoint.
  9. */
  10. class WPSEO_Configuration_Endpoint {
  11. /**
  12. * Holds the REST namespace.
  13. *
  14. * @var string
  15. */
  16. const REST_NAMESPACE = 'yoast/v1';
  17. /**
  18. * Holds the endpoint to retrieve from.
  19. *
  20. * @var string
  21. */
  22. const ENDPOINT_RETRIEVE = 'configurator';
  23. /**
  24. * Holds the endpoint to store to.
  25. *
  26. * @var string
  27. */
  28. const ENDPOINT_STORE = 'configurator';
  29. /**
  30. * Holds the capability that can retrieve from the endpoint.
  31. *
  32. * @var string
  33. */
  34. const CAPABILITY_RETRIEVE = 'wpseo_manage_options';
  35. /**
  36. * Holds the capability that can store to the endpoint.
  37. *
  38. * @var string
  39. */
  40. const CAPABILITY_STORE = 'wpseo_manage_options';
  41. /**
  42. * Service to use.
  43. *
  44. * @var WPSEO_Configuration_Service
  45. */
  46. protected $service;
  47. /**
  48. * Sets the service to use.
  49. *
  50. * @param WPSEO_Configuration_Service $service Service to use.
  51. */
  52. public function set_service( WPSEO_Configuration_Service $service ) {
  53. $this->service = $service;
  54. }
  55. /**
  56. * Register REST routes.
  57. */
  58. public function register() {
  59. // Register fetch config.
  60. $route_args = [
  61. 'methods' => 'GET',
  62. 'callback' => [ $this->service, 'get_configuration' ],
  63. 'permission_callback' => [ $this, 'can_retrieve_data' ],
  64. ];
  65. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_RETRIEVE, $route_args );
  66. // Register save changes.
  67. $route_args = [
  68. 'methods' => 'POST',
  69. 'callback' => [ $this->service, 'set_configuration' ],
  70. 'permission_callback' => [ $this, 'can_save_data' ],
  71. ];
  72. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_STORE, $route_args );
  73. }
  74. /**
  75. * Permission callback implementation.
  76. *
  77. * @return bool
  78. */
  79. public function can_retrieve_data() {
  80. return current_user_can( self::CAPABILITY_RETRIEVE );
  81. }
  82. /**
  83. * Permission callback implementation.
  84. *
  85. * @return bool
  86. */
  87. public function can_save_data() {
  88. return current_user_can( self::CAPABILITY_STORE );
  89. }
  90. }