PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.0.0
Brevo – Email, SMS, Web Push, Chat, and more. v3.0.0
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
862 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 delete_transient( SIB_Manager::API_KEY_V3_OPTION_NAME);
220 }
221
222 /**
223 * Send Identify User for MA
224 *
225 * @param array $data - data.
226 */
227 public static function identify_user( $data ) {
228 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
229 if (isset($general_settings['ma_key'])) {
230 try {
231 $event = new Sendinblue( $general_settings['ma_key'] );
232 $event->identify( $data );
233 } catch (Exception $exception) {
234 echo $exception->getMessage() . "\n";
235 }
236 }
237 }
238
239 /**
240 * Send email through Sendinblue
241 *
242 * @param array $data - mail data.
243 * @return array|mixed|object
244 */
245 public static function send_email( $data ) {
246 $mailin = new SendinblueApiClient( );
247 $result = $mailin->sendEmail( $data );
248 return $result;
249 }
250
251 /**
252 * Validation the email if it exist in contact list
253 *
254 * @param string $type - form type.
255 * @param string $email - email.
256 * @param array $list_id - list ids.
257 * @return array
258 */
259 static function validation_email( $type = 'simple', $email, $list_id ) {
260 $mailin = new SendinblueApiClient( );
261
262 $isDopted = false;
263
264 $temp_dopt_list = get_option( SIB_Manager::TEMPLIST_OPTION_NAME );
265
266 $desired_lists = $list_id;
267
268 if ( 'double-optin' == $type ) {
269 $list_id = array( $temp_dopt_list );
270 }
271
272 $data = array(
273 'email' => $email,
274 );
275 $res = $mailin->getUser( $data["email"] );
276
277 // new user.
278 if ( isset($res['code']) && $res['code'] == 'document_not_found' ) {
279 $ret = array(
280 'code' => 'new',
281 'isDopted' => $isDopted,
282 'listid' => $list_id,
283 );
284 return $ret;
285 }
286
287 $listid = $res['listIds'];
288
289 // update user when listid is empty.
290 if ( ! isset( $listid ) || ! is_array( $listid ) ) {
291 $ret = array(
292 'code' => 'update',
293 'isDopted' => $isDopted,
294 'listid' => $list_id,
295 );
296 return $ret;
297 }
298
299 $attrs = $res['attributes'];
300 if ( isset( $attrs['DOUBLE_OPT-IN'] ) && '1' == $attrs['DOUBLE_OPT-IN'] ) {
301 $isDopted = true;
302 }
303 // remove dopt temp list from $listid.
304 if (($key = array_search($temp_dopt_list, $listid)) !== false) {
305 unset($listid[$key]);
306 }
307
308 $diff = array_diff( $desired_lists, $listid );
309 if ( ! empty( $diff ) ) {
310 $status = 'update';
311 if ( 'double-optin' != $type ) {
312 $listid = array_unique( array_merge( $listid, $list_id ) );
313 }
314 if ( ( 'double-optin' == $type && ! $isDopted) ) {
315 array_push( $listid, $temp_dopt_list );
316 }
317 } else {
318 if ( '1' == $res['emailBlacklisted'] ) {
319 $status = 'update';
320 } else {
321 $status = 'already_exist';
322 }
323 }
324
325 $ret = array(
326 'code' => $status,
327 'isDopted' => $isDopted,
328 'listid' => $listid,
329 );
330 return $ret;
331 }
332
333 /**
334 * Signup process
335 *
336 * @param string $type - simple, confirm, double-optin / subscribe.
337 * @param $email - subscriber email.
338 * @param $list_id - desired list ids.
339 * @param $info - user's attributes.
340 * @param null $list_unlink - remove temp list.
341 * @return string
342 */
343 public static function create_subscriber( $type = 'simple', $email, $list_id, $info, $list_unlink = null ) {
344
345 $response = self::validation_email( $type, $email, $list_id );
346 $exist = '';
347
348 if ( 'already_exist' == $response['code'] ) {
349 $exist = 'already_exist';
350 }
351
352 if ( 'subscribe' == $type ) {
353 $info['DOUBLE_OPT-IN'] = '1'; // Yes.
354 } else {
355 if ( 'double-optin' == $type ) {
356 if ( ( 'new' == $response['code'] && ! $response['isDopted']) || ( 'update' == $response['code'] && ! $response['isDopted']) ) {
357 $info['DOUBLE_OPT-IN'] = '2'; // No.
358 }
359 }
360 }
361 $listid = $response['listid'];
362 if( $list_unlink != null )
363 {
364 $listid = array_diff( $listid, $list_unlink );
365 }
366
367 $mailin = new SendinblueApiClient();
368 $user = $mailin->getUser($email);
369 if($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK && isset($user['email']))
370 {
371 unset($info["email"]);
372 if(isset($info["internalUserHistory"]) && is_array($info["internalUserHistory"])) {
373 $info["internalUserHistory"][] = array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress");
374 } else {
375 $info["internalUserHistory"] = array(array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress"));
376 }
377 $data = [
378 'email' => $email,
379 'attributes' => $info,
380 'emailBlacklisted' => false,
381 'smsBlacklisted' => false,
382 'listIds' => $listid,
383 'unlinkListIds' => $list_unlink
384 ];
385 $mailin->updateUser($email ,$data );
386 }
387 else
388 {
389 $info["internalUserHistory"] = array(array("action"=>"SUBSCRIBE_BY_PLUGIN", "id"=> 1, "name"=>"wordpress"));
390 $data = [
391 'email' => $email,
392 'attributes' => $info,
393 'emailBlacklisted' => false,
394 'smsBlacklisted' => false,
395 'listIds' => $listid
396 ];
397
398 $created_user = $mailin->createUser( $data );
399 }
400
401 if('' != $exist)
402 {
403 $response['code'] = $exist;
404 }
405 else if(isset($created_user['id']))
406 {
407 $response['code'] = "success";
408 }
409 return $response['code'];
410 }
411
412 /**
413 * Send a mail for confirmation through Sendinblue
414 *
415 * @param string $type - confirm or double-optin.
416 * @param $to_email - receive email.
417 * @param string $template_id - template id.
418 * @param null $attributes - attributes.
419 * @param string $code - code.
420 */
421 public static function send_comfirm_email( $type = 'confirm', $to_email, $template_id = '-1', $attributes = null, $code = '' ) {
422 $mailin = new SendinblueApiClient();
423
424 // set subject info.
425 if ( 'confirm' == $type ) {
426 $subject = __( 'Subscription confirmed', 'sib_lang' );
427 } elseif ( 'double-optin' == $type ) {
428 $subject = __( 'Please confirm subscription', 'sib_lang' );
429 }
430
431 // get sender info.
432 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
433 if ( isset( $home_settings['sender'] ) ) {
434 $sender_name = $home_settings['from_name'];
435 $sender_email = $home_settings['from_email'];
436 } else {
437 $sender_email = trim( get_bloginfo( 'admin_email' ) );
438 $sender_name = trim( get_bloginfo( 'name' ) );
439 }
440 if ( '' == $sender_email ) {
441 $sender_email = __( 'no-reply@sendinblue.com', 'sib_lang' );
442 $sender_name = __( 'Sendinblue', 'sib_lang' );
443 }
444
445 $template_contents = self::get_email_template( $type );
446 $html_content = $template_contents['html_content'];
447
448 $transactional_tags = 'WordPress Mailin';
449 $attachment = array();
450
451 // get info from SIB template.
452 if ( intval( $template_id ) > 0 ) {
453 $data = array(
454 'id' => $template_id,
455 );
456 $response = $mailin->getEmailTemplate( $data["id"] );
457 if ( $mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK ) {
458 $html_content = $response['htmlContent'];
459 if ( trim( $response['subject'] ) != '' ) {
460 $subject = trim( $response['subject'] );
461 }
462 if ( ( '[DEFAULT_FROM_NAME]' != $response['sender']['name'] ) &&
463 ( '[DEFAULT_FROM_EMAIL]' != $response['sender']['email'] ) &&
464 ( '' != $response['sender']['email'] )
465 ) {
466 $sender_name = $response['sender']['name'];
467 $sender_email = $response['sender']['email'];
468 }
469 $transactional_tags = $response['sender']['name'];
470
471 // pls ask Ekta about attachment of template.
472 }
473 }
474
475 // send mail.
476 $to = array(
477 $to_email => '',
478 );
479 $from = array( $sender_email, $sender_name );
480
481 $site_domain = str_replace( 'https://', '', home_url() );
482 $site_domain = str_replace( 'http://', '', $site_domain );
483
484 $html_content = str_replace( '{title}', $subject, $html_content );
485
486 $html_content = str_replace( '{site_domain}', $site_domain, $html_content );
487 $encodedEmail = rtrim( strtr( base64_encode( $to_email ), '+/', '-_' ), '=' );
488 $search_value = "({{\s*doubleoptin\s*}})";
489
490 // double optin
491 $html_content = str_replace( 'https://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
492 $html_content = str_replace( 'http://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
493 $html_content = str_replace( 'https://{{doubleoptin}}', '{subscribe_url}', $html_content );
494 $html_content = str_replace( 'http://{{doubleoptin}}', '{subscribe_url}', $html_content );
495 $html_content = str_replace( 'https://{{ doubleoptin }}', '{subscribe_url}', $html_content );
496 $html_content = str_replace( 'http://{{ doubleoptin }}', '{subscribe_url}', $html_content );
497 $html_content = str_replace( '[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
498 $html_content = preg_replace($search_value, '{subscribe_url}', $html_content);
499 $html_content = str_replace(
500 '{subscribe_url}', add_query_arg(
501 array(
502 'sib_action' => 'subscribe',
503 'code' => $code,
504 ), home_url()
505 ), $html_content
506 );
507
508 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
509 if ( 'yes' == $home_settings['activate_email'] ) {
510
511 if ( intval( $template_id ) > 0 && is_array( $attributes ) && ( 'confirm' == $type ) ) {
512 $attrs = array_merge(
513 $attributes, array(
514 'EMAIL' => $to_email,
515 )
516 );
517
518 $res = $mailin->sendTransactionalTemplate( intval( $template_id ),['emailTo' => [$to_email]]);
519 } else {
520
521 $data = [
522 'sender' => [
523 'email' => $from[0],
524 'name' => $from[1],
525 ],
526 'replyTo' => [
527 'email' => $from[0],
528 ],
529 'to' => [
530 [
531 'email' => $to_email
532 ]
533 ],
534 'subject' => $subject,
535 'htmlContent' => $html_content
536 ];
537
538 $res = $mailin->sendEmail( $data );
539 }
540 } else {
541 $headers[] = 'Content-Type: text/html; charset=UTF-8';
542 $headers[] = "From: $sender_name <$sender_email>";
543 @wp_mail( $to_email, $subject, $html_content, $headers );
544 }
545 }
546
547 /**
548 * Get email template by type (test, confirmation, double-optin).
549 *
550 * @param string $type - email template type.
551 * @return array
552 */
553 static function get_email_template( $type = 'test' ) {
554 $lang = get_bloginfo( 'language' );
555 if ( 'fr-FR' == $lang ) {
556 $file = 'temp_fr-FR';
557 } else {
558 $file = 'temp';
559 }
560
561 $file_path = SIB_Manager::$plugin_dir . '/inc/templates/' . $type . '/';
562 // get html content.
563 $html_content = file_get_contents( $file_path . $file . '.html' );
564 // get text content.
565 $text_content = file_get_contents( $file_path . $file . '.txt' );
566 $templates = array(
567 'html_content' => $html_content,
568 'text_content' => $text_content,
569 );
570 return $templates;
571 }
572
573 /**
574 * Sync wp users to contact list.
575 *
576 * @param string $users_info - user's attributes.
577 * @param array $list_ids - desired lists
578 * @return array|mixed|object
579 */
580 public static function sync_users( $users_info, $list_ids ) {
581 $client = new SendinblueApiClient();
582 $data = array(
583 'fileBody' => $users_info,
584 'listIds' => $list_ids,
585 );
586 $client->importContacts($data);
587 if ( SendinblueApiClient::RESPONSE_CODE_ACCEPTED == $client->getLastResponseCode() ) {
588 $response = array(
589 'code' => 'success',
590 'message' => __( 'Contact synchronization has started.', 'sib_lang' )
591 );
592 } else {
593 $response = array(
594 'code' => 'failed',
595 'message' => __( 'Something went wrong. PLease try again.', 'sib_lang' )
596 );
597 }
598 return $response;
599 }
600
601 /**
602 * Subscribe process for double optin subscribers
603 */
604 public static function subscribe() {
605 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '';
606
607 $contact_info = SIB_Model_Users::get_data_by_code( $code );
608
609 if ( false != $contact_info ) {
610 $email = $contact_info['email'];
611 $info = maybe_unserialize( $contact_info['info'] );
612 $list_id = maybe_unserialize( $contact_info['listIDs'] );
613 $form_id = $contact_info['frmid'];
614 $current_form = SIB_Forms::getForm( $form_id );
615 $unlinkedLists = null;
616 if( isset( $info['unlinkedLists'] ) )
617 {
618 $unlinkedLists = $info['unlinkedLists'];
619 unset($info['unlinkedLists']);
620 }
621 if ( '1' == $current_form['isDopt'] )
622 {
623 SIB_API_Manager::send_comfirm_email( 'confirm', $email, $current_form['confirmID'], $info );
624 }
625 // temp dopt list.
626 $temp_list = get_option( SIB_Manager::TEMPLIST_OPTION_NAME );
627 if( $unlinkedLists != null ) {
628 $unlinkedLists[] = $temp_list;
629 self::create_subscriber( 'subscribe', $email, $list_id, $info, $unlinkedLists );
630 }
631 else {
632 self::create_subscriber( 'subscribe', $email, $list_id, $info, array( $temp_list ) );
633 }
634
635 // remove the record.
636 $id = $contact_info['id'];
637 SIB_Model_Users::remove_record( $id );
638 }
639
640 if ( '' != $contact_info['redirectUrl'] ) {
641 wp_safe_redirect( $contact_info['redirectUrl'] );
642 exit;
643 }
644
645 $site_domain = str_replace( 'https://', '', home_url() );
646 $site_domain = str_replace( 'http://', '', $site_domain );
647 ?>
648 <body style="margin:0; padding:0;">
649 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
650 <tbody>
651 <tr style="border-collapse:collapse;">
652 <td style="border-collapse:collapse;" align="center">
653 <table cellpadding="0" cellspacing="0" border="0" width="540">
654 <tbody>
655 <tr>
656 <td style="line-height:0; font-size:0;" height="20"></td>
657 </tr>
658 </tbody>
659 </table>
660 <table cellpadding="0" cellspacing="0" border="0" width="540">
661 <tbody>
662 <tr>
663 <td style="line-height:0; font-size:0;" height="20">
664 <div
665 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
666 <?php esc_attr_e( 'Thank you for subscribing', 'sib_lang' ); ?></div>
667 </td>
668 </tr>
669 </tbody>
670 </table>
671 <table cellpadding="0" cellspacing="0" border="0" width="540">
672 <tbody>
673 <tr>
674 <td style="line-height:0; font-size:0;" height="20"></td>
675 </tr>
676 </tbody>
677 </table>
678 <table cellpadding="0" cellspacing="0" border="0" width="540">
679 <tbody>
680 <tr>
681 <td align="left">
682
683 <div
684 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
685 <br>
686 <?php echo esc_attr__( 'You have just subscribed to the newsletter of ', 'sib_lang' ) . esc_attr( $site_domain ) . ' .'; ?>
687 <br><br>
688 <?php esc_attr_e( '-Sendinblue', 'sib_lang' ); ?></div>
689 </td>
690 </tr>
691 </tbody>
692 </table>
693 <table cellpadding="0" cellspacing="0" border="0" width="540">
694 <tbody>
695 <tr>
696 <td style="line-height:0; font-size:0;" height="20">
697 </td>
698 </tr>
699 </tbody>
700 </table>
701 </td>
702 </tr>
703 </tbody>
704 </table>
705 </body>
706 <?php
707 exit;
708 }
709
710 /**
711 * Unsubscribe process
712 */
713 function unsubscribe() {
714 $mailin = new SendinblueApiClient();
715 $code = isset( $_GET['code'] ) ? esc_attr( $_GET['code'] ) : '' ;
716 $list_id = isset( $_GET['li'] ) ? intval( esc_attr( $_GET['li'] ) ) : '' ;
717
718 $email = base64_decode( strtr( $code, '-_', '+/' ) );
719 $data = array(
720 'email' => $email,
721 );
722 $response = $mailin->get_user( $data );
723
724 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
725 $attributes = $response['attributes'];
726
727 $listid = $response['listIds'];
728
729 $blacklisted = $response['emailBlacklisted'];
730 $diff_listid = array_diff( $listid, array( $list_id ) );
731
732 if ( count( $diff_listid ) == 0 ) {
733 $blacklisted = true;
734 $diff_listid = $listid;
735 }
736 $data = array(
737 'email' => $email,
738 'data' =>'{"listIds":'.$diff_listid.',"emailBlacklisted":'.$blacklisted.'}'
739 );
740 $mailin->updateUser( $data["email"],$data["data"] );
741 }
742 ?>
743 <body style="margin:0; padding:0;">
744 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
745 <tbody>
746 <tr style="border-collapse:collapse;">
747 <td style="border-collapse:collapse;" align="center">
748 <table cellpadding="0" cellspacing="0" border="0" width="540">
749 <tbody>
750 <tr>
751 <td style="line-height:0; font-size:0;" height="20"></td>
752 </tr>
753 </tbody>
754 </table>
755 <table cellpadding="0" cellspacing="0" border="0" width="540">
756 <tbody>
757 <tr>
758 <td style="line-height:0; font-size:0;" height="20">
759 <div
760 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
761 <?php esc_attr_e( 'Unsubscribe', 'sib_lang' ); ?></div>
762 </td>
763 </tr>
764 </tbody>
765 </table>
766 <table cellpadding="0" cellspacing="0" border="0" width="540">
767 <tbody>
768 <tr>
769 <td style="line-height:0; font-size:0;" height="20"></td>
770 </tr>
771 </tbody>
772 </table>
773 <table cellpadding="0" cellspacing="0" border="0" width="540">
774 <tbody>
775 <tr>
776 <td align="left">
777
778 <div
779 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
780 <br>
781 <?php esc_attr_e( 'Your request has been taken into account.', 'sib_lang' ); ?><br>
782 <br>
783 <?php esc_attr_e( 'The user has been unsubscribed', 'sib_lang' ); ?><br>
784 <br>
785 -Sendinblue
786 </div>
787 </td>
788 </tr>
789 </tbody>
790 </table>
791 <table cellpadding="0" cellspacing="0" border="0" width="540">
792 <tbody>
793 <tr>
794 <td style="line-height:0; font-size:0;" height="20">
795 </td>
796 </tr>
797 </tbody>
798 </table>
799 </td>
800 </tr>
801 </tbody>
802 </table>
803 </body>
804 <?php
805 exit;
806 }
807
808 /** Create list and attribute for double optin */
809 public static function create_default_dopt() {
810
811 $mailin = new SendinblueApiClient();
812 // add list.
813 $isEmpty = false;
814
815 $list_data = $mailin->getAllLists();
816 foreach ( $list_data['lists'] as $value ) {
817 if ( 'Temp - DOUBLE OPTIN' == $value['name'] ) {
818 $isEmpty = true;
819 break;
820 }
821 }
822
823 if(!$isEmpty) {
824 $data = array(
825 'name' => 'Temp - DOUBLE OPTIN',
826 'folderId' => 1,
827 );
828 $mailin->createList( $data );
829 }
830
831
832 // add attribute.
833 $isEmpty = false;
834 $ret = $mailin->getAttributes();
835
836 foreach ($ret["attributes"] as $key => $value) {
837 if($value["category"] == "category" && 'DOUBLE_OPT-IN' == $value['name'] && ! empty( $value['enumeration'] ) ) {
838 $isEmpty = true;
839 }
840 }
841
842 if ( ! $isEmpty ) {
843 $data = [
844 'enumeration' => [
845 'type' => 'category',
846 [
847 'value' => 1,
848 'label' => 'Yes'
849 ],
850 [
851 'value' => 2,
852 'label' => 'No'
853 ],
854 ]
855 ];
856 $client->createAttribute('category', 'DOUBLE_OPT', $data);
857 }
858 }
859
860 }
861 }
862