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