<?php
/** PHP does not allow , (comma) so when PHP sets cookies we may want to change this to | (pipe) */
const GDPRC_COOKIE_DELIMITER = ',';

/**
 * Get the gdprc_consents cookie in safe way respecting GDPRC_COOKIE_DELIMITER
 *
 * @return string $ids
 */
if (!function_exists('gdprc_consent_cookie_get')) {
    function gdprc_consent_cookie_get()
    {
        $blog_id = '';
        if (!empty(\get_current_blog_id())) {
            $blog_id = '_'.(string)\get_current_blog_id();
        }
        if (isset($_COOKIE['gdprc_consents'.$blog_id])) {
            return \preg_replace('/[^\d'.GDPRC_COOKIE_DELIMITER.']+/i', GDPRC_COOKIE_DELIMITER, \sanitize_text_field($_COOKIE['gdprc_consents'.$blog_id]));
        }
        return null;
    }
}

/**
 * Set the gdprc_consents cookie in safe way
 * NOTE: Simulates the same working as setCookie in plugin.js
 *
 * @param string $value
 * @param int $exdays Days valid (default: 35)
 * @return string $ids
 */
if (!function_exists('gdprc_consent_cookie_set')) {
    function gdprc_consent_cookie_set($value, $exdays = null)
    {
        $blog_id = '';
        if (!empty(\get_current_blog_id())) {
            $blog_id = '_'.(string)\get_current_blog_id();
        }
        $_COOKIE['gdprc_consents'.$blog_id] = $value;
        \setrawcookie('gdprc_consents'.$blog_id, $value, \time() + ($exdays ? $exdays : 365 * 10) * 24 * 60 * 60, '/');
        return $value;
    }
}

if (!function_exists('gdprConsentPluginIsProEdition')) {
    function gdprConsentPluginIsProEdition()
    {
        return class_exists('\Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin') ? isset(\Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin::$useProEdition) && \Wpupgrader\WordPress\Plugins\GdprConsentPlugin\GdprConsentPlugin::$useProEdition === 1 : !0 && eval(\base64_decode('cmV0dXJuIGlzX2RpcihfX0RJUl9fLicvLi4vLi4nLmJhc2U2NF9kZWNvZGUoJ0wxTnlZeTlIWkhCeVEyOXVjMlZ1ZEZCeWJ3JykpOw'));
    }
}

/**
 * Check if WPML plugin is installed and active and returns the SitePress instance (when available)
 *
 * @return \SitePress global $sitepress
 */
if (!function_exists('gdprc_wpml_plugin_install')) {
    function gdprc_wpml_plugin_install()
    {
        global $sitepress;
        if (function_exists('icl_object_id') && \is_object($sitepress) && \is_a($sitepress, 'SitePress', true)) {
            return $sitepress;
        }
        return null;
    }
}

/**
 * Checks if the user is logged in based on the wordpress cookie
 */
if (!function_exists('gdprc_user_is_logged_in')) {
    function gdprc_user_is_logged_in()
    {
        return !empty($_COOKIE) ? !empty(array_filter(array_keys($_COOKIE), function($cookie) {
            return preg_match('~wordpress_logged_in_[\w]+~', $cookie);
        })) : false;
    }
}

/**
 * Returns the GDPR SVG icon
 */
if (!function_exists('get_gdprc_admin_icon')) {
    function get_gdprc_admin_icon()
    {
        return 'data:image/svg+xml;base64,'.base64_encode('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="#777" viewBox="0 0 24 24"><path d="M12 0c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm10 12c0 .685-.07 1.354-.202 2h-3.853c.121-1.283.129-2.621 0-4h3.853c.132.646.202 1.315.202 2zm-.841-4h-3.5c-.383-1.96-1.052-3.751-1.948-5.278 2.435.977 4.397 2.882 5.448 5.278zm-5.554 0h-2.605v-5.658c1.215 1.46 2.117 3.41 2.605 5.658zm-4.605-5.658v5.658h-2.605c.488-2.248 1.39-4.198 2.605-5.658zm0 7.658v4h-2.93c-.146-1.421-.146-2.577 0-4h2.93zm0 6v5.658c-1.215-1.46-2.117-3.41-2.605-5.658h2.605zm2 5.658v-5.658h2.605c-.488 2.248-1.39 4.198-2.605 5.658zm0-7.658v-4h2.93c.146 1.421.146 2.577 0 4h-2.93zm-4.711-11.278c-.896 1.527-1.565 3.318-1.948 5.278h-3.5c1.051-2.396 3.013-4.301 5.448-5.278zm-6.087 7.278h3.853c-.121 1.283-.129 2.621 0 4h-3.853c-.132-.646-.202-1.315-.202-2s.07-1.354.202-2zm.639 6h3.5c.383 1.96 1.052 3.751 1.948 5.278-2.435-.977-4.397-2.882-5.448-5.278zm12.87 5.278c.896-1.527 1.565-3.318 1.948-5.278h3.5c-1.051 2.396-3.013 4.301-5.448 5.278z"/></svg>');
    }
}