Inject WP functions in Services API

[MAILPOET-2831]
This commit is contained in:
Jan Jakeš
2020-04-13 11:21:53 +02:00
committed by Veljko V
parent b74d2f3fea
commit e748391602
2 changed files with 28 additions and 20 deletions

View File

@ -42,6 +42,9 @@ class Services extends APIEndpoint {
/** @var ServicesChecker */
private $servicesChecker;
/** @var WPFunctions */
private $wp;
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
];
@ -53,7 +56,8 @@ class Services extends APIEndpoint {
SPFCheck $spfCheck,
SendingServiceKeyCheck $mssWorker,
PremiumKeyCheck $premiumWorker,
ServicesChecker $servicesChecker
ServicesChecker $servicesChecker,
WPFunctions $wp
) {
$this->bridge = $bridge;
$this->settings = $settings;
@ -63,6 +67,7 @@ class Services extends APIEndpoint {
$this->premiumWorker = $premiumWorker;
$this->dateTime = new DateTime();
$this->servicesChecker = $servicesChecker;
$this->wp = $wp;
}
public function checkSPFRecord($data = []) {
@ -73,7 +78,7 @@ class Services extends APIEndpoint {
if (!$result) {
return $this->errorResponse(
[APIError::BAD_REQUEST => WPFunctions::get()->__('SPF check has failed.', 'mailpoet')],
[APIError::BAD_REQUEST => $this->wp->__('SPF check has failed.', 'mailpoet')],
['sender_address' => $senderAddress]
);
}
@ -86,7 +91,7 @@ class Services extends APIEndpoint {
if (!$key) {
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet'),
APIError::BAD_REQUEST => $this->wp->__('Please specify a key.', 'mailpoet'),
]);
}
@ -113,10 +118,10 @@ class Services extends APIEndpoint {
$successMessage = null;
if ($state == Bridge::KEY_VALID) {
$successMessage = WPFunctions::get()->__('Your MailPoet Sending Service key has been successfully validated', 'mailpoet');
$successMessage = $this->wp->__('Your MailPoet Sending Service key has been successfully validated', 'mailpoet');
} elseif ($state == Bridge::KEY_EXPIRING) {
$successMessage = sprintf(
WPFunctions::get()->__('Your MailPoet Sending Service key expires on %s!', 'mailpoet'),
$this->wp->__('Your MailPoet Sending Service key expires on %s!', 'mailpoet'),
$this->dateTime->formatDate(strtotime($result['data']['expire_at']))
);
}
@ -131,17 +136,17 @@ class Services extends APIEndpoint {
switch ($state) {
case Bridge::KEY_INVALID:
$error = WPFunctions::get()->__('Your key is not valid for the MailPoet Sending Service', 'mailpoet');
$error = $this->wp->__('Your key is not valid for the MailPoet Sending Service', 'mailpoet');
break;
case Bridge::KEY_ALREADY_USED:
$error = WPFunctions::get()->__('Your MailPoet Sending Service key is already used on another site', 'mailpoet');
$error = $this->wp->__('Your MailPoet Sending Service key is already used on another site', 'mailpoet');
break;
default:
$code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN;
$errorMessage = WPFunctions::get()->__('Error validating MailPoet Sending Service key, please try again later (%s).', 'mailpoet');
$errorMessage = $this->wp->__('Error validating MailPoet Sending Service key, please try again later (%s).', 'mailpoet');
// If site runs on localhost
if ( 1 === preg_match("/^(http|https)\:\/\/(localhost|127\.0\.0\.1)/", WPFunctions::get()->siteUrl()) ) {
$errorMessage .= ' ' . WPFunctions::get()->__("Note that it doesn't work on localhost.", 'mailpoet');
if ( 1 === preg_match("/^(http|https)\:\/\/(localhost|127\.0\.0\.1)/", $this->wp->siteUrl()) ) {
$errorMessage .= ' ' . $this->wp->__("Note that it doesn't work on localhost.", 'mailpoet');
}
$error = sprintf(
$errorMessage,
@ -158,7 +163,7 @@ class Services extends APIEndpoint {
if (!$key) {
return $this->badRequest([
APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a key.', 'mailpoet'),
APIError::BAD_REQUEST => $this->wp->__('Please specify a key.', 'mailpoet'),
]);
}
@ -175,10 +180,10 @@ class Services extends APIEndpoint {
$successMessage = null;
if ($state == Bridge::KEY_VALID) {
$successMessage = WPFunctions::get()->__('Your Premium key has been successfully validated', 'mailpoet');
$successMessage = $this->wp->__('Your Premium key has been successfully validated', 'mailpoet');
} elseif ($state == Bridge::KEY_EXPIRING) {
$successMessage = sprintf(
WPFunctions::get()->__('Your Premium key expires on %s', 'mailpoet'),
$this->wp->__('Your Premium key expires on %s', 'mailpoet'),
$this->dateTime->formatDate(strtotime($result['data']['expire_at']))
);
}
@ -196,15 +201,15 @@ class Services extends APIEndpoint {
switch ($state) {
case Bridge::KEY_INVALID:
$error = WPFunctions::get()->__('Your key is not valid for MailPoet Premium', 'mailpoet');
$error = $this->wp->__('Your key is not valid for MailPoet Premium', 'mailpoet');
break;
case Bridge::KEY_ALREADY_USED:
$error = WPFunctions::get()->__('Your Premium key is already used on another site', 'mailpoet');
$error = $this->wp->__('Your Premium key is already used on another site', 'mailpoet');
break;
default:
$code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN;
$error = sprintf(
WPFunctions::get()->__('Error validating Premium key, please try again later (%s)', 'mailpoet'),
$this->wp->__('Error validating Premium key, please try again later (%s)', 'mailpoet'),
$this->getErrorDescriptionByCode($code)
);
break;
@ -224,10 +229,10 @@ class Services extends APIEndpoint {
private function getErrorDescriptionByCode($code) {
switch ($code) {
case Bridge::CHECK_ERROR_UNAVAILABLE:
$text = WPFunctions::get()->__('Service unavailable', 'mailpoet');
$text = $this->wp->__('Service unavailable', 'mailpoet');
break;
case Bridge::CHECK_ERROR_UNKNOWN:
$text = WPFunctions::get()->__('Contact your hosting support to check the connection between your host and https://bridge.mailpoet.com', 'mailpoet');
$text = $this->wp->__('Contact your hosting support to check the connection between your host and https://bridge.mailpoet.com', 'mailpoet');
break;
default:
$text = sprintf(_x('code: %s', 'Error code (inside parentheses)', 'mailpoet'), $code);