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:
<?php
class Leads_Tracking {
public function __construct() {
self::add_hooks();
}
public static function add_hooks() {
add_action('wp_head', array(__CLASS__, 'set_cookies'));
add_action('transition_comment_status', array(__CLASS__, 'track_comment_approval'), 10, 3);
add_action('wp_insert_comment', array(__CLASS__, 'track_comment_inserts'), 10, 2);
add_action('wp_ajax_inbound_search_store', array(__CLASS__, 'ajax_inbound_search_store'), 10, 1);
add_action('wp_ajax_nopriv_inbound_search_store', array(__CLASS__, 'ajax_inbound_search_store'), 10, 1);
}
public static function set_cookies() {
global $wpdb;
if (isset($_GET['wpl_email'])) {
$lead_id = $_GET['wpl_email'];
$query = $wpdb->prepare(
'SELECT ID FROM ' . $wpdb->posts . '
WHERE post_title = %s
AND post_type = \'wp-lead\'',
$lead_id
);
$wpdb->query($query);
if ($wpdb->num_rows) {
$lead_ID = $wpdb->get_var($query);
setcookie('wp_lead_id', $lead_ID, time() + (20 * 365 * 24 * 60 * 60), '/');
}
}
}
public static function cookie_lead_lists($lead_id, $lists = null) {
if (is_array($lists)) {
$lead_lists = array('ids' => $lists);
} else {
$terms = get_the_terms($lead_id, 'wplead_list_category');
$lists = array();
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$lists[] = $term->term_id;
}
}
}
$lead_lists = json_encode(array('ids' => $lists));
setcookie('wp_lead_list', $lead_lists, (int) (time() + (20 * 365 * 24 * 60 * 60) ), '/');
}
public static function cookie_lead_tags($lead_id, $tags = null) {
$lead_tags = array();
if (is_array($tags)) {
$lead_tags = array('ids' => $tags);
} else {
$terms = get_the_terms($lead_id, 'lead-tags');
$tags = array();
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$tags[] = $term->term_id;
}
}
$lead_tags = array('ids' => $tags);
}
$tags_json = json_encode($lead_tags);
setcookie('wp_lead_tags', $tags_json, time() + (20 * 365 * 24 * 60 * 60), '/');
}
public static function update_page_views_object($lead_data) {
if (!$lead_data['page_id']) {
return;
}
$current_page_view_count = get_post_meta($lead_data['lead_id'], 'wpleads_page_view_count', true);
$increment_page_views = $current_page_view_count + 1;
update_post_meta($lead_data['lead_id'], 'wpleads_page_view_count', $increment_page_views);
$time = current_time('timestamp', 0);
$wordpress_date_time = date("Y-m-d G:i:s T", $time);
$page_view_data = get_post_meta($lead_data['lead_id'], 'page_views', TRUE);
if ($page_view_data) {
$current_count = 0;
$timeout = 30;
$page_view_data = json_decode($page_view_data, true);
if (isset($page_view_data[$lead_data['page_id']])) {
$current_count = count($page_view_data[$lead_data['page_id']]);
$last_view = $page_view_data[$lead_data['page_id']][$current_count - 1];
$timeout = abs(strtotime($last_view) - strtotime($wordpress_date_time));
}
if ($timeout >= 30) {
$page_view_data[$lead_data['page_id']][$current_count] = $wordpress_date_time;
$page_view_data = json_encode($page_view_data);
update_post_meta($lead_data['lead_id'], 'page_views', $page_view_data);
}
} else {
$page_view_data = array();
$page_view_data[$lead_data['page_id']][0] = $wordpress_date_time;
$page_view_data = json_encode($page_view_data);
update_post_meta($lead_data['lead_id'], 'page_views', $page_view_data);
}
do_action('wplead_page_view', $lead_data);
}
public static function track_comment_approval($new_status, $old_status, $comment) {
if (!defined('INBOUND_PRO_CURRENT_VERSION')) {
if (get_option('wpl-main-comment-tracking', '') == 0) {
return;
}
} else {
$settings = Inbound_Options_API::get_option('inbound-pro', 'settings', array());
if ($settings['leads']['comment-tracking'] == 0) {
return;
}
}
if ($comment->comment_approved == 1) {
$lead_id = LeadStorage::lookup_lead_by_email($comment->comment_author_email);
if ($lead_id) {
if (!Inbound_Events::comment_exists($comment->comment_ID)) {
$agents_to_ignore = array('WooCommerce');
if (in_array($comment->comment_agent, $agents_to_ignore)) {
return;
}
$datetime = date_i18n('Y-m-d G:i:s T', strtotime($comment->comment_date));
$args = array(
'page_id' => $comment->comment_post_ID,
'lead_id' => $lead_id,
'comment_id' => $comment->comment_ID,
'event_details' => json_encode(array(
'comment_id' => $comment->comment_ID,
'comment_content' => ($comment->comment_content) ? $comment->comment_content : '',
'comment_author' => ($comment->comment_author) ? $comment->comment_author : '',
'comment_author_email' => ($comment->comment_author_email) ? $comment->comment_author_email : '',
'comment_author_url' => ($comment->comment_author_url) ? $comment->comment_author_url : '',
'comment_agent' => ($comment->comment_agent) ? $comment->comment_agent : '',
'comment_type' => ($comment->comment_type) ? $comment->comment_type : '',
'comment_parent' => ($comment->comment_parent) ? $comment->comment_parent : '',
'user_id' => ($comment->user_id) ? $comment->user_id : '',
'page_id' => $comment->comment_post_ID,
)),
'datetime' => $datetime,
);
Inbound_Events::store_comment_event($args);
}
}
}
if ($comment->comment_approved != 1) {
$lead_id = LeadStorage::lookup_lead_by_email($comment->comment_author_email);
if ($lead_id) {
Inbound_Events::remove_comment_event((int)$comment->comment_ID);
}
}
}
public static function track_comment_inserts($id, $comment) {
global $inbound_settings;
$inbound_settings['leads']['comment-tracking'] = (isset($inbound_settings['leads']['comment-tracking'])) ? $inbound_settings['leads']['comment-tracking'] : 1;
if (!defined('INBOUND_PRO_CURRENT_VERSION')) {
if (get_option('wpl-main-comment-tracking', '') == 0) {
return;
}
} else {
$settings = Inbound_Options_API::get_option('inbound-pro', 'settings', array());
if ($inbound_settings['leads']['comment-tracking'] == 0) {
return;
}
}
if ($comment->comment_approved == 1) {
$lead_id = LeadStorage::lookup_lead_by_email($comment->comment_author_email);
if ($lead_id) {
$agents_to_ignore = array('WooCommerce');
if (in_array($comment->comment_agent, $agents_to_ignore)) {
return;
}
$datetime = date_i18n('Y-m-d G:i:s');
$args = array(
'page_id' => $comment->comment_post_ID,
'lead_id' => $lead_id,
'comment_id' => $comment->comment_ID,
'event_details' => json_encode(array(
'comment_id' => $comment->comment_ID,
'comment_content' => ($comment->comment_content) ? $comment->comment_content : '',
'comment_author' => ($comment->comment_author) ? $comment->comment_author : '',
'comment_author_email' => ($comment->comment_author_email) ? $comment->comment_author_email : '',
'comment_author_url' => ($comment->comment_author_url) ? $comment->comment_author_url : '',
'comment_agent' => ($comment->comment_agent) ? $comment->comment_agent : '',
'comment_type' => ($comment->comment_type) ? $comment->comment_type : '',
'comment_parent' => ($comment->comment_parent) ? $comment->comment_parent : '',
'user_id' => ($comment->user_id) ? $comment->user_id : '',
'page_id' => $comment->comment_post_ID,
)),
'datetime' => $datetime,
);
Inbound_Events::store_comment_event($args);
}
}
}
public static function ajax_inbound_search_store() {
error_log('ajax_inbound_search_store');
$timezone = get_option('gmt_offset');
$data = json_decode(stripslashes(urldecode($_POST['data'])), true);
$lead_id = intval($_POST['lead_id']);
$clean_data = array();
foreach ($data as $key => $value) {
error_log('data');
$clean_data[$key] = array_map(function ($sub_value) {
if (!isset($sub_value) && empty($sub_value)) {
return 0;
}
if (urldecode($sub_value) === $sub_value) {
return sanitize_text_field($sub_value);
}
return sanitize_text_field(urldecode($sub_value));
}, $value);
}
if (!$lead_id) {
$lead_id = 0;
$emails = array();
foreach ($clean_data as $key => $value) {
if (isset($value['lead_id']) && !empty($value['lead_id'])) {
$lead_id = $value['lead_id'];
break;
}
if (is_email($value['email']) && !empty($value['email'])) {
$email[] = $value['email'];
}
}
}
if ($lead_id === 0) {
if (!empty($emails)) {
$counted_emails = array_count_values($emails);
foreach ($counted_emails as $email => $count) {
$lead = get_page_by_title($email, 'OBJECT', 'wp-lead');
if (!empty($lead) && !null) {
$lead_id = $lead->ID;
break;
}
}
}
if ($lead_id == 0) {
error_log('die');
die();
}
}
if (!wp_verify_nonce($_POST['nonce'], 'inbound_lead_' . $lead_id . '_nonce')) {
error_log('nonce');
die();
}
$lead = get_post($lead_id);
if (!$lead) {
error_log('no lead');
die();
}
foreach ($clean_data as $key => $value) {
if (!empty($timezone) || $timezone === 0) {
$value['timestamp'] += ($timezone * 3600);
}
$args = array(
'page_id' => $value['page_id'],
'lead_id' => $lead_id,
'lead_uid' => $value['user_UID'],
'variation_id' => $value['variation'],
'event_details' => json_encode(array(
'search_data' => $value['search_data'],
'post_type' => $value['post_type'],
'ip_address' => $value['ip_address'],
'page_id' => $value['page_id'],
'datetime' => date_i18n('Y-m-d G:i:s T', $value['timestamp']),
)),
'source' => $value['source'],
'datetime' => date('Y-m-d G:i:s T', $value['timestamp']),
);
Inbound_Events::store_search_event($args);
error_log('stored');
}
wp_send_json(array('success' => __('Searches successfully stored!', 'inbound-pro')));
}
}
new Leads_Tracking;