PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.1
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
← All changes | back/dynamics/sendgrid/plugin.php +0 -215 10.10.2 → 11.1.1 View file →
@@ -1,215 +1,0 @@
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-next-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 - $html = acym_translationSprintf(
170 - 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION',
171 - self::SENDING_METHOD_NAME,
172 - acym_translation('ACYM_GETTING_REMAINING_CREDITS'),
173 - acym_translation('ACYM_CANT_RETRIEVE_CREDITS')
174 - );
175 - } else {
176 - $html = acym_translationSprintf(
177 - 'ACYM_SENDING_METHOD_X_OF_X_UNITY',
178 - self::SENDING_METHOD_NAME,
179 - $response['remain'],
180 - $response['total'],
181 - '<span class="acym_not_bold">'.acym_translation('ACYM_CREDITS_REMAINING').'</span>'
182 - );
183 - }
184 - }
185 -
186 - public function getHeadersSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
187 - {
188 - if (empty($credentials)) {
189 - $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
190 - }
191 -
192 - return [
193 - 'Authorization: Bearer '.$credentials[self::SENDING_METHOD_ID.'_api_key'],
194 - ];
195 - }
196 -
197 - public function onAcymSendingMethodOptions(&$data)
198 - {
199 - $data['embedImage'][self::SENDING_METHOD_ID] = false;
200 - }
201 -
202 - public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
203 - {
204 - if ($method !== self::SENDING_METHOD_ID) return;
205 -
206 - if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
207 - $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
208 - if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['sendgrid']) || (!empty($wpMailSmtpSetting['sendgrid'] && empty($wpMailSmtpSetting['sendgrid']['api_key'])))) {
209 - return;
210 - }
211 -
212 - $data['sendgrid_api_key'] = $wpMailSmtpSetting['sendgrid']['api_key'];
213 - }
214 - }
215 -}