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

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

260 lines 10.0 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 use AcyMailing\Helpers\MailerHelper;
5
6 class plgAcymGoogle extends AcymPlugin
7 {
8 const SENDING_METHOD_ID = 'google';
9 const SENDING_METHOD_NAME = 'Google';
10
11 public function __construct()
12 {
13 parent::__construct();
14 $this->pluginDescription->name = acym_translation('ACYM_GOOGLE');
15 }
16
17 public function onAcymGetSendingMethods(&$data, $isMailer = false)
18 {
19 if ($isMailer) {
20 return;
21 }
22
23 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
24 'name' => $this->pluginDescription->name,
25 'image' => ACYM_IMAGES.'mailers/google.svg',
26 ];
27 }
28
29 public function onAcymGetSendingMethodsHtmlSetting(&$data)
30 {
31 $username = $this->config->get('google_username', $this->config->get('from_email'));
32 $setupDocumentation = ACYM_DOCUMENTATION.'setup/configuration/mail-configuration/set-up-oauth-2.0';
33 $redirectUrl = trim($this->config->get('google_redirect_url', acym_baseURI()));
34
35 $refreshToken = $this->config->get('google_refresh_token');
36 $refreshTokenExpiration = $this->config->get('google_refresh_token_expiration');
37 $mustAuthenticate = empty($refreshToken) || (!empty($refreshTokenExpiration) && $refreshTokenExpiration < time());
38
39 $mailerHelper = new MailerHelper();
40
41 ob_start();
42 ?>
43 <div class="send_settings grid-x cell large-6 xlarge-5 xxlarge-4 margin-auto" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
44 <div class="acym_port_465_closed is-hidden">
45 <?php acym_display(acym_translation('ACYM_PORT_NEEDED'), 'error', false); ?>
46 </div>
47 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
48 <label for="google_username" class="cell">
49 <?php echo acym_translation('ACYM_SMTP_USERNAME').acym_info(['textShownInTooltip' => 'ACYM_SMTP_USERNAME_DESC']); ?>
50 </label>
51 <input id="google_username"
52 class="cell"
53 type="text"
54 name="config[google_username]"
55 value="<?php echo acym_escape($username); ?>">
56 </div>
57 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
58 <label for="google_client_id" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_ID', $setupDocumentation, false); ?></label>
59 <input id="google_client_id"
60 class="cell"
61 type="text"
62 name="config[google_client_id]"
63 value="<?php echo acym_escape($this->config->get('google_client_id')); ?>">
64 </div>
65 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
66 <label for="google_client_secret" class="cell"><?php echo acym_externalLink('ACYM_SMTP_CLIENT_SECRET', $setupDocumentation, false); ?></label>
67 <input id="google_client_secret"
68 class="cell"
69 type="text"
70 name="config[google_client_secret]"
71 value="<?php echo acym_escape($this->config->get('google_client_secret')); ?>">
72 </div>
73 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
74 <label for="google_redirect_url" class="cell"><?php echo acym_translation('ACYM_SMTP_REDIRECT_URL'); ?></label>
75 <input id="google_redirect_url"
76 class="cell"
77 type="text"
78 name="config[google_redirect_url]"
79 value="<?php echo acym_escape($redirectUrl); ?>">
80 </div>
81 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
82 <?php if ($mustAuthenticate) { ?>
83 <button data-before-action="configSave"
84 data-task="loginForOAuth2Smtp"
85 class="button acy_button_submit margin-next-1">
86 <?php echo acym_translation('ACYM_AUTHENTICATE'); ?>
87 </button>
88 <?php echo $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp', false); ?>
89 <?php } else { ?>
90 <button data-before-action="configSave"
91 data-task="logoutForOAuth2Smtp"
92 class="button acy_button_submit button-secondary margin-bottom-1">
93 <?php echo acym_translation('ACYM_REVOKE_PERMISSIONS'); ?>
94 </button>
95 <?php
96 if (!empty($refreshTokenExpiration)) {
97 echo acym_translationSprintf('ACYM_AUTHENTICATION_WILL_EXPIRE', acym_date($refreshTokenExpiration, 'ACYM_DATE_FORMAT_LC2'));
98 }
99 ?>
100 <?php } ?>
101 </div>
102 </div>
103 <?php
104 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
105 }
106
107 public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method)
108 {
109 if ($method !== self::SENDING_METHOD_ID) {
110 return;
111 }
112
113 if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') {
114 $wpMailSmtpSetting = get_option('wp_mail_smtp', '');
115
116 if (empty($wpMailSmtpSetting['gmail'])) {
117 return;
118 }
119
120 $data['google_username'] = $wpMailSmtpSetting['gmail']['user_details']['email'] ?? '';
121 $data['google_client_id'] = $wpMailSmtpSetting['gmail']['client_id'] ?? '';
122 $data['google_client_secret'] = $wpMailSmtpSetting['gmail']['client_secret'] ?? '';
123 }
124 }
125
126 public function onAcymOauthAuthenticate(string &$consentUrl, bool $isSmtp): void
127 {
128 if ($isSmtp) {
129 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
130 return;
131 }
132
133 $clientId = $this->config->get('google_client_id');
134 $clientSecret = $this->config->get('google_client_secret');
135 $redirectUrl = $this->config->get('google_redirect_url');
136 } else {
137 if ($this->config->get('bounce_server') !== 'imap.gmail.com') {
138 return;
139 }
140
141 $clientId = $this->config->get('bounce_client_id');
142 $clientSecret = $this->config->get('bounce_client_secret');
143 $redirectUrl = acym_baseURI();
144 }
145
146 if (empty($clientId) || empty($clientSecret) || empty($redirectUrl)) {
147 acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
148
149 return;
150 }
151
152 $consentUrl = 'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&';
153 $consentUrl .= 'client_id='.urlencode($clientId);
154 $consentUrl .= '&response_type=code';
155 $consentUrl .= '&redirect_uri='.urlencode($redirectUrl);
156 $consentUrl .= '&scope='.urlencode('https://mail.google.com/');
157 $consentUrl .= '&state=acymailing'.($isSmtp ? 'smtp' : 'bounce');
158 }
159
160 public function onAcymOauthCredentialsCreation(bool $isSmtp, string $code): void
161 {
162 if ($isSmtp) {
163 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
164 return;
165 }
166
167 $clientId = trim($this->config->get('google_client_id'));
168 $clientSecret = trim($this->config->get('google_client_secret'));
169 $redirectUrl = trim($this->config->get('google_redirect_url', acym_baseURI()));
170 } else {
171 if ($this->config->get('bounce_server') !== 'imap.gmail.com') {
172 return;
173 }
174
175 $clientId = trim($this->config->get('bounce_client_id'));
176 $clientSecret = trim($this->config->get('bounce_client_secret'));
177 $redirectUrl = acym_baseURI();
178 }
179
180 if (empty($clientId) || empty($clientSecret)) {
181 acym_enqueueMessage(acym_translation('ACYM_PLEASE_FILL_CLIENT_ID_SECRET'), 'error', false);
182
183 return;
184 }
185
186 $response = acym_makeCurlCall(
187 'https://oauth2.googleapis.com/token',
188 [
189 'method' => 'POST',
190 'data' => [
191 'grant_type' => 'authorization_code',
192 'code' => $code,
193 'client_id' => $clientId,
194 'client_secret' => $clientSecret,
195 'redirect_uri' => $redirectUrl,
196 ],
197 ]
198 );
199
200 acym_logError('Response from OAuth call: '.json_encode($response), self::SENDING_METHOD_ID);
201
202 if (!empty($response['error'])) {
203 acym_enqueueMessage(acym_translationSprintf('ACYM_SMTP_OAUTH_ERROR', $response['error']), 'error', false);
204
205 return;
206 }
207
208 $expiringTime = time() + (int)$response['expires_in'];
209
210 if (!empty($response['refresh_token_expires_in'])) {
211 $refreshExpiringTime = time() + (int)$response['refresh_token_expires_in'];
212
213 acym_enqueueMessage(
214 acym_translationSprintf(
215 'ACYM_OAUTH_TEMPORARY',
216 acym_date($refreshExpiringTime, 'ACYM_DATE_FORMAT_LC2')
217 ),
218 'info',
219 false
220 );
221 } else {
222 $refreshExpiringTime = 0;
223 acym_enqueueMessage(acym_translation('ACYM_SMTP_OAUTH_OK'), 'info');
224 }
225
226 if ($isSmtp) {
227 $newConfig = [
228 'google_access_token' => $response['access_token'],
229 'google_access_token_expiration' => $expiringTime,
230 'google_refresh_token' => $response['refresh_token'],
231 'google_refresh_token_expiration' => $refreshExpiringTime,
232 ];
233 } else {
234 $newConfig = [
235 'bounce_access_token' => $response['access_token'],
236 'bounce_access_token_expiration' => $expiringTime,
237 'bounce_refresh_token' => $response['refresh_token'],
238 'bounce_refresh_token_expiration' => $refreshExpiringTime,
239 ];
240 }
241
242 $this->config->saveConfig($newConfig);
243 acym_config(true);
244 }
245
246 public function onAcymOauthRevoke(): void
247 {
248 if ($this->config->get('mailer_method') !== self::SENDING_METHOD_ID) {
249 return;
250 }
251
252 $this->config->saveConfig(
253 [
254 'google_refresh_token' => '',
255 'google_refresh_token_expiration' => '',
256 ]
257 );
258 }
259 }
260