Convert variable names to camel case
[MAILPOET-1796]
This commit is contained in:
@ -44,18 +44,18 @@ class API {
|
||||
|
||||
public function __construct(
|
||||
ContainerInterface $container,
|
||||
AccessControl $access_control,
|
||||
AccessControl $accessControl,
|
||||
SettingsController $settings,
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->container = $container;
|
||||
$this->access_control = $access_control;
|
||||
$this->accessControl = $accessControl;
|
||||
$this->settings = $settings;
|
||||
$this->wp = $wp;
|
||||
foreach ($this->_available_api_versions as $available_api_version) {
|
||||
foreach ($this->availableApiVersions as $availableApiVersion) {
|
||||
$this->addEndpointNamespace(
|
||||
sprintf('%s\%s', __NAMESPACE__, $available_api_version),
|
||||
$available_api_version
|
||||
sprintf('%s\%s', __NAMESPACE__, $availableApiVersion),
|
||||
$availableApiVersion
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -91,62 +91,62 @@ class API {
|
||||
|
||||
$ignoreToken = (
|
||||
$this->settings->get('captcha.type') != Captcha::TYPE_DISABLED &&
|
||||
$this->_request_endpoint === 'subscribers' &&
|
||||
$this->_request_method === 'subscribe'
|
||||
$this->requestEndpoint === 'subscribers' &&
|
||||
$this->requestMethod === 'subscribe'
|
||||
);
|
||||
|
||||
if (!$ignoreToken && $this->checkToken() === false) {
|
||||
$error_message = WPFunctions::get()->__("Sorry, but we couldn't connect to the MailPoet server. Please refresh the web page and try again.", 'mailpoet');
|
||||
$error_response = $this->createErrorResponse(Error::UNAUTHORIZED, $error_message, Response::STATUS_UNAUTHORIZED);
|
||||
return $error_response->send();
|
||||
$errorMessage = WPFunctions::get()->__("Sorry, but we couldn't connect to the MailPoet server. Please refresh the web page and try again.", 'mailpoet');
|
||||
$errorResponse = $this->createErrorResponse(Error::UNAUTHORIZED, $errorMessage, Response::STATUS_UNAUTHORIZED);
|
||||
return $errorResponse->send();
|
||||
}
|
||||
|
||||
$response = $this->processRoute();
|
||||
$response->send();
|
||||
}
|
||||
|
||||
public function setRequestData($data, $request_type) {
|
||||
$this->_request_api_version = !empty($data['api_version']) ? $data['api_version'] : false;
|
||||
public function setRequestData($data, $requestType) {
|
||||
$this->requestApiVersion = !empty($data['api_version']) ? $data['api_version'] : false;
|
||||
|
||||
$this->_request_endpoint = isset($data['endpoint'])
|
||||
$this->requestEndpoint = isset($data['endpoint'])
|
||||
? Helpers::underscoreToCamelCase(trim($data['endpoint']))
|
||||
: null;
|
||||
|
||||
// JS part of /wp-admin/customize.php does not like a 'method' field in a form widget
|
||||
$method_param_name = isset($data['mailpoet_method']) ? 'mailpoet_method' : 'method';
|
||||
$this->_request_method = isset($data[$method_param_name])
|
||||
? Helpers::underscoreToCamelCase(trim($data[$method_param_name]))
|
||||
$methodParamName = isset($data['mailpoet_method']) ? 'mailpoet_method' : 'method';
|
||||
$this->requestMethod = isset($data[$methodParamName])
|
||||
? Helpers::underscoreToCamelCase(trim($data[$methodParamName]))
|
||||
: null;
|
||||
$this->_request_type = $request_type;
|
||||
$this->requestType = $requestType;
|
||||
|
||||
$this->_request_token = isset($data['token'])
|
||||
$this->requestToken = isset($data['token'])
|
||||
? trim($data['token'])
|
||||
: null;
|
||||
|
||||
if (!$this->_request_endpoint || !$this->_request_method || !$this->_request_api_version) {
|
||||
$error_message = WPFunctions::get()->__('Invalid API request.', 'mailpoet');
|
||||
$error_response = $this->createErrorResponse(Error::BAD_REQUEST, $error_message, Response::STATUS_BAD_REQUEST);
|
||||
return $error_response;
|
||||
} else if (!empty($this->_endpoint_namespaces[$this->_request_api_version])) {
|
||||
foreach ($this->_endpoint_namespaces[$this->_request_api_version] as $namespace) {
|
||||
$endpoint_class = sprintf(
|
||||
if (!$this->requestEndpoint || !$this->requestMethod || !$this->requestApiVersion) {
|
||||
$errorMessage = WPFunctions::get()->__('Invalid API request.', 'mailpoet');
|
||||
$errorResponse = $this->createErrorResponse(Error::BAD_REQUEST, $errorMessage, Response::STATUS_BAD_REQUEST);
|
||||
return $errorResponse;
|
||||
} else if (!empty($this->endpointNamespaces[$this->requestApiVersion])) {
|
||||
foreach ($this->endpointNamespaces[$this->requestApiVersion] as $namespace) {
|
||||
$endpointClass = sprintf(
|
||||
'%s\%s',
|
||||
$namespace,
|
||||
ucfirst($this->_request_endpoint)
|
||||
ucfirst($this->requestEndpoint)
|
||||
);
|
||||
if ($this->container->has($endpoint_class)) {
|
||||
$this->_request_endpoint_class = $endpoint_class;
|
||||
if ($this->container->has($endpointClass)) {
|
||||
$this->requestEndpointClass = $endpointClass;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->_request_data = isset($data['data'])
|
||||
$this->requestData = isset($data['data'])
|
||||
? WPFunctions::get()->stripslashesDeep($data['data'])
|
||||
: [];
|
||||
|
||||
// remove reserved keywords from data
|
||||
if (is_array($this->_request_data) && !empty($this->_request_data)) {
|
||||
if (is_array($this->requestData) && !empty($this->requestData)) {
|
||||
// filter out reserved keywords from data
|
||||
$reserved_keywords = [
|
||||
$reservedKeywords = [
|
||||
'token',
|
||||
'endpoint',
|
||||
'method',
|
||||
@ -154,9 +154,9 @@ class API {
|
||||
'mailpoet_method', // alias of 'method'
|
||||
'mailpoet_redirect',
|
||||
];
|
||||
$this->_request_data = array_diff_key(
|
||||
$this->_request_data,
|
||||
array_flip($reserved_keywords)
|
||||
$this->requestData = array_diff_key(
|
||||
$this->requestData,
|
||||
array_flip($reservedKeywords)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -164,54 +164,54 @@ class API {
|
||||
|
||||
public function processRoute() {
|
||||
try {
|
||||
if (empty($this->_request_endpoint_class) ||
|
||||
!$this->container->has($this->_request_endpoint_class)
|
||||
if (empty($this->requestEndpointClass) ||
|
||||
!$this->container->has($this->requestEndpointClass)
|
||||
) {
|
||||
throw new \Exception(__('Invalid API endpoint.', 'mailpoet'));
|
||||
}
|
||||
|
||||
$endpoint = $this->container->get($this->_request_endpoint_class);
|
||||
if (!method_exists($endpoint, $this->_request_method)) {
|
||||
$endpoint = $this->container->get($this->requestEndpointClass);
|
||||
if (!method_exists($endpoint, $this->requestMethod)) {
|
||||
throw new \Exception(__('Invalid API endpoint method.', 'mailpoet'));
|
||||
}
|
||||
|
||||
if (!$endpoint->isMethodAllowed($this->_request_method, $this->_request_type)) {
|
||||
if (!$endpoint->isMethodAllowed($this->requestMethod, $this->requestType)) {
|
||||
throw new \Exception(__('HTTP request method not allowed.', 'mailpoet'));
|
||||
}
|
||||
|
||||
if (class_exists(Debugger::class)) {
|
||||
ApiPanel::init($endpoint, $this->_request_method, $this->_request_data);
|
||||
ApiPanel::init($endpoint, $this->requestMethod, $this->requestData);
|
||||
DIPanel::init();
|
||||
}
|
||||
|
||||
// check the accessibility of the requested endpoint's action
|
||||
// by default, an endpoint's action is considered "private"
|
||||
if (!$this->validatePermissions($this->_request_method, $endpoint->permissions)) {
|
||||
$error_message = WPFunctions::get()->__('You do not have the required permissions.', 'mailpoet');
|
||||
$error_response = $this->createErrorResponse(Error::FORBIDDEN, $error_message, Response::STATUS_FORBIDDEN);
|
||||
return $error_response;
|
||||
if (!$this->validatePermissions($this->requestMethod, $endpoint->permissions)) {
|
||||
$errorMessage = WPFunctions::get()->__('You do not have the required permissions.', 'mailpoet');
|
||||
$errorResponse = $this->createErrorResponse(Error::FORBIDDEN, $errorMessage, Response::STATUS_FORBIDDEN);
|
||||
return $errorResponse;
|
||||
}
|
||||
$response = $endpoint->{$this->_request_method}($this->_request_data);
|
||||
$response = $endpoint->{$this->requestMethod}($this->requestData);
|
||||
return $response;
|
||||
} catch (\Exception $e) {
|
||||
if (class_exists(Debugger::class) && Debugger::$logDirectory) {
|
||||
Debugger::log($e, ILogger::EXCEPTION);
|
||||
}
|
||||
$error_message = $e->getMessage();
|
||||
$error_response = $this->createErrorResponse(Error::BAD_REQUEST, $error_message, Response::STATUS_BAD_REQUEST);
|
||||
return $error_response;
|
||||
$errorMessage = $e->getMessage();
|
||||
$errorResponse = $this->createErrorResponse(Error::BAD_REQUEST, $errorMessage, Response::STATUS_BAD_REQUEST);
|
||||
return $errorResponse;
|
||||
}
|
||||
}
|
||||
|
||||
public function validatePermissions($request_method, $permissions) {
|
||||
public function validatePermissions($requestMethod, $permissions) {
|
||||
// validate method permission if defined, otherwise validate global permission
|
||||
return(!empty($permissions['methods'][$request_method])) ?
|
||||
$this->access_control->validatePermission($permissions['methods'][$request_method]) :
|
||||
$this->access_control->validatePermission($permissions['global']);
|
||||
return(!empty($permissions['methods'][$requestMethod])) ?
|
||||
$this->accessControl->validatePermission($permissions['methods'][$requestMethod]) :
|
||||
$this->accessControl->validatePermission($permissions['global']);
|
||||
}
|
||||
|
||||
public function checkToken() {
|
||||
return WPFunctions::get()->wpVerifyNonce($this->_request_token, 'mailpoet_token');
|
||||
return WPFunctions::get()->wpVerifyNonce($this->requestToken, 'mailpoet_token');
|
||||
}
|
||||
|
||||
public function setTokenAndAPIVersion() {
|
||||
@ -227,30 +227,30 @@ class API {
|
||||
}
|
||||
|
||||
public function addEndpointNamespace($namespace, $version) {
|
||||
if (!empty($this->_endpoint_namespaces[$version][$namespace])) return;
|
||||
$this->_endpoint_namespaces[$version][] = $namespace;
|
||||
if (!empty($this->endpointNamespaces[$version][$namespace])) return;
|
||||
$this->endpointNamespaces[$version][] = $namespace;
|
||||
}
|
||||
|
||||
public function getEndpointNamespaces() {
|
||||
return $this->_endpoint_namespaces;
|
||||
return $this->endpointNamespaces;
|
||||
}
|
||||
|
||||
public function getRequestedEndpointClass() {
|
||||
return $this->_request_endpoint_class;
|
||||
return $this->requestEndpointClass;
|
||||
}
|
||||
|
||||
public function getRequestedAPIVersion() {
|
||||
return $this->_request_api_version;
|
||||
return $this->requestApiVersion;
|
||||
}
|
||||
|
||||
public function createErrorResponse($error_type, $error_message, $response_status) {
|
||||
$error_response = new ErrorResponse(
|
||||
public function createErrorResponse($errorType, $errorMessage, $responseStatus) {
|
||||
$errorResponse = new ErrorResponse(
|
||||
[
|
||||
$error_type => $error_message,
|
||||
$errorType => $errorMessage,
|
||||
],
|
||||
[],
|
||||
$response_status
|
||||
$responseStatus
|
||||
);
|
||||
return $error_response;
|
||||
return $errorResponse;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user