refactored API class
This commit is contained in:
@ -33,7 +33,18 @@ class API {
|
|||||||
|
|
||||||
function setupAjax() {
|
function setupAjax() {
|
||||||
$this->getRequestData();
|
$this->getRequestData();
|
||||||
$this->checkToken();
|
|
||||||
|
if($this->checkToken() === false) {
|
||||||
|
$error_response = new ErrorResponse(
|
||||||
|
array(
|
||||||
|
Error::UNAUTHORIZED => __('Invalid request.', 'mailpoet')
|
||||||
|
),
|
||||||
|
array(),
|
||||||
|
Response::STATUS_UNAUTHORIZED
|
||||||
|
);
|
||||||
|
$error_response->send();
|
||||||
|
}
|
||||||
|
|
||||||
$this->processRoute();
|
$this->processRoute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,23 +107,7 @@ class API {
|
|||||||
||
|
||
|
||||||
$permissions[$this->_method] !== Access::ALL
|
$permissions[$this->_method] !== Access::ALL
|
||||||
) {
|
) {
|
||||||
$this->checkPermissions();
|
if($this->checkPermissions() === false) {
|
||||||
}
|
|
||||||
|
|
||||||
$response = $endpoint->{$this->_method}($this->_data);
|
|
||||||
$response->send();
|
|
||||||
} catch(\Exception $e) {
|
|
||||||
$error_response = new ErrorResponse(
|
|
||||||
array($e->getCode() => $e->getMessage())
|
|
||||||
);
|
|
||||||
$error_response->send();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkPermissions() {
|
|
||||||
$has_permission = current_user_can('manage_options');
|
|
||||||
|
|
||||||
if($has_permission === false) {
|
|
||||||
$error_response = new ErrorResponse(
|
$error_response = new ErrorResponse(
|
||||||
array(
|
array(
|
||||||
Error::FORBIDDEN => __(
|
Error::FORBIDDEN => __(
|
||||||
@ -127,21 +122,24 @@ class API {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkToken() {
|
$response = $endpoint->{$this->_method}($this->_data);
|
||||||
$is_valid_token = wp_verify_nonce($this->_token, 'mailpoet_token');
|
$response->send();
|
||||||
|
} catch(\Exception $e) {
|
||||||
if($is_valid_token === false) {
|
|
||||||
$error_response = new ErrorResponse(
|
$error_response = new ErrorResponse(
|
||||||
array(
|
array($e->getCode() => $e->getMessage())
|
||||||
Error::UNAUTHORIZED => __('Invalid request.', 'mailpoet')
|
|
||||||
),
|
|
||||||
array(),
|
|
||||||
Response::STATUS_UNAUTHORIZED
|
|
||||||
);
|
);
|
||||||
$error_response->send();
|
$error_response->send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkPermissions() {
|
||||||
|
return current_user_can('manage_options');
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkToken() {
|
||||||
|
return wp_verify_nonce($this->_token, 'mailpoet_token');
|
||||||
|
}
|
||||||
|
|
||||||
function setToken() {
|
function setToken() {
|
||||||
$global = '<script type="text/javascript">';
|
$global = '<script type="text/javascript">';
|
||||||
$global .= 'var mailpoet_token = "';
|
$global .= 'var mailpoet_token = "';
|
||||||
|
Reference in New Issue
Block a user