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 / mailgun / plugin.php

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

230 lines 9.7 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 plgAcymMailgun extends AcymPlugin
6 {
7 const SENDING_METHOD_ID = 'mailgun';
8 const SENDING_METHOD_NAME = 'Mailgun';
9 const SENDING_METHOD_API_URL_US = 'https://api.mailgun.net/v3/';
10 const SENDING_METHOD_API_URL_EU = 'https://api.eu.mailgun.net/v3/';
11
12 public $sendingMethodApiUrl;
13
14 public function __construct()
15 {
16 parent::__construct();
17 $this->pluginDescription->name = self::SENDING_METHOD_NAME;
18 }
19
20 public function onAcymGetSendingMethods(&$data, $isMailer = false)
21 {
22 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
23 'name' => $this->pluginDescription->name,
24 'image' => ACYM_IMAGES.'mailers/mailgun.svg',
25 'image_class' => 'acym__selection__card__image__mailgun',
26 ];
27 }
28
29 public function onAcymGetSendingMethodsHtmlSetting(&$data)
30 {
31 $regions = [
32 'us' => acym_translation('ACYM_US'),
33 'eu' => acym_translation('ACYM_EU'),
34 ];
35 $config = empty($data['tab']) ? $this->config : $data['tab']->config;
36 $defaultDomain = $config->get(self::SENDING_METHOD_ID.'_api_domain');
37 $defaultApiKey = $config->get(self::SENDING_METHOD_ID.'_api_key');
38 ob_start();
39 ?>
40 <div class="send_settings cell grid-x acym_vcenter" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
41 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
42 <label class="cell large-3 medium-4 margin-right-1">
43 <?php
44 echo acym_translationSprintf('ACYM_SENDING_METHOD_API_REGION', self::SENDING_METHOD_NAME);
45 echo acym_info(acym_translationSprintf('ACYM_SENDING_METHOD_API_REGION_DESC', self::SENDING_METHOD_NAME)); ?>
46 </label>
47 <?php
48 echo acym_radio(
49 $regions,
50 'config['.self::SENDING_METHOD_ID.'_api_region]',
51 $this->config->get(self::SENDING_METHOD_ID.'_api_region', 'us')
52 );
53 ?>
54 </div>
55 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
56 <label class="cell" for="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-domain">
57 <?php echo acym_translationSprintf(
58 'ACYM_SENDING_METHOD_API_DOMAIN',
59 self::SENDING_METHOD_NAME
60 ); ?>
61 </label>
62 <input type="text"
63 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-domain"
64 value="<?php echo empty($defaultDomain) ? $this->config->get(self::SENDING_METHOD_ID.'_api_domain') : $defaultDomain; ?>"
65 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_domain]"
66 class="cell acym__configuration__mail__settings__text">
67 </div>
68 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
69 <label class="cell shrink margin-right-1" for="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key">
70 <?php echo acym_translationSprintf(
71 'ACYM_SENDING_METHOD_API_KEY',
72 self::SENDING_METHOD_NAME
73 ); ?>
74 </label>
75 <?php echo $this->getLinks('https://signup.mailgun.com/new/signup', 'https://www.mailgun.com/pricing/'); ?>
76 <input type="text"
77 id="<?php echo self::SENDING_METHOD_ID; ?>_settings_api-key"
78 value="<?php echo empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey; ?>"
79 name="config[<?php echo self::SENDING_METHOD_ID; ?>_api_key]"
80 class="cell acym__configuration__mail__settings__text">
81 <?php echo $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
82 <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?>
83 </div>
84 </div>
85 <?php
86 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
87 }
88
89 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
90 {
91 if ($sendingMethod !== self::SENDING_METHOD_ID) return;
92
93 $this->setSendingMethodApiUrl($credentials);
94 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
95 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, $credentials);
96 $data = [
97 'from' => $this->config->get('from_email'),
98 'to' => acym_currentUserEmail(),
99 'subject' => 'Test email',
100 'html' => 'Test email body',
101 'o:testmode' => true,
102 ];
103
104 $response = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
105
106 if (empty($response)) {
107 acym_sendAjaxResponse(acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY'), [], false);
108 } else {
109 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
110 }
111 }
112
113 public function onAcymSendEmail(&$response, $mailerHelper, $to, $from, $replyTo, $bcc = [], $attachments = [], $sendingMethodListParams = [])
114 {
115 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return;
116
117 $this->setSendingMethodApiUrl();
118 $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID);
119 $authentication = $this->getAuthenticationSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams);
120 $fromData = $from['email'];
121 $replyToData = $replyTo['email'] ?? '';
122 $toData = $to['email'];
123 if ($this->config->get('add_names', 1) == 1) {
124 if (!empty($from['name'])) $fromData = $from['name'].' <'.$fromData.'>';
125 if (!empty($replyTo['name']) && !empty($replyToData)) $replyToData = $replyTo['name'].' <'.$replyToData.'>';
126 if (!empty($to['name'])) $toData = $to['name'].' <'.$toData.'>';
127 }
128 $data = [
129 'from' => $fromData,
130 'to' => $toData,
131 'subject' => $mailerHelper->Subject,
132 'html' => $mailerHelper->Body,
133 ];
134
135 if (!empty($replyToData)) {
136 $data['h:Reply-To'] = $replyToData;
137 }
138
139 if (!empty($bcc)) {
140 foreach ($bcc as $key => $bccEmail) {
141 $data['bcc['.$key.']'] = $bccEmail[0];
142 }
143 }
144
145 if (!empty($attachments)) {
146 foreach ($attachments as $key => $attachment) {
147 $data['attachment['.$key.']'] = curl_file_create($attachment[0]);
148 }
149 }
150
151 $responseMailer = $this->callApiSendingMethod($this->sendingMethodApiUrl.'messages', $data, $headers, 'POST', $authentication, true);
152
153 if (empty($responseMailer['message']) || empty($responseMailer['id']) || $responseMailer['message'] != 'Queued. Thank you.') {
154 $response['error'] = true;
155 $response['message'] = $responseMailer['message'];
156 } else {
157 $response['error'] = false;
158 }
159 }
160
161 private function setSendingMethodApiUrl($credentials = [])
162 {
163 if (empty($credentials)) {
164 $this->onAcymGetCredentialsSendingMethod($credentials, self::SENDING_METHOD_ID);
165 }
166
167 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_US;
168 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_region']) && $credentials[self::SENDING_METHOD_ID.'_api_region'] === 'eu') {
169 $this->sendingMethodApiUrl = self::SENDING_METHOD_API_URL_EU;
170 }
171
172 if (!empty($credentials[self::SENDING_METHOD_ID.'_api_domain'])) {
173 $this->sendingMethodApiUrl .= $credentials[self::SENDING_METHOD_ID.'_api_domain'].'/';
174 }
175 }
176
177 public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = [])
178 {
179 if ($sendingMethod != self::SENDING_METHOD_ID) return;
180
181 $apiKey = self::SENDING_METHOD_ID.'_api_key';
182 $domain = self::SENDING_METHOD_ID.'_api_domain';
183 $region = self::SENDING_METHOD_ID.'_api_region';
184
185 $credentials = [
186 $apiKey => $sendingMethodListParams[$apiKey] ?? $this->config->get($apiKey, ''),
187 $domain => $sendingMethodListParams[$domain] ?? $this->config->get($domain, ''),
188 $region => $sendingMethodListParams[$region] ?? $this->config->get($region, 'us'),
189 ];
190 }
191
192 public function getHeadersSendingMethod($sendingMethod, $credentials = [])
193 {
194 return ['content-type: multipart/form-data'];
195 }
196
197 public function getAuthenticationSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = [])
198 {
199 if (empty($credentials)) $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams);
200
201 return [
202 'name' => 'api',
203 'pwd' => $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['mailgun'])) return;
219
220 $settings = $wpMailSmtpSetting['mailgun'];
221
222 if (empty($settings['api_key']) || empty($settings['domain']) || empty($settings['region'])) return;
223
224 $data['mailgun_api_domain'] = $settings['domain'];
225 $data['mailgun_api_key'] = $settings['api_key'];
226 $data['mailgun_api_region'] = $settings['region'];
227 }
228 }
229 }
230