diff --git a/CHANGELOG.md b/CHANGELOG.md index ee874e2..17a7555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,15 @@ +# v0.1.7 +## 3/2022 +1. [](#new) +* add option to include a script only on specific pages + # v0.1.6 -## 2/2022 +## 2/2022 1. [](#new) * fix a missing |raw filter # v0.1.5 -## 2/2022 +## 2/2022 1. [](#new) * add pull requests from git hub to solve bugs diff --git a/README.md b/README.md index 0592006..b49ad99 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ The Plugin requires the use of the Grav the admin plugin. - admin setting default active/not active means, if script is included in website - there is an option to set the script position (body-top, body-bottom, head-bottom) - there is an option to include the script after first scroll of page and/or a timeout/delay +- there is an option to include the script only on specific pages - you can override styles in your own css **Note** diff --git a/assets/js/tecart-cookie-manager.js b/assets/js/tecart-cookie-manager.js index 4bc68e0..05b0ac1 100644 --- a/assets/js/tecart-cookie-manager.js +++ b/assets/js/tecart-cookie-manager.js @@ -37,7 +37,7 @@ class TecartCookieBanner{ // Button settings let buttonSettings = ''; if(this.getCookieBannerContent(this.cookieBannerData).buttonSettingsActive == 'activated'){ - buttonSettings = '<a aria-label="settings" role="button" tabIndex="0" class="button cc-btn cc-setting tcb-settings-btn '+this.getCookieBannerContent(this.cookieBannerData).buttonSettingsLayout+'" id="tcb-settings">'+this.getCookieBannerContent(this.cookieBannerData).settings+'</a>'; + buttonSettings = '<a aria-label="settings" role="button" tabIndex="0" class="button cc-btn cc-setting tcb-settings-btn '+this.getCookieBannerContent(this.cookieBannerData).buttonSettingsLayout+'" id="tcb-settings">'+this.getCookieBannerContent(this.cookieBannerData).settings+'</a>'; } // Button deny @@ -374,28 +374,27 @@ class TecartCookieBanner{ if(code.length > 0){ - const codeContent = data.code_title; - const codePos = data.code_position; - const codeTag = data.code_tag; - const codeLoadTimeout = data.code_load_with_timeout; - const codeLoadTimeoutTime = data.code_load_with_timeout_time; - const codeLoadOnScroll = data.code_load_on_scroll; + const codeContent = data.code_title; + const codePos = data.code_position; + const codeTag = data.code_tag; + const codeLoadTimeout = data.code_load_with_timeout; + const codeLoadTimeoutTime = data.code_load_with_timeout_time; + const codeLoadOnScroll = data.code_load_on_scroll; const codeLoadOnScrollPercent = data.code_load_on_scroll_percent; + const codeLoadOnPages = ("code_load_on_pages" in data) ? data.code_load_on_pages : []; + const currPagePath = window.location.pathname; + + let pageAllowed = true; + // do not include code when current page not in array of allowed pages + if (codeLoadOnPages.length > 0 && codeLoadOnPages.indexOf(currPagePath) === -1) { + pageAllowed = false; + } let codeItem = ""; - if(codeTag === 'noscript'){ - // insertAdjacentHTML with script does not execute script after include - // codeItem = '<noscript data-title="'+scriptTitle.trim()+'" data-tcb="'+scriptTitleEncoded+'" data-position="tcb-' + codePos + '" >' + - // codeContent.trim() + - // '</noscript>'; + if (codeTag === 'noscript') { codeItem = document.createElement("noscript"); - } - else { - // insertAdjacentHTML with script does not execute script after include - // codeItem = '<script type="text/javascript" data-title="'+scriptTitle.trim()+'" data-tcb="'+scriptTitleEncoded+'" data-position="tcb-' + codePos + '" async >' + - // codeContent.trim() + - // '</script>'; + } else { codeItem = document.createElement("script"); codeItem.setAttribute('type', 'text/javascript'); } @@ -408,47 +407,44 @@ class TecartCookieBanner{ // inner tag code codeItem.innerHTML = codeContent.trim(); - if(this.cookieConsentScrolled === 'scrolled'){ - // aliasing to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call - const self = this; - if(codeLoadTimeout === '1'){ - // Delay some seconds in loading function - setTimeout(function() { - // bind self to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call - self.addScriptTagToDOM(codePos, codeItem); - }, codeLoadTimeoutTime); - } - else{ - self.addScriptTagToDOM(codePos, codeItem); - } - } - else{ - // load script after scroll must be called on load again so save scroll scripts in cookie - if(codeLoadOnScroll === '1'){ - this.cookieConsentOnScrollScriptsArray.set(scriptTitleEncoded,{"allowed":true}); - } - // load script after timeout without scroll - else if(codeLoadTimeout === '1' && codeLoadOnScroll === '0'){ + if (pageAllowed) { + if (this.cookieConsentScrolled === 'scrolled') { // aliasing to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call const self = this; - // Delay some seconds in loading function - setTimeout(function() { - // bind to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call + if (codeLoadTimeout === '1') { + // Delay some seconds in loading function + setTimeout(function () { + // bind self to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call + self.addScriptTagToDOM(codePos, codeItem); + }, codeLoadTimeoutTime); + } else { self.addScriptTagToDOM(codePos, codeItem); - }, codeLoadTimeoutTime); - } - else{ - this.addScriptTagToDOM(codePos, codeItem); + } + } else { + // load script after scroll must be called on load again so save scroll scripts in cookie + if (codeLoadOnScroll === '1') { + this.cookieConsentOnScrollScriptsArray.set(scriptTitleEncoded, {"allowed": true}); + } + // load script after timeout without scroll + else if (codeLoadTimeout === '1' && codeLoadOnScroll === '0') { + // aliasing to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call + const self = this; + // Delay some seconds in loading function + setTimeout(function () { + // bind to avoid Uncaught TypeError: this.addScriptTagToDOM is not a function while nested call + self.addScriptTagToDOM(codePos, codeItem); + }, codeLoadTimeoutTime); + } else { + this.addScriptTagToDOM(codePos, codeItem); + } } } - - } } /** * add script tags to DOM - */ + */ addScriptTagToDOM(codePos, codeItem) { //add script to head tag @@ -474,7 +470,7 @@ class TecartCookieBanner{ /** * remove tags with given data-title - */ + */ removeCodeTagByTitle(dataTitle) { if(dataTitle !== ''){ @@ -581,9 +577,9 @@ class TecartCookieBanner{ let cat_toggler = ''; if (json_data[i].category_cookies === 'activated') { cat_toggler = '<label class="tcb-switch tcb-categories-switch">' + - '<input type="checkbox" '+activated+' id="cat_'+catTitleEncoded+'" value="'+catTitleEncoded+'" name="tcbCookieCategories" />' + - '<span class="tcb-slider tcb-round"></span>' + - '</label>'; + '<input type="checkbox" '+activated+' id="cat_'+catTitleEncoded+'" value="'+catTitleEncoded+'" name="tcbCookieCategories" />' + + '<span class="tcb-slider tcb-round"></span>' + + '</label>'; } //set checked tab @@ -751,7 +747,7 @@ class TecartCookieBanner{ /** * checkboxes selected - */ + */ onScrollIncludes() { if(this.cookieConsentScrolled !== 'scrolled'){ diff --git a/assets/js/tecart-cookie-manager.min.js b/assets/js/tecart-cookie-manager.min.js index af3ea96..600b60e 100644 --- a/assets/js/tecart-cookie-manager.min.js +++ b/assets/js/tecart-cookie-manager.min.js @@ -1 +1 @@ -class TecartCookieBanner{constructor(e,t,o,i){this.cookieBannerData=e,this.cookieBannerScripts=t,this.cookieBannerCategories=o,this.cookieBannerCategoriesData=i,null==this.cookieBannerScripts&&(this.cookieBannerScripts=[]),null==this.cookieBannerCategories&&(this.cookieBannerCategories=[]),null==this.cookieBannerCategoriesData&&(this.cookieBannerCategoriesData=[]),null==this.cookieConsentCategoriesArray&&(this.cookieConsentCategoriesArray=new Map),null==this.cookieConsentScriptsArray&&(this.cookieConsentScriptsArray=new Map),null==this.cookieConsentOnScrollScriptsArray&&(this.cookieConsentOnScrollScriptsArray=new Map),this.cookieConsent=this.getCookieByName("cookieconsent_status"),this.cookieConsentScripts=this.getCookieByName("cookieconsent_scripts"),this.cookieConsentCategories=this.getCookieByName("cookieconsent_categories"),this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.cookieConsentScrolled=this.getCookieByName("cookieconsent_scrolled")}init(){let e="";"activated"==this.getCookieBannerContent(this.cookieBannerData).buttonSettingsActive&&(e='<a aria-label="settings" role="button" tabIndex="0" class="button cc-btn cc-setting tcb-settings-btn '+this.getCookieBannerContent(this.cookieBannerData).buttonSettingsLayout+'" id="tcb-settings">'+this.getCookieBannerContent(this.cookieBannerData).settings+"</a>");let t="";"activated"==this.getCookieBannerContent(this.cookieBannerData).buttonDenyActive&&(t='<a aria-label="deny cookies" role="button" tabIndex="0" class="button cc-btn cc-deny '+this.getCookieBannerContent(this.cookieBannerData).buttonDenyLayout+'">'+this.getCookieBannerContent(this.cookieBannerData).deny+"</a>");const o={position:"bottom-left",type:"opt-in",palette:{popup:{background:"#f6f9fc",text:"#1e2022",border:"#1e2022"},button:{background:"#fff",text:"#5a5f69",border:"#5a5f69"}},layout:"tecart-cookie-banner",layouts:{"tecart-cookie-banner":'<div id="tecart-cookie-banner"><div class="cc-header">'+this.getCookieBannerContent(this.cookieBannerData).header+'</div><div id="cookieconsent:desc" class="cc-message">'+this.getCookieBannerContent(this.cookieBannerData).message+'</div><div class="cc-compliance cc-highlight"><a aria-label="allow cookies" role="button" tabIndex="0" class="button cc-btn cc-allow '+this.getCookieBannerContent(this.cookieBannerData).buttonAcceptLayout+'">'+this.getCookieBannerContent(this.cookieBannerData).allow+"</a>"+e+t+"</div></div>"},revokable:!0,animateRevokable:!0,revokeBtn:'<div class="cc-revoke cc-bottom cc-left cc-animate cc-revoke-from-plugin" style="color: '+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableTextColor+"; background-color: "+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableColor+"; border-color: "+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableColor+';">'+this.getCookieBannerContent(this.cookieBannerData).revoke+"</div>",onStatusChange:function(e,t){const o=this.hasConsented(),i=new TecartCookieBanner(this.cookieBannerData,this.cookieBannerScripts,this.cookieBannerCategories);o?i.allowAllCookies():!1===o?i.denyAllCookies():i.allowSettingsCookies()},onRevokeChoice:function(e){}};window.cookieconsent.initialise(o),"dismiss"===this.cookieConsent?this.createScriptCode(this.cookieConsentScripts):(this.setCookieConsentCategoriesArray(),this.setCookieConsentScriptsArray()),this.removeSettingsModal(),this.createSettingsModal(this.cookieConsent)}allowAllCookies(){const e=this.cookieExpireDate();document.cookie="cookieconsent_status=allow;"+e+";path=/",console.log("All scripts for this website have been activated in this browser."),window.location.reload()}denyAllCookies(){this.deleteAllCookies(),this.removeCodeTags();const e=this.cookieExpireDate();document.cookie="cookieconsent_status=deny;"+e+";path=/",console.log("All scripts for this website have been deactivated in this browser."),window.location.reload()}allowSettingsCookies(){const e=this.cookieExpireDate();document.cookie="cookieconsent_status=dismiss;"+e+";path=/",console.log("All scripts for this website have set like in settings in this browser.")}setScrolledCookie(){const e=this.cookieExpireDate();document.cookie="cookieconsent_scrolled=scrolled;"+e+";path=/",console.log("Page was scrolled.")}setConsentCategoriesToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_categories="+o+";"+t+";path=/"}setConsentScriptsToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_scripts="+o+";"+t+";path=/"}setConsentOnScrollScriptsToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_onscroll_scripts="+o+";"+t+";path=/"}cookieExpireDate(){const e=new Date;return e.setTime(e.getTime()+31536e6),"expires="+e.toUTCString()}deleteAllCookies(){const e=document.cookie.split("; ");for(let t=0;t<e.length;t++){const o=window.location.hostname.split(".");for(;o.length>0;){const i=encodeURIComponent(e[t].split(";")[0].split("=")[0])+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain="+o.join(".")+" ;path=",n=location.pathname.split("/");for(document.cookie=i+"/";n.length>0;)document.cookie=i+n.join("/"),n.pop();o.shift()}}window.localStorage.clear(),console.log("all cookies cleared")}getCookieByName(e){const t=document.cookie.match("(^|[^;]+)\\s*"+e+"\\s*=\\s*([^;]+)");return t?t.pop():""}deleteCookieByName(e){document.cookie=e+"=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;",console.log(e+" cookie deletetd")}setCookieConsentCategoriesArray(){let e;const t=this.cookieBannerCategories;if(t.length>0)for(let o=0;o<t.length;o++)"allow"===this.cookieConsent?(e=encodeURIComponent(t[o].category_title),this.cookieConsentCategoriesArray.set(e,{allowed:!0}),this.addScriptsFromAllowedCatToCookie(t[o].category_title)):"activated"===t[o].category_cookies&&(e=encodeURIComponent(t[o].category_title),this.cookieConsentCategoriesArray.set(e,{allowed:!0}),this.addScriptsFromAllowedCatToCookie(t[o].category_title));return this.setConsentCategoriesToCookie(this.cookieConsentCategoriesArray),this.cookieConsentCategoriesArray}setCookieConsentScriptsArray(){let e;const t=this.cookieBannerScripts;if(t.length>0)for(let o=0;o<t.length;o++)"allow"===this.cookieConsent?(e=encodeURIComponent(t[o].script_title),this.cookieConsentScriptsArray.set(e,{allowed:!0})):"activated"===t[o].script_cookies_standard&&(e=encodeURIComponent(t[o].script_title),this.cookieConsentScriptsArray.set(e,{allowed:!0}));this.setConsentScriptsToCookie(this.cookieConsentScriptsArray);const o=this.cookieConsentScriptsArray;this.createScriptCode(o)}addScriptsFromAllowedCatToCookie(e){const t=this.getElementByValue(this.cookieBannerScripts,"script_category",e);for(let e=0;e<t.length;e++){const o=encodeURIComponent(t[e].script_title);this.cookieConsentScriptsArray.set(o,{allowed:!0})}}createScriptCode(e){let t=[];null!==e&&(e instanceof Map&&(e=JSON.stringify(Array.from(e))),t=JSON.parse(e));const o=this.cookieBannerScripts;if(t.length>0&&o.length>0)for(let e=0;e<t.length;e++){const i=t[e][0],n=decodeURIComponent(i);if(o.length>0&&""!==n)for(let e=0;e<o.length;e++)if(o[e].script_title===n&&o[e].script_codes){const t=o[e].script_codes;for(let e=0;e<t.length;e++)this.createCodeTag(t[e],i,n)}}this.setConsentOnScrollScriptsToCookie(this.cookieConsentOnScrollScriptsArray)}createCodeTag(e,t,o){if(JSON.stringify(e).length>0){const i=e.code_title,n=e.code_position,s=e.code_tag,c=e.code_load_with_timeout,a=e.code_load_with_timeout_time,r=e.code_load_on_scroll;e.code_load_on_scroll_percent;let l="";if("noscript"===s?l=document.createElement("noscript"):(l=document.createElement("script")).setAttribute("type","text/javascript"),l.setAttribute("data-title",o.trim()),l.setAttribute("data-tcb",t),l.setAttribute("data-position",n),l.innerHTML=i.trim(),"scrolled"===this.cookieConsentScrolled){const e=this;"1"===c?setTimeout(function(){e.addScriptTagToDOM(n,l)},a):e.addScriptTagToDOM(n,l)}else if("1"===r)this.cookieConsentOnScrollScriptsArray.set(t,{allowed:!0});else if("1"===c&&"0"===r){const e=this;setTimeout(function(){e.addScriptTagToDOM(n,l)},a)}else this.addScriptTagToDOM(n,l)}}addScriptTagToDOM(e,t){"head"===e?document.head.append(t):"body-top"===e?document.body.prepend(t):document.body.appendChild(t)}removeCodeTagByTitle(e){if(""!==e){const t=document.querySelectorAll("[data-tcb]");for(const o in t)t.hasOwnProperty(o)&&t[o].getAttribute("data-tcb")===e&&t[o].remove()}}removeCodeTags(){const e=document.querySelectorAll("[data-tcb]");for(const t in e)e.hasOwnProperty(t)&&e[t].remove()}createSettingsModal(e=""){const t='<div id="tcb-settings-modal" class="tcb-settings-modal" style="display: none;"><div id="tcb-settings-close-btn">×</div><div class="tcb-settings-modal-header"><div class="cc-header">'+this.getCookieBannerContent(this.cookieBannerData).settingsTitle+'</div></div>\n<div class="tcb-settings-modal-content">'+this.createSettingsModalTabs(e)+"</div></div>";document.body.insertAdjacentHTML("beforeend",t)}removeSettingsModal(){const e=document.getElementById("tcb-settings-modal");null!==e&&e.remove()}createSettingsModalTabs(e=""){let t="",o="";const i=this.cookieBannerCategories;if(!(i.length>0))return this.getCookieBannerContent(this.cookieBannerData).settingsEmpty;for(let n=0;n<i.length;n++){this.cookieBannerScripts.length>0&&(o=this.createSettingsModalTabsScripts(i[n].category_title,e));const s=i[n].category_text?i[n].category_text:"",c=i[n].category_title?i[n].category_title:"",a=encodeURIComponent(c);let r="";"allow"===e?r="checked":this.cookieConsentCategories.length>0?!0===this.cookieConsentCategories.includes(a)&&(r="checked"):"activated"===i[n].category_cookies&&(r="checked");let l="";"activated"===i[n].category_cookies&&(l='<label class="tcb-switch tcb-categories-switch"><input type="checkbox" '+r+' id="cat_'+a+'" value="'+a+'" name="tcbCookieCategories" /><span class="tcb-slider tcb-round"></span></label>'),t+='<section id="section'+n+'"><input type="radio" name="sections" id="option'+n+'" '+(0===n?'checked=""':"")+' /><label for="option'+n+'">'+i[n].category_title+'</label><article><div class="tcb-tabs-cat-title"><p>'+c+"</p>"+l+"</div><p>"+s+"</p>"+o+"</article></section>"}let n="Save Settings";this.cookieBannerCategoriesData.categories_save_text&&(n=this.cookieBannerCategoriesData.categories_save_text);let s="layout-1";return this.cookieBannerCategoriesData.categories_save_layout&&(s=this.cookieBannerCategoriesData.categories_save_layout),'<div id="tcbSettings" class="tcb-catTabs">'+t+'</div><div class="tcb-saveSettings"><a id="tcb-save-settings-btn" aria-label="save settings" role="button" tabIndex="0" class="button cc-btn '+s+'">'+n+"</a></div>"}createSettingsModalTabsScripts(e="",t=""){let o="";if(""!==e&&this.cookieBannerScripts.length>0){const i=this.getElementByValue(this.cookieBannerScripts,"script_category",e);if(i.length>0){o+="<ul>";for(let e=0;e<i.length;e++){const n=i[e].script_title?i[e].script_title:"",s=encodeURIComponent(n);let c="";"allow"===t?c="checked":this.cookieConsentScripts.length>0?!0===this.cookieConsentScripts.includes(s)&&(c="checked"):"activated"===i[e].script_cookies_standard&&(c="checked");let a="",r="",l="tcbCookieScripts";"0"===i[e].script_cookies&&(a='disabled="disabled"',r="disabled",l="tcbDisabledCookieScripts"),o+='<li><div class="tcb-script-title">'+n+'</div><label class="tcb-switch tcb-script-switch '+r+'"><input type="checkbox" '+c+' id="script_'+s+'" value="'+s+'" name="'+l+'" '+a+' /><span class="tcb-slider tcb-round"></span></label><div class="tcb-script-text">'+i[e].script_text+"</div></li>"}o+="</ul>"}}return o}onCheckboxSettings(){this.deleteCookieByName("cookieconsent_scripts"),this.deleteCookieByName("cookieconsent_categories"),this.deleteAllCookies();let e=[],t=[];if(this.cookieConsentScriptsArray=new Map,(e=Array.from(document.querySelectorAll("input[name=tcbCookieScripts]")).filter(e=>e.checked).map(e=>e.value)).length>0)for(let t=0;t<e.length;t++)this.cookieConsentScriptsArray.set(e[t],{allowed:!0});if((t=Array.from(document.querySelectorAll("input[name=tcbCookieCategories]")).filter(e=>e.checked).map(e=>e.value)).length>0)for(let e=0;e<t.length;e++)this.cookieConsentCategoriesArray.set(t[e],{allowed:!0}),this.addScriptsFromAllowedCatToCookie(decodeURIComponent(t[e]));this.setConsentOnScrollScriptsToCookie(this.cookieConsentOnScrollScriptsArray),this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.setConsentCategoriesToCookie(this.cookieConsentCategoriesArray),this.setConsentScriptsToCookie(this.cookieConsentScriptsArray),this.allowSettingsCookies(),this.removeCodeTags();const o=this.cookieConsentScriptsArray;this.createScriptCode(o),window.location.reload()}onScrollIncludes(){"scrolled"!==this.cookieConsentScrolled&&(this.cookieConsentScrolled="scrolled",this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.setScrolledCookie(),this.createScriptCode(this.cookieConsentOnScrollScripts))}getElementByValue(e,t,o){return e.filter(function(e){return e[t]===o})}getCookieBannerContent(e){const t={header:"Cookies used on the website!",allow:"Allow",deny:"Deny",denyActive:"activated",message:"This website uses cookies to ensure you get the best experience on our website.",settings:"Settings",settingsEmpty:"There are no settings available.",settingsActive:"activated",revoke:"Cookie Policy",buttonRevokableColor:"#7faf34",buttonRevokableTextColor:"#ffffff",settingsTitle:"Your personal settings"};return null!=e&&(e.banner_title&&(t.header=e.banner_title),e.button_deny_text&&(t.deny=e.button_deny_text),e.button_accept_text&&(t.allow=e.button_accept_text),e.banner_text&&(t.message=e.banner_text),e.button_settings_text&&(t.settings=e.button_settings_text),e.button_settings_empty&&(t.settingsEmpty=e.button_settings_empty),e.banner_revokable_text&&(t.revoke=e.banner_revokable_text),e.button_revokable_color&&(t.buttonRevokableColor=e.button_revokable_color),e.button_revokable_color&&(t.buttonRevokableTextColor=e.button_revokable_text_color),e.banner_cat_title&&(t.settingsTitle=e.banner_cat_title),t.buttonDenyLayout=e.button_deny_layout,t.buttonDenyActive=e.button_deny_active,t.buttonSettingsLayout=e.button_settings_layout,t.buttonSettingsActive=e.button_settings_active,t.buttonAcceptLayout=e.button_accept_layout),t}} +class TecartCookieBanner{constructor(e,t,o,i){this.cookieBannerData=e,this.cookieBannerScripts=t,this.cookieBannerCategories=o,this.cookieBannerCategoriesData=i,null==this.cookieBannerScripts&&(this.cookieBannerScripts=[]),null==this.cookieBannerCategories&&(this.cookieBannerCategories=[]),null==this.cookieBannerCategoriesData&&(this.cookieBannerCategoriesData=[]),null==this.cookieConsentCategoriesArray&&(this.cookieConsentCategoriesArray=new Map),null==this.cookieConsentScriptsArray&&(this.cookieConsentScriptsArray=new Map),null==this.cookieConsentOnScrollScriptsArray&&(this.cookieConsentOnScrollScriptsArray=new Map),this.cookieConsent=this.getCookieByName("cookieconsent_status"),this.cookieConsentScripts=this.getCookieByName("cookieconsent_scripts"),this.cookieConsentCategories=this.getCookieByName("cookieconsent_categories"),this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.cookieConsentScrolled=this.getCookieByName("cookieconsent_scrolled")}init(){let e="";"activated"==this.getCookieBannerContent(this.cookieBannerData).buttonSettingsActive&&(e='<a aria-label="settings" role="button" tabIndex="0" class="button cc-btn cc-setting tcb-settings-btn '+this.getCookieBannerContent(this.cookieBannerData).buttonSettingsLayout+'" id="tcb-settings">'+this.getCookieBannerContent(this.cookieBannerData).settings+"</a>");let t="";"activated"==this.getCookieBannerContent(this.cookieBannerData).buttonDenyActive&&(t='<a aria-label="deny cookies" role="button" tabIndex="0" class="button cc-btn cc-deny '+this.getCookieBannerContent(this.cookieBannerData).buttonDenyLayout+'">'+this.getCookieBannerContent(this.cookieBannerData).deny+"</a>");const o={position:"bottom-left",type:"opt-in",palette:{popup:{background:"#f6f9fc",text:"#1e2022",border:"#1e2022"},button:{background:"#fff",text:"#5a5f69",border:"#5a5f69"}},layout:"tecart-cookie-banner",layouts:{"tecart-cookie-banner":'<div id="tecart-cookie-banner"><div class="cc-header">'+this.getCookieBannerContent(this.cookieBannerData).header+'</div><div id="cookieconsent:desc" class="cc-message">'+this.getCookieBannerContent(this.cookieBannerData).message+'</div><div class="cc-compliance cc-highlight"><a aria-label="allow cookies" role="button" tabIndex="0" class="button cc-btn cc-allow '+this.getCookieBannerContent(this.cookieBannerData).buttonAcceptLayout+'">'+this.getCookieBannerContent(this.cookieBannerData).allow+"</a>"+e+t+"</div></div>"},revokable:!0,animateRevokable:!0,revokeBtn:'<div class="cc-revoke cc-bottom cc-left cc-animate cc-revoke-from-plugin" style="color: '+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableTextColor+"; background-color: "+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableColor+"; border-color: "+this.getCookieBannerContent(this.cookieBannerData).buttonRevokableColor+';">'+this.getCookieBannerContent(this.cookieBannerData).revoke+"</div>",onStatusChange:function(e,t){const o=this.hasConsented(),i=new TecartCookieBanner(this.cookieBannerData,this.cookieBannerScripts,this.cookieBannerCategories);o?i.allowAllCookies():!1===o?i.denyAllCookies():i.allowSettingsCookies()},onRevokeChoice:function(e){}};window.cookieconsent.initialise(o),"dismiss"===this.cookieConsent?this.createScriptCode(this.cookieConsentScripts):(this.setCookieConsentCategoriesArray(),this.setCookieConsentScriptsArray()),this.removeSettingsModal(),this.createSettingsModal(this.cookieConsent)}allowAllCookies(){const e=this.cookieExpireDate();document.cookie="cookieconsent_status=allow;"+e+";path=/",console.log("All scripts for this website have been activated in this browser."),window.location.reload()}denyAllCookies(){this.deleteAllCookies(),this.removeCodeTags();const e=this.cookieExpireDate();document.cookie="cookieconsent_status=deny;"+e+";path=/",console.log("All scripts for this website have been deactivated in this browser."),window.location.reload()}allowSettingsCookies(){const e=this.cookieExpireDate();document.cookie="cookieconsent_status=dismiss;"+e+";path=/",console.log("All scripts for this website have set like in settings in this browser.")}setScrolledCookie(){const e=this.cookieExpireDate();document.cookie="cookieconsent_scrolled=scrolled;"+e+";path=/",console.log("Page was scrolled.")}setConsentCategoriesToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_categories="+o+";"+t+";path=/"}setConsentScriptsToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_scripts="+o+";"+t+";path=/"}setConsentOnScrollScriptsToCookie(e){e instanceof Map&&(e=Array.from(e));const t=this.cookieExpireDate(),o=JSON.stringify(e);document.cookie="cookieconsent_onscroll_scripts="+o+";"+t+";path=/"}cookieExpireDate(){const e=new Date;return e.setTime(e.getTime()+31536e6),"expires="+e.toUTCString()}deleteAllCookies(){const e=document.cookie.split("; ");for(let t=0;t<e.length;t++){const o=window.location.hostname.split(".");for(;o.length>0;){const i=encodeURIComponent(e[t].split(";")[0].split("=")[0])+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain="+o.join(".")+" ;path=",n=location.pathname.split("/");for(document.cookie=i+"/";n.length>0;)document.cookie=i+n.join("/"),n.pop();o.shift()}}window.localStorage.clear(),console.log("all cookies cleared")}getCookieByName(e){const t=document.cookie.match("(^|[^;]+)\\s*"+e+"\\s*=\\s*([^;]+)");return t?t.pop():""}deleteCookieByName(e){document.cookie=e+"=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;",console.log(e+" cookie deletetd")}setCookieConsentCategoriesArray(){let e;const t=this.cookieBannerCategories;if(t.length>0)for(let o=0;o<t.length;o++)"allow"===this.cookieConsent?(e=encodeURIComponent(t[o].category_title),this.cookieConsentCategoriesArray.set(e,{allowed:!0}),this.addScriptsFromAllowedCatToCookie(t[o].category_title)):"activated"===t[o].category_cookies&&(e=encodeURIComponent(t[o].category_title),this.cookieConsentCategoriesArray.set(e,{allowed:!0}),this.addScriptsFromAllowedCatToCookie(t[o].category_title));return this.setConsentCategoriesToCookie(this.cookieConsentCategoriesArray),this.cookieConsentCategoriesArray}setCookieConsentScriptsArray(){let e;const t=this.cookieBannerScripts;if(t.length>0)for(let o=0;o<t.length;o++)"allow"===this.cookieConsent?(e=encodeURIComponent(t[o].script_title),this.cookieConsentScriptsArray.set(e,{allowed:!0})):"activated"===t[o].script_cookies_standard&&(e=encodeURIComponent(t[o].script_title),this.cookieConsentScriptsArray.set(e,{allowed:!0}));this.setConsentScriptsToCookie(this.cookieConsentScriptsArray);const o=this.cookieConsentScriptsArray;this.createScriptCode(o)}addScriptsFromAllowedCatToCookie(e){const t=this.getElementByValue(this.cookieBannerScripts,"script_category",e);for(let e=0;e<t.length;e++){const o=encodeURIComponent(t[e].script_title);this.cookieConsentScriptsArray.set(o,{allowed:!0})}}createScriptCode(e){let t=[];null!==e&&(e instanceof Map&&(e=JSON.stringify(Array.from(e))),t=JSON.parse(e));const o=this.cookieBannerScripts;if(t.length>0&&o.length>0)for(let e=0;e<t.length;e++){const i=t[e][0],n=decodeURIComponent(i);if(o.length>0&&""!==n)for(let e=0;e<o.length;e++)if(o[e].script_title===n&&o[e].script_codes){const t=o[e].script_codes;for(let e=0;e<t.length;e++)this.createCodeTag(t[e],i,n)}}this.setConsentOnScrollScriptsToCookie(this.cookieConsentOnScrollScriptsArray)}createCodeTag(e,t,o){if(JSON.stringify(e).length>0){const i=e.code_title,n=e.code_position,s=e.code_tag,c=e.code_load_with_timeout,a=e.code_load_with_timeout_time,r=e.code_load_on_scroll,l=(e.code_load_on_scroll_percent,"code_load_on_pages"in e?e.code_load_on_pages:[]),d=window.location.pathname;let h=!0;l.length>0&&-1===l.indexOf(d)&&(h=!1);let k="";if("noscript"===s?k=document.createElement("noscript"):(k=document.createElement("script")).setAttribute("type","text/javascript"),k.setAttribute("data-title",o.trim()),k.setAttribute("data-tcb",t),k.setAttribute("data-position",n),k.innerHTML=i.trim(),h)if("scrolled"===this.cookieConsentScrolled){const e=this;"1"===c?setTimeout(function(){e.addScriptTagToDOM(n,k)},a):e.addScriptTagToDOM(n,k)}else if("1"===r)this.cookieConsentOnScrollScriptsArray.set(t,{allowed:!0});else if("1"===c&&"0"===r){const e=this;setTimeout(function(){e.addScriptTagToDOM(n,k)},a)}else this.addScriptTagToDOM(n,k)}}addScriptTagToDOM(e,t){"head"===e?document.head.append(t):"body-top"===e?document.body.prepend(t):document.body.appendChild(t)}removeCodeTagByTitle(e){if(""!==e){const t=document.querySelectorAll("[data-tcb]");for(const o in t)t.hasOwnProperty(o)&&t[o].getAttribute("data-tcb")===e&&t[o].remove()}}removeCodeTags(){const e=document.querySelectorAll("[data-tcb]");for(const t in e)e.hasOwnProperty(t)&&e[t].remove()}createSettingsModal(e=""){const t='<div id="tcb-settings-modal" class="tcb-settings-modal" style="display: none;"><div id="tcb-settings-close-btn">×</div><div class="tcb-settings-modal-header"><div class="cc-header">'+this.getCookieBannerContent(this.cookieBannerData).settingsTitle+'</div></div>\n<div class="tcb-settings-modal-content">'+this.createSettingsModalTabs(e)+"</div></div>";document.body.insertAdjacentHTML("beforeend",t)}removeSettingsModal(){const e=document.getElementById("tcb-settings-modal");null!==e&&e.remove()}createSettingsModalTabs(e=""){let t="",o="";const i=this.cookieBannerCategories;if(!(i.length>0))return this.getCookieBannerContent(this.cookieBannerData).settingsEmpty;for(let n=0;n<i.length;n++){this.cookieBannerScripts.length>0&&(o=this.createSettingsModalTabsScripts(i[n].category_title,e));const s=i[n].category_text?i[n].category_text:"",c=i[n].category_title?i[n].category_title:"",a=encodeURIComponent(c);let r="";"allow"===e?r="checked":this.cookieConsentCategories.length>0?!0===this.cookieConsentCategories.includes(a)&&(r="checked"):"activated"===i[n].category_cookies&&(r="checked");let l="";"activated"===i[n].category_cookies&&(l='<label class="tcb-switch tcb-categories-switch"><input type="checkbox" '+r+' id="cat_'+a+'" value="'+a+'" name="tcbCookieCategories" /><span class="tcb-slider tcb-round"></span></label>'),t+='<section id="section'+n+'"><input type="radio" name="sections" id="option'+n+'" '+(0===n?'checked=""':"")+' /><label for="option'+n+'">'+i[n].category_title+'</label><article><div class="tcb-tabs-cat-title"><p>'+c+"</p>"+l+"</div><p>"+s+"</p>"+o+"</article></section>"}let n="Save Settings";this.cookieBannerCategoriesData.categories_save_text&&(n=this.cookieBannerCategoriesData.categories_save_text);let s="layout-1";return this.cookieBannerCategoriesData.categories_save_layout&&(s=this.cookieBannerCategoriesData.categories_save_layout),'<div id="tcbSettings" class="tcb-catTabs">'+t+'</div><div class="tcb-saveSettings"><a id="tcb-save-settings-btn" aria-label="save settings" role="button" tabIndex="0" class="button cc-btn '+s+'">'+n+"</a></div>"}createSettingsModalTabsScripts(e="",t=""){let o="";if(""!==e&&this.cookieBannerScripts.length>0){const i=this.getElementByValue(this.cookieBannerScripts,"script_category",e);if(i.length>0){o+="<ul>";for(let e=0;e<i.length;e++){const n=i[e].script_title?i[e].script_title:"",s=encodeURIComponent(n);let c="";"allow"===t?c="checked":this.cookieConsentScripts.length>0?!0===this.cookieConsentScripts.includes(s)&&(c="checked"):"activated"===i[e].script_cookies_standard&&(c="checked");let a="",r="",l="tcbCookieScripts";"0"===i[e].script_cookies&&(a='disabled="disabled"',r="disabled",l="tcbDisabledCookieScripts"),o+='<li><div class="tcb-script-title">'+n+'</div><label class="tcb-switch tcb-script-switch '+r+'"><input type="checkbox" '+c+' id="script_'+s+'" value="'+s+'" name="'+l+'" '+a+' /><span class="tcb-slider tcb-round"></span></label><div class="tcb-script-text">'+i[e].script_text+"</div></li>"}o+="</ul>"}}return o}onCheckboxSettings(){this.deleteCookieByName("cookieconsent_scripts"),this.deleteCookieByName("cookieconsent_categories"),this.deleteAllCookies();let e=[],t=[];if(this.cookieConsentScriptsArray=new Map,(e=Array.from(document.querySelectorAll("input[name=tcbCookieScripts]")).filter(e=>e.checked).map(e=>e.value)).length>0)for(let t=0;t<e.length;t++)this.cookieConsentScriptsArray.set(e[t],{allowed:!0});if((t=Array.from(document.querySelectorAll("input[name=tcbCookieCategories]")).filter(e=>e.checked).map(e=>e.value)).length>0)for(let e=0;e<t.length;e++)this.cookieConsentCategoriesArray.set(t[e],{allowed:!0}),this.addScriptsFromAllowedCatToCookie(decodeURIComponent(t[e]));this.setConsentOnScrollScriptsToCookie(this.cookieConsentOnScrollScriptsArray),this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.setConsentCategoriesToCookie(this.cookieConsentCategoriesArray),this.setConsentScriptsToCookie(this.cookieConsentScriptsArray),this.allowSettingsCookies(),this.removeCodeTags();const o=this.cookieConsentScriptsArray;this.createScriptCode(o),window.location.reload()}onScrollIncludes(){"scrolled"!==this.cookieConsentScrolled&&(this.cookieConsentScrolled="scrolled",this.cookieConsentOnScrollScripts=this.getCookieByName("cookieconsent_onscroll_scripts"),this.setScrolledCookie(),this.createScriptCode(this.cookieConsentOnScrollScripts))}getElementByValue(e,t,o){return e.filter(function(e){return e[t]===o})}getCookieBannerContent(e){const t={header:"Cookies used on the website!",allow:"Allow",deny:"Deny",denyActive:"activated",message:"This website uses cookies to ensure you get the best experience on our website.",settings:"Settings",settingsEmpty:"There are no settings available.",settingsActive:"activated",revoke:"Cookie Policy",buttonRevokableColor:"#7faf34",buttonRevokableTextColor:"#ffffff",settingsTitle:"Your personal settings"};return null!=e&&(e.banner_title&&(t.header=e.banner_title),e.button_deny_text&&(t.deny=e.button_deny_text),e.button_accept_text&&(t.allow=e.button_accept_text),e.banner_text&&(t.message=e.banner_text),e.button_settings_text&&(t.settings=e.button_settings_text),e.button_settings_empty&&(t.settingsEmpty=e.button_settings_empty),e.banner_revokable_text&&(t.revoke=e.banner_revokable_text),e.button_revokable_color&&(t.buttonRevokableColor=e.button_revokable_color),e.button_revokable_color&&(t.buttonRevokableTextColor=e.button_revokable_text_color),e.banner_cat_title&&(t.settingsTitle=e.banner_cat_title),t.buttonDenyLayout=e.button_deny_layout,t.buttonDenyActive=e.button_deny_active,t.buttonSettingsLayout=e.button_settings_layout,t.buttonSettingsActive=e.button_settings_active,t.buttonAcceptLayout=e.button_accept_layout),t}} \ No newline at end of file diff --git a/blueprints.yaml b/blueprints.yaml index 422dd1d..7af8533 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,11 +1,11 @@ name: TecArt Cookie Manager -version: 0.1.6 +version: 0.1.7 description: "TecArt cookie banner manager provides a cookie banner plugin with complete cookie management in admin and extended frontend views. The plugin is based on the popular **[CookieConsent](https://cookieconsent.osano.com/) JS-library** by Osano." icon: shield author: - name: Christiana Holland-Jobb | TecArt GmbH - email: christiana.holland-jobb@tecart.de - url: http://www.tecart.de + name: Christiana Holland-Jobb | TecArt GmbH + email: christiana.holland-jobb@tecart.de + url: https://www.tecart.de homepage: https://github.com/TecArt/grav-plugin-tecart-cookie-manager keywords: grav, plugin, cookie, banner, manager, cookieconsent, opt-in, opt-out, tecart bugs: https://github.com/TecArt/grav-plugin-tecart-cookie-manager/issues @@ -13,53 +13,53 @@ docs: https://github.com/TecArt/grav-plugin-tecart-cookie-manager/blob/main/READ license: MIT dependencies: - - { name: grav, version: '>=1.6.27' } - - { name: admin, version: '>=1.9.16' } + - { name: grav, version: '>=1.7.23' } + - { name: admin, version: '>=1.10.23' } form: - validation: strict - fields: - enabled: - type: toggle - label: PLUGIN_ADMIN.PLUGIN_STATUS - highlight: 1 - default: 0 - options: - 1: PLUGIN_ADMIN.ENABLED - 0: PLUGIN_ADMIN.DISABLED - validate: - type: bool - data_storage: - type: select - size: small - classes: fancy - label: Data Storage - default: data - options: - pages: pages/assets - data: user/data - custom_settings_link: - type: section - title: Custom Link to Settings - underline: true - fields: - custom_settings_link_info: - type: display - label: Info - markdown: true - content: "Texts and colors, if a link to open the setting should be set anywhere in the theme, but the text is maintained in the admin panel. It is important to use the **tcb-settings-link** class, which opens the modal. Text and color can be called up in Twig via the plugin config variables. See readme for more info." - custom_settings_link_text: - type: text - size: large - default: Cookie Banner Einstellungen - label: Link Text - custom_settings_link_color: - type: text - size: small - label: Background color if button - default: '#7faf34' - custom_settings_link_text_color: - type: text - size: small - label: Text color - default: '#ffffff' + validation: strict + fields: + enabled: + type: toggle + label: PLUGIN_ADMIN.PLUGIN_STATUS + highlight: 1 + default: 0 + options: + 1: PLUGIN_ADMIN.ENABLED + 0: PLUGIN_ADMIN.DISABLED + validate: + type: bool + data_storage: + type: select + size: small + classes: fancy + label: Data Storage + default: data + options: + pages: pages/assets + data: user/data + custom_settings_link: + type: section + title: Custom Link to Settings + underline: true + fields: + custom_settings_link_info: + type: display + label: Info + markdown: true + content: "Texts and colors, if a link to open the setting should be set anywhere in the theme, but the text is maintained in the admin panel. It is important to use the **tcb-settings-link** class, which opens the modal. Text and color can be called up in Twig via the plugin config variables. See readme for more info." + custom_settings_link_text: + type: text + size: large + default: Cookie Banner Einstellungen + label: Link Text + custom_settings_link_color: + type: text + size: small + label: Background color if button + default: '#7faf34' + custom_settings_link_text_color: + type: text + size: small + label: Text color + default: '#ffffff' diff --git a/blueprints/cookie-manager-scripts.yaml b/blueprints/cookie-manager-scripts.yaml index fed752e..4c08069 100644 --- a/blueprints/cookie-manager-scripts.yaml +++ b/blueprints/cookie-manager-scripts.yaml @@ -108,3 +108,19 @@ form: options: 1: PLUGIN_TECART_COOKIE_MANAGER.YES 0: PLUGIN_TECART_COOKIE_MANAGER.NO + .code_load_on_pages: + type: pages + size: large + classes: fancy + label: PLUGIN_TECART_COOKIE_MANAGER.SCRIPT_CODE_LOAD_ON_PAGES + show_all: false + show_modular: false + show_root: false + show_fullpath: false + multiple: true + help: PLUGIN_TECART_COOKIE_MANAGER.SCRIPT_CODE_LOAD_ON_PAGES_HELP + validate: + type: array + .code_load_on_pages_info: + type: display + content: PLUGIN_TECART_COOKIE_MANAGER.SCRIPT_CODE_LOAD_ON_PAGES_HELP diff --git a/classes/CookieConsent/CookieConsent.php b/classes/CookieConsent/CookieConsent.php index 2c4f9be..cabf591 100644 --- a/classes/CookieConsent/CookieConsent.php +++ b/classes/CookieConsent/CookieConsent.php @@ -24,7 +24,7 @@ public static function getYamlDataByType($type) { //location of yaml files $dataStorage = 'user://data'; - if(array_key_exists('data_storage', Grav::instance()['config']['plugins']['tecart-cookie-manager'])) { + if (array_key_exists('data_storage', Grav::instance()['config']['plugins']['tecart-cookie-manager'])) { if (Grav::instance()['config']['plugins']['tecart-cookie-manager']['data_storage'] == "pages") { $dataStorage = 'page://assets'; } diff --git a/classes/CookieManager/CookieManager.php b/classes/CookieManager/CookieManager.php index 280e566..783ca8f 100644 --- a/classes/CookieManager/CookieManager.php +++ b/classes/CookieManager/CookieManager.php @@ -15,111 +15,111 @@ */ class CookieManager extends Data { - /** - * Get the cookie manager data list from user/data/ yaml files - * - * @return array - */ - public static function getCookieManagerData() { + /** + * Get the cookie manager data list from user/data/ yaml files + * + * @return array + */ + public static function getCookieManagerData() { - $cookieManagerData = self::getYamlDataObjType(self::getCurrentCookieManagerPath()); + $cookieManagerData = self::getYamlDataObjType(self::getCurrentCookieManagerPath()); - return $cookieManagerData; - } + return $cookieManagerData; + } + + /** + * Get the cookie manager twig vars + * + * @return array + */ + public static function getCookieManagerDataTwigVars() { - /** - * Get the cookie manager twig vars - * - * @return array - */ - public static function getCookieManagerDataTwigVars() { + $vars = []; - $vars = []; + $blueprints = self::getCurrentCookieManagerBlueprint(); + $content = self::getCookieManagerData(); - $blueprints = self::getCurrentCookieManagerBlueprint(); - $content = self::getCookieManagerData(); + $cookieManagerData = new Data($content, $blueprints); - $cookieManagerData = new Data($content, $blueprints); + $vars['cookieManagerData'] = $cookieManagerData; - $vars['cookieManagerData'] = $cookieManagerData; + return $vars; + } - return $vars; - } + /** + * get current cookie manager blueprint + * + * @return string + */ + public static function getCurrentCookieManagerBlueprint() { - /** - * get current cookie manager blueprint - * - * @return string - */ - public static function getCurrentCookieManagerBlueprint() { + $blueprints = new Blueprints; + $currentCookieManagerBlueprint = $blueprints->get(self::getCurrentCookieManagerPath()); - $blueprints = new Blueprints; - $currentCookieManagerBlueprint = $blueprints->get(self::getCurrentCookieManagerPath()); + return $currentCookieManagerBlueprint; + } - return $currentCookieManagerBlueprint; - } + /** + * get current path of cookie manager for config info + * + * @return string + */ + public static function getCurrentCookieManagerPath() { - /** - * get current path of cookie manager for config info - * - * @return string - */ - public static function getCurrentCookieManagerPath() { + $uri = Grav::instance()['uri']; + $currentCookieManagerPath = 'cookie-manager'; - $uri = Grav::instance()['uri']; - $currentCookieManagerPath = 'cookie-manager'; + if(isset($uri->paths()[1])){ + $currentCookieManagerPath = $uri->paths()[1]; + } - if(isset($uri->paths()[1])){ - $currentCookieManagerPath = $uri->paths()[1]; + return $currentCookieManagerPath; } - return $currentCookieManagerPath; - } + /** + * get data object of given type + * + * @return object + */ + public static function getYamlDataObjType($type) { - /** - * get data object of given type - * - * @return object - */ - public static function getYamlDataObjType($type) { + //location of yaml files + $dataStorage = 'user://data'; - //location of yaml files - $dataStorage = 'user://data'; + if (array_key_exists('data_storage', Grav::instance()['config']['plugins']['tecart-cookie-manager'])) { + if (Grav::instance()['config']['plugins']['tecart-cookie-manager']['data_storage'] == "pages") { + $dataStorage = 'page://assets'; + } + } - if(array_key_exists('data_storage', Grav::instance()['config']['plugins']['tecart-cookie-manager'])) { - if (Grav::instance()['config']['plugins']['tecart-cookie-manager']['data_storage'] == "pages") { - $dataStorage = 'page://assets'; - } + return CompiledYamlFile::instance(Grav::instance()['locator']->findResource($dataStorage) . DS . $type . ".yaml")->content(); } - return CompiledYamlFile::instance(Grav::instance()['locator']->findResource($dataStorage) . DS . $type . ".yaml")->content(); - } - - /** - * function to call with parameters from blueprints to dynamically fetch the categories option list - * do this by using data-*@: notation as the key, where * is the field name you want to fill with the result of the function call - * - * data-options@: 'Grav\Plugin\TecartCookieManager\Classes\CookieManager\CookieManager::getCategoriesForBlueprintOptions' - * - * @return array - */ - public static function getCategoriesForBlueprintOptions() { - - $categories = []; - - $catData = self::getYamlDataObjType('cookie-manager-categories'); - - if(is_array($catData) && !empty($catData)){ - if(isset($catData['categories'])){ - foreach($catData['categories'] as $category){ - if(isset($category['category_title'])){ - $categories[$category['category_title']] = $category['category_title']; - } + /** + * function to call with parameters from blueprints to dynamically fetch the categories option list + * do this by using data-*@: notation as the key, where * is the field name you want to fill with the result of the function call + * + * data-options@: 'Grav\Plugin\TecartCookieManager\Classes\CookieManager\CookieManager::getCategoriesForBlueprintOptions' + * + * @return array + */ + public static function getCategoriesForBlueprintOptions() { + + $categories = []; + + $catData = self::getYamlDataObjType('cookie-manager-categories'); + + if(is_array($catData) && !empty($catData)){ + if(isset($catData['categories'])){ + foreach($catData['categories'] as $category){ + if(isset($category['category_title'])){ + $categories[$category['category_title']] = $category['category_title']; + } + } + } } - } - } - return $categories; - } + return $categories; + } } diff --git a/languages/de.yaml b/languages/de.yaml index b6a58c6..4f56b7c 100644 --- a/languages/de.yaml +++ b/languages/de.yaml @@ -63,6 +63,8 @@ PLUGIN_TECART_COOKIE_MANAGER: SCRIPT_CODE_LOAD_WITH_TIMEOUT: Zeitverzögertes Laden SCRIPT_CODE_LOAD_WITH_TIMEOUT_TIME: Zeitverzögerung in ms (3s = 3000ms) SCRIPT_CODE_LOAD_ON_SCROLL: Script beim ersten Scrollen laden + SCRIPT_CODE_LOAD_ON_PAGES: Script auf Seiten laden + SCRIPT_CODE_LOAD_ON_PAGES_HELP: Es ist möglich die Suche durch Eintippen des Seitennamens in das Feld abzukürzen.<br />Script nur auf den ausgewählten Seiten laden. YES: Ja NO: Nein TITLE_VALIDATE_MESSAGE: Bitte geben Sie den Titel an! diff --git a/languages/en.yaml b/languages/en.yaml index 8ada4d4..cd8e1d7 100644 --- a/languages/en.yaml +++ b/languages/en.yaml @@ -63,6 +63,8 @@ PLUGIN_TECART_COOKIE_MANAGER: SCRIPT_CODE_LOAD_WITH_TIMEOUT: Load script with timeout SCRIPT_CODE_LOAD_WITH_TIMEOUT_TIME: Timeout in ms (3s = 3000ms) SCRIPT_CODE_LOAD_ON_SCROLL: Load script on scroll + SCRIPT_CODE_LOAD_ON_PAGES: Load script on pages + SCRIPT_CODE_LOAD_ON_PAGES_HELP: Include the script only on specific pages YES: yes NO: no TITLE_VALIDATE_MESSAGE: Please enter a title!