<?php

/* ********************
** GET COUNTRY VISITOR **
******************** */
function lpGetIPAddress()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // if ($ip == '::1') $ip = '';
    return $ip;
}

add_action('init', 'lp_set_country_ip_cookie');

function lp_set_country_ip_cookie()
{
    // Avoid setting cookies during cron or when headers already sent
    if (defined('DOING_CRON') && DOING_CRON) {
        return false;
    }
    if (headers_sent()) {
        return false;
    }
    if (isset($_COOKIE['lp_country']) && !empty($_COOKIE['lp_country'])) {
        return $_COOKIE['lp_country'];
    }
    $ip = lpGetIPAddress();

    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.country.is/" . $ip);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $details = json_decode(curl_exec($ch));
        curl_close($ch);


        // $details = json_decode(file_get_contents("https://api.country.is/" . $ip));
        if (lpGetIPAddress() == '::1') {
            if (!headers_sent()) {
                setcookie('lp_country', $details->country, time() + 3600 * 2, '/');
            }
            return 'fr';
        } elseif (isset($details->country) && strlen($details->country) == 2) {
            $country = strtolower($details->country);
            if ($country == 'uk') $country = 'en';
            if ($country == 'gb') $country = 'en';
            if (!headers_sent()) {
                setcookie('lp_country', $country, time() + 3600 * 2, '/');
            }
            return $country;
        } else {

            return false;
        }
    } else {
        return false;
    }
}

function lp_get_country_ip()
{
    if (isset($_COOKIE['lp_country']) && !empty($_COOKIE['lp_country'])) {
        return $_COOKIE['lp_country'];
    } else {
        $ip = lpGetIPAddress();
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://api.country.is/" . $ip);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $details = json_decode(curl_exec($ch));
            curl_close($ch);
            // $details = json_decode(file_get_contents("https://api.country.is/".$ip));
            if (isset($details->country) && strlen($details->country) == 2) {
                $country = strtolower($details->country);
                if ($country == 'uk') $country = 'en';
                if ($country == 'gb') $country = 'en';
                return $country;
            } else {
                return false;
            }
        }
    }
    return false;
}


function is_display_buy_buttons()
{
    $my_current_lang = apply_filters('wpml_current_language', NULL);
    $country_ip = lp_get_country_ip();

    // error_log('country_ip: ' . $country_ip . ' my_current_lang: ' . $my_current_lang);

    if ($country_ip !== false && strtolower($country_ip) == strtolower($my_current_lang) && in_array(strtolower($my_current_lang), ['fr', 'en', 'uk', 'gb'])) {
        return true;
    } else {
        //cas localhost
        if (lpGetIPAddress() == '::1') {
            return true;
        } else {
            return false;
        }
    }
}
