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/outlook/plugin.php +0 -285 10.2.2 → 11.1.1 View file →
@@ -1,285 +1,0 @@
1 -<?php
2 -
3 -use AcyMailing\Core\AcymPlugin;
4 -
5 -class plgAcymOutlook extends AcymPlugin
6 -{
7 - const SENDING_METHOD_ID = 'outlook';
8 - const SENDING_METHOD_NAME = 'Outlook';
9 - const SCOPE_SMTP = 'openid offline_access https://outlook.office.com/SMTP.Send';
10 - const SCOPE_IMAP = 'openid offline_access https://outlook.office.com/IMAP.AccessAsUser.All';
11 -
12 - public function __construct()
13 - {
14 - parent::__construct();
15 - $this->pluginDescription->name = acym_translation('ACYM_OUTLOOK');
16 - }
17 -
18 - public function onAcymGetSendingMethods(&$data, $isMailer = false)
19 - {
20 - if ($isMailer) {
21 - return;
22 - }
23 -
24 - $data['sendingMethods'][self::SENDING_METHOD_ID] = [
25 - 'name' => $this->pluginDescription->name,
26 - 'image' => ACYM_IMAGES.'mailers/outlook.svg',
27 - ];
28 - }
29 -
30 - public function onAcymGetSendingMethodsHtmlSetting(&$data)
31 - {
32 - $username = $this->config->get('outlook_username', $this->config->get('from_email'));
33 - $setupDocumentation = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0';
34 - $redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
35 -
36 - $refreshToken = $this->config->get('outlook_refresh_token');
37 - $refreshTokenExpiration = $this->config->get('outlook_refresh_token_expiration');
38 - $mustAuthenticate = empty($refreshToken) || (!empty($refreshTokenExpiration) && $refreshTokenExpiration < time());
39 -
40 - ob_start();
41 - ?>
42 - <div class="send_settings grid-x cell large-6 xlarge-5 xxlarge-4 margin-auto" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
43 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
44 - <label for="outlook_username" class="cell"><?php echo acym_translation('ACYM_SMTP_USERNAME').acym_info('ACYM_SMTP_USERNAME_DESC'); ?></label>
45 - <input id="outlook_username"
46 - class="cell"
47 - type="text"
48 - name="config[outlook_username]"
49 - value="<?php echo acym_escape($username); ?>">
50 - </div>
51 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
52 - <label for="outlook_tenant" class="cell"><?php echo acym_externalLink('ACYM_TENANT', $setupDocumentation, false); ?></label>
53 - <?php
54 - $value = $this->config->get('outlook_tenant');
55 - echo acym_select(
56 - [
57 - 'common' => 'ACYM_ANY_ACCOUNT_TYPE',
58 - 'consumers' => 'ACYM_MICROSOFT_ACCOUNTS',
59 - 'organizations' => 'ACYM_ORGANIZATIONS',
60 - ],
61 - 'config[outlook_tenant]',
62 - empty($value) ? 'consumers' : $value,
63 - [
64 - 'class' => 'acym__select',
65 - ],
66 - '',
67 - '',
68 - '',
69 - true
70 - );
71 - ?>
72 - </div>
73 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
74 - <label for="outlook_client_id" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_ID', $setupDocumentation, false); ?></label>
75 - <input id="outlook_client_id"
76 - class="cell"
77 - type="text"
78 - name="config[outlook_client_id]"
79 - value="<?php echo acym_escape($this->config->get('outlook_client_id')); ?>">
80 - </div>
81 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
82 - <label for="outlook_client_secret" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_SECRET', $setupDocumentation, false); ?></label>
83 - <input id="outlook_client_secret"
84 - class="cell"
85 - type="text"
86 - name="config[outlook_client_secret]"
87 - value="<?php echo acym_escape($this->config->get('outlook_client_secret')); ?>">
88 - </div>
89 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
90 - <label for="outlook_redirect_url" class="cell"><?php echo acym_translation('ACYM_SMTP_REDIRECT_URL'); ?></label>
91 - <input id="outlook_redirect_url"
92 - class="cell"
93 - type="text"
94 - name="config[outlook_redirect_url]"
95 - value="<?php echo acym_escape($redirectUrl); ?>">
96 - </div>
97 - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
98 - <?php if ($mustAuthenticate) { ?>
99 - <button acym-data-before="jQuery.acymConfigSave();"
100 - data-task="loginForOAuth2Smtp"
101 - class="button acy_button_submit margin-right-1">
102 - <?php echo acym_translation('ACYM_AUTHENTICATE'); ?>
103 - </button>
104 - <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp', false); ?>
105 - <?php } else { ?>
106 - <button acym-data-before="jQuery.acymConfigSave();"
107 - data-task="logoutForOAuth2Smtp"
108 - class="button acy_button_submit button-secondary margin-bottom-1">
109 - <?php echo acym_translation('ACYM_REVOKE_PERMISSIONS'); ?>
110 - </button>
111 - <?php
112 - if (!empty($refreshTokenExpiration)) {
113 - echo acym_translationSprintf('ACYM_AUTHENTICATION_WILL_EXPIRE', acym_date($refreshTokenExpiration, 'ACYM_DATE_FORMAT_LC2'));
114 - }
115 - ?>
116 - <?php } ?>
117 - </div>
118 - </div>
119 - <?php
120 - $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
121 - }
122 -
123 - public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
124 - {
125 - if ($method !== self::SENDING_METHOD_ID) {
126 - return;
127 - }
128 -
129 - if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
130 - $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
131 -
132 - if (empty($wpMailSmtpSetting['outlook'])) {
133 - return;
134 - }
135 -
136 - $data['outlook_client_id'] = $wpMailSmtpSetting['outlook']['client_id'] ?? '';
137 - $data['outlook_client_secret'] = $wpMailSmtpSetting['outlook']['client_secret'] ?? '';
138 - }
139 - }
140 -
141 - public function onAcymOauthAuthenticate(string &$consentUrl, bool $isSmtp): void
142 - {
143 - if ($isSmtp) {
144 - if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
145 - return;
146 - }
147 -
148 - $clientId = $this->config->get('outlook_client_id');
149 - $clientSecret = $this->config->get('outlook_client_secret');
150 - $tenant = $this->config->get('outlook_tenant');
151 - $redirectUrl = $this->config->get('outlook_redirect_url');
152 - $scope = self::SCOPE_SMTP;
153 - } else {
154 - if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
155 - return;
156 - }
157 -
158 - $clientId = $this->config->get('bounce_client_id');
159 - $clientSecret = $this->config->get('bounce_client_secret');
160 - $tenant = $this->config->get('bounce_tenant');
161 - $redirectUrl = acym_baseURI();
162 - $scope = self::SCOPE_IMAP;
163 - }
164 -
165 - if (empty($clientId) || empty($clientSecret) || empty($redirectUrl)) {
166 - acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
167 -
168 - return;
169 - }
170 -
171 - $consentUrl = 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/authorize';
172 - $consentUrl .= '?response_mode=query';
173 - $consentUrl .= '&client_id='.urlencode($clientId);
174 - $consentUrl .= '&prompt=consent';
175 - $consentUrl .= '&response_type=code';
176 - $consentUrl .= '&redirect_uri='.urlencode($redirectUrl);
177 - $consentUrl .= '&scope='.urlencode($scope);
178 - $consentUrl .= '&state=acymailing'.($isSmtp ? 'smtp' : 'bounce');
179 - }
180 -
181 - public function onAcymOauthCredentialsCreation(bool $isSmtp, string $code): void
182 - {
183 - if ($isSmtp) {
184 - if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
185 - return;
186 - }
187 -
188 - $clientId = trim($this->config->get('outlook_client_id'));
189 - $clientSecret = trim($this->config->get('outlook_client_secret'));
190 - $tenant = trim($this->config->get('outlook_tenant', 'consumers'));
191 - $redirectUrl = trim($this->config->get('outlook_redirect_url', acym_baseURI()));
192 - $scope = self::SCOPE_SMTP;
193 - } else {
194 - if ($this->config->get('bounce_server') !== 'outlook.office365.com') {
195 - return;
196 - }
197 -
198 - $clientId = trim($this->config->get('bounce_client_id'));
199 - $clientSecret = trim($this->config->get('bounce_client_secret'));
200 - $tenant = trim($this->config->get('bounce_tenant', 'consumers'));
201 - $redirectUrl = acym_baseURI();
202 - $scope = self::SCOPE_IMAP;
203 - }
204 -
205 - if (empty($clientId) || empty($clientSecret)) {
206 - acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
207 -
208 - return;
209 - }
210 -
211 - $response = acym_makeCurlCall(
212 - 'https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token',
213 - [
214 - 'method' => 'POST',
215 - 'data' => [
216 - 'grant_type' => 'authorization_code',
217 - 'code' => $code,
218 - 'client_id' => $clientId,
219 - 'client_secret' => $clientSecret,
220 - 'redirect_uri' => $redirectUrl,
221 - 'scope' => $scope,
222 - ],
223 - ]
224 - );
225 -
226 - acym_logError('Response from OAuth call: '.json_encode($response), self::SENDING_METHOD_ID);
227 -
228 - if (!empty($response['error'])) {
229 - acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false);
230 -
231 - return;
232 - }
233 -
234 - $expiringTime = time() + (int)$response['expires_in'];
235 -
236 - if (!empty($response['refresh_token_expires_in'])) {
237 - $refreshExpiringTime = time() + (int)$response['refresh_token_expires_in'];
238 -
239 - acym_enqueueMessage(
240 - acym_translationSprintf(
241 - 'ACYM_OAUTH_TEMPORARY',
242 - acym_date($refreshExpiringTime, 'ACYM_DATE_FORMAT_LC2')
243 - ),
244 - 'info',
245 - false
246 - );
247 - } else {
248 - $refreshExpiringTime = 0;
249 - acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info');
250 - }
251 -
252 - if ($isSmtp) {
253 - $newConfig = [
254 - 'outlook_access_token' => $response['access_token'],
255 - 'outlook_access_token_expiration' => $expiringTime,
256 - 'outlook_refresh_token' => $response['refresh_token'],
257 - 'outlook_refresh_token_expiration' => $refreshExpiringTime,
258 - ];
259 - } else {
260 - $newConfig = [
261 - 'bounce_access_token' => $response['access_token'],
262 - 'bounce_access_token_expiration' => $expiringTime,
263 - 'bounce_refresh_token' => $response['refresh_token'],
264 - 'bounce_refresh_token_expiration' => $refreshExpiringTime,
265 - ];
266 - }
267 -
268 - $this->config->save($newConfig);
269 - acym_config(true);
270 - }
271 -
272 - public function onAcymOauthRevoke(): void
273 - {
274 - if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
275 - return;
276 - }
277 -
278 - $this->config->save(
279 - [
280 - 'outlook_refresh_token' => '',
281 - 'outlook_refresh_token_expiration' => '',
282 - ]
283 - );
284 - }
285 -}