mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-05-30 03:07:56 -04:00
- Update README.md to reflect information about routes - Add registerAllNamespaces() function to correctly load plugin namespaces - center text in modal title - Properly decrypt the api key - Refactor getAllLists to getLists - Naming simplification of strings when mailchimp_ is redundant or unnecessary - Do not attempt to decrypt a plaintext api_key pasted into the form - Register namespaces early on in system init Signed-off-by: objec <objecttothis@gmail.com>
341 lines
12 KiB
PHP
341 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Plugins\MailchimpPlugin\Libraries;
|
|
|
|
use App\Plugins\MailchimpPlugin\Enums\SubscriptionStatus;
|
|
use Exception;
|
|
use stdClass;
|
|
|
|
/**
|
|
* Mailchimp library, usable from CI code
|
|
*
|
|
* Library with utility queries to interface Mailchimp v3 API
|
|
*
|
|
* Inspired by the work of ThinkShout: https://github.com/thinkshout/mailchimp-api-php
|
|
*/
|
|
|
|
class MailchimpLibrary
|
|
{
|
|
private MailchimpConnector $connector;
|
|
private array $settings;
|
|
|
|
public function __construct(array $settings = [])
|
|
{
|
|
$apiKey = $settings['api_key'] ?? '';
|
|
$this->connector = new MailchimpConnector($apiKey);
|
|
|
|
$this->settings = $settings;
|
|
}
|
|
|
|
/**
|
|
* Gets information about all lists owned by the authenticated account.
|
|
*
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* By the default it places a simple query to list name & id and count of members & merge_fields
|
|
* NOTE: no space between , and next word is allowed. You will not get the filter to work in full but just the first tag
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists
|
|
*/
|
|
public function getLists(array $parameters = ['fields' => 'lists.id,lists.name,lists.stats.member_count,lists.stats.merge_field_count']): bool|array
|
|
{
|
|
return $this->connector->call('/lists', 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Gets a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param array $parameters Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists_list_id
|
|
*/
|
|
public function getList(string $listId, array $parameters = ['fields' => 'id,name,stats.member_count,stats.merge_field_count']): bool|array
|
|
{
|
|
return $this->connector->call("/lists/$listId", 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Gets information about all members of a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param int $count
|
|
* @param int $offset
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members
|
|
*/
|
|
public function getMembers(string $listId, int $count, int $offset, array $parameters = ['fields' => 'members.id,members.email_address,members.unique_email_id,members.status,members.merge_fields']): bool|array
|
|
{
|
|
$parameters += [
|
|
'count' => $count,
|
|
'offset' => $offset
|
|
];
|
|
|
|
return $this->connector->call("/lists/$listId/members", 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Gets information about a member of a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $md5id
|
|
* The member's email address md5 hash which is the id.
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
|
|
*/
|
|
public function getMemberInfoById(string $listId, string $md5id, array $parameters = ['fields' => 'email_address,status,merge_fields']): bool|array
|
|
{
|
|
return $this->connector->call("/lists/$listId/members/$md5id", 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Gets information about a member of a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $email
|
|
* The member's email address.
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
|
|
*/
|
|
public function getMemberInfo(string $listId, string $email, array $parameters = []): bool|array
|
|
{
|
|
return $this->connector->call("/lists/$listId/members/" . md5(strtolower($email)), 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Gets activity related to a member of a MailChimp list.
|
|
*
|
|
* @param string $listId The ID of the list.
|
|
* @param string $email The member's email address.
|
|
* @param array $parameters Associative array of optional request parameters.
|
|
* @return array|bool Associative array of results or false.
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/activity/#read-get_lists_list_id_members_subscriber_hash_activity
|
|
*/
|
|
public function getMemberActivity(string $listId, string $email, array $parameters = []): bool|array
|
|
{
|
|
return $this->connector->call("/lists/$listId/members/" . md5(strtolower($email)) . '/activity', 'GET', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Adds a new member to a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $email
|
|
* The email address to add.
|
|
* @param string $firstName
|
|
* @param string $lastName
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
|
|
*/
|
|
public function addMember(string $listId, string $email, string $firstName, string $lastName, array $parameters = []): bool|array
|
|
{
|
|
$parameters += [
|
|
'email_address' => $email,
|
|
'status' => 'subscribed',
|
|
'merge_fields' => [
|
|
'FNAME' => $firstName,
|
|
'LNAME' => $lastName
|
|
]
|
|
];
|
|
|
|
return $this->connector->call("/lists/$listId/members/", 'POST', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Removes a member from a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $email
|
|
* The member's email address.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#delete-delete_lists_list_id_members_subscriber_hash
|
|
*/
|
|
public function removeMember(string $listId, string $email): bool|array
|
|
{
|
|
return $this->connector->call("/lists/$listId/members/" . md5(strtolower($email)), 'DELETE');
|
|
}
|
|
|
|
/**
|
|
* Updates a member of a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $email
|
|
* The member's email address.
|
|
* @param string $firstName
|
|
* @param string $lastName
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash
|
|
*/
|
|
public function updateMember(string $listId, string $email, string $firstName, string $lastName, array $parameters = []): bool|array
|
|
{
|
|
$parameters += [
|
|
'status' => 'subscribed',
|
|
'merge_fields' => [
|
|
'FNAME' => $firstName,
|
|
'LNAME' => $lastName
|
|
]
|
|
];
|
|
|
|
return $this->connector->call("/lists/$listId/members/" . md5(strtolower($email)), 'PATCH', $parameters);
|
|
}
|
|
|
|
/**
|
|
* Adds new or update an existing member of a MailChimp list.
|
|
*
|
|
* @param string $listId
|
|
* The ID of the list.
|
|
* @param string $email
|
|
* The member's email address.
|
|
* @param string $firstName
|
|
* @param string $lastName
|
|
* @param string $status
|
|
* @param array $parameters
|
|
* Associative array of optional request parameters.
|
|
* @return array|bool
|
|
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash
|
|
*/
|
|
public function addOrUpdateMember(string $listId, string $email, string $firstName, string $lastName, string $status, array $parameters = []): bool|array
|
|
{
|
|
$parameters += [
|
|
'email_address' => $email,
|
|
'status' => $status,
|
|
'status_if_new' => 'subscribed',
|
|
'merge_fields' => [
|
|
'FNAME' => $firstName,
|
|
'LNAME' => $lastName
|
|
]
|
|
];
|
|
|
|
return $this->connector->call("/lists/$listId/members/" . md5(strtolower($email)), 'PUT', $parameters);
|
|
}
|
|
|
|
public function synchronizeSubscription(array $customerData): bool
|
|
{
|
|
try {
|
|
return $this->subscribeCustomer($customerData);
|
|
} catch (Exception $e) {
|
|
log_message('error', "Failed to sync customer to Mailchimp: {$e->getMessage()}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function subscribeCustomer(array $customerData): bool
|
|
{
|
|
$apiKey = $this->settings['api_key'];
|
|
$listId = $this->settings['list_id'];
|
|
|
|
if (empty($apiKey) || empty($listId)) {
|
|
log_message('warning', 'Mailchimp API key or List ID not configured');
|
|
return false;
|
|
}
|
|
|
|
if (empty($customerData['email'])) {
|
|
log_message('debug', 'Customer has no email, skipping Mailchimp sync');
|
|
return false;
|
|
}
|
|
|
|
$result = $this->addOrUpdateMember(
|
|
$listId,
|
|
$customerData['email'],
|
|
$customerData['first_name'] ?? '',
|
|
$customerData['last_name'] ?? '',
|
|
'subscribed'
|
|
);
|
|
|
|
if ($result) {
|
|
log_message('info', "Successfully subscribed customer ID {$customerData['person_id']} to Mailchimp");
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function deleteSubscription(stdClass $customer): bool
|
|
{
|
|
$listId = $this->settings['list_id'];
|
|
|
|
return !($this->removeMember($listId, $customer->email) === false);
|
|
}
|
|
|
|
public function getMailchimpViewData(array $customerData): array
|
|
{
|
|
if (!empty($customerData->email)) {
|
|
$listId = $this->settings['list_id'];
|
|
$mailchimpInfo = $this->getMemberInfo($listId, $customerData->email);
|
|
|
|
if ($mailchimpInfo !== false) {
|
|
$mailchimpData['mailchimpActivity'] = $mailchimpInfo;
|
|
|
|
$mailchimpData['subscriptionStatusOptions'] = $this->getSubscriptionStatusOptionViewData();
|
|
|
|
$customerActivities = $this->getMemberActivity($listId, $customerData->email);
|
|
if ($customerActivities !== false) {
|
|
if (array_key_exists('activity', $customerActivities)) {
|
|
$open = 0;
|
|
$unopen = 0;
|
|
$click = 0;
|
|
$total = 0;
|
|
$lastOpen = '';
|
|
|
|
foreach ($customerActivities['activity'] as $activity) {
|
|
if ($activity['action'] == 'sent') {
|
|
++$unopen;
|
|
} elseif ($activity['action'] == 'open') {
|
|
if (empty($lastOpen)) {
|
|
$lastOpen = substr($activity['timestamp'], 0, 10);
|
|
}
|
|
++$open;
|
|
} elseif ($activity['action'] == 'click') {
|
|
if (empty($lastOpen)) {
|
|
$lastOpen = substr($activity['timestamp'], 0, 10);
|
|
}
|
|
++$click;
|
|
}
|
|
|
|
++$total;
|
|
}
|
|
|
|
$mailchimpData['mailchimpActivity']['total'] = $total;
|
|
$mailchimpData['mailchimpActivity']['open'] = $open;
|
|
$mailchimpData['mailchimpActivity']['unopen'] = $unopen;
|
|
$mailchimpData['mailchimpActivity']['click'] = $click;
|
|
$mailchimpData['mailchimpActivity']['last_open'] = $lastOpen;
|
|
}
|
|
}
|
|
|
|
return $mailchimpData;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
private function getSubscriptionStatusOptionViewData(): array
|
|
{
|
|
$statusOptions = [];
|
|
foreach (SubscriptionStatus::cases() as $case) {
|
|
$lowercaseName = strtolower($case->name);
|
|
$statusOptions[(int)$case] = lang("MailchimpPlugin.subscription_status_{$lowercaseName}");
|
|
}
|
|
|
|
return $statusOptions;
|
|
}
|
|
}
|