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:
@ -36,19 +36,19 @@ class API {
|
||||
|
||||
function setupAdmin() {
|
||||
if($this->checkToken() === false) {
|
||||
$this->errorResponse(
|
||||
(new ErrorResponse(
|
||||
array('unauthorized' => __('This request is not authorized.')),
|
||||
array(),
|
||||
APIResponse::STATUS_UNAUTHORIZED
|
||||
)->send();
|
||||
Response::STATUS_UNAUTHORIZED
|
||||
))->send();
|
||||
}
|
||||
|
||||
if($this->checkPermissions() === false) {
|
||||
$this->errorResponse(
|
||||
(new ErrorResponse(
|
||||
array('forbidden' => __('You do not have the required permissions.')),
|
||||
array(),
|
||||
APIResponse::STATUS_FORBIDDEN
|
||||
)->send();
|
||||
Response::STATUS_FORBIDDEN
|
||||
))->send();
|
||||
}
|
||||
|
||||
$this->processRoute();
|
||||
@ -56,9 +56,9 @@ class API {
|
||||
|
||||
function setupPublic() {
|
||||
if($this->checkToken() === false) {
|
||||
$response = new APIErrorResponse(array(
|
||||
$response = new ErrorResponse(array(
|
||||
'unauthorized' => __('This request is not authorized.')
|
||||
), APIResponse::STATUS_UNAUTHORIZED);
|
||||
), Response::STATUS_UNAUTHORIZED);
|
||||
$response->send();
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ class API {
|
||||
|
||||
function processRoute() {
|
||||
$class = ucfirst($_POST['endpoint']);
|
||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
||||
$endpoint = __NAMESPACE__ . "\\Endpoints\\" . $class;
|
||||
$method = $_POST['method'];
|
||||
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
@ -101,9 +101,7 @@ class API {
|
||||
wp_send_json($response);
|
||||
}
|
||||
} catch(\Exception $e) {
|
||||
$this->errorResponse(array(
|
||||
$e->getMessage()
|
||||
))->send();
|
||||
(new ErrorResponse(array($e->getMessage())))->send();
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,22 +123,4 @@ class API {
|
||||
wp_verify_nonce($_POST['token'], 'mailpoet_token')
|
||||
);
|
||||
}
|
||||
|
||||
function successResponse(
|
||||
$data = array(), $meta = array(), $status = APIResponse::STATUS_OK
|
||||
) {
|
||||
|
||||
return new APISuccessResponse($data, $meta, $status);
|
||||
}
|
||||
|
||||
function errorResponse(
|
||||
$errors = array(), $meta = array(), $status = APIResponse::STATUS_NOT_FOUND
|
||||
) {
|
||||
|
||||
return new APIErrorResponse($errors, $meta, $status);
|
||||
}
|
||||
|
||||
function badRequest($errors = array(), $meta = array()) {
|
||||
return new APIErrorResponse($errors, $meta, APIResponse::STATUS_BAD_REQUEST);
|
||||
}
|
||||
}
|
25
lib/API/Endpoint.php
Normal file
25
lib/API/Endpoint.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace MailPoet\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
abstract class Endpoint {
|
||||
|
||||
function successResponse(
|
||||
$data = array(), $meta = array(), $status = Response::STATUS_OK
|
||||
) {
|
||||
|
||||
return new SuccessResponse($data, $meta, $status);
|
||||
}
|
||||
|
||||
function errorResponse(
|
||||
$errors = array(), $meta = array(), $status = Response::STATUS_NOT_FOUND
|
||||
) {
|
||||
|
||||
return new ErrorResponse($errors, $meta, $status);
|
||||
}
|
||||
|
||||
function badRequest($errors = array(), $meta = array()) {
|
||||
return new ErrorResponse($errors, $meta, Response::STATUS_BAD_REQUEST);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Cron\CronHelper;
|
||||
use MailPoet\Cron\Supervisor;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use \MailPoet\Models\CustomField;
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use \MailPoet\Models\Form;
|
||||
use \MailPoet\Models\StatisticsForms;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Subscribers\ImportExport\Import\MailChimp;
|
||||
use MailPoet\Models\CustomField;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Models\NewsletterTemplate;
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Listing;
|
||||
use MailPoet\Models\Newsletter;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use \MailPoet\Models\Segment;
|
||||
use \MailPoet\Models\SubscriberSegment;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Models\Newsletter;
|
@ -1,10 +1,11 @@
|
||||
<?php
|
||||
namespace MailPoet\API;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
use \MailPoet\API\Endpoint as APIEndpoint;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Settings extends API {
|
||||
class Settings extends APIEndpoint {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
@ -20,7 +21,7 @@ class Settings extends API {
|
||||
foreach($settings as $name => $value) {
|
||||
Setting::setValue($name, $value);
|
||||
}
|
||||
return $this->successResponse();
|
||||
return $this->successResponse(Setting::getAll());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
|
||||
namespace MailPoet\API\Endpoints;
|
||||
use \MailPoet\Config\Activator;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
namespace MailPoet\API
|
||||
;
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Listing;
|
||||
use MailPoet\Models\Subscriber;
|
@ -3,7 +3,7 @@ namespace MailPoet\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class APIErrorResponse extends APIResponse {
|
||||
class ErrorResponse extends Response {
|
||||
public $errors;
|
||||
|
||||
function __construct($errors = array(), $meta = array(), $status = self::STATUS_NOT_FOUND) {
|
@ -3,7 +3,7 @@ namespace MailPoet\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
abstract class APIResponse {
|
||||
abstract class Response {
|
||||
const STATUS_OK = 200;
|
||||
const STATUS_BAD_REQUEST = 400;
|
||||
const STATUS_UNAUTHORIZED = 401;
|
@ -3,7 +3,7 @@ namespace MailPoet\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class APISuccessResponse extends APIResponse {
|
||||
class SuccessResponse extends Response {
|
||||
public $data;
|
||||
|
||||
function __construct($data = array(), $meta = array(), $status = self::STATUS_OK) {
|
Reference in New Issue
Block a user