PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.40
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.40
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 4 years ago SendinblueAccount.php 5 years ago SendinblueApiClient.php 4 years ago function.wp_mail.php 8 years ago index.php 8 years ago mailin.php 5 years ago sendinblue.php 4 years ago sib-api-manager.php 4 years ago sib-form-preview.php 4 years ago sib-sms-code.php 8 years ago table-forms.php 4 years ago
sib-api-manager.php
924 lines
1 <?php
2 /**
3 * Manage Sendinblue API
4 *
5 * Use wp API transient to reduce loading time of API call
6 *
7 * @package SIB_API_Manager
8 */
9
10 if ( ! class_exists( 'SIB_API_Manager' ) ) {
11 /**
12 * Class SIB_API_Manager.
13 * Main API class for sendinblue module.
14 */
15 class SIB_API_Manager {
16
17 /** Transient delay time */
18 const DELAYTIME = 900;
19 /** Constant for Plugin name */
20 const PLUGIN_NAME = 'wordpress';
21
22 /**
23 * SIB_API_Manager constructor.
24 */
25 function __construct() {
26
27 }
28
29 /** Get account info */
30 public static function get_account_info() {
31 // get account's info.
32 $account_info = get_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ) );
33 if ( false === $account_info || false == $account_info ) {
34 $client = new SendinblueApiClient();
35 $account = $client->getAccount();
36 if ($client->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK && !empty($account['email'])) {
37 $account_email = $account['email'];
38
39 $account_info = array(
40 'account_email' => $account_email,
41 'account_user_name' => $account['firstName'] . ' ' . $account['lastName'],
42 'account_data' => $account['plan'],
43 );
44 set_transient( 'sib_credit_' . md5( SIB_Manager::$access_key ), $account_info, self::DELAYTIME );
45 } elseif ($client->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_UNAUTHORIZED) {
46 delete_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
47 }
48 }
49 return $account_info;
50 }
51
52 /** Get smtp status */
53 public static function get_smtp_status() {
54 $status = get_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ) );
55 if ( false === $status || false == $status ) {
56 $client = new SendinblueApiClient();
57 $account = $client->getAccount();
58 $status = 'disabled';
59 if ($client->getLastResponseCode() == 200) {
60 $status = $account['relay']['enabled'] ? 'enabled' : 'disabled';
61 set_transient( 'sib_smtp_status_' . md5( SIB_Manager::$access_key ), $status, self::DELAYTIME );
62
63 // get Marketing Automation API key.
64 if ( isset( $account['marketingAutomation']['enabled'] ) && true == $account['marketingAutomation']['enabled'] ) {
65 $ma_key = $account['marketingAutomation']['key'];
66 } else {
67 $ma_key = '';
68 }
69 $general_settings = get_option( SIB_Manager::MAIN_OPTION_NAME, array() );
70 $general_settings['ma_key'] = $ma_key;
71 update_option( SIB_Manager::MAIN_OPTION_NAME, $general_settings );
72 }
73 }
74 return $status;
75 }
76
77 /** Get all attributes */
78 public static function get_attributes() {
79 // get attributes.
80 $attrs = get_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ) );
81
82 if ( false === $attrs || false == $attrs ) {
83 $mailin = new SendinblueApiClient();
84 $response = $mailin->getAttributes();
85 $attributes = $response['attributes'];
86 $attrs = array(
87 'attributes' => array(
88 'normal_attributes' => array(),
89 'category_attributes' => array(),
90 )
91 );
92
93 if ( count( $attributes ) > 0 ) {
94 foreach ($attributes as $key => $value) {
95 if ($value["category"] == "normal") {
96 $attrs['attributes']['normal_attributes'][] = $value;
97 }
98 elseif ($value["category"] == "category") {
99 $value["type"] = "category";
100 $attrs['attributes']['category_attributes'][] = $value;
101 }
102
103 }
104 }
105
106 set_transient( 'sib_attributes_' . md5( SIB_Manager::$access_key ), $attrs, self::DELAYTIME );
107 }
108
109 return $attrs;
110
111 }
112
113 /** Get all smtp templates */
114 public static function get_templates() {
115
116 // get templates.
117 $templates = get_transient( 'sib_template_' . md5( SIB_Manager::$access_key ) );
118
119 if ( false === $templates || false == $templates ) {
120 $mailin = new SendinblueApiClient();
121 $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
280 $result = $mailin->sendEmail( $data );
281 if (SendinblueApiClient::RESPONSE_CODE_CREATED == $mailin->getLastResponseCode()) {
282 return ['code' => 'success'];
283 }
284
285 return $result;
286 }
287
288 /**
289 * Validation the email if it exist in contact list
290 *
291 * @param $res
292 * @param string $type - form type.
293 * @param string $email - email.
294 * @param array $list_id - list ids.
295 * @return array
296 */
297 static function validation_email( $res, $email, $list_id, $type = 'simple' ) {
298
299 $isDopted = false;
300
301 $desired_lists = $list_id;
302
303 if ( 'double-optin' == $type ) {
304 $list_id = array();
305 }
306
307 // new user.
308 if ( isset($res['code']) && $res['code'] == 'document_not_found' ) {
309 $ret = array(
310 'code' => 'new',
311 'isDopted' => $isDopted,
312 'listid' => $list_id,
313 );
314 return $ret;
315 }
316
317 $listid = $res['listIds'];
318
319 // update user when listid is empty.
320 if ( ! isset( $listid ) || ! is_array( $listid ) ) {
321 $ret = array(
322 'code' => 'update',
323 'isDopted' => $isDopted,
324 'listid' => $list_id,
325 );
326 return $ret;
327 }
328
329 $attrs = $res['attributes'];
330 if ( isset( $attrs['DOUBLE_OPT-IN'] ) && '1' == $attrs['DOUBLE_OPT-IN'] ) {
331 $isDopted = true;
332 }
333
334 $diff = array_diff( $desired_lists, $listid );
335 if ( ! empty( $diff ) ) {
336 $status = 'update';
337 if ( 'double-optin' != $type ) {
338 $listid = array_unique( array_merge( $listid, $list_id ) );
339 }
340 } else {
341 if ( '1' == $res['emailBlacklisted'] ) {
342 $status = 'update';
343 } else {
344 $status = 'already_exist';
345 }
346 }
347
348 $ret = array(
349 'code' => $status,
350 'isDopted' => $isDopted,
351 'listid' => $listid,
352 );
353 return $ret;
354 }
355
356 /**
357 * Signup process
358 *
359 * @param string $type - simple, confirm, double-optin / subscribe.
360 * @param $email - subscriber email.
361 * @param $list_id - desired list ids.
362 * @param $info - user's attributes.
363 * @param null $list_unlink - remove temp list.
364 * @return string
365 */
366 public static function create_subscriber( $email, $list_id, $info, $type = 'simple', $list_unlink = null ) {
367 $mailin = new SendinblueApiClient();
368 $user = $mailin->getUser($email);
369
370 $response = self::validation_email( $user, $email, $list_id, $type );
371 $exist = '';
372
373 if ( 'already_exist' == $response['code'] ) {
374 $exist = 'already_exist';
375 }
376
377 if ( 'subscribe' == $type ) {
378 $info['DOUBLE_OPT-IN'] = '1'; // Yes.
379 } else {
380 if ( 'double-optin' == $type ) {
381 if ( ( 'new' == $response['code'] && ! $response['isDopted']) || ( 'update' == $response['code'] && ! $response['isDopted']) ) {
382 $info['DOUBLE_OPT-IN'] = '2'; // No.
383 }
384 }
385 }
386
387 $listid = $response['listid'];
388 if ( $list_unlink != null ) {
389 $listid = array_diff( $listid, $list_unlink );
390 }
391
392 $attributes = SIB_API_Manager::get_attributes();
393 if( !empty($attributes["attributes"]["normal_attributes"]) ) {
394 foreach ( $attributes["attributes"]["normal_attributes"] as $key => $value ) {
395 if( "boolean" == $value["type"] && array_key_exists($value["name"], $info) )
396 if( in_array($info[ $value["name"] ], array("true","True","TRUE",1)) ) {
397 $info[ $value["name"] ] = true;
398 }
399 else {
400 $info[ $value["name"] ] = false;
401 }
402 if( "date" == $value["type"] && array_key_exists($value["name"], $info) ) {
403 $date = $info[ $value["name"] ];
404 $tempDate = explode('-', $date);
405 $error = false;
406 foreach ( $tempDate as $key => $val ) {
407 if ( $val == "0" || $val == "00" || $val == "0000" ) {
408 $error = true;
409 }
410 }
411 if ( $error ) {
412 wp_send_json(
413 array(
414 'status' => 'failure',
415 'msg' => [
416 'errorMsg' => 'Date format is invalid',
417 ]
418 )
419 );
420 } else {
421 try {
422 $dateCheck = (new DateTime($date))->format('Y-m-d');
423 $info[ $value["name"] ] = $dateCheck;
424 } catch (Exception $exception) {
425 wp_send_json(
426 array(
427 'status' => 'failure',
428 'msg' => [
429 'errorMsg' => 'Date format is invalid',
430 ]
431 )
432 );
433 }
434 }
435 }
436 }
437 }
438
439 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK && isset($user['email'])) {
440 unset($info["email"]);
441 if(!($type == 'double-optin')){
442 $data = [
443 'email' => $email,
444 'attributes' => $info,
445 'emailBlacklisted' => false,
446 'smsBlacklisted' => false,
447 'listIds' => $listid,
448 'unlinkListIds' => $list_unlink
449 ];
450 } else {
451 if($info['DOUBLE_OPT-IN'] == '1'){
452 $data = [
453 'email' => $email,
454 'attributes' => $info,
455 'emailBlacklisted' => false,
456 'smsBlacklisted' => false,
457 'listIds' => $listid,
458 'unlinkListIds' => $list_unlink
459 ];
460 } else {
461 $data = [
462 'email' => $email,
463 'attributes' => $info,
464 'emailBlacklisted' => (($user["emailBlacklisted"] == '1') ? $user["emailBlacklisted"] : false),
465 'smsBlacklisted' => false,
466 'listIds' => $listid,
467 'unlinkListIds' => $list_unlink
468 ];
469 }
470 }
471 $mailin->updateUser($email ,$data );
472 $exist = $mailin->getLastResponseCode() == 204 ? 'success' : '' ;
473 } else {
474 $info['sibInternalSource'] = self::PLUGIN_NAME;
475 $info["internalUserHistory"] = array( array( "action" => "SUBSCRIBE_BY_PLUGIN", "id" => 1, "name" => self::PLUGIN_NAME ) );
476 $data = [
477 'email' => $email,
478 'attributes' => $info,
479 'emailBlacklisted' => false,
480 'smsBlacklisted' => false,
481 'listIds' => $listid
482 ];
483
484 $created_user = $mailin->createUser( $data );
485 }
486
487 if ('' != $exist) {
488 $response['code'] = $exist;
489 } else if(isset($created_user['id'])) {
490 $response['code'] = "success";
491 }
492
493 return $response['code'];
494 }
495
496 /**
497 * Send a mail for confirmation through Sendinblue
498 *
499 * @param string $type - confirm or double-optin.
500 * @param $to_email - receive email.
501 * @param string $template_id - template id.
502 * @param null $attributes - attributes.
503 * @param string $code - code.
504 */
505 public static function send_comfirm_email( $to_email, $type = 'confirm', $template_id = '-1', $attributes = null, $code = '' ) {
506 $mailin = new SendinblueApiClient();
507
508 // set subject info.
509 if ( 'confirm' == $type ) {
510 $subject = __( 'Subscription confirmed', 'mailin' );
511 } elseif ( 'double-optin' == $type ) {
512 $subject = __( 'Please confirm subscription', 'mailin' );
513 }
514
515 // get sender info.
516 $home_settings = get_option( SIB_Manager::HOME_OPTION_NAME );
517 if ( isset( $home_settings['sender'] ) ) {
518 $sender_name = $home_settings['from_name'];
519 $sender_email = $home_settings['from_email'];
520 } else {
521 $sender_email = trim( get_bloginfo( 'admin_email' ) );
522 $sender_name = trim( get_bloginfo( 'name' ) );
523 }
524 if ( '' == $sender_email ) {
525 $sender_email = __( 'no-reply@sendinblue.com', 'mailin' );
526 $sender_name = __( 'Sendinblue', 'mailin' );
527 }
528
529 $template_contents = self::get_email_template( $type );
530 $html_content = $template_contents['html_content'];
531
532 $transactional_tags = 'WordPress Mailin';
533 $attachment = array();
534
535 // get info from SIB template.
536 if ( 'yes' == $home_settings['activate_email'] && intval( $template_id ) > 0 && ( 'confirm' == $type ) ) {
537 $data = array(
538 'replyTo' => array('email' => $sender_email),
539 'to' => array(array('email' => $to_email)),
540 );
541 $data["templateId"] = intval( $template_id );
542 $mailin->sendEmail( $data );
543 return;
544 }
545 else if ( intval( $template_id ) > 0 ) {
546 $data = array(
547 'id' => $template_id,
548 );
549 $response = $mailin->getEmailTemplate( $data["id"] );
550 if ( $mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK ) {
551 $html_content = $response['htmlContent'];
552 if ( trim( $response['subject'] ) != '' ) {
553 $subject = trim( $response['subject'] );
554 }
555 if ( ( '[DEFAULT_FROM_NAME]' != $response['sender']['name'] ) &&
556 ( '[DEFAULT_FROM_EMAIL]' != $response['sender']['email'] ) &&
557 ( '' != $response['sender']['email'] )
558 ) {
559 $sender_name = $response['sender']['name'];
560 $sender_email = $response['sender']['email'];
561 }
562 $transactional_tags = $response['sender']['name'];
563
564 // pls ask Ekta about attachment of template.
565 }
566 }
567
568 // send mail.
569 $to = array(
570 $to_email => '',
571 );
572 $from = array( $sender_email, $sender_name );
573
574 $site_domain = str_replace( 'https://', '', home_url() );
575 $site_domain = str_replace( 'http://', '', $site_domain );
576
577 $html_content = str_replace( '{title}', $subject, $html_content );
578
579 $html_content = str_replace( '{site_domain}', $site_domain, $html_content );
580 $encodedEmail = rtrim( strtr( base64_encode( $to_email ), '+/', '-_' ), '=' );
581 $search_value = "({{\s*doubleoptin\s*}})";
582
583 // double optin
584 $html_content = str_replace( 'https://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
585 $html_content = str_replace( 'http://[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
586 $html_content = str_replace( 'https://{{doubleoptin}}', '{subscribe_url}', $html_content );
587 $html_content = str_replace( 'http://{{doubleoptin}}', '{subscribe_url}', $html_content );
588 $html_content = str_replace( 'https://{{ doubleoptin }}', '{subscribe_url}', $html_content );
589 $html_content = str_replace( 'http://{{ doubleoptin }}', '{subscribe_url}', $html_content );
590 $html_content = str_replace( '[DOUBLEOPTIN]', '{subscribe_url}', $html_content );
591 $html_content = preg_replace($search_value, '{subscribe_url}', $html_content);
592 $html_content = str_replace(
593 '{subscribe_url}', add_query_arg(
594 array(
595 'sib_action' => 'subscribe',
596 'code' => $code,
597 ), home_url()
598 ), $html_content
599 );
600
601 if ( 'yes' == $home_settings['activate_email'] ) {
602
603 $data = array(
604 'replyTo' => array('email' => $from[0]),
605 'to' => array(array('email' => $to_email)),
606 );
607 $data['sender'] = [ 'email' => $from[0], 'name' => $from[1] ];
608 $data['htmlContent'] = $html_content;
609 $data['subject'] = $subject;
610
611 $res = $mailin->sendEmail( $data );
612
613 } else {
614 $headers[] = 'Content-Type: text/html; charset=UTF-8';
615 $headers[] = "From: $sender_name <$sender_email>";
616 @wp_mail( $to_email, $subject, $html_content, $headers );
617 }
618 }
619
620 /**
621 * Get email template by type (test, confirmation, double-optin).
622 *
623 * @param string $type - email template type.
624 * @return array
625 */
626 static function get_email_template( $type = 'test' ) {
627 $lang = get_bloginfo( 'language' );
628 if ( 'fr-FR' == $lang ) {
629 $file = 'temp_fr-FR';
630 } else {
631 $file = 'temp';
632 }
633
634 $file_path = SIB_Manager::$plugin_dir . '/inc/templates/' . $type . '/';
635 // get html content.
636 $html_content = file_get_contents( $file_path . $file . '.html' );
637 // get text content.
638 $text_content = file_get_contents( $file_path . $file . '.txt' );
639 $templates = array(
640 'html_content' => $html_content,
641 'text_content' => $text_content,
642 );
643 return $templates;
644 }
645
646 /**
647 * Sync wp users to contact list.
648 *
649 * @param string $users_info - user's attributes.
650 * @param array $list_ids - desired lists
651 * @return array|mixed|object
652 */
653 public static function sync_users( $users_info, $list_ids ) {
654 $client = new SendinblueApiClient();
655 $data = array(
656 'fileBody' => $users_info,
657 'listIds' => $list_ids,
658 );
659 $client->importContacts($data);
660 if ( SendinblueApiClient::RESPONSE_CODE_ACCEPTED == $client->getLastResponseCode() ) {
661 $response = array(
662 'code' => 'success',
663 'message' => __( 'Contact synchronization has started.', 'mailin' )
664 );
665 } else {
666 $response = array(
667 'code' => 'failed',
668 'message' => __( 'Something went wrong. PLease try again.', 'mailin' )
669 );
670 }
671 return $response;
672 }
673
674 /**
675 * Subscribe process for double optin subscribers
676 */
677 public static function subscribe( $contact_info ) {
678 if ( false != $contact_info ) {
679 $email = $contact_info['email'];
680 $info = maybe_unserialize( $contact_info['info'] );
681 $list_id = maybe_unserialize( $contact_info['listIDs'] );
682 $form_id = $contact_info['frmid'];
683 $current_form = SIB_Forms::getForm( $form_id );
684 $unlinkedLists = null;
685 if( isset( $info['unlinkedLists'] ) )
686 {
687 $unlinkedLists = $info['unlinkedLists'];
688 unset($info['unlinkedLists']);
689 }
690 if ( '1' == $current_form['isDopt'] )
691 {
692 SIB_API_Manager::send_comfirm_email( $email, 'confirm', $current_form['confirmID'], $info );
693 }
694
695 if( $unlinkedLists != null ) {
696 self::create_subscriber( $email, $list_id, $info, 'subscribe', $unlinkedLists );
697 }
698 else {
699 self::create_subscriber( $email, $list_id, $info, 'subscribe' );
700 }
701
702 }
703
704 if ( '' != $contact_info['redirectUrl'] ) {
705 wp_redirect( $contact_info['redirectUrl'] );
706 exit;
707 }
708
709 $type = 'Subscribe';
710 self::template_subscribe( $type );
711 exit;
712 }
713
714 /**
715 * Unsubscribe process
716 */
717 function unsubscribe() {
718 $mailin = new SendinblueApiClient();
719 $code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : '' ;
720 $list_id = isset( $_GET['li'] ) ? intval( $_GET['li'] ) : '' ;
721
722 $email = base64_decode( strtr( $code, '-_', '+/' ) );
723 $data = array(
724 'email' => $email,
725 );
726 $response = $mailin->get_user( $data );
727
728 if ($mailin->getLastResponseCode() === SendinblueApiClient::RESPONSE_CODE_OK) {
729 $attributes = $response['attributes'];
730
731 $listid = $response['listIds'];
732
733 $blacklisted = $response['emailBlacklisted'];
734 $diff_listid = array_diff( $listid, array( $list_id ) );
735
736 if ( count( $diff_listid ) == 0 ) {
737 $blacklisted = true;
738 $diff_listid = $listid;
739 }
740 $data = array(
741 'email' => $email,
742 'data' =>'{"listIds":'.$diff_listid.',"emailBlacklisted":'.$blacklisted.'}'
743 );
744 $mailin->updateUser( $data["email"],$data["data"] );
745 }
746 ?>
747 <body style="margin:0; padding:0;">
748 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
749 <tbody>
750 <tr style="border-collapse:collapse;">
751 <td style="border-collapse:collapse;" align="center">
752 <table cellpadding="0" cellspacing="0" border="0" width="540">
753 <tbody>
754 <tr>
755 <td style="line-height:0; font-size:0;" height="20"></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">
763 <div
764 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
765 <?php esc_attr_e( 'Unsubscribe', 'mailin' ); ?></div>
766 </td>
767 </tr>
768 </tbody>
769 </table>
770 <table cellpadding="0" cellspacing="0" border="0" width="540">
771 <tbody>
772 <tr>
773 <td style="line-height:0; font-size:0;" height="20"></td>
774 </tr>
775 </tbody>
776 </table>
777 <table cellpadding="0" cellspacing="0" border="0" width="540">
778 <tbody>
779 <tr>
780 <td align="left">
781
782 <div
783 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
784 <br>
785 <?php esc_attr_e( 'Your request has been taken into account.', 'mailin' ); ?><br>
786 <br>
787 <?php esc_attr_e( 'The user has been unsubscribed', 'mailin' ); ?><br>
788 <br>
789 -Sendinblue
790 </div>
791 </td>
792 </tr>
793 </tbody>
794 </table>
795 <table cellpadding="0" cellspacing="0" border="0" width="540">
796 <tbody>
797 <tr>
798 <td style="line-height:0; font-size:0;" height="20">
799 </td>
800 </tr>
801 </tbody>
802 </table>
803 </td>
804 </tr>
805 </tbody>
806 </table>
807 </body>
808 <?php
809 exit;
810 }
811
812 /** Create list and attribute for double optin */
813 public static function create_default_dopt() {
814
815 $mailin = new SendinblueApiClient();
816
817 // add attribute.
818 $isEmpty = false;
819 $ret = $mailin->getAttributes();
820
821 if (isset($ret["attributes"])) {
822 foreach ($ret["attributes"] as $key => $value) {
823 if($value["category"] == "category" && 'DOUBLE_OPT-IN' == $value['name'] && ! empty( $value['enumeration'] ) ) {
824 $isEmpty = true;
825 }
826 }
827
828 if ( ! $isEmpty ) {
829 $data = [
830 'type' => 'category',
831 'enumeration' => [
832 [
833 'value' => 1,
834 'label' => 'Yes'
835 ],
836 [
837 'value' => 2,
838 'label' => 'No'
839 ],
840 ]
841 ];
842 $mailin->createAttribute('category', 'DOUBLE_OPT-IN', $data);
843 }
844 }
845 }
846
847 /** Template for subscriber and bot event using $type */
848 public static function template_subscribe( $type ) {
849 $site_domain = str_replace( 'https://', '', home_url() );
850 $site_domain = str_replace( 'http://', '', $site_domain );
851 ?>
852 <body style="margin:0; padding:0;">
853 <table style="background-color:#ffffff" cellpadding="0" cellspacing="0" border="0" width="100%">
854 <tbody>
855 <tr style="border-collapse:collapse;">
856 <td style="border-collapse:collapse;" align="center">
857 <table cellpadding="0" cellspacing="0" border="0" width="540">
858 <tbody>
859 <tr>
860 <td style="line-height:0; font-size:0;" height="20"></td>
861 </tr>
862 </tbody>
863 </table>
864 <table cellpadding="0" cellspacing="0" border="0" width="540">
865 <tbody>
866 <tr>
867 <td style="line-height:0; font-size:0;" height="20">
868 <div
869 style="font-family:arial,sans-serif; color:#61a6f3; font-size:20px; font-weight:bold; line-height:28px;">
870 <?php
871 if ( 'Subscribe' === $type ) {
872 esc_attr_e( 'Thank you for subscribing', 'mailin' );
873 } elseif ( 'Bot Event' === $type ) {
874 esc_attr_e( 'Please Try Again', 'mailin' );
875 }
876 ?>
877 </div>
878 </td>
879 </tr>
880 </tbody>
881 </table>
882 <table cellpadding="0" cellspacing="0" border="0" width="540">
883 <tbody>
884 <tr>
885 <td style="line-height:0; font-size:0;" height="20"></td>
886 </tr>
887 </tbody>
888 </table>
889 <table cellpadding="0" cellspacing="0" border="0" width="540">
890 <tbody>
891 <tr>
892 <td align="left">
893
894 <div
895 style="font-family:arial,sans-serif; font-size:14px; margin:0; line-height:24px; color:#555555;">
896 <br>
897 <?php
898 if ( 'Subscribe' === $type ) {
899 echo esc_attr__( 'You have just subscribed to the newsletter of ', 'mailin' ) . esc_attr( $site_domain ) . ' .'; }
900 ?>
901 <br><br>
902 <?php esc_attr_e( '-Sendinblue', 'mailin' ); ?></div>
903 </td>
904 </tr>
905 </tbody>
906 </table>
907 <table cellpadding="0" cellspacing="0" border="0" width="540">
908 <tbody>
909 <tr>
910 <td style="line-height:0; font-size:0;" height="20">
911 </td>
912 </tr>
913 </tbody>
914 </table>
915 </td>
916 </tr>
917 </tbody>
918 </table>
919 </body>
920 <?php
921 }
922 }
923 }
924