| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace MailPoet\API\JSON\v1;
- if (!defined('ABSPATH')) exit;
- use MailPoet\Analytics\Analytics as AnalyticsHelper;
- use MailPoet\API\JSON\Endpoint as APIEndpoint;
- use MailPoet\API\JSON\Error as APIError;
- use MailPoet\API\JSON\Response;
- use MailPoet\Config\AccessControl;
- use MailPoet\Config\Installer;
- use MailPoet\Config\ServicesChecker;
- use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
- use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
- use MailPoet\Mailer\MailerLog;
- use MailPoet\Services\Bridge;
- use MailPoet\Services\CongratulatoryMssEmailController;
- use MailPoet\Settings\SettingsController;
- use MailPoet\WP\DateTime;
- use MailPoet\WP\Functions as WPFunctions;
- class Services extends APIEndpoint {
- /** @var Bridge */
- private $bridge;
- /** @var SettingsController */
- private $settings;
- /** @var AnalyticsHelper */
- private $analytics;
- /** @var DateTime */
- public $dateTime;
- /** @var SendingServiceKeyCheck */
- private $mssWorker;
- /** @var PremiumKeyCheck */
- private $premiumWorker;
- /** @var ServicesChecker */
- private $servicesChecker;
- /** @var CongratulatoryMssEmailController */
- private $congratulatoryMssEmailController;
- /** @var WPFunctions */
- private $wp;
- public $permissions = [
- 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
- ];
- public function __construct(
- Bridge $bridge,
- SettingsController $settings,
- AnalyticsHelper $analytics,
- SendingServiceKeyCheck $mssWorker,
- PremiumKeyCheck $premiumWorker,
- ServicesChecker $servicesChecker,
- CongratulatoryMssEmailController $congratulatoryMssEmailController,
- WPFunctions $wp
- ) {
- $this->bridge = $bridge;
- $this->settings = $settings;
- $this->analytics = $analytics;
- $this->mssWorker = $mssWorker;
- $this->premiumWorker = $premiumWorker;
- $this->dateTime = new DateTime();
- $this->servicesChecker = $servicesChecker;
- $this->congratulatoryMssEmailController = $congratulatoryMssEmailController;
- $this->wp = $wp;
- }
- public function checkMSSKey($data = []) {
- $key = isset($data['key']) ? trim($data['key']) : null;
- if (!$key) {
- return $this->badRequest([
- APIError::BAD_REQUEST => $this->wp->__('Please specify a key.', 'mailpoet'),
- ]);
- }
- $wasPendingApproval = $this->servicesChecker->isMailPoetAPIKeyPendingApproval();
- try {
- $result = $this->bridge->checkMSSKey($key);
- $this->bridge->storeMSSKeyAndState($key, $result);
- } catch (\Exception $e) {
- return $this->errorResponse([
- $e->getCode() => $e->getMessage(),
- ]);
- }
- // pause sending when key is pending approval, resume when not pending anymore
- $isPendingApproval = $this->servicesChecker->isMailPoetAPIKeyPendingApproval();
- if (!$wasPendingApproval && $isPendingApproval) {
- MailerLog::pauseSending(MailerLog::getMailerLog());
- } elseif ($wasPendingApproval && !$isPendingApproval) {
- MailerLog::resumeSending();
- }
- $state = !empty($result['state']) ? $result['state'] : null;
- $successMessage = null;
- if ($state == Bridge::KEY_VALID) {
- $successMessage = $this->wp->__('Your MailPoet Sending Service key has been successfully validated', 'mailpoet');
- } elseif ($state == Bridge::KEY_EXPIRING) {
- $successMessage = sprintf(
- $this->wp->__('Your MailPoet Sending Service key expires on %s!', 'mailpoet'),
- $this->dateTime->formatDate(strtotime($result['data']['expire_at']))
- );
- }
- if (!empty($result['data']['public_id'])) {
- $this->analytics->setPublicId($result['data']['public_id']);
- }
- if ($successMessage) {
- return $this->successResponse(['message' => $successMessage]);
- }
- switch ($state) {
- case Bridge::KEY_INVALID:
- $error = $this->wp->__('Your key is not valid for the MailPoet Sending Service', 'mailpoet');
- break;
- case Bridge::KEY_ALREADY_USED:
- $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 = $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)/", $this->wp->siteUrl()) ) {
- $errorMessage .= ' ' . $this->wp->__("Note that it doesn't work on localhost.", 'mailpoet');
- }
- $error = sprintf(
- $errorMessage,
- $this->getErrorDescriptionByCode($code)
- );
- break;
- }
- return $this->errorResponse([APIError::BAD_REQUEST => $error]);
- }
- public function checkPremiumKey($data = []) {
- $key = isset($data['key']) ? trim($data['key']) : null;
- if (!$key) {
- return $this->badRequest([
- APIError::BAD_REQUEST => $this->wp->__('Please specify a key.', 'mailpoet'),
- ]);
- }
- try {
- $result = $this->bridge->checkPremiumKey($key);
- $this->bridge->storePremiumKeyAndState($key, $result);
- } catch (\Exception $e) {
- return $this->errorResponse([
- $e->getCode() => $e->getMessage(),
- ]);
- }
- $state = !empty($result['state']) ? $result['state'] : null;
- $successMessage = null;
- if ($state == Bridge::KEY_VALID) {
- $successMessage = $this->wp->__('Your Premium key has been successfully validated', 'mailpoet');
- } elseif ($state == Bridge::KEY_EXPIRING) {
- $successMessage = sprintf(
- $this->wp->__('Your Premium key expires on %s', 'mailpoet'),
- $this->dateTime->formatDate(strtotime($result['data']['expire_at']))
- );
- }
- if (!empty($result['data']['public_id'])) {
- $this->analytics->setPublicId($result['data']['public_id']);
- }
- if ($successMessage) {
- return $this->successResponse(
- ['message' => $successMessage],
- Installer::getPremiumStatus()
- );
- }
- switch ($state) {
- case Bridge::KEY_INVALID:
- $error = $this->wp->__('Your key is not valid for MailPoet Premium', 'mailpoet');
- break;
- case Bridge::KEY_ALREADY_USED:
- $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(
- $this->wp->__('Error validating Premium key, please try again later (%s)', 'mailpoet'),
- $this->getErrorDescriptionByCode($code)
- );
- break;
- }
- return $this->errorResponse(
- [APIError::BAD_REQUEST => $error],
- ['code' => $result['code'] ?? null]
- );
- }
- public function recheckKeys() {
- $this->mssWorker->init();
- $this->mssWorker->checkKey();
- $this->premiumWorker->init();
- $this->premiumWorker->checkKey();
- return $this->successResponse();
- }
- public function sendCongratulatoryMssEmail() {
- if (!Bridge::isMPSendingServiceEnabled()) {
- return $this->createBadRequest(__('MailPoet Sending Service is not active.', 'mailpoet'));
- }
- $authorizedEmails = $this->bridge->getAuthorizedEmailAddresses();
- if (!$authorizedEmails) {
- return $this->createBadRequest(__('No FROM email addresses are authorized.', 'mailpoet'));
- }
- $fromEmail = $this->settings->get('sender.address');
- if (!$fromEmail) {
- return $this->createBadRequest(__('Sender email address is not set.', 'mailpoet'));
- }
- if (!in_array($fromEmail, $authorizedEmails, true)) {
- return $this->createBadRequest(sprintf(__("Sender email address '%s' is not authorized.", 'mailpoet'), $fromEmail));
- }
- try {
- // congratulatory email is sent to the current FROM address (authorized at this point)
- $this->congratulatoryMssEmailController->sendCongratulatoryEmail($fromEmail);
- } catch (\Throwable $e) {
- return $this->errorResponse([
- APIError::UNKNOWN => __('Sending of congratulatory email failed.', 'mailpoet'),
- ], [], Response::STATUS_UNKNOWN);
- }
- return $this->successResponse([
- 'email_address' => $fromEmail,
- ]);
- }
- private function getErrorDescriptionByCode($code) {
- switch ($code) {
- case Bridge::CHECK_ERROR_UNAVAILABLE:
- $text = $this->wp->__('Service unavailable', 'mailpoet');
- break;
- case Bridge::CHECK_ERROR_UNKNOWN:
- $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);
- break;
- }
- return $text;
- }
- private function createBadRequest(string $message) {
- return $this->badRequest([
- APIError::BAD_REQUEST => $message,
- ]);
- }
- }
|