1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726:
<?php
class Inbound_Mail_Daemon {
static $table_name;
static $email_service;
static $send_limit;
static $thread_limit;
static $timestamp;
static $dom;
static $row;
static $email_settings;
static $templates;
static $tags;
static $email;
static $results;
static $response;
static $error_mode;
static $last;
static $first;
function __construct() {
self::load_static_vars();
self::load_hooks();
}
public static function load_static_vars() {
global $wpdb, $inbound_settings;
self::$email_service = (isset($inbound_settings['mailer']['mail-service'])) ? $inbound_settings['mailer']['mail-service'] : 'sparkpost';
self::$send_limit = (isset($inbound_settings['mailer']['processing-limit'])) ? $inbound_settings['mailer']['processing-limit'] : 100;
self::$thread_limit = (isset($inbound_settings['mailer']['thread-limit'])) ? $inbound_settings['mailer']['thread-limit'] : 1;
self::$table_name = $wpdb->prefix . "inbound_email_queue";
self::$timestamp = gmdate("Y-m-d\\TG:i:s\\Z");
self::$error_mode = Inbound_Options_API::get_option('inbound-email', 'errors-detected', false);
}
public static function load_hooks() {
if (!self::$email_service) {
return;
}
add_action('inbound_mailer_heartbeat', array(__CLASS__, 'process_mail_queue'));
add_filter('init', array(__CLASS__, 'process_mail_queue'), 12);
}
public static function process_mail_queue() {
if (!isset($_GET['forceprocess']) && current_filter() == 'init') {
return;
}
self::send_automated_emails();
self::send_batch_emails();
}
public static function toggle_email_type() {
add_filter('wp_mail_content_type', array(__CLASS__, 'toggle_email_type_html'));
}
public static function toggle_email_type_html($type) {
return 'text/html';
}
public static function toggle_dom_parser() {
self::$dom = new DOMDocument;
}
public static function rebuild_links($html) {
preg_match_all('/href="([^\s"]+)/', $html, $links);
if (!$links) {
return $html;
}
foreach ($links[1] as $link) {
if (strstr($link, '?token=') || !strstr($link, '://')) {
continue;
}
$safe_link = Inbound_API::analytics_track_links(array(
'email_id' => self::$row->email_id,
'lead_lists' => implode(',', self::$email_settings['recipients']),
'id' => self::$row->lead_id,
'lead_id' => self::$row->lead_id,
'page_id' => 0,
'vid' => self::$row->variation_id,
'url' => $link,
'utm_source' => urlencode(self::$email['email_title']),
'utm_medium' => 'email',
'utm_campaign' => '',
'tracking_id' => urlencode(self::$email['email_title'])
));
$html = str_replace("'" . $link . "'", "'" . $safe_link['url'] . "'", $html);
$html = str_replace('"' . $link . '"', '"' . $safe_link['url'] . '"', $html);
}
return $html;
}
public static function send_automated_emails() {
global $wpdb;
$query = "select * from " . self::$table_name . " WHERE `status` != 'processed' && `type` = 'automated' && `datetime` < '" . self::$timestamp . "' && `email_id` = `email_id` order by email_id ASC LIMIT " . self::$send_limit;
self::$results = $wpdb->get_results($query);
if (!self::$results) {
return;
}
self::$row = self::$results[0];
self::$email['email_title'] = get_the_title(self::$row->email_id);
self::$email_settings = Inbound_Email_Meta::get_settings(self::$row->email_id);
if (isset(self::$row->list_ids)) {
self::$email_settings['recipients'] = json_decode(self::$row->list_ids ,true);
}
self::get_templates();
self::get_tags();
self::toggle_email_type();
self::toggle_dom_parser();
$i=0;
foreach (self::$results as $row) {
self::$row = $row;
self::get_email();
switch (self::$email_service) {
case "sparkpost":
Inbound_Mailer_SparkPost::send_email( true );
break;
}
self::check_response();
if (self::$error_mode) {
error_log('error mode');
return;
}
self::delete_from_queue();
$i++;
}
}
public static function send_batch_emails() {
global $wpdb;
$query = "select * from " . self::$table_name . " WHERE `status` != 'processed' && `type` = 'batch' && email_id = email_id order by email_id ASC LIMIT " . self::$send_limit;
self::$results = $wpdb->get_results($query);
if (!self::$results) {
return;
}
$wordpress_date_time = date_i18n('Y-m-d G:i:s');
self::$row = self::$results[0];
self::$email['email_title'] = get_the_title(self::$row->email_id);
self::$email_settings = Inbound_Email_Meta::get_settings(self::$row->email_id);
self::get_templates();
self::get_tags();
self::toggle_email_type();
self::toggle_dom_parser();
$send_count = 1;
foreach (self::$results as $row) {
self::$row = $row;
if ($send_count > self::$send_limit) {
return;
}
$pass = self::check_stop_rules();
if (!$pass) {
self::delete_from_queue();
continue;
}
self::get_email();
if (!self::$email['send_address']) {
self::delete_from_queue();
continue;
}
switch (self::$email_service) {
case "sparkpost":
Inbound_Mailer_SparkPost::send_email();
break;
}
self::check_response();
if (self::$error_mode) {
return;
}
self::delete_from_queue();
$send_count++;
}
$count = $wpdb->get_var("SELECT COUNT(*) FROM " . self::$table_name . " where email_id = '" . self::$row->email_id . "'");
if ($count < 1) {
self::mark_email_sent();
}
}
public static function send_solo_email($args) {
global $wpdb;
if (!$args['email_id'] || !$args['email_address']) {
return;
}
self::$tags[$args['email_id']] = (isset($args['tags'])) ? $args['tags'] : array('test');
self::$row = new stdClass();
self::$row->email_id = $args['email_id'];
self::$row->variation_id = $args['vid'];
self::$row->lead_id = (isset($args['lead_id'])) ? $args['lead_id'] : 0;
self::$row->datetime = gmdate('Y-m-d h:i:s \G\M\T');
self::$row->rule_id = (isset($args['rule_id'])) ? $args['rule_id'] : 0;
self::$row->job_id = (isset($args['job_id'])) ? $args['job_id'] : 0;
self::$row->tokens = (isset($args['tokens'])) ? $args['tokens'] : '';
self::$email_settings = Inbound_Email_Meta::get_settings(self::$row->email_id);
self::$email_settings['recipients'] = (isset($args['lead_lists'])) ? $args['lead_lists'] : array();
self::get_templates(self::$row->variation_id);
self::toggle_dom_parser();
self::$email['send_address'] = $args['email_address'];
self::$email['subject'] = self::get_variation_subject();
self::$email['from_name'] = self::get_variation_from_name();
self::$email['from_email'] = self::get_variation_from_email();
self::$email['email_title'] = get_the_title(self::$row->email_id);
self::$email['reply_email'] = self::get_variation_reply_email();
if (isset($args['is_direct']) && $args['is_direct']) {
$email = get_post($args['email_id']);
self::$email['body'] = do_shortcode($email->post_content);
} else {
self::$email['body'] = self::get_email_body();
}
if (isset($args['is_test']) && $args['is_test']) {
self::$email['is_test'] = true;
}
switch (self::$email_service) {
case "sparkpost":
Inbound_Mailer_SparkPost::send_email(true);
break;
}
return self::$response;
}
public static function delete_from_queue() {
global $wpdb;
$query = "delete from " . self::$table_name . " where `id` = '" . self::$row->id . "'";
$wpdb->query($query);
}
public static function mark_email_sent() {
$wordpress_date_time = date_i18n('Y-m-d G:i:s');
$today = new DateTime($wordpress_date_time);
$schedule_date = new DateTime(self::$row->datetime);
$interval = $today->diff($schedule_date);
$status = ($interval->format('%R') == '+') ? 'scheduled' : 'sent';
$args = array(
'ID' => self::$row->email_id,
'post_status' => $status,
);
wp_update_post($args);
}
public static function get_templates($variation_id = null) {
self::$templates = array();
if (!isset(self::$email_settings['variations']) || !self::$email_settings['variations']) {
return array();
}
foreach (self::$email_settings['variations'] as $vid => $variation) {
if ($variation_id !== null && $vid != $variation_id) {
continue;
}
$permalink = get_post_permalink(self::$row->email_id);
$query_args = array('inbvid' => $vid, 'disable_shortcodes' => true);
if (isset(self::$row->post_id)) {
$query_args['post_id'] = self::$row->post_id ;
}
if (self::$row->tokens && strlen(self::$row->tokens) < 1000 ) {
$token = Inbound_API::analytics_get_tracking_code(json_decode(self::$row->tokens,true));
$query_args['tokens'] = $token ;
}
$permalink = add_query_arg( $query_args , $permalink);
self::$templates[self::$row->email_id][$vid] = self::get_variation_html($permalink);
}
}
public static function get_tags() {
$array = array();
$array[] = self::$email_settings['email_type'];
self::$tags[self::$row->email_id] = $array;
}
public static function get_email() {
self::$email['send_address'] = Leads_Field_Map::get_field(self::$row->lead_id, 'wpleads_email_address');
self::$email['from_name'] = self::get_variation_from_name();
self::$email['from_email'] = self::get_variation_from_email();
self::$email['reply_email'] = self::get_variation_reply_email();
self::$email['body'] = self::get_email_body();
self::$email['subject'] = self::get_variation_subject();
}
public static function get_email_body() {
$last = self::$last;
self::$email_settings['recipients'] = (isset(self::$email_settings['recipients'])) ? self::$email_settings['recipients'] : array();
$html = self::$templates[self::$row->email_id][self::$row->variation_id];
$html = str_replace('[lead-field ', '[lead-field lead_id="' . self::$row->lead_id . '" ', $html);
$unsubscribe = do_shortcode('[unsubscribe-link lead_id="' . self::$row->lead_id . '" list_ids="' . implode(',', self::$email_settings['recipients']) . '" email_id="' . self::$row->email_id . '" rule_id="' . self::$row->rule_id . '" job_id="' . self::$row->job_id . '"]');
$html = str_replace('[unsubscribe-link]', $unsubscribe, $html);
$html = Inbound_List_Double_Optin::add_confirm_link_shortcode_params($html, array(
'lead_id' => self::$row->lead_id,
'list_ids' => self::$email_settings['recipients'],
'email_id' => self::$row->email_id
));
$html = str_replace('”', '"', $html);
$pattern = '/\[(cta).*?\]/';
$content = preg_replace($pattern, '', $html);
$html = do_shortcode($html);
$html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);
return $html;
}
public static function get_variation_html($permalink) {
$response = wp_remote_get($permalink, array('timeout' => 120));
if (is_wp_error($response)) {
error_log(print_r($response, true));
print_r($response);
exit;
}
$html = wp_remote_retrieve_body($response);
return $html;
}
public static function get_variation_subject() {
$subject = str_replace('[lead-field ', '[lead-field lead_id="' . self::$row->lead_id . '" ', self::$email_settings['variations'] [self::$row->variation_id] ['subject']);
$email_tokens = json_decode(self::$row->tokens , true);
if (isset(self::$row->post_id)) {
$post = get_post(self::$row->post_id);
$email_tokens = (array) $post;
$email_tokens['permalink'] = get_the_permalink((int) self::$row->post_id);
$email_tokens['featured_image'] = wp_get_attachment_url(get_post_thumbnail_id(self::$row->post_id));
wp_reset_query();
}
$subject = Inbound_Mailer_Tokens::process_tokens($subject , $email_tokens );
return do_shortcode($subject);
}
public static function get_variation_from_name() {
return self::$email_settings['variations'] [self::$row->variation_id] ['from_name'];
}
public static function get_variation_from_email() {
return self::$email_settings['variations'] [self::$row->variation_id] ['from_email'];
}
public static function get_variation_reply_email() {
self::$email_settings['variations'][self::$row->variation_id]['reply_email'] = str_replace('[lead-field ', '[lead-field lead_id="' . self::$row->lead_id . '" ', self::$email_settings['variations'][self::$row->variation_id]['reply_email']);
self::$email_settings['variations'][self::$row->variation_id]['reply_email'] = Inbound_Mailer_Tokens::process_tokens(
self::$email_settings['variations'][self::$row->variation_id]['reply_email'] ,
json_decode(self::$row->tokens , true)
);
return do_shortcode(self::$email_settings['variations'][self::$row->variation_id]['reply_email']);
}
public static function get_text_version() {
}
public static function check_stop_rules() {
$stop_rules = Inbound_Mailer_Unsubscribe::get_stop_rules(self::$row->lead_id);
$wordpress_date_time = date_i18n('Y-m-d G:i:s');
$passed = 0;
$failed = 0;
foreach (self::$email_settings['recipients'] as $list_id) {
if (!isset($stop_rules[$list_id])) {
$passed++;
continue;
}
if ($stop_rules[$list_id] == 'unsubscribed') {
$failed++;
continue;
}
if (!is_string($stop_rules[$list_id])) {
$passed++;
continue;
}
if (strtotime($stop_rules[$list_id]) > strtotime($wordpress_date_time)) {
$failed++;
} else {
$passed++;
Inbound_Mailer_Unsubscribe::remove_stop_rule(self::$row->lead_id, $list_id);
}
}
if ($passed >= $failed) {
return true;
} else {
return false;
}
}
public static function check_response() {
global $current_user, $post;
$user_id = $current_user->ID;
if (isset(self::$response['status']) && self::$response['status'] == 'error' || isset(self::$response['error'])) {
if (isset($resonse['description'])) {
self::$response['message'] = self::$response['message'] . ' : ' . self::$response['description'];
}
Inbound_Options_API::update_option('inbound-email', 'errors-detected', self::$response['message']);
self::$error_mode = true;
return;
}
if (self::$error_mode) {
Inbound_Options_API::update_option('inbound-email', 'errors-detected', false);
self::$error_mode = false;
}
}
public static function time_elapsed( $last = null)
{
self::$last = ($last) ? $last : self::$last;
$now = microtime(true);
if (self::$last != null) {
$elapsed = '<!---- ' . date("H:i:s", $now - self::$first) . ' ' . date("H:i:s", $now - self::$last) . ' now: '.$now.' last: '.self::$last.' -->';
}
self::$last = ($last) ? $last :$now;
self::$first = (self::$first) ? self::$first : self::$last;
return $elapsed;
}
}
function load_inbound_mail_daemon() {
new Inbound_Mail_Daemon();
}
add_action('init', 'load_inbound_mail_daemon', 2);