/*
Theme Name: Enfold Child
Description: A <a href='http://codex.wordpress.org/Child_Themes'>Child Theme</a> for the Enfold Wordpress Theme. If you plan to do a lot of file modifications we recommend to use this Theme instead of the original Theme. Updating wil be much easier then.
Version: 1.0
Author: Kriesi
Author URI: http://www.kriesi.at
Template: enfold
*/

// Hides the Compilance Banner for users from Switzerland
function hide_element_for_swiss_visitors() {
    $user_info = geoip_detect2_get_info_from_current_ip();
    $country_code = $user_info->country->isoCode;

   if ($country_code == 'CH') { // If the user is from Switzerland
        echo '<style>#cmplz-cookiebanner-container, #cmplz-manage-consent { display: none !important; }</style>';
    }
}
add_action('wp_head', 'hide_element_for_swiss_visitors');

// Prechecks the Compilance Banner for users from Switzerland and accept it automatically
function check_options_for_swiss_visitors() {
    $user_info = geoip_detect2_get_info_from_current_ip();
    $country_code = $user_info->country->isoCode;

    if ($country_code == 'CH') { // If the user is from Switzerland
        echo '
        <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            var checkboxes = document.querySelectorAll("#cmplz-cookiebanner-container input[type=checkbox]");
            for(var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = true;
            }
            // Assuming that "Akzeptieren" button has class "cmplz-btn cmplz-accept"
            document.querySelector(".cmplz-btn.cmplz-accept").click();
        });
        </script>';
    } else {
        echo '
        <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            var checkboxes = document.querySelectorAll("#cmplz-cookiebanner-container input[type=checkbox]");
            for(var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = false;
            }
        });
        </script>';
    }
}
add_action('wp_head', 'check_options_for_swiss_visitors');

/*Add your own styles here:*/