| @@ -1,0 +1,190 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +use AcyMailing\Types\DelayType; | |
| 4 | + | |
| 5 | +class SendinblueCredentials extends SendinblueClass | |
| 6 | +{ | |
| 7 | + /** | |
| 8 | + * @param array $credentials | |
| 9 | + * @param string $sendingMethod | |
| 10 | + * @param array $sendingMethodListParams this parameter is only used for the plugin sending method list | |
| 11 | + * | |
| 12 | + * @return void | |
| 13 | + */ | |
| 14 | + public function onAcymGetCredentialsSendingMethod(array &$credentials, string $sendingMethod, array $sendingMethodListParams = []) | |
| 15 | + { | |
| 16 | + if ($sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return; | |
| 17 | + | |
| 18 | + $key = plgAcymSendinblue::SENDING_METHOD_ID.'_api_key'; | |
| 19 | + | |
| 20 | + $credentials = [ | |
| 21 | + $key => $sendingMethodListParams[$key] ?? $this->config->get($key, ''), | |
| 22 | + ]; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function getHeadersSendingMethod($sendingMethod, $credentials = []): array | |
| 26 | + { | |
| 27 | + if (empty($credentials)) { | |
| 28 | + $this->onAcymGetCredentialsSendingMethod($credentials, $sendingMethod); | |
| 29 | + } | |
| 30 | + | |
| 31 | + return [ | |
| 32 | + 'api-key:'.$credentials[plgAcymSendinblue::SENDING_METHOD_ID.'_api_key'], | |
| 33 | + 'accept: application/json', | |
| 34 | + 'content-type: application/json', | |
| 35 | + ]; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function getSendingMethodsHtmlSetting(&$data) | |
| 39 | + { | |
| 40 | + $needValidation = $this->config->get('sendinblue_validation', 0); | |
| 41 | + $remindMe = json_decode($this->config->get('remindme', '[]'), true); | |
| 42 | + | |
| 43 | + if (intval($needValidation) === 1 && !in_array('sendinblue_validation', $remindMe)) { | |
| 44 | + $docUrl = ACYM_DOCUMENTATION.'external-sending-method/sendinblue#my-account-needs-validation'; | |
| 45 | + | |
| 46 | + $message = acym_translation('ACYM_SENDINBLUE_VALIDATION_NEEDED'); | |
| 47 | + $message .= '<br/><a href="'.$docUrl.'" target="_blank">'.$docUrl.'</a>'; | |
| 48 | + $message .= ' <a href="#" class="acym__do__not__remindme acym__do__not__remindme__info" title="sendinblue_validation">'; | |
| 49 | + $message .= acym_translation('ACYM_DO_NOT_REMIND_ME'); | |
| 50 | + $message .= '</a>'; | |
| 51 | + | |
| 52 | + $notification = [ | |
| 53 | + 'name' => 'sendinblue_validation', | |
| 54 | + 'removable' => 0, | |
| 55 | + ]; | |
| 56 | + acym_enqueueMessage($message, 'info', true, [$notification]); | |
| 57 | + } else { | |
| 58 | + acym_removeDashboardNotification('sendinblue_validation'); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $delayType = new DelayType(); | |
| 62 | + ob_start(); | |
| 63 | + $delayType->display( | |
| 64 | + 'config['.plgAcymSendinblue::SENDING_METHOD_ID.'_clean_frequency]', | |
| 65 | + $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_clean_frequency', 604800), | |
| 66 | + DelayType::TYPE_WEEKS_MONTHS | |
| 67 | + ); | |
| 68 | + $delay = ob_get_clean(); | |
| 69 | + $config = empty($data['tab']) ? $this->config : $data['tab']->config; | |
| 70 | + $defaultApiKey = $config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_api_key'); | |
| 71 | + ?> | |
| 72 | + <div class="send_settings cell grid-x acym_vcenter" id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings"> | |
| 73 | + <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings"> | |
| 74 | + <label class="cell shrink margin-right-1" for="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings_api-key"> | |
| 75 | + <?php echo esc_html(acym_translationSprintf('ACYM_SENDING_METHOD_API_KEY', plgAcymSendinblue::SENDING_METHOD_NAME)); ?> | |
| 76 | + </label> | |
| 77 | + <?php $this->getLinks('https://get.brevo.com/hbvmwg6onvve'); ?> | |
| 78 | + <input type="text" | |
| 79 | + id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings_api-key" | |
| 80 | + value="<?php echo esc_attr(empty($defaultApiKey) ? $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_api_key') : $defaultApiKey); ?>" | |
| 81 | + name="config[<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_api_key]" | |
| 82 | + class="cell margin-next-1 acym__configuration__mail__settings__text"> | |
| 83 | + <?php $this->getTestCredentialsSendingMethodButton(plgAcymSendinblue::SENDING_METHOD_ID); ?> | |
| 84 | + <?php $this->getCopySettingsButton($data, plgAcymSendinblue::SENDING_METHOD_ID, 'wp_mail_smtp'); ?> | |
| 85 | + <div class="cell grid-x margin-top-1 acym__sending__methods__synch"> | |
| 86 | + <button type="button" | |
| 87 | + sending-method-id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>" | |
| 88 | + class="acym__configuration__sending__synch__users cell shrink button button-secondary"> | |
| 89 | + <?php echo esc_html(acym_translation('ACYM_SYNCHRO_EXISTING_USERS')); ?> | |
| 90 | + </button> | |
| 91 | + <span class="acym__configuration__sending__method-icon cell shrink margin-left-1 acym_vcenter"></span> | |
| 92 | + <span class="acym__configuration__sending__method-synch__message cell shrink margin-left-1 acym_vcenter"></span> | |
| 93 | + </div> | |
| 94 | + <?php if (!$this->plugin->isLogFileEmpty()) { ?> | |
| 95 | + <div class="cell grid-x margin-top-1 acym__sending__methods__log"> | |
| 96 | + <?php | |
| 97 | + acym_modal( | |
| 98 | + acym_translation('ACYM_REPORT_SEE'), | |
| 99 | + '', | |
| 100 | + null, | |
| 101 | + [], | |
| 102 | + [ | |
| 103 | + 'class' => 'button', | |
| 104 | + 'data-ajax' => 'true', | |
| 105 | + 'data-iframe' => '&ctrl=configuration&task=seeLogs&filename='.$this->plugin->logFilename, | |
| 106 | + ] | |
| 107 | + ); | |
| 108 | + ?> | |
| 109 | + </div> | |
| 110 | + <?php } ?> | |
| 111 | + <div class="cell grid-x margin-top-1 acym_vcenter acym__sending__methods__clean__data"> | |
| 112 | + <?php echo wp_kses( | |
| 113 | + acym_translationSprintf('ACYM_CLEAN_DATA_ON_X_X', plgAcymSendinblue::SENDING_METHOD_NAME, $delay), | |
| 114 | + [ | |
| 115 | + 'input' => [ | |
| 116 | + 'class' => true, | |
| 117 | + 'type' => true, | |
| 118 | + 'id' => true, | |
| 119 | + 'value' => true, | |
| 120 | + 'onchange' => true, | |
| 121 | + 'min' => true, | |
| 122 | + ], | |
| 123 | + 'select' => [ | |
| 124 | + 'name' => true, | |
| 125 | + 'id' => true, | |
| 126 | + 'class' => true, | |
| 127 | + 'onchange' => true, | |
| 128 | + ], | |
| 129 | + 'option' => [ | |
| 130 | + 'value' => true, | |
| 131 | + 'selected' => true, | |
| 132 | + ], | |
| 133 | + ] | |
| 134 | + ); ?> | |
| 135 | + </div> | |
| 136 | + </div> | |
| 137 | + </div> | |
| 138 | + <?php | |
| 139 | + } | |
| 140 | + | |
| 141 | + public function testCredentialSendingMethod($sendingMethod, $credentials) | |
| 142 | + { | |
| 143 | + if ($sendingMethod == plgAcymSendinblue::SENDING_METHOD_ID) { | |
| 144 | + $headers = $this->getHeadersSendingMethod(plgAcymSendinblue::SENDING_METHOD_ID, $credentials); | |
| 145 | + $response = $this->callApiSendingMethod('account', [], $headers); | |
| 146 | + if (!empty($response['error_curl'])) { | |
| 147 | + acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false); | |
| 148 | + } elseif (!empty($response['code'])) { | |
| 149 | + $message = $response['code'] == 'unauthorized' | |
| 150 | + ? acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY') | |
| 151 | + : acym_translationSprintf( | |
| 152 | + 'ACYM_API_RETURN_THIS_ERROR', | |
| 153 | + $response['message'] | |
| 154 | + ); | |
| 155 | + acym_sendAjaxResponse($message, [], false); | |
| 156 | + } else { | |
| 157 | + acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT')); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + public function getCreditRemainingSendingMethod(&$html) | |
| 163 | + { | |
| 164 | + $sendingMethod = $this->config->get('mailer_method', ''); | |
| 165 | + if (empty($sendingMethod) || $sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return; | |
| 166 | + $apiKey = $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_api_key', ''); | |
| 167 | + if (empty($apiKey)) return; | |
| 168 | + | |
| 169 | + $headers = $this->getHeadersSendingMethod(plgAcymSendinblue::SENDING_METHOD_ID); | |
| 170 | + $response = $this->callApiSendingMethod('account', [], $headers); | |
| 171 | + | |
| 172 | + if (!empty($response['code']) || empty($response['plan'])) { | |
| 173 | + $html = acym_translationSprintf( | |
| 174 | + 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION', | |
| 175 | + plgAcymSendinblue::SENDING_METHOD_NAME, | |
| 176 | + acym_translation('ACYM_GETTING_REMAINING_CREDITS'), | |
| 177 | + $response['message'] | |
| 178 | + ); | |
| 179 | + | |
| 180 | + return; | |
| 181 | + } | |
| 182 | + | |
| 183 | + $html = acym_translationSprintf( | |
| 184 | + 'ACYM_SENDING_METHOD_X_UNITY', | |
| 185 | + plgAcymSendinblue::SENDING_METHOD_NAME, | |
| 186 | + $response['plan'][0]['credits'], | |
| 187 | + '<span class="acym_not_bold">'.acym_translation('ACYM_CREDITS_REMAINING').'</span>' | |
| 188 | + ); | |
| 189 | + } | |
| 190 | +} | |