You've already forked grav-plugin-listmonk
Initial commit
This commit is contained in:
67
vendor/adnanhussainturki/listmonk-php/src/Controllers/ListsController.php
vendored
Normal file
67
vendor/adnanhussainturki/listmonk-php/src/Controllers/ListsController.php
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace AdnanHussainTurki\ListMonk\Controllers;
|
||||
|
||||
use AdnanHussainTurki\ListMonk\ListMonk;
|
||||
use AdnanHussainTurki\ListMonk\Models\MonkList;
|
||||
|
||||
class ListsController {
|
||||
protected $listMonk;
|
||||
|
||||
function __construct(ListMonk $listMonk)
|
||||
{
|
||||
$this->listMonk = $listMonk;
|
||||
}
|
||||
|
||||
function getAll($page=1, $perPage=100) : array
|
||||
{
|
||||
$response = $this->listMonk->http('/api/lists' . '?page=' . $page . '&per_page=' . $perPage);
|
||||
$lists = json_decode($response)->data->results;
|
||||
$listObjects = [];
|
||||
foreach ($lists as $key => $list) {
|
||||
$listObjects[$key] = new MonkList($list);
|
||||
}
|
||||
return $listObjects;
|
||||
}
|
||||
|
||||
function get($id) {
|
||||
$response = $this->listMonk->http('/api/lists/' . $id);
|
||||
return new MonkList(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function create(MonkList $list) {
|
||||
$response = $this->listMonk->http('/api/lists', 'post', [
|
||||
'name' => $list->getName(),
|
||||
'type' => $list->getType(),
|
||||
'optin' => $list->getOptin(),
|
||||
'tags' => $list->getTags()
|
||||
]);
|
||||
return new MonkList(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function update(MonkList $list) {
|
||||
if ($list->getId() == null) throw new \Exception("List id is required");
|
||||
$response = $this->listMonk->http('/api/lists/' . $list->getId(), 'put', [
|
||||
'list_id' => $list->getId(),
|
||||
'name' => $list->getName(),
|
||||
'type' => $list->getType(),
|
||||
'optin' => $list->getOptin(),
|
||||
'tags' => $list->getTags()
|
||||
]);
|
||||
return new MonkList(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
$stored = $this->get($id);
|
||||
if ($stored == null) throw new \Exception("List not found");
|
||||
$response = $this->listMonk->http('/api/lists/' . $id, 'delete');
|
||||
try {
|
||||
$this->get($id);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return $stored;
|
||||
}
|
||||
throw new \Exception("List not deleted");
|
||||
}
|
||||
|
||||
}
|
||||
81
vendor/adnanhussainturki/listmonk-php/src/Controllers/SubscribersController.php
vendored
Normal file
81
vendor/adnanhussainturki/listmonk-php/src/Controllers/SubscribersController.php
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace AdnanHussainTurki\ListMonk\Controllers;
|
||||
|
||||
use AdnanHussainTurki\ListMonk\ListMonk;
|
||||
use AdnanHussainTurki\ListMonk\Models\MonkSubscriber;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
|
||||
class SubscribersController {
|
||||
protected $listMonk;
|
||||
function __construct(ListMonk $listMonk)
|
||||
{
|
||||
$this->listMonk = $listMonk;
|
||||
}
|
||||
function getAll($page=1, $perPage=100) : array
|
||||
{
|
||||
$response = $this->listMonk->http('/api/subscribers' . '?page=' . $page . '&per_page=' . $perPage);
|
||||
$subscribers = json_decode($response)->data->results;
|
||||
$subscriberObjects = [];
|
||||
foreach ($subscribers as $key => $subscriber) {
|
||||
$subscriberObjects[$key] = new MonkSubscriber($subscriber);
|
||||
}
|
||||
return $subscriberObjects;
|
||||
}
|
||||
|
||||
function get($id) {
|
||||
$response = $this->listMonk->http('/api/subscribers/' . $id);
|
||||
return new MonkSubscriber(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function create(MonkSubscriber $subscriber, $preConfirm = false) {
|
||||
$data = [
|
||||
'name' => $subscriber->getName(),
|
||||
'email' => $subscriber->getEmail(),
|
||||
'status' => $subscriber->getStatus(),
|
||||
'lists' => $subscriber->getLists(),
|
||||
'preconfirm_subscriptions' => $preConfirm
|
||||
];
|
||||
if ($subscriber->getAttribs() != null) {
|
||||
$data['attribs'] = $subscriber->getAttribs();
|
||||
}
|
||||
try {
|
||||
$response = $this->listMonk->http('/api/subscribers', 'post', $data);
|
||||
} catch (ClientException $th) {
|
||||
throw new \Exception($th->getResponse()->getBody()->getContents());
|
||||
}
|
||||
|
||||
return new MonkSubscriber(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function modifyLists($subscriber_ids, $action, $target_list_ids, $status = null) {
|
||||
if (!in_array($action, ["add", "remove", "unsubscribe"])) throw new \Exception("Invalid action, allowed actions are: add, remove, unsubscribe.");
|
||||
if ($action == "add") {
|
||||
if (!in_array($status, ["confirmed", "unsubscribed", "unconfirmed"])) throw new \Exception("Invalid status, allowed status are: subscribed, unsubscribed, unconfirmed.");
|
||||
}
|
||||
return $this->listMonk->http('/api/subscribers/lists', 'put', [
|
||||
'ids' => $subscriber_ids,
|
||||
'action' => $action,
|
||||
'target_list_ids' => $target_list_ids,
|
||||
'status' => $status
|
||||
]);
|
||||
}
|
||||
function update(MonkSubscriber $subscriber) {
|
||||
if ($subscriber->getId() == null) throw new \Exception("Subscriber id is required");
|
||||
$response = $this->listMonk->http('/api/subscribers/' . $subscriber->getId(), 'put', $subscriber->toArray());
|
||||
return new MonkSubscriber(json_decode($response)->data);
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
$stored = $this->get($id);
|
||||
if ($stored == null) throw new \Exception("Subscriber not found");
|
||||
$response = $this->listMonk->http('/api/subscribers/' . $id, 'delete');
|
||||
try {
|
||||
$this->get($id);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return $stored;
|
||||
}
|
||||
throw new \Exception("Subscriber not deleted");
|
||||
}
|
||||
}
|
||||
68
vendor/adnanhussainturki/listmonk-php/src/ListMonk.php
vendored
Normal file
68
vendor/adnanhussainturki/listmonk-php/src/ListMonk.php
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace AdnanHussainTurki\ListMonk;
|
||||
|
||||
use AdnanHussainTurki\ListMonk\Controllers\ListsController;
|
||||
use AdnanHussainTurki\ListMonk\Controllers\SubscribersController;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
class ListMonk
|
||||
{
|
||||
private $serverUrl;
|
||||
private $username;
|
||||
private $password;
|
||||
private $listsController;
|
||||
private $subscribersController;
|
||||
|
||||
public function __construct($serverUrl, $username, $password)
|
||||
{
|
||||
$this->serverUrl = $serverUrl;
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
}
|
||||
|
||||
public function lists()
|
||||
{
|
||||
$this->listsController = new ListsController($this);
|
||||
return $this->listsController;
|
||||
}
|
||||
|
||||
public function subscribers()
|
||||
{
|
||||
$this->subscribersController = new SubscribersController($this);
|
||||
return $this->subscribersController;
|
||||
}
|
||||
|
||||
public function getServerUrl()
|
||||
{
|
||||
return $this->serverUrl;
|
||||
}
|
||||
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function http($path, $method = "get", $data = [])
|
||||
{
|
||||
$client = new Client();
|
||||
$headers = [
|
||||
'Authorization' => 'Basic ' . base64_encode($this->username . ":" . $this->password)
|
||||
];
|
||||
if (strtolower($method) == "get") {
|
||||
$request = new Request(strtoupper($method), $this->serverUrl . $path, $headers);
|
||||
} else {
|
||||
$headers['Content-Type'] = 'application/json';
|
||||
$request = new Request(strtoupper($method), $this->serverUrl . $path, $headers, json_encode($data));
|
||||
}
|
||||
$res = $client->sendAsync($request)->wait();
|
||||
return $res->getBody();
|
||||
}
|
||||
}
|
||||
126
vendor/adnanhussainturki/listmonk-php/src/Models/MonkList.php
vendored
Normal file
126
vendor/adnanhussainturki/listmonk-php/src/Models/MonkList.php
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace AdnanHussainTurki\ListMonk\Models;
|
||||
|
||||
class MonkList {
|
||||
private $id;
|
||||
private $created_at;
|
||||
private $updated_at;
|
||||
private $uuid;
|
||||
private $name;
|
||||
private $type;
|
||||
private $optin;
|
||||
private $tags = [];
|
||||
private $subscriber_count;
|
||||
private $allowed_types = ["public", "private"];
|
||||
private $allowed_options = ["single", "double"];
|
||||
|
||||
public function __construct(object $list = null) {
|
||||
if ($list == null) return;
|
||||
$this->id = $list->id;
|
||||
$this->created_at = $list->created_at;
|
||||
$this->updated_at = $list->updated_at;
|
||||
$this->uuid = $list->uuid;
|
||||
$this->name = $list->name;
|
||||
$this->type = $list->type;
|
||||
$this->optin = $list->optin;
|
||||
$this->tags = $list->tags;
|
||||
$this->subscriber_count = $list->subscriber_count;
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getCreatedAt() {
|
||||
return $this->created_at;
|
||||
}
|
||||
|
||||
public function setCreatedAt($createdAt) {
|
||||
$this->created_at = $createdAt;
|
||||
}
|
||||
|
||||
public function getUpdatedAt() {
|
||||
return $this->updated_at;
|
||||
}
|
||||
|
||||
public function setUpdatedAt($updatedAt) {
|
||||
$this->updated_at = $updatedAt;
|
||||
}
|
||||
|
||||
public function getUuid() {
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function setUuid($uuid) {
|
||||
$this->uuid = $uuid;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getType() {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function setType($type) {
|
||||
if (!in_array($type, $this->allowed_types)) {
|
||||
throw new \Exception("Invalid list type. Allowed types are: " . implode(", ", $this->allowed_types));
|
||||
}
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function getOptin() {
|
||||
return $this->optin;
|
||||
}
|
||||
|
||||
public function setOptin($optin) {
|
||||
if (!in_array($optin, $this->allowed_options)) {
|
||||
throw new \Exception("Invalid optin type. Allowed types are: " . implode(", ", $this->allowed_options));
|
||||
}
|
||||
$this->optin = $optin;
|
||||
}
|
||||
|
||||
public function getTags() {
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
public function setTags($tags) {
|
||||
$this->tags = $tags;
|
||||
}
|
||||
|
||||
public function getSubscriberCount() {
|
||||
return $this->subscriber_count;
|
||||
}
|
||||
|
||||
public function setSubscriberCount($subscriber_count) {
|
||||
$this->subscriber_count = $subscriber_count;
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'type' => $this->type,
|
||||
'optin' => $this->optin,
|
||||
'tags' => $this->tags,
|
||||
'subscriber_count' => $this->subscriber_count
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
126
vendor/adnanhussainturki/listmonk-php/src/Models/MonkSubscriber.php
vendored
Normal file
126
vendor/adnanhussainturki/listmonk-php/src/Models/MonkSubscriber.php
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace AdnanHussainTurki\ListMonk\Models;
|
||||
|
||||
class MonkSubscriber {
|
||||
|
||||
private $id;
|
||||
private $created_at;
|
||||
private $updated_at;
|
||||
private $uuid;
|
||||
private $email;
|
||||
private $name;
|
||||
private $attribs = [];
|
||||
private $status;
|
||||
private $lists = [];
|
||||
|
||||
private $allowed_status = ["enabled", "disabled", "blocklisted"];
|
||||
|
||||
// Constructor to initialize the object
|
||||
public function __construct(object $subscriber = null) {
|
||||
if ($subscriber == null) return;
|
||||
$this->id = $subscriber->id;
|
||||
$this->created_at = $subscriber->created_at;
|
||||
$this->updated_at = $subscriber->updated_at;
|
||||
$this->uuid = $subscriber->uuid;
|
||||
$this->email = $subscriber->email;
|
||||
$this->name = $subscriber->name;
|
||||
$this->attribs = $subscriber->attribs;
|
||||
$this->status = $subscriber->status;
|
||||
$this->lists = $subscriber->lists;
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getCreatedAt() {
|
||||
return $this->created_at;
|
||||
}
|
||||
|
||||
public function setCreatedAt($createdAt) {
|
||||
$this->created_at = $createdAt;
|
||||
}
|
||||
|
||||
public function getUpdatedAt() {
|
||||
return $this->updated_at;
|
||||
}
|
||||
|
||||
public function setUpdatedAt($updatedAt) {
|
||||
$this->updated_at = $updatedAt;
|
||||
}
|
||||
|
||||
public function getUuid() {
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function setUuid($uuid) {
|
||||
$this->uuid = $uuid;
|
||||
}
|
||||
|
||||
public function getEmail() {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail($email) {
|
||||
$this->email= $email;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name) {
|
||||
$this->name= $name;
|
||||
}
|
||||
|
||||
public function getAttribs() {
|
||||
return $this->attribs;
|
||||
}
|
||||
|
||||
public function setAttribs($attribs) {
|
||||
$this->attribs= $attribs;
|
||||
}
|
||||
|
||||
public function getStatus() {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus($status) {
|
||||
if (!in_array($status, $this->allowed_status)) {
|
||||
throw new \Exception("Invalid status, allowed status are: " . implode(", ", $this->allowed_status) . ".");
|
||||
}
|
||||
$this->status= $status;
|
||||
}
|
||||
|
||||
public function getLists() {
|
||||
return $this->lists;
|
||||
}
|
||||
|
||||
public function setLists($lists) {
|
||||
$this->lists= $lists;
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
'uuid' => $this->uuid,
|
||||
'email' => $this->email,
|
||||
'name' => $this->name,
|
||||
'attribs' => $this->attribs,
|
||||
'status' => $this->status,
|
||||
'lists' => $this->lists
|
||||
];
|
||||
}
|
||||
|
||||
public function toJson() {
|
||||
return json_encode($this->toArray());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user