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