Consolidate Sending Service API calls in one class [MAILPOET-795]
This commit is contained in:
@@ -7,6 +7,7 @@ use MailPoet\Mailer\Mailer;
|
|||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
|
use MailPoet\Services\Bridge\API;
|
||||||
use MailPoet\Util\Helpers;
|
use MailPoet\Util\Helpers;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
@@ -31,7 +32,7 @@ class Bounce {
|
|||||||
function initApi() {
|
function initApi() {
|
||||||
if(!$this->api) {
|
if(!$this->api) {
|
||||||
$mailer_config = Mailer::getMailerConfig();
|
$mailer_config = Mailer::getMailerConfig();
|
||||||
$this->api = new Bounce\API($mailer_config['mailpoet_api_key']);
|
$this->api = new API($mailer_config['mailpoet_api_key']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +141,7 @@ class Bounce {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processEmails(array $subscriber_emails) {
|
function processEmails(array $subscriber_emails) {
|
||||||
$checked_emails = $this->api->check($subscriber_emails);
|
$checked_emails = $this->api->checkBounces($subscriber_emails);
|
||||||
$this->processApiResponse((array)$checked_emails);
|
$this->processApiResponse((array)$checked_emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace MailPoet\Cron\Workers\Bounce;
|
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class API {
|
|
||||||
public $url = 'https://bridge.mailpoet.com/api/v0/bounces/search';
|
|
||||||
public $api_key;
|
|
||||||
|
|
||||||
function __construct($api_key) {
|
|
||||||
$this->api_key = $api_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function check(array $emails) {
|
|
||||||
$result = wp_remote_post(
|
|
||||||
$this->url,
|
|
||||||
$this->request($emails)
|
|
||||||
);
|
|
||||||
if(wp_remote_retrieve_response_code($result) === 200) {
|
|
||||||
return json_decode(wp_remote_retrieve_body($result), true);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function auth() {
|
|
||||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function request($body) {
|
|
||||||
return array(
|
|
||||||
'timeout' => 10,
|
|
||||||
'httpversion' => '1.0',
|
|
||||||
'method' => 'POST',
|
|
||||||
'headers' => array(
|
|
||||||
'Content-Type' => 'application/json',
|
|
||||||
'Authorization' => $this->auth()
|
|
||||||
),
|
|
||||||
'body' => json_encode($body)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -4,18 +4,18 @@ namespace MailPoet\Mailer\Methods;
|
|||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
use MailPoet\Config\ServicesChecker;
|
use MailPoet\Config\ServicesChecker;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
|
use MailPoet\Services\Bridge\API;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class MailPoet {
|
class MailPoet {
|
||||||
public $url = 'https://bridge.mailpoet.com/api/v0/messages';
|
public $api;
|
||||||
public $api_key;
|
|
||||||
public $sender;
|
public $sender;
|
||||||
public $reply_to;
|
public $reply_to;
|
||||||
public $services_checker;
|
public $services_checker;
|
||||||
|
|
||||||
function __construct($api_key, $sender, $reply_to) {
|
function __construct($api_key, $sender, $reply_to) {
|
||||||
$this->api_key = $api_key;
|
$this->api = new API($api_key);
|
||||||
$this->sender = $sender;
|
$this->sender = $sender;
|
||||||
$this->reply_to = $reply_to;
|
$this->reply_to = $reply_to;
|
||||||
$this->services_checker = new ServicesChecker(false);
|
$this->services_checker = new ServicesChecker(false);
|
||||||
@@ -26,25 +26,22 @@ class MailPoet {
|
|||||||
$response = __('MailPoet API key is invalid!', 'mailpoet');
|
$response = __('MailPoet API key is invalid!', 'mailpoet');
|
||||||
return Mailer::formatMailerSendErrorResult($response);
|
return Mailer::formatMailerSendErrorResult($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
$message_body = $this->getBody($newsletter, $subscriber);
|
$message_body = $this->getBody($newsletter, $subscriber);
|
||||||
$result = wp_remote_post(
|
$result = $this->api->sendMessages($message_body);
|
||||||
$this->url,
|
|
||||||
$this->request($message_body)
|
switch($result['status']) {
|
||||||
);
|
case API::SENDING_STATUS_CONNECTION_ERROR:
|
||||||
if(is_wp_error($result)) {
|
return Mailer::formatMailerConnectionErrorResult($result['message']);
|
||||||
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
case API::SENDING_STATUS_SEND_ERROR:
|
||||||
|
if(!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
|
||||||
|
Bridge::invalidateKey();
|
||||||
|
}
|
||||||
|
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||||
|
case API::SENDING_STATUS_OK:
|
||||||
|
default:
|
||||||
|
return Mailer::formatMailerSendSuccessResult();
|
||||||
}
|
}
|
||||||
$response_code = wp_remote_retrieve_response_code($result);
|
|
||||||
if($response_code !== 201) {
|
|
||||||
if($response_code === 401) {
|
|
||||||
Bridge::invalidateKey();
|
|
||||||
}
|
|
||||||
$response = (wp_remote_retrieve_body($result)) ?
|
|
||||||
wp_remote_retrieve_body($result) :
|
|
||||||
wp_remote_retrieve_response_message($result);
|
|
||||||
return Mailer::formatMailerSendErrorResult($response);
|
|
||||||
}
|
|
||||||
return Mailer::formatMailerSendSuccessResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function processSubscriber($subscriber) {
|
function processSubscriber($subscriber) {
|
||||||
@@ -98,21 +95,4 @@ class MailPoet {
|
|||||||
}
|
}
|
||||||
return $body;
|
return $body;
|
||||||
}
|
}
|
||||||
|
|
||||||
function auth() {
|
|
||||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
function request($body) {
|
|
||||||
return array(
|
|
||||||
'timeout' => 10,
|
|
||||||
'httpversion' => '1.0',
|
|
||||||
'method' => 'POST',
|
|
||||||
'headers' => array(
|
|
||||||
'Content-Type' => 'application/json',
|
|
||||||
'Authorization' => $this->auth()
|
|
||||||
),
|
|
||||||
'body' => json_encode($body)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -4,8 +4,17 @@ namespace MailPoet\Services\Bridge;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
public $url = 'https://bridge.mailpoet.com/api/v0/me';
|
const SENDING_STATUS_OK = 'ok';
|
||||||
public $api_key;
|
const SENDING_STATUS_CONNECTION_ERROR = 'connection_error';
|
||||||
|
const SENDING_STATUS_SEND_ERROR = 'send_error';
|
||||||
|
|
||||||
|
const RESPONSE_CODE_KEY_INVALID = 401;
|
||||||
|
|
||||||
|
private $api_key;
|
||||||
|
|
||||||
|
public $url_me = 'https://bridge.mailpoet.com/api/v0/me';
|
||||||
|
public $url_messages = 'https://bridge.mailpoet.com/api/v0/messages';
|
||||||
|
public $url_bounces = 'https://bridge.mailpoet.com/api/v0/bounces/search';
|
||||||
|
|
||||||
function __construct($api_key) {
|
function __construct($api_key) {
|
||||||
$this->setKey($api_key);
|
$this->setKey($api_key);
|
||||||
@@ -13,17 +22,10 @@ class API {
|
|||||||
|
|
||||||
function checkKey() {
|
function checkKey() {
|
||||||
$result = wp_remote_post(
|
$result = wp_remote_post(
|
||||||
$this->url,
|
$this->url_me,
|
||||||
$this->request(array('site' => home_url()))
|
$this->request(array('site' => home_url()))
|
||||||
);
|
);
|
||||||
return $this->processResponse($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setKey($api_key) {
|
|
||||||
$this->api_key = $api_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function processResponse($result) {
|
|
||||||
$code = wp_remote_retrieve_response_code($result);
|
$code = wp_remote_retrieve_response_code($result);
|
||||||
switch($code) {
|
switch($code) {
|
||||||
case 200:
|
case 200:
|
||||||
@@ -41,6 +43,50 @@ class API {
|
|||||||
return array('code' => $code, 'data' => $body);
|
return array('code' => $code, 'data' => $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendMessages($message_body) {
|
||||||
|
$result = wp_remote_post(
|
||||||
|
$this->url_messages,
|
||||||
|
$this->request($message_body)
|
||||||
|
);
|
||||||
|
if(is_wp_error($result)) {
|
||||||
|
return array(
|
||||||
|
'status' => self::SENDING_STATUS_CONNECTION_ERROR,
|
||||||
|
'message' => $result->get_error_message()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$response_code = wp_remote_retrieve_response_code($result);
|
||||||
|
if($response_code !== 201) {
|
||||||
|
$response = (wp_remote_retrieve_body($result)) ?
|
||||||
|
wp_remote_retrieve_body($result) :
|
||||||
|
wp_remote_retrieve_response_message($result);
|
||||||
|
return array(
|
||||||
|
'status' => self::SENDING_STATUS_SEND_ERROR,
|
||||||
|
'message' => $response,
|
||||||
|
'code' => $response_code
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return array('status' => self::SENDING_STATUS_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkBounces(array $emails) {
|
||||||
|
$result = wp_remote_post(
|
||||||
|
$this->url,
|
||||||
|
$this->request($emails)
|
||||||
|
);
|
||||||
|
if(wp_remote_retrieve_response_code($result) === 200) {
|
||||||
|
return json_decode(wp_remote_retrieve_body($result), true);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setKey($api_key) {
|
||||||
|
$this->api_key = $api_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKey() {
|
||||||
|
return $this->api_key;
|
||||||
|
}
|
||||||
|
|
||||||
private function auth() {
|
private function auth() {
|
||||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use MailPoet\Cron\CronHelper;
|
use MailPoet\Cron\CronHelper;
|
||||||
use MailPoet\Cron\Workers\Bounce;
|
use MailPoet\Cron\Workers\Bounce;
|
||||||
use MailPoet\Cron\Workers\Bounce\API;
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
|
@@ -4,7 +4,7 @@ namespace MailPoet\Cron\Workers\Bounce;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class MockAPI {
|
class MockAPI {
|
||||||
function check(array $emails) {
|
function checkBounces(array $emails) {
|
||||||
return array_map(
|
return array_map(
|
||||||
function ($email) {
|
function ($email) {
|
||||||
return array(
|
return array(
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
use Codeception\Util\Stub;
|
use Codeception\Util\Stub;
|
||||||
use MailPoet\Mailer\Methods\MailPoet;
|
use MailPoet\Mailer\Methods\MailPoet;
|
||||||
use MailPoet\Config\ServicesChecker;
|
use MailPoet\Config\ServicesChecker;
|
||||||
|
use MailPoet\Services\Bridge\API;
|
||||||
|
|
||||||
class MailPoetAPITest extends MailPoetTest {
|
class MailPoetAPITest extends MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
@@ -68,17 +69,6 @@ class MailPoetAPITest extends MailPoetTest {
|
|||||||
expect($body[0]['text'])->equals($this->newsletter['body']['text']);
|
expect($body[0]['text'])->equals($this->newsletter['body']['text']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanCreateRequest() {
|
|
||||||
$body = $this->mailer->getBody($this->newsletter, $this->subscriber);
|
|
||||||
$request = $this->mailer->request($body);
|
|
||||||
expect($request['timeout'])->equals(10);
|
|
||||||
expect($request['httpversion'])->equals('1.0');
|
|
||||||
expect($request['method'])->equals('POST');
|
|
||||||
expect($request['headers']['Content-Type'])->equals('application/json');
|
|
||||||
expect($request['headers']['Authorization'])->equals($this->mailer->auth());
|
|
||||||
expect($request['body'])->equals(json_encode($body));
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItCanProcessSubscriber() {
|
function testItCanProcessSubscriber() {
|
||||||
expect($this->mailer->processSubscriber('test@test.com'))
|
expect($this->mailer->processSubscriber('test@test.com'))
|
||||||
->equals(
|
->equals(
|
||||||
@@ -100,11 +90,6 @@ class MailPoetAPITest extends MailPoetTest {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanDoBasicAuth() {
|
|
||||||
expect($this->mailer->auth())
|
|
||||||
->equals('Basic ' . base64_encode('api:' . $this->settings['api_key']));
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItWillNotSendIfApiKeyIsMarkedInvalid() {
|
function testItWillNotSendIfApiKeyIsMarkedInvalid() {
|
||||||
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
||||||
$this->mailer->api_key = 'someapi';
|
$this->mailer->api_key = 'someapi';
|
||||||
@@ -122,7 +107,7 @@ class MailPoetAPITest extends MailPoetTest {
|
|||||||
|
|
||||||
function testItCannotSendWithoutProperApiKey() {
|
function testItCannotSendWithoutProperApiKey() {
|
||||||
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
||||||
$this->mailer->api_key = 'someapi';
|
$this->mailer->api->setKey('someapi');
|
||||||
$result = $this->mailer->send(
|
$result = $this->mailer->send(
|
||||||
$this->newsletter,
|
$this->newsletter,
|
||||||
$this->subscriber
|
$this->subscriber
|
||||||
|
Reference in New Issue
Block a user