| 1 |
<?php |
| 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 STATUS_BAD_REQUEST = 400; |
| 13 |
const STATUS_UNAUTHORIZED = 401; |
| 14 |
const STATUS_FORBIDDEN = 403; |
| 15 |
const STATUS_NOT_FOUND = 404; |
| 16 |
const STATUS_CONFLICT = 409; |
| 17 |
const STATUS_UNKNOWN = 500; |
| 18 |
|
| 19 |
public $status; |
| 20 |
public $meta; |
| 21 |
|
| 22 |
public function __construct($status, $meta = []) { |
| 23 |
$this->status = $status; |
| 24 |
$this->meta = $meta; |
| 25 |
} |
| 26 |
|
| 27 |
public function send() { |
| 28 |
WPFunctions::get()->statusHeader($this->status); |
| 29 |
|
| 30 |
$data = $this->getData(); |
| 31 |
$response = []; |
| 32 |
|
| 33 |
if (!empty($this->meta)) { |
| 34 |
$response['meta'] = $this->meta; |
| 35 |
} |
| 36 |
if ($data === null) { |
| 37 |
$data = []; |
| 38 |
} |
| 39 |
$response = array_merge($response, $data); |
| 40 |
|
| 41 |
@header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 42 |
echo WPFunctions::get()->wpJsonEncode($response); |
| 43 |
die(); |
| 44 |
} |
| 45 |
|
| 46 |
public abstract function getData(); |
| 47 |
} |
| 48 |
|