From 1c6fca7f83005b708a2e766abce1f29c220e1ae5 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Wed, 3 Aug 2016 16:04:45 +0200 Subject: [PATCH] fixed ErrorResponse call in API::setupPublic() - removed empty constructors in updated endpoints - added missing keys to Error class (unauthorized, forbidden) --- lib/API/API.php | 23 ++++++++++++++++------- lib/API/Endpoints/Settings.php | 3 --- lib/API/Endpoints/Setup.php | 3 --- lib/API/Error.php | 2 ++ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/API/API.php b/lib/API/API.php index b935e622b0..dedde00618 100644 --- a/lib/API/API.php +++ b/lib/API/API.php @@ -37,7 +37,9 @@ class API { function setupAdmin() { if($this->checkToken() === false) { (new ErrorResponse( - array('unauthorized' => __('This request is not authorized.')), + array( + Error::UNAUTHORIZED => __('You need to specify a valid API token.') + ), array(), Response::STATUS_UNAUTHORIZED ))->send(); @@ -45,7 +47,9 @@ class API { if($this->checkPermissions() === false) { (new ErrorResponse( - array('forbidden' => __('You do not have the required permissions.')), + array( + Error::FORBIDDEN => __('You do not have the required permissions.') + ), array(), Response::STATUS_FORBIDDEN ))->send(); @@ -56,10 +60,13 @@ class API { function setupPublic() { if($this->checkToken() === false) { - $response = new ErrorResponse(array( - 'unauthorized' => __('This request is not authorized.') - ), Response::STATUS_UNAUTHORIZED); - $response->send(); + (new ErrorResponse( + array( + Error::UNAUTHORIZED => __('You need to specify a valid API token.') + ), + array(), + Response::STATUS_UNAUTHORIZED + ))->send(); } $this->processRoute(); @@ -101,7 +108,9 @@ class API { wp_send_json($response); } } catch(\Exception $e) { - (new ErrorResponse(array($e->getMessage())))->send(); + (new ErrorResponse( + array($e->getCode() => $e->getMessage()) + ))->send(); } } diff --git a/lib/API/Endpoints/Settings.php b/lib/API/Endpoints/Settings.php index 6c8dcaa335..582819e644 100644 --- a/lib/API/Endpoints/Settings.php +++ b/lib/API/Endpoints/Settings.php @@ -7,9 +7,6 @@ use \MailPoet\Models\Setting; if(!defined('ABSPATH')) exit; class Settings extends APIEndpoint { - function __construct() { - } - function get() { return $this->successResponse(Setting::getAll()); } diff --git a/lib/API/Endpoints/Setup.php b/lib/API/Endpoints/Setup.php index 65f03403d0..b3957aa706 100644 --- a/lib/API/Endpoints/Setup.php +++ b/lib/API/Endpoints/Setup.php @@ -6,9 +6,6 @@ use \MailPoet\Config\Activator; if(!defined('ABSPATH')) exit; class Setup extends APIEndpoint { - function __construct() { - } - function reset() { try { $activator = new Activator(); diff --git a/lib/API/Error.php b/lib/API/Error.php index 9503015e77..7c5a13d84f 100644 --- a/lib/API/Error.php +++ b/lib/API/Error.php @@ -6,6 +6,8 @@ if(!defined('ABSPATH')) exit; final class Error { const UNKNOWN = 'unknown'; const BAD_REQUEST = 'bad_request'; + const UNAUTHORIZED = 'unauthorized'; + const FORBIDDEN = 'forbidden'; private function __construct() {