PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.1.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.1.4
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 / sendgrid / plugin.php

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

217 lines 8.5 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 plgAcymSendgrid extends AcymPlugin
6 {
7 const SENDING_METHOD_ID = 'sendgrid';
8 const SENDING_METHOD_NAME = 'SendGrid';
9 const SENDING_METHOD_API_URL = 'https://api.sendgrid.com/v3/';
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/sendgrid.png',
22 ];
23 }
24
25 public function onAcymGetSendingMethodsHtmlSetting(&$data)
26 {
27 $config = empty($data['tab']) ? $this->config : $data['tab']->config;
28 $defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key');
29 ob_start();
30 ?>
31 <div class="send_settings cell grid-x acym_vcenter" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
32 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
33 <label class="cell shrink margin-right-1" for="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key">
34 <?php echo acym_translationSprintf(
35 'ACYM_SENDING_METHOD_API_KEY',
36 self::SENDING_METHOD_NAME
37 ); ?>
38 </label>
39 <?php echo $this->getLinks('https://signup.sendgrid.com/', 'https://sendgrid.com/pricing/'); ?>
40 <input type="text"
41 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key"
42 value="<?php echo empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey; ?>"
43 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_key]"
44 class="cell margin-right-1 acym__configuration__mail__settings__text">
45 <?php echo $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
46 <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?>
47 </div>
48 </div>
49 <?php
50 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
51 }
52
53 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
54 {
55 if ($sendingMethod == self::SENDING_METHOD_ID) {
56 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, $credentials);
57 $response = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'scopes', [], $headers);
58 if (!empty($response['error_curl'])) {
59 acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false);
60 } elseif (!empty($response['errors'])) {
61 $message = $response['errors'][0]['message'] == 'authorization required'
62 ? acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY')
63 : acym_translationSprintf(
64 'ACYM_API_RETURN_THIS_ERROR',
65 $response['errors'][0]['message']
66 );
67 acym_sendAjaxResponse($message, [], false);
68 } else {
69 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
70 }
71 }
72 }
73
74 public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $reply_to, $bcc = [], $attachments = [], $sendingMethodListParams = [])
75 {
76 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
77 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
78 $headers[] = 'Content-Type: application/json';
79
80 $data = [
81 'personalizations' => [
82 [
83 'to' => [
84 [
85 'email' => $to['email'],
86 ],
87 ],
88 'subject' => $mailerHelper->Subject,
89 ],
90 ],
91 'from' => $from,
92 'reply_to' => $reply_to,
93 'content' => [
94 [
95 'type' => 'text/html',
96 'value' => $mailerHelper->Body,
97 ],
98 ],
99 ];
100
101 $mailHeaders = [];
102
103 $unsubscribeHeaderKey = array_search('List-Unsubscribe', array_column($mailerHelper->CustomHeader, 0));
104 if (!empty($unsubscribeHeaderKey)) {
105 $mailHeaders['List-Unsubscribe'] = $mailerHelper->CustomHeader[$unsubscribeHeaderKey][1];
106 $mailHeaders['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
107 }
108
109 if (!empty($mailHeaders)) {
110 $data['headers'] = (object)$mailHeaders;
111 }
112
113 if (!empty($bcc)) $data['personalizations'][0]['bcc'] = [['email' => $bcc[0][0]]];
114
115
116 if (!empty($attachments)) {
117 $data['attachments'] = [];
118 foreach ($attachments as $key => $attachment) {
119 $data['attachments'][] = [
120 'content' => base64_encode(acym_fileGetContent($attachment[0])),
121 'filename' => $attachment[1],
122 'disposition' => 'attachment',
123 ];
124 }
125 }
126
127 $responseMailer = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'mail/send', $data, $headers, 'POST');
128
129 if (empty($responseMailer['errors'][0]['message'])) {
130 $response['error'] = false;
131 } else {
132 $response['error'] = true;
133 $response['message'] = $responseMailer['errors'][0]['message'];
134 }
135 }
136
137 public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
138 {
139 if ($sendingMethod != self::SENDING_METHOD_ID) return;
140
141 $key = self::SENDING_METHOD_ID.'_api_key';
142
143 $credentials = [
144 $key => $sendingMethodListParams[$key] ?? $this->config->get($key, ''),
145 ];
146 }
147
148 public function onAcymGetCreditRemainingSendingMethod(&$html, $reloading = false)
149 {
150 $sendingMethod = $this->config->get('mailer_method', '');
151 if (empty($sendingMethod) || $sendingMethod != self::SENDING_METHOD_ID) return;
152
153 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
154
155 $response = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'user/credits', [], $headers);
156
157 if (!empty($response['errors'])) {
158 $html = acym_translationSprintf(
159 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION',
160 self::SENDING_METHOD_NAME,
161 acym_translation('ACYM_GETTING_REMAINING_CREDITS'),
162 $response['errors'][0]['message']
163 );
164
165 return;
166 }
167
168 if (!isset($response['remain']) || !isset($response['total'])) {
169
170 $html = acym_translationSprintf(
171 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION',
172 self::SENDING_METHOD_NAME,
173 acym_translation('ACYM_GETTING_REMAINING_CREDITS'),
174 acym_translation('ACYM_CANT_RETRIEVE_CREDITS')
175 );
176 } else {
177 $html = acym_translationSprintf(
178 'ACYM_SENDING_METHOD_X_OF_X_UNITY',
179 self::SENDING_METHOD_NAME,
180 $response['remain'],
181 $response['total'],
182 '<span class="acym_not_bold">'.acym_translation('ACYM_CREDITS_REMAINING').'</span>'
183 );
184 }
185 }
186
187 public function getHeadersSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
188 {
189 if (empty($credentials)) {
190 $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
191 }
192
193 return [
194 'Authorization: Bearer '.$credentials[self::SENDING_METHOD_ID.'_api_key'],
195 ];
196 }
197
198 public function onAcymSendingMethodOptions(&$data)
199 {
200 $data['embedImage'][self::SENDING_METHOD_ID] = false;
201 }
202
203 public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
204 {
205 if ($method != self::SENDING_METHOD_ID) return;
206
207 if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
208 $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
209 if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['sendgrid']) || (!empty($wpMailSmtpSetting['sendgrid'] && empty($wpMailSmtpSetting['sendgrid']['api_key'])))) {
210 return;
211 }
212
213 $data['sendgrid_api_key'] = $wpMailSmtpSetting['sendgrid']['api_key'];
214 }
215 }
216 }
217