PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.2.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.2.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / dynamics / postmark / plugin.php

plugin.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.2.2, at back/dynamics/postmark/plugin.php

158 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use AcyMailing\Core\AcymPlugin;
4
5 class plgAcymPostmark extends AcymPlugin
6 {
7 const SENDING_METHOD_ID = 'postmark';
8 const SENDING_METHOD_NAME = 'Postmark';
9 const SENDING_METHOD_API_URL = 'https://api.postmarkapp.com/';
10
11 public function __construct()
12 {
13 parent::__construct();
14 $this->pluginDescription->name = self::SENDING_METHOD_NAME;
15 }
16
17 public function onAcymGetSendingMethods(&$data, $isMailer = false)
18 {
19 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
20 'name' => $this->pluginDescription->name,
21 'image' => ACYM_IMAGES.'mailers/postmark.svg',
22 'image_class' => '',
23 ];
24 }
25
26 public function onAcymGetSendingMethodsHtmlSetting(&$data)
27 {
28 $config = empty($data['tab']) ? $this->config : $data['tab']->config;
29 $defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key');
30 ob_start();
31 ?>
32 <div class="send_settings cell grid-x acym_vcenter" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
33 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
34 <label class="cell shrink margin-right-1" for="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key">
35 <?php echo acym_translationSprintf(
36 'ACYM_SENDING_METHOD_API_KEY',
37 self::SENDING_METHOD_NAME
38 ); ?>
39 </label>
40 <?php echo $this->getLinks('https://account.postmarkapp.com/sign_up', 'https://postmarkapp.com/pricing'); ?>
41 <input type="text"
42 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key"
43 value="<?php echo empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey; ?>"
44 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_key]"
45 class="cell margin-right-1 acym__configuration__mail__settings__text">
46 <?php echo $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
47 </div>
48 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
49 <label class="cell medium-6">
50 <?php echo acym_translation('ACYM_POSTMARK_STREAM_ID').acym_info('ACYM_STREAM_ID_INFO'); ?>
51 <input type="text"
52 class="cell auto"
53 name="config[postmark_stream_id]"
54 value="<?php echo acym_escape(empty($this->config->get('postmark_stream_id')) ? '' : $this->config->get('postmark_stream_id')); ?>">
55 </label>
56 </div>
57 </div>
58 <?php
59 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
60 }
61
62 public function getHeadersSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
63 {
64 if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
65
66 return [
67 'X-Postmark-Server-Token:'.$credentials[self::SENDING_METHOD_ID.'_api_key'],
68 'Accept: application/json',
69 'Content-type: application/json',
70 ];
71 }
72
73 public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
74 {
75 if ($sendingMethod != self::SENDING_METHOD_ID) return;
76
77 $key = self::SENDING_METHOD_ID.'_api_key';
78
79 $credentials = [
80 $key => $sendingMethodListParams[$key] ?? $this->config->get($key, ''),
81 ];
82 }
83
84 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
85 {
86 if ($sendingMethod == self::SENDING_METHOD_ID) {
87 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, $credentials);
88 $response = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'server', [], $headers);
89 if (!empty($response['error_curl'])) {
90 acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false);
91 } elseif (!empty($response['ErrorCode']) && in_array($response['ErrorCode'], ['Unauthorized', '10'])) {
92 $message = acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY');
93 acym_sendAjaxResponse($message, [], false);
94 } elseif (!empty($response['ErrorCode'])) {
95 $message = acym_translationSprintf('ACYM_API_RETURN_THIS_ERROR', $response['ErrorCode'].': '.$response['Message']);
96 acym_sendAjaxResponse($message, [], false);
97 } else {
98 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
99 }
100 }
101 }
102
103 public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $reply_to, $bcc = [], $attachments = [], $sendingMethodListParams = [])
104 {
105 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
106
107 $data = [
108 'From' => $from['email'],
109 'ReplyTo' => $reply_to['email'],
110 'To' => $to['email'],
111 'Subject' => $mailerHelper->Subject,
112 'HtmlBody' => $mailerHelper->Body,
113 'MessageStream' => 'outbound',
114 ];
115 if (!empty($bcc)) $data['Bcc'] = $bcc[0][0];
116
117 if (!empty($attachments)) {
118 $attachFormated = [];
119 foreach ($attachments as $oneAttach) {
120 $attachFormated[] = [
121 'Name' => $oneAttach[1],
122 'Content' => $oneAttach['contentEncoded'],
123 'ContentType' => $oneAttach[4],
124 ];
125 }
126 $data['Attachments'] = $attachFormated;
127 }
128
129 if (!empty($mailerHelper->mailId)) {
130 $mailId = $mailerHelper->mailId;
131 if (acym_isMultilingual()) {
132 $parentId = acym_loadResult('SELECT parent_id FROM `#__acym_mail` WHERE id = '.intval($mailId));
133 if (!empty($parentId)) $mailId = $parentId;
134 }
135
136 $sendParams = acym_loadResult('SELECT sending_params FROM `#__acym_campaign` WHERE mail_id = '.intval($mailId));
137 if (!empty($sendParams)) {
138 $sendParams = json_decode($sendParams, true);
139 if (!empty($sendParams['message_stream_id'])) {
140 $data['MessageStream'] = $sendParams['message_stream_id'];
141 } else {
142 $data['MessageStream'] = $this->config->get('postmark_stream_id');
143 }
144 }
145 }
146
147 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
148 $responseMailer = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'email', $data, $headers, 'POST');
149
150 if (!empty($responseMailer['ErrorCode'])) {
151 $response['error'] = true;
152 $response['message'] = $responseMailer['Message'];
153 } else {
154 $response['error'] = false;
155 }
156 }
157 }
158