| @@ -1,223 +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 | - ?> | |
| 30 | - <div class="send_settings cell grid-x acym_vcenter" id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings"> | |
| 31 | - <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 32 | - <label class="cell shrink margin-right-1" for="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key"> | |
| 33 | - <?php echo esc_html( | |
| 34 | - acym_translationSprintf( | |
| 35 | - 'ACYM_SENDING_METHOD_API_KEY', | |
| 36 | - self::SENDING_METHOD_NAME | |
| 37 | - ) | |
| 38 | - ); ?> | |
| 39 | - </label> | |
| 40 | - <?php $this->getLinks('https://signup.sendgrid.com/', 'https://sendgrid.com/pricing/'); ?> | |
| 41 | - <input type="text" | |
| 42 | - id="<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_settings_api-key" | |
| 43 | - value="<?php echo esc_attr(empty($defaultApiKey) ? $this->config->get(self::SENDING_METHOD_ID.'_api_key') : $defaultApiKey); ?>" | |
| 44 | - name="config[<?php echo esc_attr(self::SENDING_METHOD_ID); ?>_api_key]" | |
| 45 | - class="cell margin-next-1 acym__configuration__mail__settings__text"> | |
| 46 | - <?php $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?> | |
| 47 | - <?php $this->getCopySettingsButton($data, self::SENDING_METHOD_ID, 'wp_mail_smtp'); ?> | |
| 48 | - </div> | |
| 49 | - </div> | |
| 50 | - <?php | |
| 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 | - //https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html | |
| 77 | - if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) return; | |
| 78 | - $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID, [], $sendingMethodListParams); | |
| 79 | - $headers[] = 'Content-Type: application/json'; | |
| 80 | - | |
| 81 | - $data = [ | |
| 82 | - 'personalizations' => [ | |
| 83 | - [ | |
| 84 | - 'to' => [ | |
| 85 | - [ | |
| 86 | - 'email' => $to['email'], | |
| 87 | - ], | |
| 88 | - ], | |
| 89 | - 'subject' => $mailerHelper->Subject, | |
| 90 | - ], | |
| 91 | - ], | |
| 92 | - 'from' => $from, | |
| 93 | - 'reply_to' => $reply_to, | |
| 94 | - 'content' => [ | |
| 95 | - [ | |
| 96 | - 'type' => 'text/html', | |
| 97 | - 'value' => $mailerHelper->Body, | |
| 98 | - ], | |
| 99 | - ], | |
| 100 | - ]; | |
| 101 | - | |
| 102 | - $mailHeaders = []; | |
| 103 | - | |
| 104 | - $unsubscribeHeaderKey = array_search('List-Unsubscribe', array_column($mailerHelper->CustomHeader, 0)); | |
| 105 | - if (!empty($unsubscribeHeaderKey)) { | |
| 106 | - $mailHeaders['List-Unsubscribe'] = $mailerHelper->CustomHeader[$unsubscribeHeaderKey][1]; | |
| 107 | - $mailHeaders['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click'; | |
| 108 | - } | |
| 109 | - | |
| 110 | - if (!empty($mailHeaders)) { | |
| 111 | - $data['headers'] = (object)$mailHeaders; | |
| 112 | - } | |
| 113 | - | |
| 114 | - if (!empty($bcc)) $data['personalizations'][0]['bcc'] = [['email' => $bcc[0][0]]]; | |
| 115 | - | |
| 116 | - | |
| 117 | - if (!empty($attachments)) { | |
| 118 | - $data['attachments'] = []; | |
| 119 | - foreach ($attachments as $key => $attachment) { | |
| 120 | - $data['attachments'][] = [ | |
| 121 | - 'content' => base64_encode(acym_fileGetContent($attachment[0])), | |
| 122 | - 'filename' => $attachment[1], | |
| 123 | - 'disposition' => 'attachment', | |
| 124 | - ]; | |
| 125 | - } | |
| 126 | - } | |
| 127 | - | |
| 128 | - $responseMailer = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'mail/send', $data, $headers, 'POST'); | |
| 129 | - | |
| 130 | - if (empty($responseMailer['errors'][0]['message'])) { | |
| 131 | - $response['error'] = false; | |
| 132 | - } else { | |
| 133 | - $response['error'] = true; | |
| 134 | - $response['message'] = $responseMailer['errors'][0]['message']; | |
| 135 | - } | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * @param array $credentials | |
| 140 | - * @param string $sendingMethod | |
| 141 | - * @param array $sendingMethodListParams this parameter is only used for the plugin sending method list | |
| 142 | - * | |
| 143 | - * @return void | |
| 144 | - */ | |
| 145 | - public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = []) | |
| 146 | - { | |
| 147 | - if ($sendingMethod !== self::SENDING_METHOD_ID) return; | |
| 148 | - | |
| 149 | - $key = self::SENDING_METHOD_ID.'_api_key'; | |
| 150 | - | |
| 151 | - $credentials = [ | |
| 152 | - $key => $sendingMethodListParams[$key] ?? $this->config->get($key, ''), | |
| 153 | - ]; | |
| 154 | - } | |
| 155 | - | |
| 156 | - public function onAcymGetCreditRemainingSendingMethod(&$html, $reloading = false) | |
| 157 | - { | |
| 158 | - $sendingMethod = $this->config->get('mailer_method', ''); | |
| 159 | - if (empty($sendingMethod) || $sendingMethod !== self::SENDING_METHOD_ID) return; | |
| 160 | - | |
| 161 | - $headers = $this->getHeadersSendingMethod(self::SENDING_METHOD_ID); | |
| 162 | - | |
| 163 | - $response = $this->callApiSendingMethod(self::SENDING_METHOD_API_URL.'user/credits', [], $headers); | |
| 164 | - | |
| 165 | - if (!empty($response['errors'])) { | |
| 166 | - $html = acym_translationSprintf( | |
| 167 | - 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION', | |
| 168 | - self::SENDING_METHOD_NAME, | |
| 169 | - acym_translation('ACYM_GETTING_REMAINING_CREDITS'), | |
| 170 | - $response['errors'][0]['message'] | |
| 171 | - ); | |
| 172 | - | |
| 173 | - return; | |
| 174 | - } | |
| 175 | - | |
| 176 | - if (!isset($response['remain']) || !isset($response['total'])) { | |
| 177 | - $html = acym_translationSprintf( | |
| 178 | - 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION', | |
| 179 | - self::SENDING_METHOD_NAME, | |
| 180 | - acym_translation('ACYM_GETTING_REMAINING_CREDITS'), | |
| 181 | - acym_translation('ACYM_CANT_RETRIEVE_CREDITS') | |
| 182 | - ); | |
| 183 | - } else { | |
| 184 | - $html = acym_translationSprintf( | |
| 185 | - 'ACYM_SENDING_METHOD_X_OF_X_UNITY', | |
| 186 | - self::SENDING_METHOD_NAME, | |
| 187 | - $response['remain'], | |
| 188 | - $response['total'], | |
| 189 | - '<span class="acym_not_bold">'.acym_translation('ACYM_CREDITS_REMAINING').'</span>' | |
| 190 | - ); | |
| 191 | - } | |
| 192 | - } | |
| 193 | - | |
| 194 | - public function getHeadersSendingMethod($sendingMethod, $credentials = [], $sendingMethodListParams = []) | |
| 195 | - { | |
| 196 | - if (empty($credentials)) { | |
| 197 | - $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod, $sendingMethodListParams); | |
| 198 | - } | |
| 199 | - | |
| 200 | - return [ | |
| 201 | - 'Authorization: Bearer '.$credentials[self::SENDING_METHOD_ID.'_api_key'], | |
| 202 | - ]; | |
| 203 | - } | |
| 204 | - | |
| 205 | - public function onAcymSendingMethodOptions(&$data) | |
| 206 | - { | |
| 207 | - $data['embedImage'][self::SENDING_METHOD_ID] = false; | |
| 208 | - } | |
| 209 | - | |
| 210 | - public function onAcymGetSettingsSendingMethodFromPlugin(&$data, $plugin, $method) | |
| 211 | - { | |
| 212 | - if ($method !== self::SENDING_METHOD_ID) return; | |
| 213 | - | |
| 214 | - if (ACYM_CMS === 'wordpress' && $plugin === 'wp_mail_smtp') { | |
| 215 | - $wpMailSmtpSetting = get_option('wp_mail_smtp', ''); | |
| 216 | - if (empty($wpMailSmtpSetting) || empty($wpMailSmtpSetting['sendgrid']) || (!empty($wpMailSmtpSetting['sendgrid'] && empty($wpMailSmtpSetting['sendgrid']['api_key'])))) { | |
| 217 | - return; | |
| 218 | - } | |
| 219 | - | |
| 220 | - $data['sendgrid_api_key'] = $wpMailSmtpSetting['sendgrid']['api_key']; | |
| 221 | - } | |
| 222 | - } | |
| 223 | -} | |