PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.0.4
Brevo – Email, SMS, Web Push, Chat, and more. v3.0.4
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / inc / sib-api-manager.php
mailin / inc Last commit date
templates 5 years ago SendinblueApiClient.php 5 years ago function.wp_mail.php 5 years ago index.php 5 years ago mailin.php 5 years ago sendinblue.php 5 years ago sib-api-manager.php 5 years ago sib-form-preview.php 5 years ago sib-sms-code.php 5 years ago table-forms.php 5 years ago
sib-api-manager.php
893 lines
1 <?php
2 /**
3 * Manage Sendinblue API
4 *
5 * Use wp API transient to reduce loading time of API call
6 *
7 * @package SIB_API_Manager
8 */
9
10 if ( ! class_exists( 'SIB_API_Manager' ) ) {
11 /**
12 * Class SIB_API_Manager.
13 * Main API class for sendinblue module.
14 */
15 class SIB_API_Manager {
16
17 /** Transient delay time */
18 const DELAYTIME = HOUR_IN_SECONDS;
19
20 /**
21 * SIB_API_Manager constructor.
22 */
23 function __construct() {
24
25 }
26
27 /** Get account info */
28 public static function get_account_info() {
29 // get account's info.
30 $account_info = get_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ) );
31 if ( false === $account_info || false == $account_info ) {
32 $client = new SendinblueApiClient();
33 $account = $client->getAccount();
34 if ($client->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK && !empty($account['email'])) {
35 $account_email = $account['email'];
36
37 $account_info = array(
38 'account_email' => $account_email,
39 'account_user_name' => $account['firstName'] . ' ' . $account['lastName'],
40 'account_data' => $account['plan'],
41 );
42 } else {
43 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
44 }
45 set_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ), $account_info, self::DELAYTIME );
46 }
47 return $account_info;
48 }
49
50 /** Get smtp status */
51 public static function get_smtp_status() {
52 $status = get_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ) );
53 if ( false === $status || false == $status ) {
54 $client = new SendinblueApiClient();
55 $account = $client->getAccount();
56 $status = 'disabled';
57 if ($client->getLastResponseCode() == 200) {
58 $status = $account['relay']['enabled'] ? 'enabled' : 'disabled';
59 set_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ), $status, self::DELAYTIME );
60
61 // get Marketing Automation API key.
62 if ( isset( $account['marketingAutomation']['enabled'] ) && true == $account['marketingAutomation']['enabled'] ) {
63 $ma_key = $account['marketingAutomation']['key'];
64 } else {
65 $ma_key = '';
66 }
67 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
68 $general_settings['ma_key'] = $ma_key;
69 update_option( SIB_Manager::MAIN_OPTION_NAME, $general_settings );
70 }
71 }
72 return $status;
73 }
74
75 /** Get all attributes */
76 public static function get_attributes() {
77 // get attributes.
78 $attrs = get_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ) );
79
80 if ( false === $attrs || false == $attrs ) {
81 $mailin = new SendinblueApiClient();
82 $response = $mailin->getAttributes();
83 $attributes = $response['attributes'];
84 $attrs = array(
85 'attributes' => array(
86 'normal_attributes' => array(),
87 'category_attributes' => array(),
88 )
89 );
90
91 if ( count( $attributes ) > 0 ) {
92 foreach ($attributes as $key => $value) {
93 if ($value["category"] == "normal") {
94 $attrs['attributes']['normal_attributes'][] = $value;
95 }
96 elseif ($value["category"] == "category") {
97 $value["type"] = "category";
98 $attrs['attributes']['category_attributes'][] = $value;
99 }
100
101 }
102 }
103
104 set_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ), $attrs, self::DELAYTIME );
105 }
106
107 return $attrs;
108
109 }
110
111 /** Get all smtp templates */
112 public static function get_templates() {
113
114 // get templates.
115 $templates = get_transient( 'sib_template_' . md5( SIB_Manager::$access_key ) );
116
117 if ( false === $templates || false == $templates ) {
118 $mailin = new SendinblueApiClient();
119 $data = array(
120 'templateStatus' => true
121 );
122 $templates = $mailin->getEmailTemplates( $data );
123 $template_data = array();
124
125 if ( $mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK ) {
126
127 foreach ( $templates['templates'] as $template ) {
128 $is_dopt = 0;
129 if ( strpos( $template['htmlContent'], 'DOUBLEOPTIN' ) != false || strpos( $template['htmlContent'], 'doubleoptin' ) != false) {
130 $is_dopt = 1;
131 }
132 $template_data[] = array(
133 'id' => $template['id'],
134 'name' => $template['name'],
135 'is_dopt' => $is_dopt,
136 );
137
138 }
139 }
140 $templates = $template_data;
141 if ( count( $templates ) > 0 ) {
142 set_transient( 'sib_template_' . md5( SIB_Manager::$access_key ), $templates, self::DELAYTIME );
143 }
144 }
145
146 return $templates;
147 }
148
149 /** Get default list id after install */
150 public static function get_default_list_id() {
151 $lists = self::get_lists();
152 return strval( $lists[0]['id'] );
153 }
154
155 /** Get all lists */
156 public static function get_lists() {
157 // get lists.
158 $lists = get_transient( 'sib_list_' . md5( SIB_Manager::$access_key ) );
159 if ( false === $lists || false == $lists ) {
160
161 $mailin = new SendinblueApiClient();
162 $lists = array();
163 $list_data = $mailin->getAllLists();
164
165 if (!empty($list_data['lists'])) {
166 foreach ( $list_data['lists'] as $value ) {
167 if ( 'Temp - DOUBLE OPTIN' == $value['name'] ) {
168 $tempList = $value['id'];
169 update_option( SIB_Manager::TEMPLIST_OPTION_NAME, $tempList );
170 continue;
171 }
172 $lists[] = array(
173 'id' => $value['id'],
174 'name' => $value['name'],
175 );
176 }
177 }
178 }
179 if ( count( $lists ) > 0 ) {
180 set_transient( 'sib_list_' . md5( SIB_Manager::$access_key ), $lists, self::DELAYTIME );
181 }
182 return $lists;
183 }
184
185 /** Get all sender of sendinblue */
186 public static function get_sender_lists() {
187 $senders = get_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ) );
188 if ( false === $senders || false == $senders ) {
189 $mailin = new SendinblueApiClient();
190 $response = $mailin->getSenders();
191 $senders = array();
192 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
193 // reorder by id.
194 foreach ( $response['senders'] as $sender ) {
195 $senders[] = array(
196 'id' => $sender['id'],
197 'from_name' => $sender['name'],
198 'from_email' => $sender['email'],
199 );
200 }
201 }
202 if ( count( $senders ) > 0 ) {
203 set_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ), $senders, self::DELAYTIME );
204 }
205 }
206 return $senders;
207 }
208 /** Remove all transients */
209 public static function remove_transients() {
210 // remove all transients.
211 delete_transient( 'sib_list_' . md5( SIB_Manager::$access_key ) );
212 delete_transient( 'sib_totalusers_' . md5( SIB_Manager::$access_key ) );
213 delete_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ) );
214 delete_transient( 'sib_campaigns_' . md5( SIB_Manager::$access_key ) );
215 delete_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ) );
216 delete_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ) );
217 delete_transient( 'sib_template_' . md5( SIB_Manager::$access_key ) );
218 delete_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ) );
219 }
220
221 /**
222 * Send Identify User for MA
223 *
224 * @param array $data - data.
225 */
226 public static function identify_user( $data ) {
227 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
228 if (isset($general_settings['ma_key'])) {
229 try {
230 $event = new Sendinblue( $general_settings['ma_key'] );
231 $event->identify( $data );
232 } catch (Exception $exception) {
233 echo $exception->getMessage() . "\n";
234 }
235 }
236 }
237
238 /**
239 * Send email through Sendinblue
240 *
241 * @param array $data - mail data.
242 * @return array|mixed|object
243 */
244 public static function send_email( $data ) {
245 $mailin = new SendinblueApiClient( );
246 try {
247 if (isset($data['headers'])) {
248 $emailHeaders = preg_split("/\r\n|\n|\r/", $data['headers']);
249 unset($data['headers']);
250 $preparedHeaders = [];
251 foreach ($emailHeaders as $header) {
252 $header = explode(': ', $header);
253 if (is_array($header) && 2 == count($header)) {
254 if ($header[0] == 'X-Mailin-Tag') {
255 $data['tags'][] = $header[1];
256 }
257 $preparedHeaders[$header[0]] = $header[1];
258 }
259 }
260 $data['headers'] = $preparedHeaders;
261 }
262 } catch (Exception $exception) {
263
264 }
265 $home_options = get_option( SIB_Manager::HOME_OPTION_NAME);
266 if (!empty($home_options['from_email'])) {
267 $data['sender']['email'] = $home_options['from_email'];
268 if (!empty($home_options['from_name'])) {
269 $data['sender']['name'] = $home_options['from_name'];
270 }
271 }
272
273 $result = $mailin->sendEmail( $data );
274 if (SendinblueApiClient::RESPONSE_CODE_CREATED == $mailin->getLastResponseCode()) {
275 return ['code' => 'success'];
276 }
277
278 return $result;
279 }
280
281 /**
282 * Validation the email if it exist in contact list
283 *
284 * @param string $type - form type.
285 * @param string $email - email.
286 * @param array $list_id - list ids.
287 * @return array
288 */
289 static function validation_email( $type = 'simple', $email, $list_id ) {
290 $mailin = new SendinblueApiClient( );
291
292 $isDopted = false;
293
294 $temp_dopt_list = get_option( SIB_Manager::TEMPLIST_OPTION_NAME );
295
296 $desired_lists = $list_id;
297
298 if ( 'double-optin' == $type ) {
299 $list_id = array( $temp_dopt_list );
300 }
301
302 $data = array(
303 'email' => $email,
304 );
305 $res = $mailin->getUser( $data["email"] );
306
307 // new user.
308 if ( isset($res['code']) && $res['code'] == 'document_not_found' ) {
309 $ret = array(
310 'code' => 'new',
311 'isDopted' => $isDopted,
312 'listid' => $list_id,
313 );
314 return $ret;
315 }
316
317 $listid = $res['listIds'];
318
319 // update user when listid is empty.
320 if ( ! isset( $listid ) || ! is_array( $listid ) ) {
321 $ret = array(
322 'code' => 'update',
323 'isDopted' => $isDopted,
324 'listid' => $list_id,
325 );
326 return $ret;
327 }
328
329 $attrs = $res['attributes'];
330 if ( isset( $attrs['DOUBLE_OPT-IN'] ) && '1' == $attrs['DOUBLE_OPT-IN'] ) {
331 $isDopted = true;
332 }
333 // remove dopt temp list from $listid.
334 if (($key = array_search($temp_dopt_list, $listid)) !== false) {
335 unset($listid[$key]);
336 }
337
338 $diff = array_diff( $desired_lists, $listid );
339 if ( ! empty( $diff ) ) {
340 $status = 'update';
341 if ( 'double-optin' != $type ) {
342 $listid = array_unique( array_merge( $listid, $list_id ) );
343 }
344 if ( ( 'double-optin' == $type && ! $isDopted) ) {
345 array_push( $listid, $temp_dopt_list );
346 }
347 } else {
348 if ( '1' == $res['emailBlacklisted'] ) {
349 $status = 'update';
350 } else {
351 $status = 'already_exist';
352 }
353 }
354
355 $ret = array(
356 'code' => $status,
357 'isDopted' => $isDopted,
358 'listid' => $listid,
359 );
360 return $ret;
361 }
362
363 /**
364 * Signup process
365 *
366 * @param string $type - simple, confirm, double-optin / subscribe.
367 * @param $email - subscriber email.
368 * @param $list_id - desired list ids.
369 * @param $info - user's attributes.
370 * @param null $list_unlink - remove temp list.
371 * @return string
372 */
373 public static function create_subscriber( $type = 'simple', $email, $list_id, $info, $list_unlink = null ) {
374
375 $response = self::validation_email( $type, $email, $list_id );
376 $exist = '';
377
378 if ( 'already_exist' == $response['code'] ) {
379 $exist = 'already_exist';
380 }
381
382 if ( 'subscribe' == $type ) {
383 $info['DOUBLE_OPT-IN'] = '1'; // Yes.
384 } else {
385 if ( 'double-optin' == $type ) {
386 if ( ( 'new' == $response['code'] && ! $response['isDopted']) || ( 'update' == $response['code'] && ! $response['isDopted']) ) {
387 $info['DOUBLE_OPT-IN'] = '2'; // No.
388 }
389 }
390 }
391 $listid = $response['listid'];
392 if( $list_unlink != null )
393 {
394 $listid = array_diff( $listid, $list_unlink );
395 }
396
397 $mailin = new SendinblueApiClient();
398 $user = $mailin->getUser($email);
399 if($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK && isset($user['email']))
400 {
401 unset($info["email"]);
402 if(isset($info["internalUserHistory"]) && is_array($info["internalUserHistory"])) {
403 $info["internalUserHistory"][] = array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress");
404 } else {
405 $info["internalUserHistory"] = array(array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress"));
406 }
407 $data = [
408 'email' => $email,
409 'attributes' => $info,
410 'emailBlacklisted' => false,
411 'smsBlacklisted' => false,
412 'listIds' => $listid,
413 'unlinkListIds' => $list_unlink
414 ];
415 $mailin->updateUser($email ,$data );
416 }
417 else
418 {
419 $info["internalUserHistory"] = array(array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress"));
420 $data = [
421 'email' => $email,
422 'attributes' => $info,
423 'emailBlacklisted' => false,
424 'smsBlacklisted' => false,
425 'listIds' => $listid
426 ];
427
428 $created_user = $mailin->createUser( $data );
429 }
430
431 if('' != $exist)
432 {
433 $response['code'] = $exist;
434 }
435 else if(isset($created_user['id']))
436 {
437 $response['code'] = "success";
438 }
439 return $response['code'];
440 }
441
442 /**
443 * Send a mail for confirmation through Sendinblue
444 *
445 * @param string $type - confirm or double-optin.
446 * @param $to_email - receive email.
447 * @param string $template_id - template id.
448 * @param null $attributes - attributes.
449 * @param string $code - code.
450 */
451 public static function send_comfirm_email( $type = 'confirm', $to_email, $template_id = '-1', $attributes = null, $code = '' ) {
452 $mailin = new SendinblueApiClient();
453
454 // set subject info.
455 if ( 'confirm' == $type ) {
456 $subject = __( 'Subscription confirmed', 'sib_lang' );
457 } elseif ( 'double-optin' == $type ) {
458 $subject = __( 'Please confirm subscription', 'sib_lang' );
459 }
460
461 // get sender info.
462 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
463 if ( isset( $home_settings['sender'] ) ) {
464 $sender_name = $home_settings['from_name'];
465 $sender_email = $home_settings['from_email'];
466 } else {
467 $sender_email = trim( get_bloginfo( 'admin_email' ) );
468 $sender_name = trim( get_bloginfo( 'name' ) );
469 }
470 if ( '' == $sender_email ) {
471 $sender_email = __( 'no-reply@sendinblue.com', 'sib_lang' );
472 $sender_name = __( 'Sendinblue', 'sib_lang' );
473 }
474
475 $template_contents = self::get_email_template( $type );
476 $html_content = $template_contents['html_content'];
477
478 $transactional_tags = 'WordPress Mailin';
479 $attachment = array();
480
481 // get info from SIB template.
482 if ( intval( $template_id ) > 0 ) {
483 $data = array(
484 'id' => $template_id,
485 );
486 $response = $mailin->getEmailTemplate( $data["id"] );
487 if ( $mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK ) {
488 $html_content = $response['htmlContent'];
489 if ( trim( $response['subject'] ) != '' ) {
490 $subject = trim( $response['subject'] );
491 }
492 if ( ( '[DEFAULT_FROM_NAME]' != $response['sender']['name'] ) &&
493 ( '[DEFAULT_FROM_EMAIL]' != $response['sender']['email'] ) &&
494 ( '' != $response['sender']['email'] )
495 ) {
496 $sender_name = $response['sender']['name'];
497 $sender_email = $response['sender']['email'];
498 }
499 $transactional_tags = $response['sender']['name'];
500
501 // pls ask Ekta about attachment of template.
502 }
503 }
504
505 // send mail.
506 $to = array(
507 $to_email => '',
508 );
509 $from = array( $sender_email, $sender_name );
510
511 $site_domain = str_replace( 'https://', '', home_url() );
512 $site_domain = str_replace( 'http://', '', $site_domain );
513
514 $html_content = str_replace( '{title}', $subject, $html_content );
515
516 $html_content = str_replace( '{site_domain}', $site_domain, $html_content );
517 $encodedEmail = rtrim( strtr( base64_encode( $to_email ), '+/', '-_' ), '=' );
518 $search_value = "({{\s*doubleoptin\s*}})";
519
520 // double optin
521 $html_content = str_replace( 'https://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
522 $html_content = str_replace( 'http://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
523 $html_content = str_replace( 'https://{{doubleoptin}}', '{subscribe_url}', $html_content );
524 $html_content = str_replace( 'http://{{doubleoptin}}', '{subscribe_url}', $html_content );
525 $html_content = str_replace( 'https://{{ doubleoptin }}', '{subscribe_url}', $html_content );
526 $html_content = str_replace( 'http://{{ doubleoptin }}', '{subscribe_url}', $html_content );
527 $html_content = str_replace( '[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
528 $html_content = preg_replace($search_value, '{subscribe_url}', $html_content);
529 $html_content = str_replace(
530 '{subscribe_url}', add_query_arg(
531 array(
532 'sib_action' => 'subscribe',
533 'code' => $code,
534 ), home_url()
535 ), $html_content
536 );
537
538 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
539 if ( 'yes' == $home_settings['activate_email'] ) {
540
541 if ( intval( $template_id ) > 0 && is_array( $attributes ) && ( 'confirm' == $type ) ) {
542 $attrs = array_merge(
543 $attributes, array(
544 'EMAIL' => $to_email,
545 )
546 );
547
548 $res = $mailin->sendTransactionalTemplate( intval( $template_id ),['emailTo' => [$to_email]]);
549 } else {
550
551 $data = [
552 'sender' => [
553 'email' => $from[0],
554 'name' => $from[1],
555 ],
556 'replyTo' => [
557 'email' => $from[0],
558 ],
559 'to' => [
560 [
561 'email' => $to_email
562 ]
563 ],
564 'subject' => $subject,
565 'htmlContent' => $html_content
566 ];
567
568 $res = $mailin->sendEmail( $data );
569 }
570 } else {
571 $headers[] = 'Content-Type: text/html; charset=UTF-8';
572 $headers[] = "From: $sender_name <$sender_email>";
573 @wp_mail( $to_email, $subject, $html_content, $headers );
574 }
575 }
576
577 /**
578 * Get email template by type (test, confirmation, double-optin).
579 *
580 * @param string $type - email template type.
581 * @return array
582 */
583 static function get_email_template( $type = 'test' ) {
584 $lang = get_bloginfo( 'language' );
585 if ( 'fr-FR' == $lang ) {
586 $file = 'temp_fr-FR';
587 } else {
588 $file = 'temp';
589 }
590
591 $file_path = SIB_Manager::$plugin_dir . '/inc/templates/' . $type . '/';
592 // get html content.
593 $html_content = file_get_contents( $file_path . $file . '.html' );
594 // get text content.
595 $text_content = file_get_contents( $file_path . $file . '.txt' );
596 $templates = array(
597 'html_content' => $html_content,
598 'text_content' => $text_content,
599 );
600 return $templates;
601 }
602
603 /**
604 * Sync wp users to contact list.
605 *
606 * @param string $users_info - user's attributes.
607 * @param array $list_ids - desired lists
608 * @return array|mixed|object
609 */
610 public static function sync_users( $users_info, $list_ids ) {
611 $client = new SendinblueApiClient();
612 $data = array(
613 'fileBody' => $users_info,
614 'listIds' => $list_ids,
615 );
616 $client->importContacts($data);
617 if ( SendinblueApiClient::RESPONSE_CODE_ACCEPTED == $client->getLastResponseCode() ) {
618 $response = array(
619 'code' => 'success',
620 'message' => __( 'Contact synchronization has started.', 'sib_lang' )
621 );
622 } else {
623 $response = array(
624 'code' => 'failed',
625 'message' => __( 'Something went wrong. PLease try again.', 'sib_lang' )
626 );
627 }
628 return $response;
629 }
630
631 /**
632 * Subscribe process for double optin subscribers
633 */
634 public static function subscribe() {
635 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '';
636
637 $contact_info = SIB_Model_Users::get_data_by_code( $code );
638
639 if ( false != $contact_info ) {
640 $email = $contact_info['email'];
641 $info = maybe_unserialize( $contact_info['info'] );
642 $list_id = maybe_unserialize( $contact_info['listIDs'] );
643 $form_id = $contact_info['frmid'];
644 $current_form = SIB_Forms::getForm( $form_id );
645 $unlinkedLists = null;
646 if( isset( $info['unlinkedLists'] ) )
647 {
648 $unlinkedLists = $info['unlinkedLists'];
649 unset($info['unlinkedLists']);
650 }
651 if ( '1' == $current_form['isDopt'] )
652 {
653 SIB_API_Manager::send_comfirm_email( 'confirm', $email, $current_form['confirmID'], $info );
654 }
655 // temp dopt list.
656 $temp_list = get_option( SIB_Manager::TEMPLIST_OPTION_NAME );
657 if( $unlinkedLists != null ) {
658 $unlinkedLists[] = $temp_list;
659 self::create_subscriber( 'subscribe', $email, $list_id, $info, $unlinkedLists );
660 }
661 else {
662 self::create_subscriber( 'subscribe', $email, $list_id, $info, array( $temp_list ) );
663 }
664
665 // remove the record.
666 $id = $contact_info['id'];
667 SIB_Model_Users::remove_record( $id );
668 }
669
670 if ( '' != $contact_info['redirectUrl'] ) {
671 wp_safe_redirect( $contact_info['redirectUrl'] );
672 exit;
673 }
674
675 $site_domain = str_replace( 'https://', '', home_url() );
676 $site_domain = str_replace( 'http://', '', $site_domain );
677 ?>
678 <body style="margin:0; padding:0;">
679 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
680 <tbody>
681 <tr style="border-collapse:collapse;">
682 <td style="border-collapse:collapse;" align="center">
683 <table cellpadding="0" cellspacing="0" border="0" width="540">
684 <tbody>
685 <tr>
686 <td style="line-height:0; font-size:0;" height="20"></td>
687 </tr>
688 </tbody>
689 </table>
690 <table cellpadding="0" cellspacing="0" border="0" width="540">
691 <tbody>
692 <tr>
693 <td style="line-height:0; font-size:0;" height="20">
694 <div
695 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
696 <?php esc_attr_e( 'Thank you for subscribing', 'sib_lang' ); ?></div>
697 </td>
698 </tr>
699 </tbody>
700 </table>
701 <table cellpadding="0" cellspacing="0" border="0" width="540">
702 <tbody>
703 <tr>
704 <td style="line-height:0; font-size:0;" height="20"></td>
705 </tr>
706 </tbody>
707 </table>
708 <table cellpadding="0" cellspacing="0" border="0" width="540">
709 <tbody>
710 <tr>
711 <td align="left">
712
713 <div
714 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
715 <br>
716 <?php echo esc_attr__( 'You have just subscribed to the newsletter of ', 'sib_lang' ) . esc_attr( $site_domain ) . ' .'; ?>
717 <br><br>
718 <?php esc_attr_e( '-Sendinblue', 'sib_lang' ); ?></div>
719 </td>
720 </tr>
721 </tbody>
722 </table>
723 <table cellpadding="0" cellspacing="0" border="0" width="540">
724 <tbody>
725 <tr>
726 <td style="line-height:0; font-size:0;" height="20">
727 </td>
728 </tr>
729 </tbody>
730 </table>
731 </td>
732 </tr>
733 </tbody>
734 </table>
735 </body>
736 <?php
737 exit;
738 }
739
740 /**
741 * Unsubscribe process
742 */
743 function unsubscribe() {
744 $mailin = new SendinblueApiClient();
745 $code = isset( $_GET['code'] ) ? esc_attr( $_GET['code'] ) : '' ;
746 $list_id = isset( $_GET['li'] ) ? intval( esc_attr( $_GET['li'] ) ) : '' ;
747
748 $email = base64_decode( strtr( $code, '-_', '+/' ) );
749 $data = array(
750 'email' => $email,
751 );
752 $response = $mailin->get_user( $data );
753
754 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
755 $attributes = $response['attributes'];
756
757 $listid = $response['listIds'];
758
759 $blacklisted = $response['emailBlacklisted'];
760 $diff_listid = array_diff( $listid, array( $list_id ) );
761
762 if ( count( $diff_listid ) == 0 ) {
763 $blacklisted = true;
764 $diff_listid = $listid;
765 }
766 $data = array(
767 'email' => $email,
768 'data' =>'{"listIds":'.$diff_listid.',"emailBlacklisted":'.$blacklisted.'}'
769 );
770 $mailin->updateUser( $data["email"],$data["data"] );
771 }
772 ?>
773 <body style="margin:0; padding:0;">
774 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
775 <tbody>
776 <tr style="border-collapse:collapse;">
777 <td style="border-collapse:collapse;" align="center">
778 <table cellpadding="0" cellspacing="0" border="0" width="540">
779 <tbody>
780 <tr>
781 <td style="line-height:0; font-size:0;" height="20"></td>
782 </tr>
783 </tbody>
784 </table>
785 <table cellpadding="0" cellspacing="0" border="0" width="540">
786 <tbody>
787 <tr>
788 <td style="line-height:0; font-size:0;" height="20">
789 <div
790 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
791 <?php esc_attr_e( 'Unsubscribe', 'sib_lang' ); ?></div>
792 </td>
793 </tr>
794 </tbody>
795 </table>
796 <table cellpadding="0" cellspacing="0" border="0" width="540">
797 <tbody>
798 <tr>
799 <td style="line-height:0; font-size:0;" height="20"></td>
800 </tr>
801 </tbody>
802 </table>
803 <table cellpadding="0" cellspacing="0" border="0" width="540">
804 <tbody>
805 <tr>
806 <td align="left">
807
808 <div
809 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
810 <br>
811 <?php esc_attr_e( 'Your request has been taken into account.', 'sib_lang' ); ?><br>
812 <br>
813 <?php esc_attr_e( 'The user has been unsubscribed', 'sib_lang' ); ?><br>
814 <br>
815 -Sendinblue
816 </div>
817 </td>
818 </tr>
819 </tbody>
820 </table>
821 <table cellpadding="0" cellspacing="0" border="0" width="540">
822 <tbody>
823 <tr>
824 <td style="line-height:0; font-size:0;" height="20">
825 </td>
826 </tr>
827 </tbody>
828 </table>
829 </td>
830 </tr>
831 </tbody>
832 </table>
833 </body>
834 <?php
835 exit;
836 }
837
838 /** Create list and attribute for double optin */
839 public static function create_default_dopt() {
840
841 $mailin = new SendinblueApiClient();
842 // add list.
843 $isEmpty = false;
844
845 $list_data = $mailin->getAllLists();
846 foreach ( $list_data['lists'] as $value ) {
847 if ( 'Temp - DOUBLE OPTIN' == $value['name'] ) {
848 $isEmpty = true;
849 break;
850 }
851 }
852
853 if(!$isEmpty) {
854 $data = array(
855 'name' => 'Temp - DOUBLE OPTIN',
856 'folderId' => 1,
857 );
858 $mailin->createList( $data );
859 }
860
861
862 // add attribute.
863 $isEmpty = false;
864 $ret = $mailin->getAttributes();
865
866 if (isset($ret["attributes"])) {
867 foreach ($ret["attributes"] as $key => $value) {
868 if($value["category"] == "category" && 'DOUBLE_OPT-IN' == $value['name'] && ! empty( $value['enumeration'] ) ) {
869 $isEmpty = true;
870 }
871 }
872
873 if ( ! $isEmpty ) {
874 $data = [
875 'type' => 'category',
876 'enumeration' => [
877 [
878 'value' => 1,
879 'label' => 'Yes'
880 ],
881 [
882 'value' => 2,
883 'label' => 'No'
884 ],
885 ]
886 ];
887 $mailin->createAttribute('category', 'DOUBLE_OPT', $data);
888 }
889 }
890 }
891 }
892 }
893