PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.87
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.87
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 1 year ago SendinblueAccount.php 5 years ago SendinblueApiClient.php 1 year ago function.wp_mail.php 8 years ago index.php 8 years ago mailin.php 3 years ago sendinblue.php 3 years ago sib-api-manager.php 1 year ago sib-form-preview.php 2 years ago sib-sms-code.php 3 years ago table-forms.php 1 year ago
sib-api-manager.php
940 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 (!empty($attributes) && 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 $templates = $mailin->getAllEmailTemplates();
122 $template_data = array();
123
124 if ( $mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK ) {
125
126 foreach ( $templates['templates'] as $template ) {
127 $is_dopt = 0;
128 if ( strpos( $template['htmlContent'], 'DOUBLEOPTIN' ) != false || strpos( $template['htmlContent'], 'doubleoptin' ) != false) {
129 $is_dopt = 1;
130 }
131 $template_data[] = array(
132 'id' => $template['id'],
133 'name' => $template['name'],
134 'is_dopt' => $is_dopt,
135 );
136
137 }
138 }
139 $templates = $template_data;
140 if ( count( $templates ) > 0 ) {
141 set_transient( 'sib_template_' . md5( SIB_Manager::$access_key ), $templates, self::DELAYTIME );
142 }
143 }
144
145 return $templates;
146 }
147
148 /** Get default list id after install */
149 public static function get_default_list_id() {
150 $lists = self::get_lists();
151 return strval( $lists[0]['id'] );
152 }
153
154 /** Get all lists */
155 public static function get_lists() {
156 // get lists.
157 $lists = get_transient( 'sib_list_' . md5( SIB_Manager::$access_key ) );
158 if ( false === $lists || false == $lists ) {
159
160 $mailin = new SendinblueApiClient();
161 $lists = array();
162 $list_data = $mailin->getAllLists();
163
164 if (!empty($list_data['lists'])) {
165 foreach ( $list_data['lists'] as $value ) {
166 if ( 'Temp - DOUBLE OPTIN' == $value['name'] ) {
167 continue;
168 }
169 $lists[] = array(
170 'id' => $value['id'],
171 'name' => $value['name'],
172 );
173 }
174 }
175
176 if ( count( $lists ) > 0 ) {
177 set_transient( 'sib_list_' . md5( SIB_Manager::$access_key ), $lists, self::DELAYTIME );
178 }
179 }
180 return $lists;
181 }
182
183 /** Get all sender of sendinblue */
184 public static function get_sender_lists() {
185 $senders = get_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ) );
186 if ( false === $senders || false == $senders ) {
187 $mailin = new SendinblueApiClient();
188 $response = $mailin->getSenders();
189 $senders = array();
190 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
191 // reorder by id.
192 foreach ( $response['senders'] as $sender ) {
193 $senders[] = array(
194 'id' => $sender['id'],
195 'from_name' => $sender['name'],
196 'from_email' => $sender['email'],
197 );
198 }
199 }
200 if ( count( $senders ) > 0 ) {
201 set_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ), $senders, self::DELAYTIME );
202 }
203 }
204 return $senders;
205 }
206 /** Remove all transients */
207 public static function remove_transients() {
208 // remove all transients.
209 delete_transient( 'sib_list_' . md5( SIB_Manager::$access_key ) );
210 delete_transient( 'sib_totalusers_' . md5( SIB_Manager::$access_key ) );
211 delete_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ) );
212 delete_transient( 'sib_campaigns_' . md5( SIB_Manager::$access_key ) );
213 delete_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ) );
214 delete_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ) );
215 delete_transient( 'sib_template_' . md5( SIB_Manager::$access_key ) );
216 delete_transient( 'sib_senders_' . md5( SIB_Manager::$access_key ) );
217 }
218
219 /**
220 * Send Identify User for MA
221 *
222 * @param array $data - data.
223 */
224 public static function identify_user( $data ) {
225 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
226 if (isset($general_settings['ma_key'])) {
227 try {
228 $event = new Sendinblue( $general_settings['ma_key'] );
229 $event->identify( $data );
230 } catch (Exception $exception) {
231 echo $exception->getMessage() . "\n";
232 }
233 }
234 }
235
236 /**
237 * Send email through Sendinblue
238 *
239 * @param array $data - mail data.
240 * @return array|mixed|object
241 */
242 public static function send_email( $data ) {
243 $mailin = new SendinblueApiClient( );
244 try {
245 if (isset($data['headers'])) {
246 $emailHeaders = $data['headers'];
247 unset($data['headers']);
248
249 if (!is_array($emailHeaders) && !is_string($emailHeaders)) {
250 return new WP_Error('email headers are not valid');
251 }
252
253 if (is_string($emailHeaders)) {
254 $emailHeaders = preg_split("/\r\n|\n|\r/", $emailHeaders);
255 }
256 $preparedHeaders = [];
257 foreach ($emailHeaders as $header) {
258 $header = explode(': ', $header);
259 if (is_array($header) && 2 == count($header)) {
260 if ($header[0] == 'X-Mailin-Tag') {
261 $data['tags'][] = $header[1];
262 }
263 $preparedHeaders[$header[0]] = $header[1];
264 }
265 }
266 $data['headers'] = $preparedHeaders;
267 }
268 } catch (Exception $exception) {
269 return new WP_Error($exception->getMessage());
270 }
271
272 $home_options = get_option( SIB_Manager::HOME_OPTION_NAME);
273 if (!empty($home_options['from_email'])) {
274 $data['sender']['email'] = $home_options['from_email'];
275 if (!empty($home_options['from_name'])) {
276 $data['sender']['name'] = $home_options['from_name'];
277 }
278 }
279 $mail_setting = get_option('wc_sendinblue_settings', array());
280 $sib_wc_plugin = is_plugin_active(
281 'woocommerce-sendinblue-newsletter-subscription/woocommerce-sendinblue.php'
282 );
283
284 if ( ! empty($mail_setting) && isset($mail_setting['ws_smtp_enable']) && 'yes' == $mail_setting['ws_smtp_enable'] && $sib_wc_plugin === true) {
285 $from_email = trim(get_bloginfo('admin_email'));
286 $from_name = trim(get_bloginfo('name'));
287 $data['sender']['email'] = apply_filters('wp_mail_from', $from_email);
288 $data['sender']['name'] = apply_filters('wp_mail_from_name', $from_name);
289 }
290
291 $result = $mailin->sendEmail( $data );
292 if (SendinblueApiClient::RESPONSE_CODE_CREATED == $mailin->getLastResponseCode()) {
293 return ['code' => 'success'];
294 }
295
296 return $result;
297 }
298
299 /**
300 * Validation the email if it exist in contact list
301 *
302 * @param $res
303 * @param string $type - form type.
304 * @param string $email - email.
305 * @param array $list_id - list ids.
306 * @return array
307 */
308 static function validation_email( $res, $email, $list_id, $type = 'simple' ) {
309
310 $isDopted = false;
311
312 $desired_lists = $list_id;
313
314 if ( 'double-optin' == $type ) {
315 $list_id = array();
316 }
317
318 // new user.
319 if ( isset($res['code']) && $res['code'] == 'document_not_found' ) {
320 $ret = array(
321 'code' => 'new',
322 'isDopted' => $isDopted,
323 'listid' => $list_id,
324 );
325 return $ret;
326 }
327
328 $listid = $res['listIds'];
329
330 // update user when listid is empty.
331 if ( ! isset( $listid ) || ! is_array( $listid ) ) {
332 $ret = array(
333 'code' => 'update',
334 'isDopted' => $isDopted,
335 'listid' => $list_id,
336 );
337 return $ret;
338 }
339
340 $attrs = $res['attributes'];
341 if ( isset( $attrs['DOUBLE_OPT-IN'] ) && '1' == $attrs['DOUBLE_OPT-IN'] ) {
342 $isDopted = true;
343 }
344
345 $diff = array_diff( $desired_lists, $listid );
346 if ( ! empty( $diff ) ) {
347 $status = 'update';
348 if ( 'double-optin' != $type ) {
349 $listid = array_unique( array_merge( $listid, $list_id ) );
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( $email, $list_id, $info, $type = 'simple', $list_unlink = null ) {
378 $mailin = new SendinblueApiClient();
379 $user = $mailin->getUser($email);
380
381 $response = self::validation_email( $user, $email, $list_id, $type );
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(!($type == 'double-optin')){
453 $data = [
454 'email' => $email,
455 'attributes' => $info,
456 'emailBlacklisted' => false,
457 'smsBlacklisted' => false,
458 'listIds' => $listid,
459 'unlinkListIds' => $list_unlink,
460 'updateEnabled' => true
461 ];
462 } else {
463 if($info['DOUBLE_OPT-IN'] == '1'){
464 $data = [
465 'email' => $email,
466 'attributes' => $info,
467 'emailBlacklisted' => false,
468 'smsBlacklisted' => false,
469 'listIds' => $listid,
470 'unlinkListIds' => $list_unlink,
471 'updateEnabled' => true
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 'updateEnabled' => true
482 ];
483 }
484 }
485 $mailin->createUser( $data );
486 $exist = $mailin->getLastResponseCode() == 204 ? 'success' : '' ;
487 } else {
488 $info['sibInternalSource'] = self::PLUGIN_NAME;
489 $info["internalUserHistory"] = array( array( "action" => "SUBSCRIBE_BY_PLUGIN", "id" => 1, "name" => self::PLUGIN_NAME ) );
490 $data = [
491 'email' => $email,
492 'attributes' => $info,
493 'emailBlacklisted' => false,
494 'smsBlacklisted' => false,
495 'listIds' => $listid,
496 'updateEnabled' => true
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( $to_email, $type = 'confirm', $template_id = '-1', $attributes = null, $code = '' ) {
521 $mailin = new SendinblueApiClient();
522
523 // set subject info.
524 if ( 'confirm' == $type ) {
525 $subject = __( 'Subscription confirmed', 'mailin' );
526 } elseif ( 'double-optin' == $type ) {
527 $subject = __( 'Please confirm subscription', 'mailin' );
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@' . parse_url(get_site_url(), PHP_URL_HOST), 'mailin' );
541 $sender_name = __( 'Brevo', 'mailin' );
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.', 'mailin' )
679 );
680 } else {
681 $response = array(
682 'code' => 'failed',
683 'message' => __( 'Something went wrong. PLease try again.', 'mailin' )
684 );
685 }
686 return $response;
687 }
688
689 /**
690 * Subscribe process for double optin subscribers
691 */
692 public static function subscribe( $contact_info ) {
693 if ( false != $contact_info ) {
694 $email = $contact_info['email'];
695 $info = maybe_unserialize( $contact_info['info'] );
696 $list_id = maybe_unserialize( $contact_info['listIDs'] );
697 $form_id = $contact_info['frmid'];
698 $current_form = SIB_Forms::getForm( $form_id );
699 $unlinkedLists = null;
700 if( isset( $info['unlinkedLists'] ) )
701 {
702 $unlinkedLists = $info['unlinkedLists'];
703 unset($info['unlinkedLists']);
704 }
705 if ( '1' == $current_form['isDopt'] && (isset($contact_info['doi_sent']) && $contact_info['doi_sent'] != 1 ))
706 {
707 SIB_API_Manager::send_comfirm_email( $email, 'confirm', $current_form['confirmID'], $info );
708 SIB_Model_Users::make_doi_sent( $contact_info['email'] );
709 }
710
711 if( $unlinkedLists != null ) {
712 self::create_subscriber( $email, $list_id, $info, 'subscribe', $unlinkedLists );
713 }
714 else {
715 self::create_subscriber( $email, $list_id, $info, 'subscribe' );
716 }
717
718 }
719
720 if ( '' != $contact_info['redirectUrl'] ) {
721 wp_redirect( $contact_info['redirectUrl'] );
722 exit;
723 }
724
725 $type = 'Subscribe';
726 self::template_subscribe( $type );
727 exit;
728 }
729
730 /**
731 * Unsubscribe process
732 */
733 function unsubscribe() {
734 $mailin = new SendinblueApiClient();
735 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '' ;
736 $list_id = isset( $_GET['li'] ) ? intval( $_GET['li'] ) : '' ;
737
738 $email = base64_decode( strtr( $code, '-_', '+/' ) );
739 $data = array(
740 'email' => $email,
741 );
742 $response = $mailin->get_user( $data );
743
744 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
745 $attributes = $response['attributes'];
746
747 $listid = $response['listIds'];
748
749 $blacklisted = $response['emailBlacklisted'];
750 $diff_listid = array_diff( $listid, array( $list_id ) );
751
752 if ( count( $diff_listid ) == 0 ) {
753 $blacklisted = true;
754 $diff_listid = $listid;
755 }
756 $data = array(
757 'email' => $email,
758 'data' =>'{"listIds":'.$diff_listid.',"emailBlacklisted":'.$blacklisted.'}'
759 );
760 $mailin->updateUser( $data["email"],$data["data"] );
761 }
762 ?>
763 <body style="margin:0; padding:0;">
764 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%" aria-describedby="Unsubscribe-table">
765 <tbody>
766 <tr style="border-collapse:collapse;">
767 <td style="border-collapse:collapse;" align="center">
768 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-table">
769 <tbody>
770 <tr>
771 <td style="line-height:0; font-size:0;" height="20"></td>
772 </tr>
773 </tbody>
774 </table>
775 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-table">
776 <tbody>
777 <tr>
778 <td style="line-height:0; font-size:0;" height="20">
779 <div
780 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
781 <?php esc_attr_e( 'Unsubscribe', 'mailin' ); ?></div>
782 </td>
783 </tr>
784 </tbody>
785 </table>
786 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-table">
787 <tbody>
788 <tr>
789 <td style="line-height:0; font-size:0;" height="20"></td>
790 </tr>
791 </tbody>
792 </table>
793 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-table">
794 <tbody>
795 <tr>
796 <td align="left">
797
798 <div
799 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
800 <br>
801 <?php esc_attr_e( 'Your request has been taken into account.', 'mailin' ); ?><br>
802 <br>
803 <?php esc_attr_e( 'The user has been unsubscribed', 'mailin' ); ?><br>
804 <br>
805 -Brevo
806 </div>
807 </td>
808 </tr>
809 </tbody>
810 </table>
811 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-table">
812 <tbody>
813 <tr>
814 <td style="line-height:0; font-size:0;" height="20">
815 </td>
816 </tr>
817 </tbody>
818 </table>
819 </td>
820 </tr>
821 </tbody>
822 </table>
823 </body>
824 <?php
825 exit;
826 }
827
828 /** Create list and attribute for double optin */
829 public static function create_default_dopt() {
830
831 $mailin = new SendinblueApiClient();
832
833 // add attribute.
834 $isEmpty = false;
835 $ret = $mailin->getAttributes();
836
837 if (isset($ret["attributes"])) {
838 foreach ($ret["attributes"] as $key => $value) {
839 if($value["category"] == "category" && 'DOUBLE_OPT-IN' == $value['name'] && ! empty( $value['enumeration'] ) ) {
840 $isEmpty = true;
841 }
842 }
843
844 if ( ! $isEmpty ) {
845 $data = [
846 'type' => 'category',
847 'enumeration' => [
848 [
849 'value' => 1,
850 'label' => 'Yes'
851 ],
852 [
853 'value' => 2,
854 'label' => 'No'
855 ],
856 ]
857 ];
858 $mailin->createAttribute('category', 'DOUBLE_OPT-IN', $data);
859 }
860 }
861 }
862
863 /** Template for subscriber and bot event using $type */
864 public static function template_subscribe( $type ) {
865 $site_domain = str_replace( 'https://', '', home_url() );
866 $site_domain = str_replace( 'http://', '', $site_domain );
867 ?>
868 <body style="margin:0; padding:0;">
869 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%" aria-describedby="Unsubscribe-template">
870 <tbody>
871 <tr style="border-collapse:collapse;">
872 <td style="border-collapse:collapse;" align="center">
873 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-template">
874 <tbody>
875 <tr>
876 <td style="line-height:0; font-size:0;" height="20"></td>
877 </tr>
878 </tbody>
879 </table>
880 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-template">
881 <tbody>
882 <tr>
883 <td style="line-height:0; font-size:0;" height="20">
884 <div
885 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
886 <?php
887 if ( 'Subscribe' === $type ) {
888 esc_attr_e( 'Thank you for subscribing', 'mailin' );
889 } elseif ( 'Bot Event' === $type ) {
890 esc_attr_e( 'Please Try Again', 'mailin' );
891 }
892 ?>
893 </div>
894 </td>
895 </tr>
896 </tbody>
897 </table>
898 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-template">
899 <tbody>
900 <tr>
901 <td style="line-height:0; font-size:0;" height="20"></td>
902 </tr>
903 </tbody>
904 </table>
905 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-template">
906 <tbody>
907 <tr>
908 <td align="left">
909
910 <div
911 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
912 <br>
913 <?php
914 if ( 'Subscribe' === $type ) {
915 echo esc_attr__( 'You have just subscribed to the newsletter of ', 'mailin' ) . esc_attr( $site_domain ) . ' .'; }
916 ?>
917 <br><br>
918 <?php esc_attr_e( '-Brevo', 'mailin' ); ?></div>
919 </td>
920 </tr>
921 </tbody>
922 </table>
923 <table cellpadding="0" cellspacing="0" border="0" width="540" aria-describedby="Unsubscribe-template">
924 <tbody>
925 <tr>
926 <td style="line-height:0; font-size:0;" height="20">
927 </td>
928 </tr>
929 </tbody>
930 </table>
931 </td>
932 </tr>
933 </tbody>
934 </table>
935 </body>
936 <?php
937 }
938 }
939 }
940