Files
feadulta/mu-plugins/fea-cookie-consent.php

113 lines
5.3 KiB
PHP

<?php
/**
* Plugin Name: Fea Cookie Consent — banner RGPD (#93)
* Description: Banner de consentimiento de cookies (Consent Mode v2). Mientras no
* haya consentimiento, GA4 (fea-analytics.php) arranca con analytics_storage
* 'denied'. Al Aceptar, el banner hace gtag consent 'update' a 'granted' y guarda
* cookie. Multiidioma por <html lang> (ES/EN/FR/IT/PT). HTML idéntico para todos
* (cacheable); el estado por usuario se aplica en JS desde la cookie.
* Cambiar preferencias: window.feaOpenCookiePrefs() (engánchalo a un enlace del pie).
*/
if (!defined('ABSPATH')) exit;
// Página de política de privacidad/cookies. ID del post madre (ES); el enlace
// se resuelve a la traducción del idioma actual vía Polylang.
define('FEA_PRIVACY_PAGE_ID', 21946);
add_action('wp_footer', function () {
if (is_admin()) return;
// Resolver la política al idioma actual (Polylang); fallback al ID madre.
$priv_id = FEA_PRIVACY_PAGE_ID;
if (function_exists('pll_get_post') && function_exists('pll_current_language')) {
$tr = pll_get_post($priv_id, pll_current_language());
if ($tr) $priv_id = $tr;
}
$privacy = get_permalink($priv_id);
$privacy = esc_url($privacy ? $privacy : '/');
?>
<style>
#fea-cc{position:fixed;left:0;right:0;bottom:0;z-index:99999;display:none;
background:#fafafa;color:#555;border-top:1px solid #e5e7eb;padding:12px 18px;
box-shadow:0 -1px 6px rgba(0,0,0,.08);font-size:13px;line-height:1.4}
#fea-cc .fea-cc-inner{max-width:1100px;margin:0 auto;display:flex;gap:14px;
align-items:center;flex-wrap:wrap;justify-content:space-between}
#fea-cc .fea-cc-text{flex:1 1 380px;min-width:240px}
#fea-cc a{color:#555;text-decoration:underline}
#fea-cc .fea-cc-btns{display:flex;gap:8px;flex-shrink:0}
#fea-cc button{cursor:pointer;border:0;border-radius:5px;padding:8px 16px;
font-size:13px;font-weight:500}
#fea-cc .fea-cc-reject{background:transparent;color:#6b7280;border:1px solid #d1d5db}
#fea-cc .fea-cc-accept{background:#6b7280;color:#fff;border:1px solid #6b7280}
</style>
<div id="fea-cc" role="dialog" aria-live="polite" aria-label="cookie consent">
<div class="fea-cc-inner">
<div class="fea-cc-text" id="fea-cc-text"></div>
<div class="fea-cc-btns">
<button type="button" class="fea-cc-reject" id="fea-cc-reject"></button>
<button type="button" class="fea-cc-accept" id="fea-cc-accept"></button>
</div>
</div>
</div>
<script>
(function(){
var PRIVACY = <?php echo json_encode($privacy); ?>;
var I18N = {
es:{t:"Usamos cookies de analítica (Google Analytics) para entender cómo se usa la web y mejorarla. ¿Nos das tu consentimiento?",a:"Aceptar",r:"Rechazar",m:"Más información"},
en:{t:"We use analytics cookies (Google Analytics) to understand how the site is used and improve it. Do you consent?",a:"Accept",r:"Reject",m:"Learn more"},
fr:{t:"Nous utilisons des cookies d'analyse (Google Analytics) pour comprendre l'usage du site et l'améliorer. Acceptez-vous ?",a:"Accepter",r:"Refuser",m:"En savoir plus"},
it:{t:"Usiamo cookie di analisi (Google Analytics) per capire come viene usato il sito e migliorarlo. Acconsenti?",a:"Accetta",r:"Rifiuta",m:"Maggiori informazioni"},
pt:{t:"Usamos cookies de análise (Google Analytics) para perceber como o site é usado e melhorá-lo. Dás o teu consentimento?",a:"Aceitar",r:"Rejeitar",m:"Saber mais"}
};
function lang(){
var l=(document.documentElement.lang||"es").slice(0,2).toLowerCase();
return I18N[l]?l:"es";
}
function getCookie(n){
var m=document.cookie.match('(?:^|; )'+n+'=([^;]*)');
return m?decodeURIComponent(m[1]):null;
}
function setCookie(n,v){
var d=";domain=.feadulta.com";
if(!/(^|\.)feadulta\.com$/.test(location.hostname)) d=""; // local: host-only
var exp=new Date(Date.now()+180*864e5).toUTCString();
document.cookie=n+"="+encodeURIComponent(v)+";path=/;expires="+exp+";SameSite=Lax"+d;
}
function grant(){
if(typeof gtag==='function'){
gtag('consent','update',{'analytics_storage':'granted'});
}
}
function render(){
var L=I18N[lang()];
var box=document.getElementById('fea-cc');
document.getElementById('fea-cc-text').innerHTML=
L.t+' <a href="'+PRIVACY+'">'+L.m+'</a>';
document.getElementById('fea-cc-accept').textContent=L.a;
document.getElementById('fea-cc-reject').textContent=L.r;
box.style.display='block';
}
function hide(){var b=document.getElementById('fea-cc');if(b)b.style.display='none';}
function init(){
var v=getCookie('fea_consent');
if(v==='granted'){grant();return;}
if(v==='denied'){return;}
render();
document.getElementById('fea-cc-accept').addEventListener('click',function(){
setCookie('fea_consent','granted');grant();hide();
});
document.getElementById('fea-cc-reject').addEventListener('click',function(){
setCookie('fea_consent','denied');hide();
});
}
// Reabrir el banner para cambiar preferencias (enlace del pie, etc.)
window.feaOpenCookiePrefs=function(){render();
document.getElementById('fea-cc-accept').onclick=function(){setCookie('fea_consent','granted');grant();hide();};
document.getElementById('fea-cc-reject').onclick=function(){setCookie('fea_consent','denied');hide();};
};
if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}
else{init();}
})();
</script>
<?php
}, 99);