Sin descripción

Response.php 1020B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace MailPoet\API\JSON;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. abstract class Response {
  6. const STATUS_OK = 200;
  7. const STATUS_BAD_REQUEST = 400;
  8. const STATUS_UNAUTHORIZED = 401;
  9. const STATUS_FORBIDDEN = 403;
  10. const STATUS_NOT_FOUND = 404;
  11. const STATUS_CONFLICT = 409;
  12. const STATUS_UNKNOWN = 500;
  13. public $status;
  14. public $meta;
  15. public function __construct(
  16. $status,
  17. $meta = []
  18. ) {
  19. $this->status = $status;
  20. $this->meta = $meta;
  21. }
  22. public function send() {
  23. WPFunctions::get()->statusHeader($this->status);
  24. $data = $this->getData();
  25. $response = [];
  26. if (!empty($this->meta)) {
  27. $response['meta'] = $this->meta;
  28. }
  29. if ($data === null) {
  30. $data = [];
  31. }
  32. $response = array_merge($response, $data);
  33. @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
  34. echo WPFunctions::get()->wpJsonEncode($response);
  35. die();
  36. }
  37. public abstract function getData();
  38. }