/**
 * Check if the cookied hashkey has been merged with another contact. If it is, set visitor's cookie to new hashkey
 *
 * @echo	Hashkey from a merged_hashkeys row, FALSE if hashkey does not exist in a merged_hashkeys row
 */
function leadout_check_merged_contact()
{
    global $wpdb;
    global $wp_version;
    $stale_hash = $_POST['li_id'];
    $escaped_hash = '';
    if ($wp_version >= 4) {
        $escaped_hash = $wpdb->esc_like($stale_hash);
    } else {
        $escaped_hash = like_escape($stale_hash);
    }
    // Check if hashkey is in a merged contact
    $q = $wpdb->prepare("SELECT hashkey, merged_hashkeys FROM {$wpdb->li_leads} WHERE merged_hashkeys LIKE '%%%s%%'", $escaped_hash);
    $row = $wpdb->get_row($q);
    if (isset($row->hashkey) && $stale_hash) {
        // One final update to set all the previous pageviews to the new hashkey
        $q = $wpdb->prepare("UPDATE {$wpdb->li_pageviews} SET lead_hashkey = %s WHERE lead_hashkey = %s", $row->hashkey, $stale_hash);
        $wpdb->query($q);
        // One final update to set all the previous submissions to the new hashkey
        $q = $wpdb->prepare("UPDATE {$wpdb->li_submissions} SET lead_hashkey = %s WHERE lead_hashkey = %s", $row->hashkey, $stale_hash);
        $wpdb->query($q);
        // Remove the passed hash from the merged hashkeys for the row
        $merged_hashkeys = array_unique(array_filter(explode(',', $row->merged_hashkeys)));
        // Delete the stale hash from the merged hashkeys array
        $merged_hashkeys = leadout_array_delete($merged_hashkeys, "'" . $stale_hash . "'");
        $q = $wpdb->prepare("UPDATE {$wpdb->li_leads} SET merged_hashkeys = %s WHERE hashkey = %s", rtrim(implode(',', $merged_hashkeys), ','), $row->hashkey);
        $wpdb->query($q);
        echo json_encode($row->hashkey);
        die;
    } else {
        echo json_encode(FALSE);
        die;
    }
}
/** 
 * Algorithm to set deleted contacts flag for 0.8.3 upgrade
 *
 */
function leadout_delete_flag_fix()
{
    global $wpdb;
    $q = $wpdb->prepare("SELECT lead_email, COUNT(hashkey) c FROM {$wpdb->li_leads} WHERE lead_email != '' AND lead_deleted = 0 GROUP BY lead_email HAVING c > 1", '');
    $duplicates = $wpdb->get_results($q);
    if (count($duplicates)) {
        foreach ($duplicates as $duplicate) {
            $q = $wpdb->prepare("SELECT lead_email, hashkey, merged_hashkeys FROM {$wpdb->li_leads} WHERE lead_email = %s AND lead_deleted = 0 ORDER BY lead_date DESC", $duplicate->lead_email);
            $existing_contacts = $wpdb->get_results($q);
            $newest = $existing_contacts[0];
            // Setup the string for the existing hashkeys
            $existing_contact_hashkeys = $newest->merged_hashkeys;
            if ($newest->merged_hashkeys && count($existing_contacts)) {
                $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 and original hashkey just in case
            $existing_contact_hashkeys = leadout_array_delete(array_unique(explode(',', $existing_contact_hashkeys)), "'" . $newest->hashkey . "'");
            // Safety precaution - trim any trailing commas
            $existing_contact_hashkey_string = rtrim(implode(',', $existing_contact_hashkeys), ',');
            if ($existing_contact_hashkey_string) {
                // Set the merged hashkeys with the fixed merged hashkey values
                $q = $wpdb->prepare("UPDATE {$wpdb->li_leads} SET merged_hashkeys = %s WHERE hashkey = %s", $existing_contact_hashkey_string, $newest->hashkey);
                $wpdb->query($q);
                // "Delete" all the old contacts
                $q = $wpdb->prepare("UPDATE {$wpdb->li_leads} SET merged_hashkeys = '', lead_deleted = 1 WHERE hashkey IN ( {$existing_contact_hashkey_string} )", '');
                $wpdb->query($q);
                // Set all the pageviews and submissions to the new hashkey just in case
                $q = $wpdb->prepare("UPDATE {$wpdb->li_pageviews} SET lead_hashkey = %s WHERE lead_hashkey IN ( {$existing_contact_hashkey_string} )", $newest->hashkey);
                $wpdb->query($q);
                // Update all the previous submissions to the new hashkey just in case
                $q = $wpdb->prepare("UPDATE {$wpdb->li_submissions} SET lead_hashkey = %s WHERE lead_hashkey IN ( {$existing_contact_hashkey_string} )", $newest->hashkey);
                $wpdb->query($q);
            }
        }
    }
    leadout_update_option('leadin_options', 'delete_flags_fixed', 1);
}
Exemple #3
0
 public static function deactivate_power_up($power_up_slug, $exit = TRUE)
 {
     if (!strlen($power_up_slug)) {
         return FALSE;
     }
     // If it's already active, then don't do it again
     $active = self::is_power_up_active($power_up_slug);
     if (!$active) {
         return TRUE;
     }
     $activated_power_ups = get_option('leadin_active_power_ups');
     $power_ups_left = leadout_array_delete(unserialize($activated_power_ups), $power_up_slug);
     update_option('leadin_active_power_ups', serialize($power_ups_left));
     if ($exit) {
         exit;
     }
 }