Add subscriber count reporting on Sending Service key validation [MAILPOET-804]

This commit is contained in:
Alexey Stoletniy
2017-02-01 15:58:49 +03:00
parent 6575d1579d
commit be0c9b71d8
3 changed files with 33 additions and 4 deletions

View File

@ -3,6 +3,7 @@ namespace MailPoet\Services;
use MailPoet\Mailer\Mailer; use MailPoet\Mailer\Mailer;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
use MailPoet\Models\Subscriber;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@ -41,7 +42,9 @@ class Bridge {
function checkKey($api_key) { function checkKey($api_key) {
$this->initApi($api_key); $this->initApi($api_key);
$result = $this->api->checkKey(); $result = $this->api->checkKey();
return $this->processResult($result); $result = $this->processResult($result);
$this->updateSubscriberCount($result);
return $result;
} }
function processResult(array $result) { function processResult(array $result) {
@ -76,6 +79,16 @@ class Bridge {
return $state; return $state;
} }
function updateSubscriberCount($result) {
if(!empty($result['state'])
&& ($result['state'] === self::MAILPOET_KEY_VALID
|| $result['state'] === self::MAILPOET_KEY_EXPIRING)
) {
return $this->api->updateSubscriberCount(Subscriber::getTotalSubscribers());
}
return null;
}
static function invalidateKey() { static function invalidateKey() {
Setting::setValue( Setting::setValue(
self::API_KEY_STATE_SETTING_NAME, self::API_KEY_STATE_SETTING_NAME,

View File

@ -15,6 +15,7 @@ class API {
public $url_me = 'https://bridge.mailpoet.com/api/v0/me'; public $url_me = 'https://bridge.mailpoet.com/api/v0/me';
public $url_messages = 'https://bridge.mailpoet.com/api/v0/messages'; public $url_messages = 'https://bridge.mailpoet.com/api/v0/messages';
public $url_bounces = 'https://bridge.mailpoet.com/api/v0/bounces/search'; public $url_bounces = 'https://bridge.mailpoet.com/api/v0/bounces/search';
public $url_stats = 'https://bridge.mailpoet.com/api/v0/stats';
function __construct($api_key) { function __construct($api_key) {
$this->setKey($api_key); $this->setKey($api_key);
@ -70,7 +71,7 @@ class API {
function checkBounces(array $emails) { function checkBounces(array $emails) {
$result = wp_remote_post( $result = wp_remote_post(
$this->url, $this->url_bounces,
$this->request($emails) $this->request($emails)
); );
if(wp_remote_retrieve_response_code($result) === 200) { if(wp_remote_retrieve_response_code($result) === 200) {
@ -79,6 +80,17 @@ class API {
return false; return false;
} }
function updateSubscriberCount($count) {
$result = wp_remote_post(
$this->url_stats,
$this->request(array('subscriber_count' => (int)$count), 'PUT')
);
if(wp_remote_retrieve_response_code($result) === 204) {
return true;
}
return false;
}
function setKey($api_key) { function setKey($api_key) {
$this->api_key = $api_key; $this->api_key = $api_key;
} }
@ -91,11 +103,11 @@ class API {
return 'Basic ' . base64_encode('api:' . $this->api_key); return 'Basic ' . base64_encode('api:' . $this->api_key);
} }
private function request($body) { private function request($body, $method = 'POST') {
return array( return array(
'timeout' => 10, 'timeout' => 10,
'httpversion' => '1.0', 'httpversion' => '1.0',
'method' => 'POST', 'method' => $method,
'headers' => array( 'headers' => array(
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'Authorization' => $this->auth() 'Authorization' => $this->auth()

View File

@ -19,6 +19,10 @@ class MockAPI {
return $this->processResponse($code); return $this->processResponse($code);
} }
function updateSubscriberCount($count) {
return true;
}
function setKey($api_key) { function setKey($api_key) {
$this->api_key = $api_key; $this->api_key = $api_key;
} }