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:
<?php
if (!class_exists('Inbound_Ajax')) {
class Inbound_Ajax {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action('wp_ajax_inbound_track_lead', array(__CLASS__, 'track_lead'));
add_action('wp_ajax_nopriv_inbound_track_lead', array(__CLASS__, 'track_lead'));
}
public static function track_lead() {
global $wpdb;
if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
return;
}
$lead_data['lead_id'] = (isset($_POST['wp_lead_id'])) ? (int) $_POST['wp_lead_id'] : '';
$lead_data['nature'] = (isset($_POST['nature'])) ? sanitize_text_field($_POST['nature']) : 'non-conversion';
$lead_data['json'] = (isset($_POST['json'])) ? addslashes($_POST['json']) : 0;
$lead_data['wp_lead_uid'] = (isset($_POST['wp_lead_uid'])) ? sanitize_text_field($_POST['wp_lead_uid']) : 0;
$lead_data['page_id'] = (isset($_POST['page_id'])) ? (int) $_POST['page_id'] : 0;
$lead_data['current_url'] = (isset($_POST['current_url'])) ? sanitize_text_field($_POST['current_url']) : 'notfound';
$lead_data['variation_id'] = (isset($_POST['variation_id'])) ? (int) $_POST['variation_id'] : '0';
$timezone_format = 'Y-m-d G:i:s T';
$lead_data['datetime'] = date_i18n($timezone_format);
$page_views = stripslashes($_POST['page_views']);
$page_views = ($page_views) ? $page_views : '';
$lead_data['event_details']['funnel'] = json_decode($page_views,true);
$lead_data['funnel'] = $page_views;
if (isset($_COOKIE['inbound_page_views']) && !$page_views) {
$_SESSION['inbound_page_views'] = stripslashes($_COOKIE['inbound_page_views']);
} else {
$_SESSION['inbound_page_views'] = $page_views;
}
if ($lead_data['lead_id']) {
self::update_page_view_obj($lead_data);
self::set_current_lists($lead_data['lead_id']);
}
if ($lead_data['page_id']) {
Inbound_Events::store_page_view($lead_data);
}
$cta_impressions = ( isset($_POST['cta_impressions']) ) ? json_decode(stripslashes($_POST['cta_impressions']),true) : array();
if (is_array($cta_impressions)) {
foreach ( $cta_impressions as $cta_id => $vid ) {
$lead_data['cta_id'] = (int) $cta_id;
$lead_data['variation_id'] = (int) $vid;
do_action('wp_cta_record_impression', $lead_data );
}
}
do_action('lp_record_impression', $lead_data['page_id'], $_POST['post_type'], $_POST['variation_id']);
die();
}
public static function update_page_view_obj($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];
$timeout = abs(strtotime($last_view) - strtotime($wordpress_date_time));
}
if ($timeout >= 30) {
$page_view_data[$lead_data['page_id']][$current_count + 1] = $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 set_current_lists($lead_id) {
$terms = get_the_terms( $lead_id , 'wplead_list_category' );
if ( $terms && ! is_wp_error( $terms ) ) {
$lead_list = array();
$count = 0;
foreach ( $terms as $term ) {
$lead_list[] = $term->term_id;
$count++;
}
$list_array = json_encode(array('ids' => $lead_list));
setcookie('wp_lead_list', $list_array, (int) ( time() + (20 * 365 * 24 * 60 * 60) ), '/');
}
}
}
$Inbound_Ajax = new Inbound_Ajax();
}