PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.2.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.2.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.2.2, at back/dynamics/google/plugin.php

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