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