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