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