class-configuration-endpoint.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. const REST_NAMESPACE = 'yoast/v1';
  12. const ENDPOINT_RETRIEVE = 'configurator';
  13. const ENDPOINT_STORE = 'configurator';
  14. const CAPABILITY_RETRIEVE = 'wpseo_manage_options';
  15. const CAPABILITY_STORE = 'wpseo_manage_options';
  16. /** @var WPSEO_Configuration_Service Service to use */
  17. protected $service;
  18. /**
  19. * Sets the service to use.
  20. *
  21. * @param WPSEO_Configuration_Service $service Service to use.
  22. */
  23. public function set_service( WPSEO_Configuration_Service $service ) {
  24. $this->service = $service;
  25. }
  26. /**
  27. * Register REST routes.
  28. */
  29. public function register() {
  30. // Register fetch config.
  31. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_RETRIEVE, array(
  32. 'methods' => 'GET',
  33. 'callback' => array(
  34. $this->service,
  35. 'get_configuration',
  36. ),
  37. 'permission_callback' => array(
  38. $this,
  39. 'can_retrieve_data',
  40. ),
  41. ) );
  42. // Register save changes.
  43. register_rest_route( self::REST_NAMESPACE, self::ENDPOINT_STORE, array(
  44. 'methods' => 'POST',
  45. 'callback' => array(
  46. $this->service,
  47. 'set_configuration',
  48. ),
  49. 'permission_callback' => array(
  50. $this,
  51. 'can_save_data',
  52. ),
  53. ) );
  54. }
  55. /**
  56. * Permission callback implementation
  57. *
  58. * @return bool
  59. */
  60. public function can_retrieve_data() {
  61. return current_user_can( self::CAPABILITY_RETRIEVE );
  62. }
  63. /**
  64. * Permission callback implementation
  65. *
  66. * @return bool
  67. */
  68. public function can_save_data() {
  69. return current_user_can( self::CAPABILITY_STORE );
  70. }
  71. }