PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 10.3.0 10.4.0 All 60 releases
acymailing / back / dynamics / sendinblue / credentials.php

credentials.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at back/dynamics/sendinblue/credentials.php

193 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ob_start();
72 ?>
73 <div class="send_settings cell grid-x acym_vcenter" id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings">
74 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
75 <label class="cell shrink margin-right-1" for="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings_api-key">
76 <?php echo esc_html(acym_translationSprintf('ACYM_SENDING_METHOD_API_KEY', plgAcymSendinblue::SENDING_METHOD_NAME)); ?>
77 </label>
78 <?php $this->getLinks('https://get.brevo.com/hbvmwg6onvve'); ?>
79 <input type="text"
80 id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_settings_api-key"
81 value="<?php echo esc_attr(empty($defaultApiKey) ? $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_api_key') : $defaultApiKey); ?>"
82 name="config[<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>_api_key]"
83 class="cell margin-next-1 acym__configuration__mail__settings__text">
84 <?php $this->getTestCredentialsSendingMethodButton(plgAcymSendinblue::SENDING_METHOD_ID); ?>
85 <?php $this->getCopySettingsButton($data, plgAcymSendinblue::SENDING_METHOD_ID, 'wp_mail_smtp'); ?>
86 <div class="cell grid-x margin-top-1 acym__sending__methods__synch">
87 <button type="button"
88 sending-method-id="<?php echo esc_attr(plgAcymSendinblue::SENDING_METHOD_ID); ?>"
89 class="acym__configuration__sending__synch__users cell shrink button button-secondary">
90 <?php echo esc_html(acym_translation('ACYM_SYNCHRO_EXISTING_USERS')); ?>
91 </button>
92 <span class="acym__configuration__sending__method-icon cell shrink margin-left-1 acym_vcenter"></span>
93 <span class="acym__configuration__sending__method-synch__message cell shrink margin-left-1 acym_vcenter"></span>
94 </div>
95 <?php if (!$this->plugin->isLogFileEmpty()) { ?>
96 <div class="cell grid-x margin-top-1 acym__sending__methods__log">
97 <?php
98 acym_modal(
99 acym_translation('ACYM_REPORT_SEE'),
100 '',
101 null,
102 [],
103 [
104 'class' => 'button',
105 'data-ajax' => 'true',
106 'data-iframe' => '&ctrl=configuration&task=seeLogs&filename='.$this->plugin->logFilename,
107 ]
108 );
109 ?>
110 </div>
111 <?php } ?>
112 <div class="cell grid-x margin-top-1 acym_vcenter acym__sending__methods__clean__data">
113 <?php echo wp_kses(
114 acym_translationSprintf('ACYM_CLEAN_DATA_ON_X_X', plgAcymSendinblue::SENDING_METHOD_NAME, $delay),
115 [
116 'input' => [
117 'class' => true,
118 'type' => true,
119 'id' => true,
120 'value' => true,
121 'onchange' => true,
122 'min' => true,
123 ],
124 'select' => [
125 'name' => true,
126 'id' => true,
127 'class' => true,
128 'onchange' => true,
129 ],
130 'option' => [
131 'value' => true,
132 'selected' => true,
133 ],
134 ]
135 ); ?>
136 </div>
137 </div>
138 </div>
139 <?php
140 $data['sendingMethodsHtmlSettings'][plgAcymSendinblue::SENDING_METHOD_ID] = ob_get_clean();
141 }
142
143 public function testCredentialSendingMethod($sendingMethod, $credentials)
144 {
145 if ($sendingMethod == plgAcymSendinblue::SENDING_METHOD_ID) {
146 $headers = $this->getHeadersSendingMethod(plgAcymSendinblue::SENDING_METHOD_ID, $credentials);
147 $response = $this->callApiSendingMethod('account', [], $headers);
148 if (!empty($response['error_curl'])) {
149 acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false);
150 } elseif (!empty($response['code'])) {
151 $message = $response['code'] == 'unauthorized'
152 ? acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY')
153 : acym_translationSprintf(
154 'ACYM_API_RETURN_THIS_ERROR',
155 $response['message']
156 );
157 acym_sendAjaxResponse($message, [], false);
158 } else {
159 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
160 }
161 }
162 }
163
164 public function getCreditRemainingSendingMethod(&$html)
165 {
166 $sendingMethod = $this->config->get('mailer_method', '');
167 if (empty($sendingMethod) || $sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return;
168 $apiKey = $this->config->get(plgAcymSendinblue::SENDING_METHOD_ID.'_api_key', '');
169 if (empty($apiKey)) return;
170
171 $headers = $this->getHeadersSendingMethod(plgAcymSendinblue::SENDING_METHOD_ID);
172 $response = $this->callApiSendingMethod('account', [], $headers);
173
174 if (!empty($response['code']) || empty($response['plan'])) {
175 $html = acym_translationSprintf(
176 'ACYM_SENDING_METHOD_ERROR_WHILE_ACTION',
177 plgAcymSendinblue::SENDING_METHOD_NAME,
178 acym_translation('ACYM_GETTING_REMAINING_CREDITS'),
179 $response['message']
180 );
181
182 return;
183 }
184
185 $html = acym_translationSprintf(
186 'ACYM_SENDING_METHOD_X_UNITY',
187 plgAcymSendinblue::SENDING_METHOD_NAME,
188 $response['plan'][0]['credits'],
189 '<span class="acym_not_bold">'.acym_translation('ACYM_CREDITS_REMAINING').'</span>'
190 );
191 }
192 }
193