PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 10.3.0 10.4.0 All 60 releases
acymailing / back / dynamics / sendgrid / plugin.php

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

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