function tiaop_process_saved_purchases($is_test = false)
{
    // Get the referral information
    if ($is_test) {
        $affiliateId = 1;
    } else {
        $affiliateId = affiliate_wp()->tracking->get_affiliate_id();
    }
    if (empty($affiliateId)) {
        return;
    }
    // Check the database for saved purchases
    $savedPurchase = tiaop_get_saved_purchase();
    if (!$savedPurchase) {
        return;
    }
    // Tell the log that we found a saved purchase and are attempting to process it
    if (WP_DEBUG === true) {
        error_log("tiaop_add_referral: Found a saved purchase; attempting to process it");
        error_log(print_r($savedPurchase, true));
    }
    $amount = $savedPurchase[2];
    $description = $savedPurchase[3];
    $reference = $savedPurchase[4];
    $expiration = $savedPurchase[5];
    // Get the visit id
    if ($is_test) {
        $visitId = 2;
    } else {
        $visitId = affiliate_wp()->tracking->get_visit_id();
    }
    // Call the function that adds the referral
    tiaop_add_referral($affiliateId, $amount, $description, $reference, "", $visitId, $is_test);
    // Log the link
    tiaop_add_history(tiaop_get_user_ip(), $amount, $description, $reference, $expiration, "Visit ID " . $visitId . " linked to Affiliate ID " . $affiliateId, $is_test);
    // Update the conversion stats
    if (!is_test) {
        tiaop_increment_ac($amount);
    }
    // Delete this entry now that it was processed
    tiaop_delete_current_ip();
}
function tiaop_delete_expired()
{
    global $wpdb;
    $db_table_name = $wpdb->prefix . "opafti_saved_purchases";
    $db_history_table = $wpdb->prefix . "opafti_history";
    // Log deletions
    $entries = $wpdb->get_results("SELECT * FROM {$db_table_name} WHERE expires < NOW()");
    foreach ($entries as $row) {
        tiaop_add_history($row->ip_address, $row->amount, $row->description, $row->reference, $row->expires, "Entry expired");
    }
    // Build delete syntax
    $deleteSql = "DELETE FROM {$db_table_name} WHERE expires < NOW()";
    // Run delete
    $wpdb->query($deleteSql);
}