function inboundrocket_ec_callback()
 {
     if (!defined('DOING_AJAX') || !DOING_AJAX) {
         exit;
     }
     $hashkey = sanitize_textfield($_POST['hashkey']);
     include_once INBOUNDROCKET_PLUGIN_DIR . "/inc/class-notifier.php";
     $ir_notifier = new IR_Notifier();
     $ir_notifier->send_new_lead_email($hashkey);
 }
/**
 * Inserts a new form submisison into the ir_submissions table and ties to the submission to a row in ir_leads
 *
 * @return	int
 */
function inboundrocket_insert_form_submission()
{
    global $wpdb;
    if (!defined('DOING_AJAX') || !DOING_AJAX) {
        exit;
    }
    if (inboundrocket_ignore_logged_in_user()) {
        return FALSE;
    }
    $hashkey = isset($_POST['ir_id']) ? sanitize_text_field($_POST['ir_id']) : inboundrocket_insert_lead();
    $submission_hash = sanitize_text_field($_POST['ir_submission_id']);
    $page_title = sanitize_text_field($_POST['ir_title']);
    $page_url = sanitize_text_field($_POST['ir_url']);
    $form_json = sanitize_text_field($_POST['ir_fields']);
    $email = sanitize_text_field($_POST['ir_email']);
    $first_name = sanitize_text_field($_POST['ir_first_name']);
    $last_name = sanitize_text_field($_POST['ir_last_name']);
    $phone = sanitize_text_field($_POST['ir_phone']);
    $form_selector_id = sanitize_text_field($_POST['ir_form_selector_id']);
    $form_selector_classes = sanitize_text_field($_POST['ir_form_selector_classes']);
    $options = get_option('inboundrocket_options');
    $ir_admin_email = isset($options['ir_email']) ? $options['ir_email'] : '';
    $contact_type = 'contact';
    // used at bottom of function
    // Check to see if the form_hashkey exists, and if it does, don't run the insert or send the email
    $q = $wpdb->prepare("SELECT form_hashkey FROM {$wpdb->ir_submissions} WHERE form_hashkey = %s AND form_deleted = 0", $submission_hash);
    $submission_hash_exists = $wpdb->get_var($q);
    if ($submission_hash_exists) {
        // The form has been inserted successful so send back a trigger to clear the cached submission cookie on the front end
        return 1;
        exit;
    }
    // Get the contact row tied to hashkey
    $q = $wpdb->prepare("SELECT * FROM {$wpdb->ir_leads} WHERE hashkey = %s AND lead_deleted = 0", $hashkey);
    $contact = $wpdb->get_row($q);
    // Check if either of the names field are set and a value was filled out that's different than the existing name field
    $lead_first_name = isset($contact->lead_first_name) ? $contact->lead_first_name : '';
    if (strlen($first_name) && $lead_first_name != $first_name) {
        $lead_first_name = $first_name;
    }
    $lead_last_name = isset($contact->lead_last_name) ? $contact->lead_last_name : '';
    if (strlen($last_name) && $lead_last_name != $last_name) {
        $lead_last_name = $last_name;
    }
    // Check for existing contacts based on whether the email is present in the contacts table
    $q = $wpdb->prepare("SELECT lead_email, hashkey, merged_hashkeys FROM {$wpdb->ir_leads} WHERE lead_email = %s AND hashkey != %s AND lead_deleted = 0", $email, $hashkey);
    $existing_contacts = $wpdb->get_results($q);
    // Setup the string for the existing hashkeys
    $existing_contact_hashkeys = isset($contact->merged_hashkeys) ? $contact->merged_hashkeys : '';
    if (count($existing_contacts) > 0 && $contact->merged_hashkeys) {
        $existing_contact_hashkeys .= ',';
    }
    // Do some merging if the email exists already in the contact table
    if (count($existing_contacts)) {
        for ($i = 0; $i < count($existing_contacts); $i++) {
            // Start with the existing contact's hashkeys and create a string containg comma-deliminated hashes
            $existing_contact_hashkeys .= "'" . $existing_contacts[$i]->hashkey . "'";
            // Add any of those existing contact row's merged hashkeys
            if ($existing_contacts[$i]->merged_hashkeys) {
                $existing_contact_hashkeys .= "," . $existing_contacts[$i]->merged_hashkeys;
            }
            // Add a comma delimiter
            if ($i != count($existing_contacts) - 1) {
                $existing_contact_hashkeys .= ",";
            }
        }
        // Remove duplicates from the array
        $existing_contact_hashkeys = implode(',', array_unique(explode(',', $existing_contact_hashkeys)));
        // Safety precaution - trim any trailing commas
        $existing_contact_hashkeys = rtrim($existing_contact_hashkeys, ',');
        // Update all the previous pageviews to the new hashkey
        $q = $wpdb->prepare("UPDATE {$wpdb->ir_pageviews} SET lead_hashkey = %s WHERE lead_hashkey IN ( {$existing_contact_hashkeys} )", $hashkey);
        $wpdb->query($q);
        // Update all the previous submissions to the new hashkey
        $q = $wpdb->prepare("UPDATE {$wpdb->ir_submissions} SET lead_hashkey = %s WHERE lead_hashkey IN ( {$existing_contact_hashkeys} )", $hashkey);
        $wpdb->query($q);
        // Update all the previous submissions to the new hashkey
        $q = $wpdb->prepare("UPDATE {$wpdb->ir_tag_relationships} SET contact_hashkey = %s WHERE contact_hashkey IN ( {$existing_contact_hashkeys} )", $hashkey);
        $wpdb->query($q);
        // "Delete" all the old leads from the leads table
        $wpdb->query("UPDATE {$wpdb->ir_leads} SET lead_deleted = 1 WHERE hashkey IN ( {$existing_contact_hashkeys} )");
    }
    // Prevent duplicate form submission entries by deleting existing submissions if it didn't finish the process before the web page refreshed
    $q = $wpdb->prepare("UPDATE {$wpdb->ir_submissions} SET form_deleted = 1 WHERE form_hashkey = %s", $submission_hash);
    $wpdb->query($q);
    // Insert the form fields and hash into the submissions table
    $result = $wpdb->insert($wpdb->ir_submissions, array('form_hashkey' => $submission_hash, 'lead_hashkey' => $hashkey, 'form_page_title' => $page_title, 'form_page_url' => $page_url, 'form_fields' => $form_json, 'form_selector_id' => $form_selector_id, 'form_selector_classes' => $form_selector_classes), array('%s', '%s', '%s', '%s', '%s', '%s', '%s'));
    // Update the contact with the new email, new names, status and merged hashkeys
    $q = $wpdb->prepare("UPDATE {$wpdb->ir_leads} SET lead_email = %s, lead_first_name = %s, lead_last_name = %s, merged_hashkeys = %s WHERE hashkey = %s", $email, $lead_first_name, $lead_last_name, $existing_contact_hashkeys, $hashkey);
    $rows_updated = $wpdb->query($q);
    // Apply the tag relationship to contacts for form id rules
    if ($form_selector_id) {
        $q = $wpdb->prepare("SELECT tag_id, tag_synced_lists FROM {$wpdb->ir_tags} WHERE tag_form_selectors LIKE '%%%s%%' AND tag_deleted = 0", '#' . $form_selector_id);
        $tagged_lists = $wpdb->get_results($q);
        if (count($tagged_lists)) {
            foreach ($tagged_lists as $list) {
                $tag_added = inboundrocket_apply_tag_to_contact($list->tag_id, $contact->hashkey, $submission_hash);
                $contact_type = 'tagged contact';
                if ($tag_added && $list->tag_synced_lists || $contact->lead_email != $email && $list->tag_synced_lists) {
                    foreach (unserialize($list->tag_synced_lists) as $synced_list) {
                        // e.g. inboundrocket_constant_contact_connect_wp
                        $inboundrocket_esp_wp = 'inboundrocket_' . $synced_list['esp'] . '_connect_wp';
                        global ${$inboundrocket_esp_wp};
                        if (isset(${$inboundrocket_esp_wp}->activated) && ${$inboundrocket_esp_wp}->activated) {
                            ${$inboundrocket_esp_wp}->push_contact_to_list($synced_list['list_id'], $email, $first_name, $last_name, $phone);
                        }
                    }
                }
            }
        }
    }
    // Apply the tag relationship to contacts for class rules
    $form_classes = '';
    if ($form_selector_classes) {
        $form_classes = explode(',', $form_selector_classes);
    }
    if (is_array($form_classes) && count($form_classes) > 0) {
        foreach ($form_classes as $class) {
            $q = $wpdb->prepare("SELECT tag_id, tag_synced_lists FROM {$wpdb->ir_tags} WHERE tag_form_selectors LIKE '%%%s%%' AND tag_deleted = 0", '.' . $class);
            $tagged_lists = $wpdb->get_results($q);
            if (count($tagged_lists)) {
                foreach ($tagged_lists as $list) {
                    $tag_added = inboundrocket_apply_tag_to_contact($list->tag_id, $contact->hashkey, $submission_hash);
                    $contact_type = 'tagged contact';
                    if ($tag_added && $list->tag_synced_lists) {
                        foreach (unserialize($list->tag_synced_lists) as $synced_list) {
                            // e.g. inboundrocket_constant_contact_connect_wp
                            $inboundrocket_esp_wp = 'inboundrocket_' . $synced_list['esp'] . '_connect_wp';
                            global ${$inboundrocket_esp_wp};
                            if (isset(${$inboundrocket_esp_wp}->activated) && ${$inboundrocket_esp_wp}->activated) {
                                ${$inboundrocket_esp_wp}->push_contact_to_list($synced_list['list_id'], $email, $first_name, $last_name, $phone);
                            }
                        }
                    }
                }
            }
        }
    }
    $ir_emailer = new IR_Notifier();
    if ($ir_admin_email) {
        $ir_emailer->send_new_lead_email($hashkey);
    }
    // Send the contact notification email
    if (strstr($form_selector_id, 'welcome_bar')) {
        // Send the subscription confirmation kickback email
        /*$inboundrocket_subscribe_settings = get_option('inboundrocket_subscribe_options');
        		if ( isset($inboundrocket_subscribe_settings['ir_subscribe_confirmation']) && $inboundrocket_subscribe_settings['ir_subscribe_confirmation'] )
        			$ir_emailer->send_subscriber_confirmation_email($hashkey);*/
        $contact_type = 'subscriber';
    } else {
        if (strstr($form_selector_id, 'commentform')) {
            $contact_type = 'comment';
        }
    }
    echo $rows_updated;
    die;
}