90 lines
1.9 KiB
PHP
90 lines
1.9 KiB
PHP
|
<?php
|
||
|
require_once('php-mailjet.class-mailjet-0.1.php');
|
||
|
|
||
|
class MailjetSyncApiClientV1 implements IMailjetSyncApiClient
|
||
|
{
|
||
|
var $api_key;
|
||
|
var $secret_key;
|
||
|
var $client;
|
||
|
|
||
|
public function __construct($api_key, $secret_key)
|
||
|
{
|
||
|
$this->api_key = $api_key;
|
||
|
$this->secret_key = $secret_key;
|
||
|
$this->client = new MailjetSyncApiV1($this->api_key, $this->secret_key);
|
||
|
}
|
||
|
|
||
|
public static function testAPI($api_key, $secret_key)
|
||
|
{
|
||
|
$client = new MailjetSyncApiV1($api_key, $secret_key);
|
||
|
$client->userInfos();
|
||
|
return ($client->_response_code == 200);
|
||
|
}
|
||
|
|
||
|
public static function checkResponseCode($code)
|
||
|
{
|
||
|
if (in_array($code, array(200, 201, 204, 304)))
|
||
|
return true;
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public function isConfigured()
|
||
|
{
|
||
|
return MailjetSyncApiClientV1::testAPI($this->api_key, $this->secret_key);
|
||
|
}
|
||
|
|
||
|
public function getLists()
|
||
|
{
|
||
|
if (!$this->isConfigured())
|
||
|
return array();
|
||
|
|
||
|
$response = $this->client->listsAll();
|
||
|
if ($response === false)
|
||
|
return array();
|
||
|
$data = array();
|
||
|
foreach ($response->lists as $l)
|
||
|
{
|
||
|
$data[] = array(
|
||
|
'id' => $l->id,
|
||
|
'name' => $l->label,
|
||
|
'subscribers' => $l->subscribers,
|
||
|
);
|
||
|
}
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function addContactToList($email, $list_id)
|
||
|
{
|
||
|
if (!$this->isConfigured())
|
||
|
return false;
|
||
|
|
||
|
$params = array(
|
||
|
'method' => 'POST',
|
||
|
'contact' => $email,
|
||
|
'id' => $list_id,
|
||
|
'force' => true,
|
||
|
);
|
||
|
$this->client->listsAddContact($params);
|
||
|
return MailjetSyncApiClientV1::checkResponseCode($this->client->_response_code);
|
||
|
}
|
||
|
|
||
|
public function deleteContactFromList($email, $list_id, $delete = false)
|
||
|
{
|
||
|
if (!$this->isConfigured())
|
||
|
return false;
|
||
|
|
||
|
$params = array(
|
||
|
'method' => 'POST',
|
||
|
'contact' => $email,
|
||
|
'id' => $list_id,
|
||
|
);
|
||
|
|
||
|
if ($delete)
|
||
|
$this->client->listsRemoveContact($params);
|
||
|
else
|
||
|
$this->client->listsUnsubContact($params);
|
||
|
return $this->checkResponseCode($this->client->_response_code);
|
||
|
}
|
||
|
}
|