Skip to content

Commit

Permalink
✨ added contactslist management
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Meilick <b@bnomei.com>
  • Loading branch information
bnomei committed Nov 8, 2019
1 parent e297972 commit 69fadbe
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -97,9 +97,9 @@ Sending campaigns consists of creating and/or updating a campaign object using t

- [ ] Explanation on how to use [Janitor Plugin](https://github.com/bnomei/kirby3-janitor) buttons to test and send E-Mails
- [ ] Getter for verified Sender-E-Mail-Adresses (to use in Panel Dropdowns)
- [ ] Getter for Contact-Lists (to use in Panel Dropdowns)
- [ ] Getter for Contactslists (to use in Panel Dropdowns)
- [ ] Getter for Segments (to use in Panel Dropdowns)
- [ ] Add/Add-Force/Unsub Contact from Contact-List
- [x] Add/Add-Force/Unsub Contact from Contactslist

## Dependencies

Expand Down
171 changes: 167 additions & 4 deletions classes/Mailjet.php
Expand Up @@ -4,7 +4,8 @@

namespace Bnomei;

use Mailjet\Client;
use \Mailjet\Client;
use \Mailjet\Resources;

final class Mailjet
{
Expand All @@ -28,9 +29,10 @@ public function __construct(array $options = [])
];
$this->options = array_merge($defaults, $options);

foreach ($options as $key => $callable) {
foreach ($this->options as $key => $callable) {
if (is_callable($callable) && in_array($key, ['apikey', 'apisecret'])) {
$this->options[$key] = $callable();
$this->options[$key] = trim($callable()) . '';

}
}

Expand Down Expand Up @@ -63,6 +65,167 @@ public function transport(): array
);
}

/**
* Get Contactlist ID by name
*
* @param int|string $name
* @return int|null
*/
public function contactslist($name): ?int
{
if (ctype_digit($name)) {
return intval($name);
}

$response = $this->client()->get(Resources::$Contactslist, ['filters' => ['Name' => $name], 'body' => null]);
if ($response->success()) {
foreach ($response->getData() as $r) {
if ($r['Name'] === $name) {
return intval($r['ID']);
}
}
}

return null;
}

/**
* Get Segment ID by name
*
* @param int|string $name
* @return int|null
*/
public function segment($name): ?int
{
if (ctype_digit($name)) {
$segid = intval($name);
$response = $this->client()->get(Resources::$Contactfilter, ['body' => null]);
foreach ($response->getData() as $r) {
if ($segid === intval($r['ID'])) {
return intval($r['ID']);
}
}
}

$response = $this->client()->get(Resources::$Contactfilter, ['filters' => ['Name' => $name], 'body' => null]);
foreach ($response->getData() as $r) {
if ($name === $r['Name']) {
return intval($r['ID']);
}
}

return null;
}

/**
* Add a Contact to Mailjet
*
* @param string $email
* @return bool
*/
public function addContact(string $email): bool
{
$response = $this->client()->post(Resources::$Contact, ['body' => [
'Email' => strtolower($email),
]]);

// new
if ($response->success()) {
// $contactID = $response->getData()[0]['ID']; // first element
return true;
}

// exists
if (intval($response->getData()['StatusCode']) === 400) {
// $contactID = strtolower($email);
return true;
}

// failed
return false;
}

/**
* @param string $email
* @param int $contactslistID
* @return bool
*/
public function removeFromContactslist(string $email, int $contactslistID): bool
{
$response = $this->client()->post(
Resources::$ContactslistManagecontact, [
'id' => $contactslistID,
'body' => ['Email' => $email, 'Action' => 'remove']
]
);
if ($response->success() || intval($response->getData()['Status']) === 400) {
return true;
}

return false;
}

/**
* @param string $email
* @param int $contactslistID
* @return bool
*/
public function unsubscribeFromContactslist(string $email, int $contactslistID): bool
{
$response = $this->client()->post(
Resources::$ContactslistManagecontact, [
'id' => $contactslistID,
'body' => ['Email' => $email, 'Action' => 'unsub']
]
);
if ($response->success() || intval($response->getData()['Status']) === 400) {
return true;
}

return false;
}

/**
* @param string $email
* @param int $contactslistID
* @param array $contactData
* @param bool $force
* @return bool
*/
public function subscribeToContactslist(string $email, int $contactslistID, array $contactData = [], bool $force = false): bool
{
if ($this->addContact($email) === false) {
return false;
}

$response = $this->client()->post(
Resources::$ContactManagecontactslists,
['id' => $email, 'body' => ['ContactsLists' => [[
'ListID' => $contactslistID,
'Action' => $force ? 'addforce' : 'addnoforce',
]]]]
);

if ($response->success()) {
$dataToAdd = [];
foreach ($contactData as $key => $value) {
if ($key === 'email') {
continue;
}
$dataToAdd[] = ['Name' => $key, 'value' => $value];
}
if (count($dataToAdd)) {
$this->client()->put(Resources::$Contactdata, [
'id' => $email,
'body' => ['Data' => $dataToAdd]
]);
}
return true;
}

return false;
}

/** @var \Bnomei\Mailjet */
private static $singleton;

Expand All @@ -72,7 +235,7 @@ public function transport(): array
*/
public static function singleton(array $options = [])
{
if (! self::$singleton) {
if (!self::$singleton) {
self::$singleton = new self($options);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "bnomei/kirby3-mailjet",
"description": "Send transactional E-Mail and Campaigns with Mailjet",
"type": "kirby-plugin",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"authors": [
{
Expand Down

0 comments on commit 69fadbe

Please sign in to comment.