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