| 1 |
<?php |
| 2 |
|
| 3 |
class SendinblueCampaign extends SendinblueClass |
| 4 |
{ |
| 5 |
private SendinblueSender $sender; |
| 6 |
private SendinblueUsers $user; |
| 7 |
private SendinblueList $list; |
| 8 |
|
| 9 |
public function __construct(&$plugin, $headers, $sender, $user, $list) |
| 10 |
{ |
| 11 |
parent::__construct($plugin, $headers); |
| 12 |
$this->sender = $sender; |
| 13 |
$this->user = $user; |
| 14 |
$this->list = $list; |
| 15 |
} |
| 16 |
|
| 17 |
public function createNewCampaign($mail, $content) |
| 18 |
{ |
| 19 |
$listIdBrevo = 0; |
| 20 |
$this->list->getListExternalSendingMethod($listIdBrevo, $mail->id); |
| 21 |
|
| 22 |
$subjectAttribute = $this->user->getSubjectAttributeName($mail->id); |
| 23 |
$contentAttribute = $this->user->getAttributeName($mail->id); |
| 24 |
|
| 25 |
if (strpos($content, 'acym__wysid__template') === false || strpos($content, '<body') === false) { |
| 26 |
$commonContent = '<html>{% autoescape off %}{{ contact.'.$contentAttribute.' }}{% endautoescape %}</html>'; |
| 27 |
} else { |
| 28 |
$commonContent = preg_replace('#<title>(.*)</title>#Uis', '<title>{{ contact.'.$subjectAttribute.' }}</title>', $content); |
| 29 |
$commonContent = preg_replace('#(<body[^>]*>).*</body>#Uis', '$1{% autoescape off %}{{ contact.'.$contentAttribute.' }}{% endautoescape %}</body>', $commonContent); |
| 30 |
} |
| 31 |
|
| 32 |
$data = [ |
| 33 |
'sender' => $this->sender->getSender($mail), |
| 34 |
'name' => 'AcyMailing Mail '.$mail->id.' ('.$mail->subject.')', |
| 35 |
'htmlContent' => $commonContent, |
| 36 |
'scheduledAt' => gmdate('c', time() + 60), |
| 37 |
'subject' => '{{ contact.'.$this->user->getSubjectAttributeName($mail->id).' }}', |
| 38 |
'replyTo' => $this->sender->getReplyToEmail($mail), |
| 39 |
'recipients' => [ |
| 40 |
'listIds' => [$listIdBrevo], |
| 41 |
], |
| 42 |
'footer' => '<span style="display: none !important;">{'.acym_translation('ACYM_UNSUBSCRIBE').'}</span>', |
| 43 |
'inlineImageActivation' => !empty($this->config->get('embed_images', 0)), |
| 44 |
]; |
| 45 |
|
| 46 |
$this->callApiSendingMethod('emailCampaigns', $data, $this->headers, 'POST'); |
| 47 |
} |
| 48 |
|
| 49 |
public function cleanCampaigns(): bool |
| 50 |
{ |
| 51 |
$cleanFrequency = $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_clean_frequency', 2592000); |
| 52 |
$lastClean = $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_last_clean', 0); |
| 53 |
|
| 54 |
$time = time(); |
| 55 |
|
| 56 |
if (!empty($lastClean) && $lastClean < ($time + $cleanFrequency)) return true; |
| 57 |
|
| 58 |
$response = $this->callApiSendingMethod( |
| 59 |
plgAcymSendinblue::SENDING_METHOD_API_URL.'emailCampaigns?status=sent&limit=500&offset=0&sort=desc', |
| 60 |
[], |
| 61 |
$this->headers |
| 62 |
); |
| 63 |
|
| 64 |
if (empty($response['campaigns'])) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
foreach ($response['campaigns'] as $campaign) { |
| 69 |
//If it is a recent campaign we don't delete it |
| 70 |
$sendDate = strtotime($campaign['sentDate'].' UTC'); |
| 71 |
if ($sendDate > $time - $cleanFrequency) continue; |
| 72 |
|
| 73 |
//If it's not an AcyMailing campaign we get out |
| 74 |
preg_match('#AcyMailing Mail ([0-9]+)#is', $campaign['name'], $match); |
| 75 |
if (empty($match)) continue; |
| 76 |
|
| 77 |
//We get the id of the email |
| 78 |
$id = intval($match[1]); |
| 79 |
|
| 80 |
if (empty($id)) continue; |
| 81 |
|
| 82 |
//We delete what we created in Sendinblue |
| 83 |
$this->user->deleteAttribute($id); |
| 84 |
$this->list->deleteList($id); |
| 85 |
$this->deleteCampaign($campaign['id']); |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
public function deleteCampaign($brevoCampaignId) |
| 92 |
{ |
| 93 |
$this->callApiSendingMethod(plgAcymSendinblue::SENDING_METHOD_API_URL.'emailCampaigns/'.$brevoCampaignId, [], $this->headers, 'DELETE'); |
| 94 |
} |
| 95 |
} |
| 96 |
|