4-rest_endpoint_with_apikey.php 916 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace cx_newsletter;
  3. use \wp_rest_request;
  4. trait rest_endpoint_with_apikey {
  5. protected function check_apikey(string $key) : bool {
  6. $valid_apikey = (
  7. $this
  8. ->get_settings()
  9. ->get('apikey')
  10. );
  11. return $key === $valid_apikey;
  12. }
  13. protected function check_request_apikey(wp_rest_request $request) : bool {
  14. $params = $request->get_query_params();
  15. if (!array_key_exists('apikey', $params)) {
  16. return false;
  17. }
  18. return $this->check_apikey($params['apikey']);
  19. }
  20. protected function bad_apikey_response() : array {
  21. $response = new response_builder();
  22. return $response
  23. ->set_code(response_code::APIKEY_FAIL)
  24. ->set_status(response_status::ERROR)
  25. ->set_message('Api Key is not valid, or not specified in the params.')
  26. ->build();
  27. }
  28. }