Skip to content

Commit

Permalink
More settings and fine grained control over the plugin
Browse files Browse the repository at this point in the history
You can now configure:
Expiry date
Path
Domain
Target of "learn more"
link
Added support for navigator.CookiesOK for if the user has enabled
automatic cookie consenting.
  • Loading branch information
BlackSkorpio committed Dec 9, 2015
1 parent a1179f7 commit 77aacb4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 21 deletions.
40 changes: 29 additions & 11 deletions cookieconsent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
var THEME_BUCKET_PATH = '//s3.amazonaws.com/cc.silktide.com/';

// No point going further if they've already dismissed.
if (document.cookie.indexOf(DISMISSED_COOKIE) > -1) {
return;
if (document.cookie.indexOf(DISMISSED_COOKIE) > -1 || (window.navigator && window.navigator.CookiesOK)) {
return;
}

// IE8...
Expand Down Expand Up @@ -91,11 +91,25 @@
return null;
},

setCookie: function (name, value, expirydays) {
setCookie: function (name, value, expiryDays, domain, path) {
expiryDays = expiryDays || 365;

var exdate = new Date();
expirydays = expirydays || 365;
exdate.setDate(exdate.getDate() + expirydays);
document.cookie = name + '=' + value + '; expires=' + exdate.toUTCString() + '; path=/'
exdate.setDate(exdate.getDate() + expiryDays);

var cookie = [
name + '=' + value,
'expires=' + exdate.toUTCString(),
'path=' + path || '/'
];

if (domain) {
cookie.push(
'domain=' + domain
);
}

document.cookie = cookie.join(';');
},

addEventListener: function (el, event, eventListener) {
Expand Down Expand Up @@ -139,7 +153,7 @@
var insertReplacements = function (htmlStr, scope) {
return htmlStr.replace(/\{\{(.*?)\}\}/g, function (_match, sub) {
var tokens = sub.split('||');
var value;
var value, token;
while (token = tokens.shift()) {
token = token.trim();

Expand Down Expand Up @@ -217,14 +231,18 @@
dismiss: 'Got it!',
learnMore: 'More info',
link: null,
target: '_self',
container: null, // selector
theme: 'light-floating',
domain: null, // default to current domain.
path: '/',
expiryDays: 365,
markup: [
'<div class="cc_banner-wrapper {{containerClasses}}">',
'<div class="cc_banner cc_container cc_container--open">',
'<a href="#null" data-cc-event="click:dismiss" class="cc_btn cc_btn_accept_all">{{options.dismiss}}</a>',
'<a href="#null" data-cc-event="click:dismiss" target="_blank" class="cc_btn cc_btn_accept_all">{{options.dismiss}}</a>',

'<p class="cc_message">{{options.message}} <a data-cc-if="options.link" class="cc_more_info" href="{{options.link || "#null"}}">{{options.learnMore}}</a></p>',
'<p class="cc_message">{{options.message}} <a data-cc-if="options.link" target="{{ options.target }}" class="cc_more_info" href="{{options.link || "#null"}}">{{options.learnMore}}</a></p>',

'<a class="cc_logo" onclick="return !window.open(this.href);" href="http://varoystrand.se/sidor/cookie-consent-for-koken/" title="Cookie Consent plugin for Koken CMS">Cookie Consent plugin for Koken</a>',
'</div>',
Expand Down Expand Up @@ -309,14 +327,14 @@
},

dismiss: function (evt) {
evt.preventDefault() && evt.preventDefault();
evt.preventDefault && evt.preventDefault();
evt.returnValue = false;
this.setDismissedCookie();
this.container.removeChild(this.element);
},

setDismissedCookie: function () {
Util.setCookie(DISMISSED_COOKIE, 'yes');
Util.setCookie(DISMISSED_COOKIE, 'yes', this.options.expiryDays, this.options.domain, this.options.path);
}
};

Expand Down
36 changes: 33 additions & 3 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "Cookie Consent",
"version": "1.0.7",
"version": "1.0.8",
"description": "Inform your users that you are using cookies",
"demo": "http://varoystrand.se/sidor/cookie-consent-for-koken/",
"author": {
"name": "Bjarne Varoystrand",
"link": "http://varoystrand.se/"
},

"data": {
"cookie_consent_message": {
"label": "The message shown",
Expand Down Expand Up @@ -62,6 +61,37 @@
{ "value": "dark-bottom", "label": "Dark bottom" },
{ "value": "false", "label": "Use your own theme" }
]
},
"cookie_consent_path": {
"label": "Cookie Path",
"info": "The path for the consent cookie that Cookie Consent uses, to remember that users have consented to cookies. Use to limit consent to a specific path within your website.",
"type": "string",
"value": "/"
"validation": {
"type": "not_empty",
"error_message": "You need to set a valid path for the cookie."
}
},
"cookie_consent_domain": {
"label": "Cookie Domain",
"info": "The domain for the consent cookie that Cookie Consent uses, to remember that users have consented to cookies. Useful if your website uses multiple subdomains, e.g. if your script is hosted at www.example.com you might override this to example.com, thereby allowing the same consent cookie to be read by subdomains like foo.example.com.",
"type": "string",
"placeholder": "example.com"
},
"cookie_consent_expirydays": {
"label": "Expiry days",
"info": "The number of days Cookie Consent should store the users consent information for. Defaults to 365 days (1 year).",
"type": "number",
"value": "365"
},
"cookie_consent_target": {
"label": "Target",
"info": "The target of the link to your cookie policy. Use to open a link in a new window.",
"type": "select",
"options": [
{ "value": "self", "label": "Same window" },
{ "value": "blank", "label": "Opens in new window" }
]
}
}
}
}
22 changes: 15 additions & 7 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ function __construct()
function render()
{

$message = $this->data->cookie_consent_message;
$dismiss = $this->data->cookie_consent_dismiss;
$learnMore = $this->data->cookie_consent_learnmore;
$link = $this->data->cookie_consent_link;
$theme = $this->data->cookie_consent_theme;
$path = $this->get_path();
$message = $this->data->cookie_consent_message;
$dismiss = $this->data->cookie_consent_dismiss;
$learnMore = $this->data->cookie_consent_learnmore;
$link = $this->data->cookie_consent_link;
$theme = $this->data->cookie_consent_theme;
$target = $this->data->cookie_consent_target;
$cookiedomain = $this->data->cookie_consent_domain;
$cookiepath = $this->data->cookie_consent_path;
$cookieexp = $this->data->cookie_consent_expirydays;
$path = $this->get_path();

echo <<<OUT
<script type="text/javascript">
Expand All @@ -25,7 +29,11 @@ function render()
dismiss: '{$dismiss}',
learnMore: '{$learnMore}',
link: '{$link}',
theme: '{$path}/styles/{$theme}.css'
target: '_{$target}',
theme: '{$path}/styles/{$theme}.css',
domain: '{$cookiedomain}',
path: '{$cookiepath}',
expiryDays: '{$cookieexp}'
}
</script>
<script type="text/javascript" src="{$path}/cookieconsent.js"></script>
Expand Down

0 comments on commit 77aacb4

Please sign in to comment.