Added API/Endpoint abstract class
- (re)Added Endpoints folder to both API and Router - fixed syntax in namespaces - xhr.responseJSON is returned to the fail() - fixed Router endpoints (view in browser, cron,...)
This commit is contained in:
42
lib/API/Response.php
Normal file
42
lib/API/Response.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace MailPoet\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
abstract class Response {
|
||||
const STATUS_OK = 200;
|
||||
const STATUS_BAD_REQUEST = 400;
|
||||
const STATUS_UNAUTHORIZED = 401;
|
||||
const STATUS_FORBIDDEN = 403;
|
||||
const STATUS_NOT_FOUND = 404;
|
||||
|
||||
public $status;
|
||||
public $meta;
|
||||
|
||||
function __construct($status, $meta = array()) {
|
||||
$this->status = $status;
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
function send() {
|
||||
status_header($this->status);
|
||||
|
||||
$data = $this->getData();
|
||||
$response = array();
|
||||
|
||||
if(!empty($this->meta)) {
|
||||
$response['meta'] = $this->meta;
|
||||
}
|
||||
if(!empty($data)) {
|
||||
$response = array_merge($response, $data);
|
||||
}
|
||||
|
||||
if(empty($response)) {
|
||||
die();
|
||||
} else {
|
||||
wp_send_json($response);
|
||||
}
|
||||
}
|
||||
|
||||
abstract function getData();
|
||||
}
|
Reference in New Issue
Block a user