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