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