| 1 |
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 |
|
| 3 |
namespace MailPoet\API\JSON; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\WP\Functions as WPFunctions; |
| 9 |
|
| 10 |
abstract class Response { |
| 11 |
const STATUS_OK = 200; |
| 12 |
const REDIRECT = 302; |
| 13 |
const STATUS_BAD_REQUEST = 400; |
| 14 |
const STATUS_UNAUTHORIZED = 401; |
| 15 |
const STATUS_FORBIDDEN = 403; |
| 16 |
const STATUS_NOT_FOUND = 404; |
| 17 |
const STATUS_CONFLICT = 409; |
| 18 |
const STATUS_UNKNOWN = 500; |
| 19 |
|
| 20 |
public $status; |
| 21 |
public $meta; |
| 22 |
public $location; |
| 23 |
|
| 24 |
public function __construct($status, $meta = [], $location = null) { // phpcs:ignore |
| 25 |
$this->status = $status; |
| 26 |
$this->meta = $meta; |
| 27 |
$this->location = $location; |
| 28 |
} |
| 29 |
|
| 30 |
public function send() { |
| 31 |
if ($this->status === self::REDIRECT && $this->location) { |
| 32 |
header("Location: " . $this->location, true, $this->status); |
| 33 |
exit; |
| 34 |
} |
| 35 |
|
| 36 |
WPFunctions::get()->statusHeader($this->status); |
| 37 |
|
| 38 |
$data = $this->getData(); |
| 39 |
$response = []; |
| 40 |
|
| 41 |
if (!empty($this->meta)) { |
| 42 |
$response['meta'] = $this->meta; |
| 43 |
} |
| 44 |
if ($data === null) { |
| 45 |
$data = []; |
| 46 |
} |
| 47 |
$response = array_merge($response, $data); |
| 48 |
|
| 49 |
$charset = get_option('blog_charset'); |
| 50 |
@header('Content-Type: application/json; charset=' . (is_string($charset) ? $charset : 'UTF-8')); |
| 51 |
echo wp_json_encode($response); |
| 52 |
die(); |
| 53 |
} |
| 54 |
|
| 55 |
public abstract function getData(); |
| 56 |
} |
| 57 |
|