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